-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_models.py
55 lines (42 loc) · 1.62 KB
/
test_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import unittest
from scrapy import Item
from scrapy.item import Field
from jedeschule.items import School
from jedeschule.pipelines.school_pipeline import SchoolPipelineItem
from jedeschule.pipelines.db_pipeline import School as DBSchool, get_session
class TestSchoolItem(Item):
name = Field()
nr = Field()
class TestSchool(unittest.TestCase):
def test_import_new(self):
# Arrange
info = School(name='Test Schule', id='NDS-1')
item = dict(name='Test Schule', nr=1)
school_item: SchoolPipelineItem = SchoolPipelineItem(info=info, item=item)
session = get_session()
db_item = DBSchool.update_or_create(school_item, session)
session.add(db_item)
session.commit()
# Act
count = session.query(DBSchool).count()
# Assert
self.assertEqual(count, 1)
def test_import_existing(self):
# This test requires the previous one to have run already so that the item
# exists in the database
# Arrange
info = School(name='Test Schule (updated)', id='NDS-1')
item = dict(name='Test Schule', nr=1)
school_item: SchoolPipelineItem = SchoolPipelineItem(info=info, item=item)
session = get_session()
db_item = DBSchool.update_or_create(school_item, session)
session.add(db_item)
session.commit()
# Act
count = session.query(DBSchool).count()
db_school = session.query(DBSchool).first()
# Assert
self.assertEqual(count, 1)
self.assertEqual(db_school.name, "Test Schule (updated)")
if __name__ == '__main__':
unittest.main()