11"""Test for cities_light_fixtures management command."""
22import bz2
3+ import json
34import os
45from unittest import mock
56
67from django import test
78from django .core .management import call_command
89from django .core .management .base import CommandError
910
10- # from dbdiff.fixture import Fixture
1111from cities_light .settings import DATA_DIR , FIXTURES_BASE_URL
1212from cities_light .management .commands .cities_light_fixtures import Command
1313from cities_light .downloader import Downloader
1414from 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 )
0 commit comments