Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pfouque committed Jan 13, 2024
1 parent 04efc9e commit b9ea72c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/cities_light/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions src/cities_light/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ 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,
stdout=out
)
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))
)
39 changes: 33 additions & 6 deletions src/cities_light/tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"""Test for cities_light_fixtures management command."""
import bz2
import json
import os
from unittest import mock

from django import test
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):
Expand All @@ -36,13 +36,34 @@ 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
and tries to save it to file."""
# 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")
Expand All @@ -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)
Expand Down Expand Up @@ -86,15 +113,15 @@ 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:
cmd = Command()
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)
Expand Down
44 changes: 20 additions & 24 deletions src/cities_light/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b9ea72c

Please sign in to comment.