root/pdw/tests/test_model.py @ 325:61a8069c7b94

Revision 325:61a8069c7b94, 3.9 KB (checked in by acracia, 6 months ago)

changes to the api page, not taking the input and processing it with the calculators.
several other changes added to the model to process from dicts and to dicts

Line 
1from datetime import datetime
2
3from pdw.tests import *
4
5import pdw.model as model 
6import pdw.model.sale
7
8
9class TestFRBR(object):
10    @classmethod
11    def setup_class(self):
12        self.fn = TestController.fxt_name
13        self.pfn = TestController.fxt_pname
14        self.title = TestController.fxt_title
15        self.ptitle = TestController.fxt_ptitle
16        TestController.create_fixtures()
17
18    @classmethod
19    def teardown_class(self):
20        TestController.teardown_fixtures()
21
22    def test_artist(self):
23        art2 = model.Person.query.filter_by(name=self.fn).one()
24        assert art2.birth_date == '1685'
25        assert art2.birth_date_normed == '1685'
26        assert art2.birth_date_ordered == 1685
27        assert art2.death_date == '1750'
28        assert art2.death_date_normed == '1750'
29        assert art2.death_date_ordered == 1750
30        assert art2.aka == u'The Greatest'
31        assert art2.srcid == TestController.fxt_srcid
32
33    def test_work(self):
34        work = model.Work.query.filter_by(title=self.title).one()
35        assert self.title == work.title
36        assert work.srcid == TestController.fxt_srcid
37        assert work.date == '1704'
38        assert work.date_normed == '1704'
39        assert work.date_ordered == 1704
40        print work
41        assert self.fn == work.persons[0].name
42        assert 1 == len(work.items)
43        print work
44        assert work.extras['source'] == 'made-up', work
45
46    def test_item(self):
47        rec = model.Item.query.filter_by(title=self.ptitle).one()
48        assert self.title == rec.works[0].title
49        assert rec.date == '1955'
50        assert rec.date_normed == '1955'
51        assert rec.date_ordered == 1955
52        assert rec.persons[0].name == self.pfn
53        assert rec.srcid == TestController.fxt_srcid
54        assert len(rec.persons) == 2
55   
56    def test_to_dict(self):
57        work = model.Work.query.filter_by(title=self.title).one()
58        out = work.to_dict()
59        assert out['title'] == work.title
60        assert out['persons'] == work.persons
61        assert out['date'] == '1704', out
62        # test it serializes ok
63        outjson = model.json.dumps(out)
64        out2 = model.json.loads(outjson)
65        assert len(out2) == len(out)
66
67    def test_from_dict(self):
68        work = model.Work.query.filter_by(title=self.title).one()
69        ourid = work.id
70        model.Session.remove()
71
72        indict = { 'id': ourid }
73        outwork = model.Work.from_dict(indict)
74        assert outwork.title == self.title
75        model.Session.remove()
76
77        newtitle = u'a new title ...'
78        name1 = u'annakarenina'
79        name2 = u'levin'
80        indict = {
81            'title': newtitle,
82            'date': u'1881',
83            'persons': [
84                { 'name': name1, 'death_date': u'1887' },
85                { 'name': name2, 'death_date': u'1987' }
86                ]
87            }
88        outwork = model.Work.from_dict(indict)
89        assert isinstance(outwork, model.Work)
90        assert outwork.title == newtitle
91        assert outwork.date_ordered == 1881
92        assert len(outwork.persons) == 2, outwork
93
94
95class TestSale(object):
96    @classmethod
97    def setup_class(self):
98        self.fn = TestController.fxt_name
99        self.pfn = TestController.fxt_pname
100        self.title = TestController.fxt_title
101        self.ptitle = TestController.fxt_ptitle
102        TestController.create_fixtures()
103        item = model.Item.query.filter_by(title=self.ptitle).first()
104        sale = pdw.model.sale.Sale()
105        sale.item = item
106        sale.price = 10.0
107        sale.extras = {'abc': 1}
108        model.Session.commit()
109
110    @classmethod
111    def teardown_class(self):
112        for s in pdw.model.sale.Sale.query.all():
113            model.Session.delete(s)
114        TestController.teardown_fixtures()
115
116    def test_sale(self):
117        sale = pdw.model.sale.Sale.query.all()[0]
118        assert sale.item is not None
119        assert sale.price == 10.0
120        assert sale.extras['abc'] == 1
Note: See TracBrowser for help on using the browser.