From b9ea72cf1b1bbcfb3dfcda2e5ad47b493ad78f30 Mon Sep 17 00:00:00 2001 From: Pascal F Date: Thu, 23 Nov 2023 17:24:02 +0100 Subject: [PATCH] wip --- src/cities_light/serializers/base.py | 3 ++ src/cities_light/tests/base.py | 8 ++--- src/cities_light/tests/test_fixtures.py | 39 ++++++++++++++++---- src/cities_light/tests/test_migrations.py | 44 +++++++++++------------ 4 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/cities_light/serializers/base.py b/src/cities_light/serializers/base.py index ed4f8aba..bd97b297 100644 --- a/src/cities_light/serializers/base.py +++ b/src/cities_light/serializers/base.py @@ -64,6 +64,9 @@ def normalize_decimals(cls, data): if value % 1 == 0: data['fields'][key] = int(value) else: + print(type(value), " => ", type(value.normalize())) + print(value, " => ", value.normalize()) + # data['fields'][key] = value data['fields'][key] = value.normalize() def get_dump_object(self, obj): diff --git a/src/cities_light/tests/base.py b/src/cities_light/tests/base.py index a2621ddf..e5401332 100644 --- a/src/cities_light/tests/base.py +++ b/src/cities_light/tests/base.py @@ -83,11 +83,11 @@ def _patch(setting, *values): force_import_all=True, **options) - def export_data(self) -> bytes: + def export_data(self, app_label=None) -> bytes: out = StringIO() management.call_command( "dumpdata", - "cities_light", + app_label or "cities_light", format="sorted_json", natural_foreign=True, indent=4, @@ -95,10 +95,10 @@ def export_data(self) -> bytes: ) return out.getvalue() - def assertNoDiff(self, fixture_path): + def assertNoDiff(self, fixture_path, app_label=None): """Assert that dumped data matches fixture.""" with open(fixture_path) as f: self.assertListEqual( - json.loads(f.read()), json.loads(self.export_data()) + json.loads(f.read()), json.loads(self.export_data(app_label)) ) diff --git a/src/cities_light/tests/test_fixtures.py b/src/cities_light/tests/test_fixtures.py index 0b994f0c..0fcecb56 100644 --- a/src/cities_light/tests/test_fixtures.py +++ b/src/cities_light/tests/test_fixtures.py @@ -1,5 +1,6 @@ """Test for cities_light_fixtures management command.""" import bz2 +import json import os from unittest import mock @@ -7,15 +8,14 @@ from django.core.management import call_command from django.core.management.base import CommandError -# from dbdiff.fixture import Fixture from cities_light.settings import DATA_DIR, FIXTURES_BASE_URL from cities_light.management.commands.cities_light_fixtures import Command from cities_light.downloader import Downloader from cities_light.models import City -from .base import FixtureDir +from .base import TestImportBase, FixtureDir -class TestCitiesLigthFixtures(test.TransactionTestCase): +class TestCitiesLigthFixtures(TestImportBase): """Tests for cities_light_fixtures management command.""" def test_dump_fixtures(self): @@ -36,6 +36,26 @@ def test_dump_fixtures(self): mock_func.assert_any_call('cities_light.SubRegion', cmd.subregion_path) mock_func.assert_any_call('cities_light.City', cmd.city_path) + # def export_data(self, app_label=None) -> bytes: + # out = StringIO() + # management.call_command( + # "dumpdata", + # app_label or "cities_light", + # format="sorted_json", + # natural_foreign=True, + # indent=4, + # stdout=out + # ) + # return out.getvalue() + + def assertNoDiff(self, fixture_path, app_label=None): + """Assert that dumped data matches fixture.""" + + with open(fixture_path) as f: + self.assertListEqual( + json.loads(f.read()), json.loads(self.export_data(app_label)) + ) + def test_dump_fixture(self): """ Test dump_fixture calls dumpdata management command @@ -43,6 +63,7 @@ def test_dump_fixture(self): # Load test data destination = FixtureDir('import').get_file_path('angouleme.json') call_command('loaddata', destination) + # Dump try: fixture_path = os.path.join(os.path.dirname(__file__), "fixtures", "test_dump_fixture.json") @@ -52,7 +73,13 @@ def test_dump_fixture(self): data = bzfile.read() with open(fixture_path, mode='wb') as file: file.write(data) - Fixture(fixture_path, models=[City]).assertNoDiff() + + # with open(destination) as f2, open(fixture_path) as f: + # assert f.read() == f2.read() + # self.assertListEqual(json.loads(f.read()), json.loads(f2.read())) + # assert destination == fixture_path.read() + self.assertNoDiff(fixture_path, 'cities_light.City') + finally: if os.path.exists(fixture_path): os.remove(fixture_path) @@ -86,7 +113,7 @@ def test_load_fixtures(self): mock_func.assert_any_call( cmd.city_url, cmd.city_path, force=True) - def test_load_fixture(self): + def test_load_fixture_result(self): """Test loaded fixture matches database content.""" destination = FixtureDir('import').get_file_path('angouleme.json') with mock.patch.object(Downloader, 'download') as mock_func: @@ -94,7 +121,7 @@ def test_load_fixture(self): cmd.load_fixture(source='/abcdefg.json', destination=destination, force=True) - Fixture(destination).assertNoDiff() + self.assertNoDiff(destination) mock_func.assert_called_with(source='/abcdefg.json', destination=destination, force=True) diff --git a/src/cities_light/tests/test_migrations.py b/src/cities_light/tests/test_migrations.py index 848c503c..79632bce 100644 --- a/src/cities_light/tests/test_migrations.py +++ b/src/cities_light/tests/test_migrations.py @@ -1,31 +1,27 @@ import unittest +from io import StringIO from django import test -from django.apps import apps -from django.db.migrations.autodetector import MigrationAutodetector -from django.db.migrations.loader import MigrationLoader -from django.db.migrations.questioner import ( - InteractiveMigrationQuestioner, ) -from django.db.migrations.state import ProjectState +from django.core.management import call_command +from django.test.utils import override_settings +@override_settings( + MIGRATION_MODULES={ + "cities_light": "cities_light.migrations", + }, +) class TestNoMigrationLeft(test.TestCase): - @unittest.skip("TODO: make the test pass") def test_no_migration_left(self): - loader = MigrationLoader(None, ignore_no_migrations=True) - conflicts = loader.detect_conflicts() - app_labels = ['cities_light'] - - autodetector = MigrationAutodetector( - loader.project_state(), - ProjectState.from_apps(apps), - InteractiveMigrationQuestioner(specified_apps=app_labels, dry_run=True), - ) - - changes = autodetector.changes( - graph=loader.graph, - trim_to_apps=app_labels or None, - convert_apps=app_labels or None, - ) - - assert 'cities_light' not in changes + out = StringIO() + try: + call_command( + "makemigrations", + "cities_light", + "--dry-run", + "--check", + stdout=out, + stderr=StringIO(), + ) + except SystemExit: # pragma: no cover + raise AssertionError("Pending migrations:\n" + out.getvalue()) from None