From 1d63bd940d7a8ac4388153ed7279671aae0beed9 Mon Sep 17 00:00:00 2001 From: Arona Jones Date: Fri, 5 Feb 2021 01:02:01 +0000 Subject: [PATCH] More test munging --- PyRIGS/tests/test_unit.py | 14 ++++------- RIGS/tests/test_models.py | 6 ++--- assets/models.py | 2 +- assets/tests/conftest.py | 4 ++-- assets/tests/test_access.py | 17 ++++++-------- assets/tests/test_interaction.py | 5 ++-- assets/tests/test_unit.py | 40 ++++++++++++++++---------------- conftest.py | 11 +++++++++ 8 files changed, 52 insertions(+), 47 deletions(-) diff --git a/PyRIGS/tests/test_unit.py b/PyRIGS/tests/test_unit.py index 943039f7..2ddd93ee 100644 --- a/PyRIGS/tests/test_unit.py +++ b/PyRIGS/tests/test_unit.py @@ -34,13 +34,11 @@ def get_request_url(url): print("Couldn't test url " + pattern) -@pytest.fixture(scope='module') -def django_db_setup(django_db_setup, django_db_blocker): - with django_db_blocker.unblock(): - from django.conf import settings - settings.DEBUG = True - call_command('generateSampleData') # We need stuff setup so we don't get 404 errors everywhere - settings.DEBUG = False +@pytest.fixture(scope='function', autouse=True) +def run_sample_data(transactional_db, settings, django_db_blocker): # We need stuff setup so we don't get 404 errors everywhere + settings.DEBUG = True + call_command('generateSampleUserData') + call_command('generateSampleAssetsData') def test_unauthenticated(client): # Nothing should be available to the unauthenticated @@ -73,10 +71,8 @@ def test_page_titles(admin_client): # print(response.content.decode(), file=open('output.html', 'w')) -@pytest.mark.django_db(transaction=True) def test_delete_sample_data(settings): settings.DEBUG = True call_command('deleteSampleData') - assert models.Asset.objects.all().count() == 0 assert models.Supplier.objects.all().count() == 0 diff --git a/RIGS/tests/test_models.py b/RIGS/tests/test_models.py index cc3d2b2a..5d602f8f 100644 --- a/RIGS/tests/test_models.py +++ b/RIGS/tests/test_models.py @@ -18,12 +18,12 @@ def test_str(): @pytest.mark.django_db -def test_find_correct(vat_rate): +def test_find_correct(): new_rate = models.VatRate.objects.create(start_at='2016-03-01', rate=0.15, comment='test2') r = models.VatRate.objects.find_rate('2015-03-01') - assert r.rate == vat_rate.rate + assert r == vat_rate r = models.VatRate.objects.find_rate('2016-03-01') - assert r.rate == new_rate.rate + assert r == new_rate def test_percent_correct(vat_rate): diff --git a/assets/models.py b/assets/models.py index 931dcdc0..8e3d4acb 100644 --- a/assets/models.py +++ b/assets/models.py @@ -148,7 +148,7 @@ class Asset(models.Model, RevisionMixin): return reverse('asset_detail', kwargs={'pk': self.asset_id}) def __str__(self): - out = str(self.asset_id) + ' - ' + self.description + out = "{} | {}".format(self.asset_id, self.description) if self.is_cable and self.cable_type is not None: out += '{} - {}m - {}'.format(self.cable_type.plug, self.length, self.cable_type.socket) return out diff --git a/assets/tests/conftest.py b/assets/tests/conftest.py index 7ac202ab..8111fef9 100644 --- a/assets/tests/conftest.py +++ b/assets/tests/conftest.py @@ -17,10 +17,10 @@ def status(db): def test_cable(db, category, status): connector = models.Connector.objects.create(description="16A IEC", current_rating=16, voltage_rating=240, num_pins=3) cable_type = models.CableType.objects.create(circuits=11, cores=3, plug=connector, socket=connector) - return models.Asset.objects.create(asset_id="666", description="125A -> Jack", comments="The cable from Hell...", status=status, category=category, date_acquired=datetime.date(2006, 6, 6), is_cable=True, cable_type=cable_type, length=10, csa="1.5") + return models.Asset.objects.create(asset_id="9666", description="125A -> Jack", comments="The cable from Hell...", status=status, category=category, date_acquired=datetime.date(2006, 6, 6), is_cable=True, cable_type=cable_type, length=10, csa="1.5") @pytest.fixture def test_asset(db, category, status): - asset = models.Asset.objects.create(asset_id="1991", description="Spaceflower", status=working, category=lighting, date_acquired=datetime.date(1991, 12, 26)) + asset = models.Asset.objects.create(asset_id="91991", description="Spaceflower", status=status, category=category, date_acquired=datetime.date(1991, 12, 26)) return asset diff --git a/assets/tests/test_access.py b/assets/tests/test_access.py index cecb6e1f..6ea9880f 100644 --- a/assets/tests/test_access.py +++ b/assets/tests/test_access.py @@ -16,15 +16,12 @@ from django.utils import timezone pytestmark = pytest.mark.django_db -@pytest.fixture(scope='module') -@pytest.mark.db(transaction=True) -def django_db_setup(django_db_setup, django_db_blocker): # We need stuff setup so we don't get 404 errors everywhere - with django_db_blocker.unblock(): - from django.conf import settings - settings.DEBUG = True - call_command('generateSampleUserData') - call_command('generateSampleAssetsData') - settings.DEBUG = False +@pytest.fixture(scope='function', autouse=True) +def run_sample_data(transactional_db, settings, django_db_blocker): # We need stuff setup so we don't get 404 errors everywhere + settings.DEBUG = True + call_command('generateSampleUserData') + call_command('generateSampleAssetsData') + settings.DEBUG = False # The fixture does reset it automatically, but we need to do it before the test runs to stop the debug toolbar polluting our HTML def test_basic_access(client): @@ -34,7 +31,7 @@ def test_basic_access(client): response = client.get(url) # Check edit and duplicate buttons NOT shown in list assertNotContains(response, 'Edit') - assertNotContains(response, 'Duplicate') + assertNotContains(response, 'Duplicate') # If this line is randomly failing, check the debug toolbar HTML hasn't crept in url = reverse('asset_detail', kwargs={'pk': 1}) response = client.get(url) diff --git a/assets/tests/test_interaction.py b/assets/tests/test_interaction.py index 31c43d38..d139349f 100644 --- a/assets/tests/test_interaction.py +++ b/assets/tests/test_interaction.py @@ -138,10 +138,11 @@ class TestAssetForm(AutoLoginTest): self.page.parent_selector.toggle() self.assertTrue(self.page.parent_selector.is_open) - self.page.parent_selector.search(self.parent.asset_id) + option = str(self.parent) + self.page.parent_selector.search(option) # Needed here but not earlier for whatever reason self.driver.implicitly_wait(1) - self.page.parent_selector.set_option(self.parent.asset_id + " | " + self.parent.description, True) + self.page.parent_selector.set_option(option, True) self.assertTrue(self.page.parent_selector.options[0].selected) self.page.parent_selector.toggle() diff --git a/assets/tests/test_unit.py b/assets/tests/test_unit.py index 1c0b0cba..e0e7919c 100644 --- a/assets/tests/test_unit.py +++ b/assets/tests/test_unit.py @@ -84,26 +84,6 @@ def test_oembed(client, test_asset): assert_oembed(alt_asset_embed_url, alt_oembed_url, client, asset_embed_url, asset_url, oembed_url) -@pytest.mark.django_db(transaction=True) -def test_generate_sample_data(settings): - settings.DEBUG = True - call_command('deleteSampleData') # TODO - # Run the management command and check there are no exceptions - call_command('generateSampleAssetsData') - - # Check there are lots - assert models.Asset.objects.all().count() > 50 - assert models.Supplier.objects.all().count() > 50 - - -def test_production_exception(client): - from django.core.management.base import CommandError - - with pytest.raises(CommandError, match=".*production"): - call_command('generateSampleAssetsData') - call_command('deleteSampleData') - - 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', 'salvage_value': '-30'}) assertFormError(response, 'form', 'asset_id', 'This field is required.') @@ -150,3 +130,23 @@ def assert_asset_form_errors(response): 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', 'salvage_value', 'A price cannot be negative') + + +def test_production_exception(client): + from django.core.management.base import CommandError + + with pytest.raises(CommandError, match=".*production"): + call_command('generateSampleAssetsData') + call_command('deleteSampleData') + + +@pytest.mark.django_db(transaction=True) +def test_generate_sample_data(settings): + settings.DEBUG = True + print(models.AssetCategory.objects.all()) + # Run the management command and check there are no exceptions + call_command('generateSampleAssetsData') + + # Check there are lots + assert models.Asset.objects.all().count() > 50 + assert models.Supplier.objects.all().count() > 50 diff --git a/conftest.py b/conftest.py index df750435..f7e19ea0 100644 --- a/conftest.py +++ b/conftest.py @@ -20,3 +20,14 @@ def splinter_webdriver(): @pytest.fixture(scope='session') def splinter_screenshot_dir(): return 'screenshots/' + + +def _has_transactional_marker(item): + db_marker = item.get_closest_marker("django_db") + if db_marker and db_marker.kwargs.get("transaction"): + return 1 + return 0 + + +def pytest_collection_modifyitems(items): # Always run database-mulching tests last + items.sort(key=_has_transactional_marker)