sqlalchemyseed 1.0.6__tar.gz → 1.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlalchemyseed-1.0.6/src/sqlalchemyseed.egg-info → sqlalchemyseed-1.0.7}/PKG-INFO +1 -1
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/setup.cfg +1 -1
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/__init__.py +1 -1
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7/src/sqlalchemyseed.egg-info}/PKG-INFO +1 -1
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed.egg-info/SOURCES.txt +6 -1
- sqlalchemyseed-1.0.7/src/sqlalchemyseed.egg-info/requires.txt +4 -0
- sqlalchemyseed-1.0.7/tests/test_json.py +58 -0
- sqlalchemyseed-1.0.7/tests/test_loader.py +41 -0
- sqlalchemyseed-1.0.7/tests/test_seeder.py +596 -0
- sqlalchemyseed-1.0.7/tests/test_temp_seeder.py +10 -0
- sqlalchemyseed-1.0.7/tests/test_validator.py +87 -0
- sqlalchemyseed-1.0.6/src/sqlalchemyseed.egg-info/requires.txt +0 -4
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/LICENSE +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/README.md +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/pyproject.toml +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/setup.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/_future/__init__.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/_future/seeder.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/attribute.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/constants.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/dynamic_seeder.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/errors.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/json.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/loader.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/seeder.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/util.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed/validator.py +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed.egg-info/dependency_links.txt +0 -0
- {sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed.egg-info/top_level.txt +0 -0
|
@@ -19,4 +19,9 @@ src/sqlalchemyseed.egg-info/dependency_links.txt
|
|
|
19
19
|
src/sqlalchemyseed.egg-info/requires.txt
|
|
20
20
|
src/sqlalchemyseed.egg-info/top_level.txt
|
|
21
21
|
src/sqlalchemyseed/_future/__init__.py
|
|
22
|
-
src/sqlalchemyseed/_future/seeder.py
|
|
22
|
+
src/sqlalchemyseed/_future/seeder.py
|
|
23
|
+
tests/test_json.py
|
|
24
|
+
tests/test_loader.py
|
|
25
|
+
tests/test_seeder.py
|
|
26
|
+
tests/test_temp_seeder.py
|
|
27
|
+
tests/test_validator.py
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from sqlalchemyseed.json import JsonWalker
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TestJsonWalker(unittest.TestCase):
|
|
7
|
+
"""
|
|
8
|
+
TestJsonWalker class
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
def setUp(self) -> None:
|
|
12
|
+
self.walker = JsonWalker()
|
|
13
|
+
|
|
14
|
+
def test_forward(self):
|
|
15
|
+
"""
|
|
16
|
+
Test JsonWalker.forward
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
json = {
|
|
20
|
+
'key': {
|
|
21
|
+
'key_1': 'value_1',
|
|
22
|
+
'arr': [
|
|
23
|
+
0,
|
|
24
|
+
1,
|
|
25
|
+
2
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
self.walker.reset(json)
|
|
31
|
+
self.walker.forward(['key', 'key_1'])
|
|
32
|
+
expected_value = json['key']['key_1']
|
|
33
|
+
self.assertEqual(self.walker.json, expected_value)
|
|
34
|
+
|
|
35
|
+
def test_backward(self):
|
|
36
|
+
"""
|
|
37
|
+
Test JsonWalker.backward
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
json = \
|
|
41
|
+
{
|
|
42
|
+
'a': {
|
|
43
|
+
'aa': {
|
|
44
|
+
'aaa': 'value'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
self.walker.reset(json)
|
|
50
|
+
self.walker.forward(['a', 'aa', 'aaa'])
|
|
51
|
+
self.walker.backward()
|
|
52
|
+
self.assertEqual(self.walker.json, json['a']['aa'])
|
|
53
|
+
self.walker.backward()
|
|
54
|
+
self.assertEqual(self.walker.json, json['a'])
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == '__main__':
|
|
58
|
+
unittest.main()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from sqlalchemyseed import loader
|
|
3
|
+
|
|
4
|
+
from src.sqlalchemyseed import load_entities_from_json
|
|
5
|
+
from src.sqlalchemyseed import load_entities_from_yaml
|
|
6
|
+
from src.sqlalchemyseed import load_entities_from_csv
|
|
7
|
+
|
|
8
|
+
class TestLoader(unittest.TestCase):
|
|
9
|
+
def test_load_entities_from_json(self):
|
|
10
|
+
entities = load_entities_from_json('tests/res/data.json')
|
|
11
|
+
self.assertEqual(len(entities), 6)
|
|
12
|
+
|
|
13
|
+
def test_load_entities_from_json_file_not_found(self):
|
|
14
|
+
self.assertRaises(FileNotFoundError,
|
|
15
|
+
lambda: load_entities_from_json('tests/res/non-existent-file'))
|
|
16
|
+
|
|
17
|
+
def test_load_entities_from_yaml(self):
|
|
18
|
+
entities = load_entities_from_yaml('tests/res/data.yml')
|
|
19
|
+
self.assertEqual(len(entities), 2)
|
|
20
|
+
|
|
21
|
+
def test_load_entities_from_yaml_file_not_found(self):
|
|
22
|
+
self.assertRaises(FileNotFoundError,
|
|
23
|
+
lambda: load_entities_from_yaml('tests/res/non-existent-file'))
|
|
24
|
+
|
|
25
|
+
def test_load_entities_from_csv_input_class(self):
|
|
26
|
+
from tests.models import Company
|
|
27
|
+
entities = load_entities_from_csv(
|
|
28
|
+
'tests/res/companies.csv', Company)
|
|
29
|
+
self.assertEqual(len(entities['data']), 3)
|
|
30
|
+
|
|
31
|
+
def test_load_entities_from_csv_input_model_string(self):
|
|
32
|
+
self.assertIsNotNone(load_entities_from_csv(
|
|
33
|
+
'tests/res/companies.csv', "tests.models.Company"))
|
|
34
|
+
|
|
35
|
+
def test_loader_yaml_not_installed(self):
|
|
36
|
+
loader.sys.modules.pop('yaml')
|
|
37
|
+
self.assertRaises(
|
|
38
|
+
ModuleNotFoundError,
|
|
39
|
+
lambda: load_entities_from_yaml('tests/res/data.yml')
|
|
40
|
+
)
|
|
41
|
+
|
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
from sqlalchemy import create_engine
|
|
5
|
+
from sqlalchemy.orm import sessionmaker
|
|
6
|
+
|
|
7
|
+
from sqlalchemyseed import HybridSeeder, Seeder, errors
|
|
8
|
+
from tests import instances as ins
|
|
9
|
+
from tests.models import Base, Company
|
|
10
|
+
from tests.relationships import association_object, many_to_many, many_to_one, one_to_many, one_to_one
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestSeederRelationship(unittest.TestCase):
|
|
14
|
+
"""
|
|
15
|
+
TestSeederRelationship class for testing Seeder class dealing with relationships.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def setUp(self) -> None:
|
|
19
|
+
|
|
20
|
+
self.engine = create_engine('sqlite://')
|
|
21
|
+
Session = sessionmaker( # pylint: disable=invalid-name
|
|
22
|
+
bind=self.engine
|
|
23
|
+
)
|
|
24
|
+
session = Session()
|
|
25
|
+
self.seeder = Seeder(session)
|
|
26
|
+
self.base = None
|
|
27
|
+
|
|
28
|
+
def tearDown(self) -> None:
|
|
29
|
+
self.base.metadata.drop_all(self.engine)
|
|
30
|
+
self.base = None
|
|
31
|
+
|
|
32
|
+
def test_seed_one_to_many(self):
|
|
33
|
+
"""
|
|
34
|
+
Test seed one to many relationship
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
self.base = one_to_many.Base
|
|
38
|
+
self.base.metadata.create_all(self.engine)
|
|
39
|
+
|
|
40
|
+
module_path = 'tests.relationships.one_to_many'
|
|
41
|
+
|
|
42
|
+
json = \
|
|
43
|
+
{
|
|
44
|
+
'model': f'{module_path}.Parent',
|
|
45
|
+
'data': {
|
|
46
|
+
'value': 'parent_1',
|
|
47
|
+
'!children': [
|
|
48
|
+
{
|
|
49
|
+
'model': f'{module_path}.Child',
|
|
50
|
+
'data': {
|
|
51
|
+
'value': 'child_1'
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
'model': f'{module_path}.Child',
|
|
56
|
+
'data': {
|
|
57
|
+
'value': 'child_2'
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
self.seeder.seed(json)
|
|
64
|
+
|
|
65
|
+
# seeder.instances should only contain the first level entities
|
|
66
|
+
self.assertEqual(len(self.seeder.instances), 1)
|
|
67
|
+
|
|
68
|
+
# assign classes to remove module
|
|
69
|
+
Parent = one_to_many.Parent
|
|
70
|
+
Child = one_to_many.Child
|
|
71
|
+
|
|
72
|
+
parent: Parent = self.seeder.instances[0]
|
|
73
|
+
children: List[Child] = parent.children
|
|
74
|
+
|
|
75
|
+
self.assertEqual(parent.value, 'parent_1')
|
|
76
|
+
self.assertEqual(len(children), 2)
|
|
77
|
+
|
|
78
|
+
self.assertEqual(children[0].value, 'child_1')
|
|
79
|
+
self.assertEqual(children[0].parent, parent)
|
|
80
|
+
|
|
81
|
+
self.assertEqual(children[1].value, 'child_2')
|
|
82
|
+
self.assertEqual(children[1].parent, parent)
|
|
83
|
+
|
|
84
|
+
def test_seed_many_to_one(self):
|
|
85
|
+
"""
|
|
86
|
+
Test seed many to one relationship
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
self.base = many_to_one.Base
|
|
90
|
+
self.base.metadata.create_all(self.engine)
|
|
91
|
+
|
|
92
|
+
module_path = 'tests.relationships.many_to_one'
|
|
93
|
+
|
|
94
|
+
json = \
|
|
95
|
+
[
|
|
96
|
+
{
|
|
97
|
+
'model': f'{module_path}.Parent',
|
|
98
|
+
'data': {
|
|
99
|
+
'value': 'parent_1',
|
|
100
|
+
'!child': {
|
|
101
|
+
'model': f'{module_path}.Child',
|
|
102
|
+
'data': {
|
|
103
|
+
'value': 'child_1'
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
'model': f'{module_path}.Parent',
|
|
110
|
+
'data': {
|
|
111
|
+
'value': 'parent_2',
|
|
112
|
+
'!child': {
|
|
113
|
+
'model': f'{module_path}.Child',
|
|
114
|
+
'data': {
|
|
115
|
+
'value': 'child_2'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
self.seeder.seed(json)
|
|
123
|
+
|
|
124
|
+
Parent = many_to_one.Parent
|
|
125
|
+
|
|
126
|
+
self.assertEqual(len(self.seeder.instances), 2)
|
|
127
|
+
|
|
128
|
+
parents: List[Parent] = self.seeder.instances
|
|
129
|
+
|
|
130
|
+
parent_1 = parents[0]
|
|
131
|
+
self.assertEqual(parent_1.value, 'parent_1')
|
|
132
|
+
self.assertEqual(parent_1.child.value, 'child_1')
|
|
133
|
+
self.assertEqual(parent_1.child.parents, [parent_1])
|
|
134
|
+
|
|
135
|
+
parent_2 = parents[1]
|
|
136
|
+
self.assertEqual(parent_2.value, 'parent_2')
|
|
137
|
+
self.assertEqual(parent_2.child.value, 'child_2')
|
|
138
|
+
self.assertEqual(parent_2.child.parents, [parent_2])
|
|
139
|
+
|
|
140
|
+
def test_seed_one_to_one(self):
|
|
141
|
+
"""
|
|
142
|
+
Test seed one to one relationship
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
self.base = one_to_one.Base
|
|
146
|
+
self.base.metadata.create_all(self.engine)
|
|
147
|
+
|
|
148
|
+
module_path = 'tests.relationships.one_to_one'
|
|
149
|
+
|
|
150
|
+
json = \
|
|
151
|
+
{
|
|
152
|
+
'model': f'{module_path}.Parent',
|
|
153
|
+
'data': {
|
|
154
|
+
'value': 'parent_1',
|
|
155
|
+
'!child': {
|
|
156
|
+
'model': f'{module_path}.Child',
|
|
157
|
+
'data': {
|
|
158
|
+
'value': 'child_1'
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
self.seeder.seed(json)
|
|
165
|
+
|
|
166
|
+
self.assertEqual(len(self.seeder.instances), 1)
|
|
167
|
+
|
|
168
|
+
parent = self.seeder.instances[0]
|
|
169
|
+
child = parent.child
|
|
170
|
+
|
|
171
|
+
self.assertEqual(parent.value, 'parent_1')
|
|
172
|
+
self.assertEqual(child.value, 'child_1')
|
|
173
|
+
|
|
174
|
+
self.assertEqual(child.parent, parent)
|
|
175
|
+
|
|
176
|
+
def test_seed_many_to_many(self):
|
|
177
|
+
"""
|
|
178
|
+
Test seed many to many relationship
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
self.base = many_to_many.Base
|
|
182
|
+
self.base.metadata.create_all(self.engine)
|
|
183
|
+
|
|
184
|
+
module_path = 'tests.relationships.many_to_many'
|
|
185
|
+
|
|
186
|
+
json = \
|
|
187
|
+
[
|
|
188
|
+
{
|
|
189
|
+
'model': f'{module_path}.Parent',
|
|
190
|
+
'data': {
|
|
191
|
+
'value': 'parent_1',
|
|
192
|
+
'!children': [
|
|
193
|
+
{
|
|
194
|
+
'model': f'{module_path}.Child',
|
|
195
|
+
'data': {
|
|
196
|
+
'value': 'child_1'
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
'model': f'{module_path}.Child',
|
|
201
|
+
'data': {
|
|
202
|
+
'value': 'child_2'
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
]
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
'model': f'{module_path}.Parent',
|
|
210
|
+
'data': {
|
|
211
|
+
'value': 'parent_2',
|
|
212
|
+
'!children': [
|
|
213
|
+
{
|
|
214
|
+
'model': f'{module_path}.Child',
|
|
215
|
+
'data': {
|
|
216
|
+
'value': 'child_3'
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
'model': f'{module_path}.Child',
|
|
221
|
+
'data': {
|
|
222
|
+
'value': 'child_4'
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
]
|
|
229
|
+
self.seeder.seed(json)
|
|
230
|
+
|
|
231
|
+
self.assertEqual(len(self.seeder.instances), 2)
|
|
232
|
+
|
|
233
|
+
parents = self.seeder.instances
|
|
234
|
+
|
|
235
|
+
parents: List[many_to_many.Parent] = self.seeder.instances
|
|
236
|
+
|
|
237
|
+
self.assertEqual(len(parents), 2)
|
|
238
|
+
|
|
239
|
+
# parent 1
|
|
240
|
+
parent_1: many_to_many.Parent = parents[0]
|
|
241
|
+
self.assertEqual(parent_1.value, 'parent_1')
|
|
242
|
+
|
|
243
|
+
parent_1_children: List[many_to_many.Child] = parent_1.children
|
|
244
|
+
self.assertEqual(len(parent_1_children), 2)
|
|
245
|
+
|
|
246
|
+
child_1 = parent_1_children[0]
|
|
247
|
+
self.assertEqual(child_1.value, 'child_1')
|
|
248
|
+
self.assertEqual(child_1.parents, [parent_1])
|
|
249
|
+
|
|
250
|
+
child_2 = parent_1_children[1]
|
|
251
|
+
self.assertEqual(child_2.value, 'child_2')
|
|
252
|
+
self.assertEqual(child_2.parents, [parent_1])
|
|
253
|
+
|
|
254
|
+
# parent 2
|
|
255
|
+
parent_2: many_to_many.Parent = parents[1]
|
|
256
|
+
self.assertEqual(parent_2.value, 'parent_2')
|
|
257
|
+
|
|
258
|
+
parent_2_children: List[many_to_many.Child] = parent_2.children
|
|
259
|
+
self.assertEqual(len(parent_2_children), 2)
|
|
260
|
+
|
|
261
|
+
child_3 = parent_2_children[0]
|
|
262
|
+
self.assertEqual(child_3.value, 'child_3')
|
|
263
|
+
self.assertEqual(child_3.parents, [parent_2])
|
|
264
|
+
|
|
265
|
+
child_4 = parent_2_children[1]
|
|
266
|
+
self.assertEqual(child_4.value, 'child_4')
|
|
267
|
+
self.assertEqual(child_4.parents, [parent_2])
|
|
268
|
+
|
|
269
|
+
def test_seed_association_object(self):
|
|
270
|
+
"""
|
|
271
|
+
Test seed association object relationship
|
|
272
|
+
"""
|
|
273
|
+
|
|
274
|
+
self.base = many_to_many.Base
|
|
275
|
+
self.base.metadata.create_all(self.engine)
|
|
276
|
+
|
|
277
|
+
module_path = 'tests.relationships.association_object'
|
|
278
|
+
|
|
279
|
+
json = \
|
|
280
|
+
{
|
|
281
|
+
'model': f'{module_path}.Parent',
|
|
282
|
+
'data': {
|
|
283
|
+
'value': 'parent_1',
|
|
284
|
+
'!children': [
|
|
285
|
+
{
|
|
286
|
+
'model': f'{module_path}.Association',
|
|
287
|
+
'data': {
|
|
288
|
+
'extra_value': 'association_1',
|
|
289
|
+
'!child': {
|
|
290
|
+
'model': f'{module_path}.Child',
|
|
291
|
+
'data': {
|
|
292
|
+
'value': 'child_1'
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
]
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
self.seeder.seed(json)
|
|
302
|
+
|
|
303
|
+
self.assertEqual(len(self.seeder.instances), 1)
|
|
304
|
+
|
|
305
|
+
parent: association_object.Parent = self.seeder.instances[0]
|
|
306
|
+
self.assertEqual(parent.value, 'parent_1')
|
|
307
|
+
|
|
308
|
+
self.assertEqual(len(parent.children), 1)
|
|
309
|
+
association: association_object.Association = parent.children[0]
|
|
310
|
+
self.assertEqual(association.extra_value, 'association_1')
|
|
311
|
+
self.assertEqual(association.parent, parent)
|
|
312
|
+
self.assertIsNotNone(association.child)
|
|
313
|
+
|
|
314
|
+
child: association_object.Child = association.child
|
|
315
|
+
self.assertEqual(child.value, 'child_1')
|
|
316
|
+
self.assertEqual(child.parents[0], association)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class TestSeeder(unittest.TestCase):
|
|
320
|
+
"""
|
|
321
|
+
Test class for Seeder class
|
|
322
|
+
"""
|
|
323
|
+
|
|
324
|
+
def setUp(self) -> None:
|
|
325
|
+
self.engine = create_engine('sqlite://')
|
|
326
|
+
self.Session = sessionmaker( # pylint: disable=invalid-name
|
|
327
|
+
bind=self.engine,
|
|
328
|
+
)
|
|
329
|
+
self.session = self.Session()
|
|
330
|
+
self.seeder = Seeder(self.session)
|
|
331
|
+
Base.metadata.create_all(self.engine)
|
|
332
|
+
|
|
333
|
+
def tearDown(self) -> None:
|
|
334
|
+
Base.metadata.drop_all(self.engine)
|
|
335
|
+
|
|
336
|
+
def test_seed_parent(self):
|
|
337
|
+
self.assertIsNone(self.seeder.seed(ins.PARENT))
|
|
338
|
+
|
|
339
|
+
def test_seed_parent_add_to_session_false(self):
|
|
340
|
+
self.assertIsNone(self.seeder.seed(ins.PARENT, add_to_session=False))
|
|
341
|
+
|
|
342
|
+
def test_seed_parent_with_multi_data(self):
|
|
343
|
+
self.assertIsNone(self.seeder.seed(ins.PARENT_WITH_MULTI_DATA))
|
|
344
|
+
|
|
345
|
+
def test_seed_parents(self):
|
|
346
|
+
self.assertIsNone(self.seeder.seed(ins.PARENTS))
|
|
347
|
+
|
|
348
|
+
def test_seed_parents_with_empty_data(self):
|
|
349
|
+
self.assertIsNone(self.seeder.seed(ins.PARENTS_WITH_EMPTY_DATA))
|
|
350
|
+
|
|
351
|
+
def test_seed_parents_with_multi_data(self):
|
|
352
|
+
self.assertIsNone(self.seeder.seed(ins.PARENTS_WITH_MULTI_DATA))
|
|
353
|
+
|
|
354
|
+
def test_seed_parent_to_child(self):
|
|
355
|
+
self.assertIsNone(self.seeder.seed(ins.PARENT_TO_CHILD))
|
|
356
|
+
|
|
357
|
+
def test_seed_parent_to_children(self):
|
|
358
|
+
self.assertIsNone(self.seeder.seed(ins.PARENT_TO_CHILDREN))
|
|
359
|
+
|
|
360
|
+
def test_seed_parent_to_children_without_model(self):
|
|
361
|
+
self.assertIsNone(self.seeder.seed(
|
|
362
|
+
ins.PARENT_TO_CHILDREN_WITHOUT_MODEL))
|
|
363
|
+
|
|
364
|
+
def test_seed_parent_to_children_with_multi_data(self):
|
|
365
|
+
self.assertIsNone(self.seeder.seed(
|
|
366
|
+
ins.PARENT_TO_CHILDREN_WITH_MULTI_DATA))
|
|
367
|
+
|
|
368
|
+
def test_seed_parent_to_child_without_child_model(self):
|
|
369
|
+
self.assertIsNone(self.seeder.seed(
|
|
370
|
+
ins.PARENT_TO_CHILD_WITHOUT_CHILD_MODEL))
|
|
371
|
+
|
|
372
|
+
def test_seed_parent_to_children_with_multi_data_without_model(self):
|
|
373
|
+
self.assertIsNone(self.seeder.seed(
|
|
374
|
+
ins.PARENT_TO_CHILDREN_WITH_MULTI_DATA_WITHOUT_MODEL))
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
class TestHybridSeeder(unittest.TestCase):
|
|
378
|
+
def setUp(self) -> None:
|
|
379
|
+
self.engine = create_engine('sqlite://')
|
|
380
|
+
self.Session = sessionmaker(
|
|
381
|
+
bind=self.engine) # pylint: disable=invalid-name
|
|
382
|
+
Base.metadata.create_all(self.engine)
|
|
383
|
+
|
|
384
|
+
def tearDown(self) -> None:
|
|
385
|
+
Base.metadata.drop_all(self.engine)
|
|
386
|
+
|
|
387
|
+
def test_hybrid_seed_with_relationship(self):
|
|
388
|
+
instance = [
|
|
389
|
+
{
|
|
390
|
+
'model': 'tests.models.Employee',
|
|
391
|
+
'data': [
|
|
392
|
+
{'name': 'John Smith'},
|
|
393
|
+
{'name': 'Juan Dela Cruz'}
|
|
394
|
+
]
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
'model': 'tests.models.Company',
|
|
398
|
+
'data': {
|
|
399
|
+
'name': 'MyCompany',
|
|
400
|
+
'!employees': {
|
|
401
|
+
'model': 'tests.models.Employee',
|
|
402
|
+
'filter': [
|
|
403
|
+
{
|
|
404
|
+
'name': 'John Smith'
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
'name': 'Juan Dela Cruz'
|
|
408
|
+
}
|
|
409
|
+
]
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}]
|
|
413
|
+
|
|
414
|
+
with self.Session() as session:
|
|
415
|
+
seeder = HybridSeeder(session)
|
|
416
|
+
seeder.seed(instance)
|
|
417
|
+
self.assertEqual(len(seeder.instances), 3)
|
|
418
|
+
# session.expire_all()
|
|
419
|
+
|
|
420
|
+
def test_filter_with_foreign_key(self):
|
|
421
|
+
instance = [
|
|
422
|
+
{
|
|
423
|
+
'model': 'tests.models.Company',
|
|
424
|
+
'data': {
|
|
425
|
+
'name': 'MyCompany'
|
|
426
|
+
}
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
'model': 'tests.models.Employee',
|
|
430
|
+
'data': [
|
|
431
|
+
{
|
|
432
|
+
'name': 'John Smith',
|
|
433
|
+
'!company_id': {
|
|
434
|
+
'filter': {
|
|
435
|
+
'name': 'MyCompany'
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
'name': 'Juan Dela Cruz',
|
|
441
|
+
'!company_id': {
|
|
442
|
+
'filter': {
|
|
443
|
+
'name': 'MyCompany'
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
]
|
|
448
|
+
},
|
|
449
|
+
]
|
|
450
|
+
|
|
451
|
+
with self.Session() as session:
|
|
452
|
+
seeder = HybridSeeder(session)
|
|
453
|
+
seeder.seed(instance)
|
|
454
|
+
self.assertEqual(len(seeder.instances), 3)
|
|
455
|
+
# session.expire_all()
|
|
456
|
+
|
|
457
|
+
def test_no_data_key_field(self):
|
|
458
|
+
instance = [
|
|
459
|
+
{
|
|
460
|
+
'model': 'tests.models.Company',
|
|
461
|
+
'filter': {'name': 'MyCompany'}
|
|
462
|
+
}
|
|
463
|
+
]
|
|
464
|
+
|
|
465
|
+
with self.Session() as session:
|
|
466
|
+
session.add(
|
|
467
|
+
Company(name='MyCompany')
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
seeder = HybridSeeder(session)
|
|
471
|
+
seeder.seed(instance)
|
|
472
|
+
self.assertEqual(len(seeder.instances), 0)
|
|
473
|
+
|
|
474
|
+
def test_seed_nested_relationships(self):
|
|
475
|
+
instance = {
|
|
476
|
+
"model": "tests.models.Parent",
|
|
477
|
+
"data": {
|
|
478
|
+
"name": "John Smith",
|
|
479
|
+
"!children": [
|
|
480
|
+
{
|
|
481
|
+
"model": "tests.models.Child",
|
|
482
|
+
"data": {
|
|
483
|
+
"name": "Mark Smith",
|
|
484
|
+
"!children": [
|
|
485
|
+
{
|
|
486
|
+
"model": "tests.models.GrandChild",
|
|
487
|
+
"data": {
|
|
488
|
+
"name": "Alice Smith"
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
]
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
]
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
with self.Session() as session:
|
|
499
|
+
seeder = HybridSeeder(session)
|
|
500
|
+
seeder.seed(instance)
|
|
501
|
+
# print(seeder.instances[0].children[0].children)
|
|
502
|
+
self.assertEqual(
|
|
503
|
+
seeder.instances[0].children[0].children[0].name, "Alice Smith")
|
|
504
|
+
|
|
505
|
+
def test_foreign_key_data_instead_of_filter(self):
|
|
506
|
+
instance = {
|
|
507
|
+
'model': 'tests.models.Employee',
|
|
508
|
+
'data': {
|
|
509
|
+
'name': 'John Smith',
|
|
510
|
+
'!company_id': {
|
|
511
|
+
'model': 'tests.models.Company',
|
|
512
|
+
'data': {
|
|
513
|
+
'name': 'MyCompany'
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
with self.Session() as session:
|
|
521
|
+
seeder = HybridSeeder(session)
|
|
522
|
+
self.assertRaises(errors.InvalidKeyError,
|
|
523
|
+
lambda: seeder.seed(instance))
|
|
524
|
+
|
|
525
|
+
def test_hybrid_seed_parent_to_child_with_ref_attribute(self):
|
|
526
|
+
with self.Session() as session:
|
|
527
|
+
seeder = HybridSeeder(session)
|
|
528
|
+
seeder.seed(ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_COLUMN)
|
|
529
|
+
employee = seeder.instances[1]
|
|
530
|
+
self.assertIsNotNone(employee.company)
|
|
531
|
+
|
|
532
|
+
def test_hybrid_seed_parent_to_child_with_ref_attribute_no_model(self):
|
|
533
|
+
with self.Session() as session:
|
|
534
|
+
seeder = HybridSeeder(session)
|
|
535
|
+
self.assertIsNone(seeder.seed(
|
|
536
|
+
ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_COLUMN_NO_MODEL))
|
|
537
|
+
# print(session.new, session.dirty)
|
|
538
|
+
|
|
539
|
+
def test_hybrid_seed_parent_to_child_with_ref_attribute_relationship(self):
|
|
540
|
+
with self.Session() as session:
|
|
541
|
+
seeder = HybridSeeder(session)
|
|
542
|
+
self.assertIsNone(seeder.seed(
|
|
543
|
+
ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_RELATIONSHIP))
|
|
544
|
+
# print(session.new, session.dirty)
|
|
545
|
+
|
|
546
|
+
def test_hybrid_seed_parent_to_child_with_ref_relationship_no_model(self):
|
|
547
|
+
with self.Session() as session:
|
|
548
|
+
seeder = HybridSeeder(session)
|
|
549
|
+
self.assertIsNone(seeder.seed(
|
|
550
|
+
ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_RELATIONSHIP_NO_MODEL))
|
|
551
|
+
# print(session.new, session.dirty)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
class TestSeederCostumizedPrefix(unittest.TestCase):
|
|
555
|
+
def setUp(self) -> None:
|
|
556
|
+
self.engine = create_engine('sqlite://')
|
|
557
|
+
self.Session = sessionmaker(bind=self.engine)
|
|
558
|
+
Base.metadata.create_all(self.engine)
|
|
559
|
+
|
|
560
|
+
def test_seeder_parent_to_child(self):
|
|
561
|
+
import json
|
|
562
|
+
custom_instance = json.dumps(ins.PARENT_TO_CHILD)
|
|
563
|
+
custom_instance = custom_instance.replace('!', '@')
|
|
564
|
+
custom_instance = json.loads(custom_instance)
|
|
565
|
+
|
|
566
|
+
with self.Session() as session:
|
|
567
|
+
seeder = Seeder(session, ref_prefix='@')
|
|
568
|
+
seeder.seed(custom_instance)
|
|
569
|
+
employee = seeder.instances[0]
|
|
570
|
+
self.assertIsNotNone(employee.company)
|
|
571
|
+
|
|
572
|
+
def test_hybrid_seeder_parent_to_child_with_ref_column(self):
|
|
573
|
+
import json
|
|
574
|
+
custom_instance = json.dumps(
|
|
575
|
+
ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_COLUMN)
|
|
576
|
+
custom_instance = custom_instance.replace('!', '@')
|
|
577
|
+
custom_instance = json.loads(custom_instance)
|
|
578
|
+
|
|
579
|
+
with self.Session() as session:
|
|
580
|
+
seeder = HybridSeeder(session, ref_prefix='@')
|
|
581
|
+
seeder.seed(custom_instance)
|
|
582
|
+
employee = seeder.instances[1]
|
|
583
|
+
self.assertIsNotNone(employee.company)
|
|
584
|
+
|
|
585
|
+
def test_hybrid_seeder_parent_to_child_with_ref_relationship(self):
|
|
586
|
+
import json
|
|
587
|
+
custom_instance = json.dumps(
|
|
588
|
+
ins.HYBRID_SEED_PARENT_TO_CHILD_WITH_REF_RELATIONSHIP)
|
|
589
|
+
custom_instance = custom_instance.replace('!', '@')
|
|
590
|
+
custom_instance = json.loads(custom_instance)
|
|
591
|
+
|
|
592
|
+
with self.Session() as session:
|
|
593
|
+
seeder = HybridSeeder(session, ref_prefix='@')
|
|
594
|
+
seeder.seed(custom_instance)
|
|
595
|
+
employee = seeder.instances[1]
|
|
596
|
+
self.assertIsNotNone(employee.company)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from sqlalchemyseed import validator
|
|
3
|
+
|
|
4
|
+
from src.sqlalchemyseed import errors
|
|
5
|
+
from src.sqlalchemyseed.validator import SchemaValidator, Key, hybrid_validate
|
|
6
|
+
from tests import instances as ins
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TestSchemaValidator(unittest.TestCase):
|
|
10
|
+
def setUp(self) -> None:
|
|
11
|
+
self.source_keys = [Key.data()]
|
|
12
|
+
|
|
13
|
+
def test_parent(self):
|
|
14
|
+
self.assertIsNone(hybrid_validate(ins.PARENT))
|
|
15
|
+
|
|
16
|
+
def test_parent_invalid(self):
|
|
17
|
+
with self.assertRaises(errors.InvalidTypeError):
|
|
18
|
+
hybrid_validate(ins.PARENT_INVALID)
|
|
19
|
+
|
|
20
|
+
def test_parent_empty(self):
|
|
21
|
+
self.assertIsNone(hybrid_validate(ins.PARENT_EMPTY))
|
|
22
|
+
|
|
23
|
+
def test_parent_empty_data_list_invalid(self):
|
|
24
|
+
with self.assertRaises(errors.EmptyDataError):
|
|
25
|
+
hybrid_validate(ins.PARENT_EMPTY_DATA_LIST_INVALID)
|
|
26
|
+
|
|
27
|
+
def test_parent_missing_model_invalid(self):
|
|
28
|
+
with self.assertRaises(errors.MissingKeyError):
|
|
29
|
+
hybrid_validate(ins.PARENT_MISSING_MODEL_INVALID)
|
|
30
|
+
|
|
31
|
+
def test_parent_invalid_model_invalid(self):
|
|
32
|
+
with self.assertRaises(errors.InvalidTypeError):
|
|
33
|
+
hybrid_validate(ins.PARENT_INVALID_MODEL_INVALID)
|
|
34
|
+
|
|
35
|
+
def test_parent_with_extra_length_invalid(self):
|
|
36
|
+
with self.assertRaises(errors.MaxLengthExceededError):
|
|
37
|
+
hybrid_validate(ins.PARENT_WITH_EXTRA_LENGTH_INVALID)
|
|
38
|
+
|
|
39
|
+
def test_parent_with_empty_data(self):
|
|
40
|
+
self.assertIsNone(hybrid_validate(ins.PARENT_WITH_EMPTY_DATA))
|
|
41
|
+
|
|
42
|
+
def test_parent_with_multi_data(self):
|
|
43
|
+
self.assertIsNone(hybrid_validate(ins.PARENT_WITH_MULTI_DATA))
|
|
44
|
+
|
|
45
|
+
def test_parent_without_data_invalid(self):
|
|
46
|
+
self.assertRaises(errors.MissingKeyError,
|
|
47
|
+
lambda: hybrid_validate(ins.PARENT_WITHOUT_DATA_INVALID))
|
|
48
|
+
|
|
49
|
+
def test_parent_with_data_and_invalid_data_invalid(self):
|
|
50
|
+
self.assertRaises(errors.InvalidTypeError,
|
|
51
|
+
lambda: hybrid_validate(ins.PARENT_WITH_DATA_AND_INVALID_DATA_INVALID))
|
|
52
|
+
|
|
53
|
+
def test_parent_with_invalid_data_invalid(self):
|
|
54
|
+
self.assertRaises(errors.InvalidTypeError,
|
|
55
|
+
lambda: hybrid_validate(ins.PARENT_WITH_INVALID_DATA_INVALID))
|
|
56
|
+
|
|
57
|
+
def test_parent_to_child(self):
|
|
58
|
+
self.assertIsNone(hybrid_validate(ins.PARENT_TO_CHILD))
|
|
59
|
+
|
|
60
|
+
def test_parent_to_children(self):
|
|
61
|
+
self.assertIsNone(hybrid_validate(ins.PARENT_TO_CHILDREN))
|
|
62
|
+
|
|
63
|
+
def test_parent_to_children_without_model(self):
|
|
64
|
+
self.assertIsNone(hybrid_validate(
|
|
65
|
+
ins.PARENT_TO_CHILDREN_WITHOUT_MODEL))
|
|
66
|
+
|
|
67
|
+
def test_parent_to_children_with_multi_data(self):
|
|
68
|
+
self.assertIsNone(hybrid_validate(
|
|
69
|
+
ins.PARENT_TO_CHILDREN_WITH_MULTI_DATA))
|
|
70
|
+
|
|
71
|
+
def test_parent_to_children_with_multi_data_without_model(self):
|
|
72
|
+
self.assertIsNone(hybrid_validate(
|
|
73
|
+
ins.PARENT_TO_CHILDREN_WITH_MULTI_DATA_WITHOUT_MODEL))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class TestKey(unittest.TestCase):
|
|
77
|
+
def test_key_equal_key(self):
|
|
78
|
+
self.assertEqual(Key.model(), Key(name='model', type_=str))
|
|
79
|
+
|
|
80
|
+
def test_key_not_equal(self):
|
|
81
|
+
self.assertNotEqual(Key.model(), Key.data())
|
|
82
|
+
|
|
83
|
+
def test_key_equal_string(self):
|
|
84
|
+
self.assertEqual(Key.model(), 'model')
|
|
85
|
+
|
|
86
|
+
def test_key_not_equal_other_instance(self):
|
|
87
|
+
self.assertNotEqual(Key.model(), object())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sqlalchemyseed-1.0.6 → sqlalchemyseed-1.0.7}/src/sqlalchemyseed.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|