root/pdw/tests/test_pd.py @ 326:46fe057f2605

Revision 326:46fe057f2605, 15.2 KB (checked in by acracia, 6 months ago)

[api]to_dict method on the model, tailored to use as input of the api

Line 
1import pdw.pd as pd
2from datetime import datetime
3try:
4    import json
5except ImportError:
6    import simplejson as json
7
8class TestPdStatusCanada:
9    jurisdiction = 'ca'
10
11    def setUp(self):
12        class X:
13            pass
14
15        artist = X()
16        artist.name = 'Bloggs, Joe'
17        artist.death_date_ordered = 1900.0
18        artist.birth_date_ordered = None
19
20        work = X()
21        work.title = 'Song for the Common Man'
22        work.persons = [ artist ]
23        work.date_ordered = 1945.0
24        work.type = 'composition'
25
26        item = X()
27        item.title = 'Songs CD'
28        item.date_ordered = 1955.0
29        item.works = [work]
30
31        work.items = [item]
32
33        self.artist = artist
34        self.work = work
35        self.item = item
36
37    def _log_has(self, str, log):
38        for log_line in log:
39            if str in log_line:
40                return True
41        return False
42       
43    def test_work_status_1(self):
44        self.artist.death_date_ordered = 1900.0
45        self.work.date_ordered = 1945.0
46
47        pdstatus = pd.determine_status(self.work, self.jurisdiction)
48        print pdstatus.log
49        assert self._log_has('publication + 50', pdstatus.log)
50        assert pdstatus.pd_prob == 1.0
51
52    def test_work_status_2(self):
53        self.artist.death_date_ordered = 1930.0
54        self.work.date_ordered = 1965.0
55
56        pdstatus = pd.determine_status(self.work, self.jurisdiction)
57        print pdstatus.log
58        assert self._log_has('publication + 50', pdstatus.log)
59        assert pdstatus.pd_prob == 0.0
60
61    def test_work_status_3(self):
62        self.artist.death_date_ordered = 1980.0
63        self.work.date_ordered = 1945.0
64
65        pdstatus = pd.determine_status(self.work, self.jurisdiction)
66        print pdstatus.log
67        assert self._log_has('death + 50', pdstatus.log)
68        assert pdstatus.pd_prob == 0.0
69
70    def test_work_status_4(self):
71        self.artist.death_date_ordered = 1860.0
72        self.work.date_ordered = 1845.0
73
74        pdstatus = pd.determine_status(self.work, self.jurisdiction)
75        print pdstatus.log
76        assert self._log_has('death + 50', pdstatus.log)
77        assert pdstatus.pd_prob == 1.0
78
79    def test_work_status_5(self):
80        self.artist.death_date_ordered = None
81        self.work.date_ordered = 1945.0
82
83        pdstatus = pd.determine_status(self.work, self.jurisdiction)
84        print pdstatus.log
85        assert self._log_has('Author alive', pdstatus.log)
86        assert pdstatus.pd_prob == 0.0
87
88class TestPdStatusUk:
89    jurisdiction = 'uk'
90
91    @classmethod
92    def setUp(self):
93        class X:
94            pass
95
96        composer = X()
97        composer.name = 'Schumann, Robert'
98        composer.birth_date_ordered = 1830.0
99        composer.death_date_ordered = 1900.0
100
101        singer = X()
102        singer.name = 'Terfel, Bryn'
103        singer.birth_date_ordered = 1974
104        singer.death_date_ordered = None
105
106        composition_work = X()
107        composition_work.title = 'I love flowers'
108        composition_work.persons = [ composer ]
109        composition_work.date_ordered = 1945.0
110        composition_work.type = 'composition'
111
112        recording_work = X()
113        recording_work.title = 'Bryn Terfel sings "I love flowers"'
114        recording_work.persons = [ composer, singer ]
115        recording_work.date_ordered = 1945.0
116        recording_work.type = 'recording'
117
118        cd_item = X()
119        cd_item.title = 'Songs CD'
120        cd_item.date_ordered = 1985.0
121        cd_item.works = [composition_work, recording_work]
122
123        score_item = X()
124        score_item.title = 'Song score'
125        score_item.date_ordered = 1955.0
126        score_item.works = [composition_work]
127
128        composition_work.items = [cd_item, score_item]
129
130        self.composer = composer
131        self.composition_work = composition_work
132        self.recording_work = recording_work
133        self.cd_item = cd_item
134        self.score_item = score_item
135
136
137    def _log_has(self, str, log):
138        for log_line in log:
139            if str in log_line:
140                return True
141        return False
142       
143    def test_work_status_1(self):
144        self.composer.death_date_ordered = 1900.0
145
146        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
147        print pdstatus
148        assert self._log_has('death + 1st Jan + 70', pdstatus.log)
149        assert pdstatus.pd_prob == 1.0
150
151    def test_work_status_2(self):
152        self.composer.death_date_ordered = 1950.0
153
154        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
155        print pdstatus
156        assert self._log_has('death + 1st Jan + 70', pdstatus.log)
157        assert pdstatus.pd_prob == 0.0
158
159    def test_work_status_3(self):
160        print "NAME", self.composition_work.persons[0].name
161        self.composition_work.persons[0].name = 'anonymous'
162        self.composition_work.date_ordered = 1930.0
163
164        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
165        print pdstatus
166        assert self._log_has('first publication + 1st Jan + 70', pdstatus.log)
167        assert pdstatus.pd_prob == 1.0
168
169    def test_work_status_4(self):
170        self.recording_work.date_ordered = 1845.0
171
172        pdstatus = pd.determine_status(self.recording_work, self.jurisdiction)
173        print pdstatus
174        assert self._log_has('first publication + 1st Jan + 50', pdstatus.log)
175        assert pdstatus.pd_prob == 1.0
176
177    def test_work_status_5(self):
178        self.recording_work.date_ordered = 1985.0
179
180        pdstatus = pd.determine_status(self.recording_work, self.jurisdiction)
181        print pdstatus
182        assert self._log_has('first publication + 1st Jan + 50', pdstatus.log)
183        assert pdstatus.pd_prob == 0.0
184
185    def test_work_status_6(self):
186        self.composer.birth_date_ordered = 1820.0
187        self.composer.death_date_ordered = None
188
189        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
190        print pdstatus
191        assert self._log_has('assuming died 100 years after birth', pdstatus.log)
192        assert self._log_has('death + 1st Jan + 70', pdstatus.log)
193        assert pdstatus.pd_prob == 1.0
194
195    def test_work_status_7(self):
196        self.composer.birth_date_ordered = 1920.0
197        self.composer.death_date_ordered = None
198
199        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
200        print pdstatus
201        assert self._log_has('assuming died 100 years after birth', pdstatus.log)
202        assert self._log_has('death + 1st Jan + 70', pdstatus.log)
203        assert pdstatus.pd_prob == 0.0
204
205class TestPdStatusUnitedStates:
206    jurisdiction = 'us'
207
208    @classmethod
209    def setUp(self):
210        class X:
211            pass
212
213        composer = X()
214        composer.name = 'Schumann, Robert'
215        composer.birth_date_ordered = 1830.0
216        composer.death_date_ordered = 1900.0
217
218        singer = X()
219        singer.name = 'Terfel, Bryn'
220        singer.birth_date_ordered = 1974
221        singer.death_date_ordered = None
222
223        composition_work = X()
224        composition_work.title = 'I love flowers'
225        composition_work.persons = [ composer ]
226        composition_work.date_ordered = 1945.0
227        composition_work.type = 'composition'
228
229        recording_work = X()
230        recording_work.title = 'Bryn Terfel sings "I love flowers"'
231        recording_work.persons = [ composer, singer ]
232        recording_work.date_ordered = 1945.0
233        recording_work.type = 'recording'
234
235        cd_item = X()
236        cd_item.title = 'Songs CD'
237        cd_item.date_ordered = 1985.0
238        cd_item.works = [composition_work, recording_work]
239
240        score_item = X()
241        score_item.title = 'Song score'
242        score_item.date_ordered = 1955.0
243        score_item.works = [composition_work]
244
245        composition_work.items = [cd_item, score_item]
246
247        self.composer = composer
248        self.composition_work = composition_work
249        self.recording_work = recording_work
250        self.cd_item = cd_item
251        self.score_item = score_item
252
253
254    def _log_has(self, str, log):
255        for log_line in log:
256            if str in log_line:
257                return True
258        return False
259       
260    def test_work_status_1(self):
261        self.composition_work.date_ordered = 1920.0
262
263        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
264        print pdstatus
265        assert self._log_has('publication + 28 + 28', pdstatus.log)
266        assert pdstatus.pd_prob == 1.0
267
268    def test_work_status_2(self):
269        self.composition_work.date_ordered = 1950.0
270
271        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
272        print pdstatus
273        assert self._log_has('publication + 95', pdstatus.log)
274        assert self._log_has('Assuming its copyright was renewed', pdstatus.log)
275        assert pdstatus.pd_prob == 0.0
276
277    def test_work_status_3(self):
278        self.composition_work.date_ordered = 1970.0
279
280        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
281        print pdstatus
282        assert self._log_has('publication + 95', pdstatus.log)
283        assert not self._log_has('Assuming its copyright was renewed', pdstatus.log)
284        assert pdstatus.pd_prob == 0.0
285
286    def test_work_status_4(self):
287        self.composition_work.date_ordered = 1980.0
288        self.composer.death_date_ordered = 1900
289
290        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
291        print pdstatus
292        assert self._log_has('death + 70', pdstatus.log)
293        assert pdstatus.pd_prob == 1.0
294
295    def test_work_status_5(self):
296        self.composition_work.date_ordered = 1980.0
297        self.composer.death_date_ordered = 1990
298
299        pdstatus = pd.determine_status(self.composition_work, self.jurisdiction)
300        print pdstatus
301        assert self._log_has('death + 70', pdstatus.log)
302        assert pdstatus.pd_prob == 0.0
303
304
305from pdw.tests import *
306import pdw.pd
307import pdw.model as model
308class TestFastPd:
309    @classmethod
310    def setup_class(self):
311        TestController.create_fixtures()
312        pdperson = model.Person.query.filter_by(
313                name=TestController.fxt_name).first()
314        myitem = model.Item(title=u'testing')
315        myitem.persons.append(pdperson)
316        myitem2 = model.Item()
317        person = model.Person(name=u'testing-2', birth_date=u'1836')
318        nonpdperson = model.Person(name=u'testing-nonpd', death_date=u'1990')
319        myitem2.persons.append(person)
320        myitem3 = model.Item(title=u'testing-3', date=u'1856')
321        myitem4 = model.Item(title=u'nonpd')
322        myitem4.persons.append(nonpdperson)
323        model.Session.commit()
324        self.item_id = myitem.id
325        self.item_id_2 = myitem2.id
326        self.item_id_3 = myitem3.id
327        self.item_id_4 = myitem4.id
328        model.Session.remove()
329        self.startnum = model.Extra.query.count()
330        pdw.pd.fast_pd()
331        pdw.pd.fast_pd_year()
332        model.Session.remove()
333
334    @classmethod
335    def teardown_class(self):
336        model.repo.rebuild_db()
337
338    def test_total(self):
339        out = model.Extra.query.all()
340        assert len(out) == self.startnum + 10, '\n'.join([ '%s' % e for e in
341            out])
342   
343    def test_death_year(self):
344        myitem = model.Item.query.get(self.item_id)
345        assert myitem.extras['pd.pdw.fast'] == 1.0, myitem.extras
346        assert myitem.extras['pd_year.life_plus_70'] == 1820, myitem.extras
347
348    def test_by_birth_year(self):
349        myitem = model.Item.query.get(self.item_id_2)
350        assert myitem.extras['pd.pdw.fast'] == 1.0, myitem.extras
351        assert myitem.extras['pd_year.life_plus_70'] == 2006, myitem.extras
352
353    def test_by_pubdate(self):
354        myitem = model.Item.query.get(self.item_id_3)
355        print myitem
356        assert myitem.extras['pd.pdw.fast'] == 1.0, myitem.extras
357        assert myitem.extras['pd_year.life_plus_70'] == 1856+80+70, myitem.extras
358
359    def test_not_pd(self):
360        myitem = model.Item.query.get(self.item_id_4)
361        assert myitem.extras['pd.pdw.fast'] == 0.0, myitem.extras
362        assert myitem.extras['pd_year.life_plus_70'] == 2060, myitem.extras
363
364class TestApiJSON:
365    @classmethod
366    def setup_class(self):
367        self.json_data2 = json.dumps({  "when": "20110101",
368                    "jurisdiction":"uk",
369                    "work": 
370                            {
371                                "title":"Collected Papers on the Public Domain (ed)", 
372                                "type": "text",
373                                "date" : "19230101",
374                                "creation_date" : "19220101",
375                                "persons" : 
376                                    [
377                                            {"name" : "Sturgeon, Theodore", 
378                                            "type" : "person",
379                                            "birth_date" :"19090101",
380                                            "death_date" : "19390205",
381                                            "country": "ca"
382                                            }
383                                    ]
384                            }
385                    })
386        self.json_data1 = json.dumps({  "when": "20110101",
387                    "jurisdiction":"ca",
388                    "work": 
389                            {
390                                "title":"Collected Papers on the Public Domain (ed)", 
391                                "type": "photograph",
392                                "date" : "20030101",
393                                "creation_date" : "20030101",
394                                "persons" : 
395                                    [
396                                            {"name" : "Boyle, James", 
397                                            "type" : "person",
398                                            "birth_date" :"19590101",
399                                            "death_date" : "19890205",
400                                            "country": "uk"
401                                            }
402                                    ]
403                            }
404                    })
405        assert json
406
407    def creating_by_hand(self):
408        newperson = model.Person(name='Borges, Jorge Luis',
409                               death_date='1986',
410                               type='person')
411        newwork = model.Work(title='El Aleph',
412                           date='1949',
413                           creation_date='1919',
414                           type='text')
415        newwork.persons = [ newperson]
416        self.person = newperson
417        self.work = newwork
418        calc = pd.determine_status(newwork,'ca')
419        return calc
420        assert 'confidence' in calc.log
421
422    def test_uk(self):
423        '''
424        test the calculator api for the UK
425        '''
426        parsed = json.loads(self.json_data2)
427        workdata = parsed['work']
428        jurisdiction = parsed['jurisdiction']
429        newwork = model.Work.from_dict(workdata)
430        model.Session.remove()
431        calc = pd.determine_status(newwork,jurisdiction)
432
433        print parsed 
434        return calc
435        assert calc.log>0
436
437    def test_ca(self):
438        '''
439        test the calculator api for Canada
440
441        '''
442        parsed = json.loads(self.json_data1)
443        workdata = parsed['work']
444        jurisdiction = parsed['jurisdiction']
445        newwork = model.Work.from_dict(workdata)
446        model.Session.remove()
447        calc = pd.determine_status(newwork,jurisdiction)
448
449        print parsed 
450        assert calc.log>0
451
Note: See TracBrowser for help on using the browser.