1
1
"""Test for cities_light_fixtures management command."""
2
2
import bz2
3
+ import json
3
4
import os
4
5
from unittest import mock
5
6
6
7
from django import test
7
8
from django .core .management import call_command
8
9
from django .core .management .base import CommandError
9
10
10
- # from dbdiff.fixture import Fixture
11
11
from cities_light .settings import DATA_DIR , FIXTURES_BASE_URL
12
12
from cities_light .management .commands .cities_light_fixtures import Command
13
13
from cities_light .downloader import Downloader
14
14
from cities_light .models import City
15
- from .base import FixtureDir
15
+ from .base import TestImportBase , FixtureDir
16
16
17
17
18
- class TestCitiesLigthFixtures (test . TransactionTestCase ):
18
+ class TestCitiesLigthFixtures (TestImportBase ):
19
19
"""Tests for cities_light_fixtures management command."""
20
20
21
21
def test_dump_fixtures (self ):
@@ -36,13 +36,34 @@ def test_dump_fixtures(self):
36
36
mock_func .assert_any_call ('cities_light.SubRegion' , cmd .subregion_path )
37
37
mock_func .assert_any_call ('cities_light.City' , cmd .city_path )
38
38
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
+
39
59
def test_dump_fixture (self ):
40
60
"""
41
61
Test dump_fixture calls dumpdata management command
42
62
and tries to save it to file."""
43
63
# Load test data
44
64
destination = FixtureDir ('import' ).get_file_path ('angouleme.json' )
45
65
call_command ('loaddata' , destination )
66
+
46
67
# Dump
47
68
try :
48
69
fixture_path = os .path .join (os .path .dirname (__file__ ), "fixtures" , "test_dump_fixture.json" )
@@ -52,7 +73,13 @@ def test_dump_fixture(self):
52
73
data = bzfile .read ()
53
74
with open (fixture_path , mode = 'wb' ) as file :
54
75
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
+
56
83
finally :
57
84
if os .path .exists (fixture_path ):
58
85
os .remove (fixture_path )
@@ -86,15 +113,15 @@ def test_load_fixtures(self):
86
113
mock_func .assert_any_call (
87
114
cmd .city_url , cmd .city_path , force = True )
88
115
89
- def test_load_fixture (self ):
116
+ def test_load_fixture_result (self ):
90
117
"""Test loaded fixture matches database content."""
91
118
destination = FixtureDir ('import' ).get_file_path ('angouleme.json' )
92
119
with mock .patch .object (Downloader , 'download' ) as mock_func :
93
120
cmd = Command ()
94
121
cmd .load_fixture (source = '/abcdefg.json' ,
95
122
destination = destination ,
96
123
force = True )
97
- Fixture ( destination ) .assertNoDiff ()
124
+ self .assertNoDiff (destination )
98
125
mock_func .assert_called_with (source = '/abcdefg.json' ,
99
126
destination = destination ,
100
127
force = True )
0 commit comments