From d33879bc96fa53f3d28db2b36260939fddc57a62 Mon Sep 17 00:00:00 2001 From: Hang Date: Sat, 22 Aug 2026 00:48:09 +0100 Subject: [PATCH] fix(tests): fix some syntax errors in test code still got lots of failed tests :( --- RIGS/tests/test_functional.py | 4 ++-- RIGS/tests/test_interaction.py | 8 +++++-- assets/tests/pages.py | 10 +++++++++ assets/tests/test_interaction.py | 14 ++++++------- assets/tests/test_unit.py | 36 ++++++++++++++++---------------- training/tests/test_unit.py | 6 +++--- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/RIGS/tests/test_functional.py b/RIGS/tests/test_functional.py index d156a61a..89d29ad2 100644 --- a/RIGS/tests/test_functional.py +++ b/RIGS/tests/test_functional.py @@ -48,9 +48,9 @@ def test_create(admin_client): response = admin_client.post(url, {'start_date': datetime.date(2020, 1, 1), 'start_time': datetime.time(10, 00), 'end_time': datetime.time(9, 00), 'access_at': datetime.datetime(2020, 1, 5, 10)}) - assertFormError(response, 'form', 'end_time', + assertFormError(response.context['form'], 'end_time', "Unless you've invented time travel, the event can't finish before it has started.") - assertFormError(response, 'form', 'access_at', + assertFormError(response.context['form'], 'access_at', "Regardless of what some clients might think, access time cannot be after the event has started.") diff --git a/RIGS/tests/test_interaction.py b/RIGS/tests/test_interaction.py index 85735dae..d973c4b9 100644 --- a/RIGS/tests/test_interaction.py +++ b/RIGS/tests/test_interaction.py @@ -91,8 +91,12 @@ class TestRigboard(BaseRigboardTest): # self.live_server_url + '/event/create/', self.driver.current_url) def test_event_order(self): - self.assertIn(self.testEvent.start_date.strftime('%-d %b %Y'), self.page.events[0].dates) - self.assertIn(self.testEvent2.start_date.strftime('%-d %b %Y'), self.page.events[1].dates) + # for cross-platform support + def format_date(date): + return f"{date.day} {date.strftime('%b %Y')}" + + self.assertIn(format_date(self.testEvent.start_date), self.page.events[0].dates) + self.assertIn(format_date(self.testEvent2.start_date), self.page.events[1].dates) def test_add_button(self): self.page.add() diff --git a/assets/tests/pages.py b/assets/tests/pages.py index 9f5afa32..338c7682 100644 --- a/assets/tests/pages.py +++ b/assets/tests/pages.py @@ -4,6 +4,7 @@ from pypom import Region from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions +from selenium.webdriver.support.ui import WebDriverWait from PyRIGS.tests import regions from PyRIGS.tests.pages import BasePage, FormPage, animation_is_finished @@ -55,7 +56,12 @@ class AssetList(BasePage): element.send_keys(queryString) def search(self): + old_assets = self.find_elements(*self._asset_item_locator) self.find_element(*self._go_button_locator).click() + WebDriverWait(self.driver, 10).until( + expected_conditions.staleness_of(old_assets[0]) + ) + def filter(self): self.find_element(*self._filter_button_locator).click() @@ -152,7 +158,11 @@ class SupplierList(BasePage): element.send_keys(queryString) def search(self): + old_suppliers = self.find_elements(*self._supplier_item_locator) self.find_element(*self._go_button_locator).click() + WebDriverWait(self.driver, 10).until( + expected_conditions.staleness_of(old_suppliers[0]) + ) class SupplierForm(FormPage): diff --git a/assets/tests/test_interaction.py b/assets/tests/test_interaction.py index f840761f..55365b08 100644 --- a/assets/tests/test_interaction.py +++ b/assets/tests/test_interaction.py @@ -235,13 +235,11 @@ class TestSupplierList(AutoLoginTest): def test_search(self): self.page.set_query("TEC") self.page.search() - self.assertTrue(len(self.page.suppliers) == 1) self.assertEqual("TEC PA & Lighting", self.page.suppliers[0].name) self.page.set_query("") self.page.search() - time.sleep(1) self.assertTrue(len(self.page.suppliers) == 7) self.page.set_query("NOTFOUND") @@ -254,8 +252,8 @@ def test_supplier_create(logged_in_browser, live_server): page.remove_all_required() page.submit() - assert !self.page.success - assert "This field is required." in self.page.errors["Name"] + assert not page.success + assert "This field is required." in page.errors["Name"] page.name = "Optican Health Supplies" page.submit() @@ -315,19 +313,19 @@ def test_audit_fail(logged_in_browser, admin_user, live_server, test_asset): page.modal.description = "" page.modal.submit() wait.until(animation_is_finished()) - assert "This field is required." in self.page.modal.errors["Description"] + assert "This field is required." in page.modal.errors["Description"] def test_audit_list(logged_in_browser, admin_user, live_server, test_asset): page = pages.AssetAuditList(logged_in_browser.driver, live_server.url).open() wait = WebDriverWait(logged_in_browser.driver, 20) - assert models.Asset.objects.filter(last_audited_at=None).count() == len(self.page.assets) + assert models.Asset.objects.filter(last_audited_at=None).count() == len(page.assets) asset_row = page.assets[0] logged_in_browser.driver.find_element(By.XPATH, "//a[contains(@class,'btn') and contains(., 'Audit')]").click() wait.until(ec.visibility_of_element_located((By.ID, 'modal'))) - assert self.page.modal.asset_id == asset_row.id + assert page.modal.asset_id == asset_row.id page.modal.close() - assert !logged_in_browser.driver.find_element(By.ID, 'modal').is_displayed() + assert not logged_in_browser.driver.find_element(By.ID, 'modal').is_displayed() # Make sure audit log was NOT filled out audited = models.Asset.objects.get(asset_id=asset_row.id) assert audited.last_audited_by is None diff --git a/assets/tests/test_unit.py b/assets/tests/test_unit.py index 2d4d94a9..89e5c2d9 100644 --- a/assets/tests/test_unit.py +++ b/assets/tests/test_unit.py @@ -16,14 +16,14 @@ pytestmark = pytest.mark.django_db def test_supplier_create(admin_client): url = reverse('supplier_create') response = admin_client.post(url) - assertFormError(response, 'form', 'name', 'This field is required.') + assertFormError(response.context['form'], 'name', 'This field is required.') def test_supplier_edit(admin_client): supplier = models.Supplier.objects.create(name="Gadgetron Corporation") url = reverse('supplier_update', kwargs={'pk': supplier.pk}) response = admin_client.post(url, {'name': ""}) - assertFormError(response, 'form', 'name', 'This field is required.') + assertFormError(response.context['form'], 'name', 'This field is required.') def test_404(admin_client): @@ -85,16 +85,16 @@ def test_oembed(client, test_asset): def test_asset_create(admin_client): response = admin_client.post(reverse('asset_create'), {'date_sold': '2000-01-01', 'date_acquired': '2020-01-01', 'purchase_price': '-30', 'replacement_cost': '-30'}) - assertFormError(response, 'form', 'asset_id', 'This field is required.') + assertFormError(response.context['form'], 'asset_id', ['This field is required.', 'An Asset ID can only consist of letters and numbers, with a final number']) assert_asset_form_errors(response) def test_cable_create(admin_client): response = admin_client.post(reverse('asset_create'), {'asset_id': 'X$%A', 'is_cable': True}) - assertFormError(response, 'form', 'asset_id', 'An Asset ID can only consist of letters and numbers, with a final number') - assertFormError(response, 'form', 'cable_type', 'A cable must have a type') - assertFormError(response, 'form', 'length', 'The length of a cable must be more than 0') - assertFormError(response, 'form', 'csa', 'The CSA of a cable must be more than 0') + assertFormError(response.context['form'], 'asset_id', 'An Asset ID can only consist of letters and numbers, with a final number') + assertFormError(response.context['form'], 'cable_type', 'A cable must have a type') + assertFormError(response.context['form'], 'length', 'The length of a cable must be more than 0') + assertFormError(response.context['form'], 'csa', 'The CSA of a cable must be more than 0') def test_asset_edit(admin_client, test_asset): @@ -108,23 +108,23 @@ def test_cable_edit(admin_client, test_cable): response = admin_client.post(url, {'is_cable': True, 'length': -3, 'csa': -3}) # TODO Can't figure out how to select the 'none' option... - # assertFormError(response, 'form', 'cable_type', 'A cable must have a type') - assertFormError(response, 'form', 'length', 'The length of a cable must be more than 0') - assertFormError(response, 'form', 'csa', 'The CSA of a cable must be more than 0') + # assertFormError(response.context['form'], 'cable_type', 'A cable must have a type') + assertFormError(response.context['form'], 'length', 'The length of a cable must be more than 0') + assertFormError(response.context['form'], 'csa', 'The CSA of a cable must be more than 0') def test_asset_duplicate(admin_client, test_cable): url = reverse('asset_duplicate', kwargs={'pk': test_cable.asset_id}) response = admin_client.post(url, {'is_cable': True, 'length': 0, 'csa': 0}) - assertFormError(response, 'form', 'length', 'The length of a cable must be more than 0') - assertFormError(response, 'form', 'csa', 'The CSA of a cable must be more than 0') + assertFormError(response.context['form'], 'length', 'The length of a cable must be more than 0') + assertFormError(response.context['form'], 'csa', 'The CSA of a cable must be more than 0') def assert_asset_form_errors(response): - assertFormError(response, 'form', 'description', 'This field is required.') - assertFormError(response, 'form', 'status', 'This field is required.') - assertFormError(response, 'form', 'category', 'This field is required.') - assertFormError(response, 'form', 'date_sold', 'Cannot sell an item before it is acquired') - assertFormError(response, 'form', 'purchase_price', 'A price cannot be negative') - assertFormError(response, 'form', 'replacement_cost', 'A price cannot be negative') + assertFormError(response.context['form'], 'description', 'This field is required.') + assertFormError(response.context['form'], 'status', 'This field is required.') + assertFormError(response.context['form'], 'category', 'This field is required.') + assertFormError(response.context['form'], 'date_sold', 'Cannot sell an item before it is acquired') + assertFormError(response.context['form'], 'purchase_price', 'A price cannot be negative') + assertFormError(response.context['form'], 'replacement_cost', 'A price cannot be negative') diff --git a/training/tests/test_unit.py b/training/tests/test_unit.py index ed3154ef..80f951e2 100644 --- a/training/tests/test_unit.py +++ b/training/tests/test_unit.py @@ -14,11 +14,11 @@ def test_add_qualification(admin_client, trainee, admin_user, training_item): url = reverse('add_qualification', kwargs={'pk': trainee.pk}) date = (timezone.now() + datetime.timedelta(days=3)).strftime("%Y-%m-%d") response = admin_client.post(url, {'date': date, 'trainee': trainee.pk, 'supervisor': trainee.pk, 'item': training_item.pk}) - assertFormError(response, 'form', 'date', 'Qualification date may not be in the future') - assertFormError(response, 'form', 'supervisor', 'One may not supervise oneself...') + assertFormError(response.context['form'], 'date', 'Qualification date may not be in the future') + assertFormError(response.context['form'], 'supervisor', 'One may not supervise oneself...') response = admin_client.post(url, {'date': date, 'trainee': admin_user.pk, 'supervisor': trainee.pk, 'item': training_item.pk}) print(response.content) - assertFormError(response, 'form', 'supervisor', 'Selected supervisor must actually *be* a supervisor...') + assertFormError(response.context['form'], 'supervisor', 'Selected supervisor must actually *be* a supervisor...') def test_add_qualification_reversion(admin_client, trainee, training_item, supervisor):