Skip to content

Commit 9f3c242

Browse files
committed
wip
1 parent 5726a2b commit 9f3c242

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

src/cities_light/serializers/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def normalize_decimals(cls, data):
6464
if value % 1 == 0:
6565
data['fields'][key] = int(value)
6666
else:
67+
print(type(value), " => ", type(value.normalize()))
68+
print(value, " => ", value.normalize())
69+
# data['fields'][key] = value
6770
data['fields'][key] = value.normalize()
6871

6972
def get_dump_object(self, obj):

src/cities_light/tests/base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,22 @@ def _patch(setting, *values):
8383
force_import_all=True,
8484
**options)
8585

86-
def export_data(self) -> bytes:
86+
def export_data(self, app_label=None) -> bytes:
8787
out = StringIO()
8888
management.call_command(
8989
"dumpdata",
90-
"cities_light",
90+
app_label or "cities_light",
9191
format="sorted_json",
9292
natural_foreign=True,
9393
indent=4,
9494
stdout=out
9595
)
9696
return out.getvalue()
9797

98-
def assertNoDiff(self, fixture_path):
98+
def assertNoDiff(self, fixture_path, app_label=None):
9999
"""Assert that dumped data matches fixture."""
100100

101101
with open(fixture_path) as f:
102102
self.assertListEqual(
103-
json.loads(f.read()), json.loads(self.export_data())
103+
json.loads(f.read()), json.loads(self.export_data(app_label))
104104
)

src/cities_light/tests/test_fixtures.py

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""Test for cities_light_fixtures management command."""
22
import bz2
3+
import json
34
import os
45
from unittest import mock
56

67
from django import test
78
from django.core.management import call_command
89
from django.core.management.base import CommandError
910

10-
# from dbdiff.fixture import Fixture
1111
from cities_light.settings import DATA_DIR, FIXTURES_BASE_URL
1212
from cities_light.management.commands.cities_light_fixtures import Command
1313
from cities_light.downloader import Downloader
1414
from cities_light.models import City
15-
from .base import FixtureDir
15+
from .base import TestImportBase, FixtureDir
1616

1717

18-
class TestCitiesLigthFixtures(test.TransactionTestCase):
18+
class TestCitiesLigthFixtures(TestImportBase):
1919
"""Tests for cities_light_fixtures management command."""
2020

2121
def test_dump_fixtures(self):
@@ -36,13 +36,34 @@ def test_dump_fixtures(self):
3636
mock_func.assert_any_call('cities_light.SubRegion', cmd.subregion_path)
3737
mock_func.assert_any_call('cities_light.City', cmd.city_path)
3838

39+
# def export_data(self, app_label=None) -> bytes:
40+
# out = StringIO()
41+
# management.call_command(
42+
# "dumpdata",
43+
# app_label or "cities_light",
44+
# format="sorted_json",
45+
# natural_foreign=True,
46+
# indent=4,
47+
# stdout=out
48+
# )
49+
# return out.getvalue()
50+
51+
def assertNoDiff(self, fixture_path, app_label=None):
52+
"""Assert that dumped data matches fixture."""
53+
54+
with open(fixture_path) as f:
55+
self.assertListEqual(
56+
json.loads(f.read()), json.loads(self.export_data(app_label))
57+
)
58+
3959
def test_dump_fixture(self):
4060
"""
4161
Test dump_fixture calls dumpdata management command
4262
and tries to save it to file."""
4363
# Load test data
4464
destination = FixtureDir('import').get_file_path('angouleme.json')
4565
call_command('loaddata', destination)
66+
4667
# Dump
4768
try:
4869
fixture_path = os.path.join(os.path.dirname(__file__), "fixtures", "test_dump_fixture.json")
@@ -52,7 +73,13 @@ def test_dump_fixture(self):
5273
data = bzfile.read()
5374
with open(fixture_path, mode='wb') as file:
5475
file.write(data)
55-
Fixture(fixture_path, models=[City]).assertNoDiff()
76+
77+
# with open(destination) as f2, open(fixture_path) as f:
78+
# assert f.read() == f2.read()
79+
# self.assertListEqual(json.loads(f.read()), json.loads(f2.read()))
80+
# assert destination == fixture_path.read()
81+
self.assertNoDiff(fixture_path, 'cities_light.City')
82+
5683
finally:
5784
if os.path.exists(fixture_path):
5885
os.remove(fixture_path)
@@ -86,15 +113,15 @@ def test_load_fixtures(self):
86113
mock_func.assert_any_call(
87114
cmd.city_url, cmd.city_path, force=True)
88115

89-
def test_load_fixture(self):
116+
def test_load_fixture_result(self):
90117
"""Test loaded fixture matches database content."""
91118
destination = FixtureDir('import').get_file_path('angouleme.json')
92119
with mock.patch.object(Downloader, 'download') as mock_func:
93120
cmd = Command()
94121
cmd.load_fixture(source='/abcdefg.json',
95122
destination=destination,
96123
force=True)
97-
Fixture(destination).assertNoDiff()
124+
self.assertNoDiff(destination)
98125
mock_func.assert_called_with(source='/abcdefg.json',
99126
destination=destination,
100127
force=True)
+20-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
import unittest
2+
from io import StringIO
23

34
from django import test
4-
from django.apps import apps
5-
from django.db.migrations.autodetector import MigrationAutodetector
6-
from django.db.migrations.loader import MigrationLoader
7-
from django.db.migrations.questioner import (
8-
InteractiveMigrationQuestioner, )
9-
from django.db.migrations.state import ProjectState
5+
from django.core.management import call_command
6+
from django.test.utils import override_settings
107

118

9+
@override_settings(
10+
MIGRATION_MODULES={
11+
"cities_light": "cities_light.migrations",
12+
},
13+
)
1214
class TestNoMigrationLeft(test.TestCase):
13-
@unittest.skip("TODO: make the test pass")
1415
def test_no_migration_left(self):
15-
loader = MigrationLoader(None, ignore_no_migrations=True)
16-
conflicts = loader.detect_conflicts()
17-
app_labels = ['cities_light']
18-
19-
autodetector = MigrationAutodetector(
20-
loader.project_state(),
21-
ProjectState.from_apps(apps),
22-
InteractiveMigrationQuestioner(specified_apps=app_labels, dry_run=True),
23-
)
24-
25-
changes = autodetector.changes(
26-
graph=loader.graph,
27-
trim_to_apps=app_labels or None,
28-
convert_apps=app_labels or None,
29-
)
30-
31-
assert 'cities_light' not in changes
16+
out = StringIO()
17+
try:
18+
call_command(
19+
"makemigrations",
20+
"cities_light",
21+
"--dry-run",
22+
"--check",
23+
stdout=out,
24+
stderr=StringIO(),
25+
)
26+
except SystemExit: # pragma: no cover
27+
raise AssertionError("Pending migrations:\n" + out.getvalue()) from None

0 commit comments

Comments
 (0)