author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 18 May 2009 19:50:23 +0200 | |
branch | stable |
changeset 1850 | 75661f0a691b |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
from cubicweb.goa.testlib import * |
2 |
||
3 |
import time |
|
4 |
from mx.DateTime import DateTimeType |
|
5 |
from datetime import datetime |
|
6 |
from cubicweb.goa import db |
|
7 |
||
8 |
from google.appengine.api import datastore |
|
9 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
10 |
class Article(db.Model): |
0 | 11 |
content = db.TextProperty() |
12 |
synopsis = db.StringProperty(default='hello') |
|
13 |
||
14 |
class Blog(db.Model): |
|
15 |
diem = db.DateProperty(required=True, auto_now_add=True) |
|
16 |
title = db.StringProperty(required=True) |
|
17 |
content = db.TextProperty() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
18 |
talks_about = db.ReferenceProperty(Article) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
19 |
cites = db.SelfReferenceProperty() |
0 | 20 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
21 |
|
0 | 22 |
class MetaDataTC(GAEBasedTC): |
23 |
MODEL_CLASSES = (Article, Blog) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
24 |
|
0 | 25 |
def setUp(self): |
26 |
GAEBasedTC.setUp(self) |
|
27 |
self.req = self.request() |
|
28 |
self.a = self.add_entity('Article') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
29 |
self.p = self.add_entity('CWProperty', pkey=u'ui.language', value=u'en') |
0 | 30 |
self.session.commit() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
31 |
|
0 | 32 |
def _test_timestamp(self, entity, attr, sleep=0.1): |
33 |
timestamp = getattr(entity, attr) |
|
34 |
self.failUnless(timestamp) |
|
35 |
self.assertIsInstance(timestamp, DateTimeType) |
|
36 |
self.assertIsInstance(entity.to_gae_model()['s_'+attr], datetime) |
|
37 |
time.sleep(sleep) |
|
38 |
if entity.id == 'Article': |
|
39 |
entity.set_attributes(content=u'zou') |
|
40 |
else: |
|
41 |
entity.set_attributes(value=u'en') |
|
42 |
self.session.commit() |
|
43 |
return timestamp |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
44 |
|
0 | 45 |
def test_creation_date_dbmodel(self): |
46 |
cdate = self._test_timestamp(self.a, 'creation_date') |
|
47 |
self.assertEquals(cdate, self.a.creation_date) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
48 |
|
0 | 49 |
def test_creation_date_yams(self): |
50 |
cdate = self._test_timestamp(self.p, 'creation_date') |
|
51 |
self.assertEquals(cdate, self.p.creation_date) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
52 |
|
0 | 53 |
def test_modification_date_dbmodel(self): |
54 |
mdate = self._test_timestamp(self.a, 'modification_date', sleep=1) |
|
55 |
a = self.execute('Any X WHERE X eid %(x)s', {'x': self.a.eid}, 'x').get_entity(0, 0) |
|
56 |
self.failUnless(mdate < a.modification_date, (mdate, a.modification_date)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
57 |
|
0 | 58 |
def test_modification_date_yams(self): |
59 |
mdate = self._test_timestamp(self.p, 'modification_date', sleep=1) |
|
60 |
p = self.execute('Any X WHERE X eid %(x)s', {'x': self.p.eid}, 'x').get_entity(0, 0) |
|
61 |
self.failUnless(mdate < p.modification_date, (mdate, p.modification_date)) |
|
62 |
||
63 |
def _test_owned_by(self, entity): |
|
64 |
self.assertEquals(len(entity.owned_by), 1) |
|
65 |
owner = entity.owned_by[0] |
|
66 |
self.assertIsInstance(owner, db.Model) |
|
67 |
dbmodel = entity.to_gae_model() |
|
68 |
self.assertEquals(len(dbmodel['s_owned_by']), 1) |
|
69 |
self.assertIsInstance(dbmodel['s_owned_by'][0], datastore.Key) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
70 |
|
0 | 71 |
def test_owned_by_dbmodel(self): |
72 |
self._test_owned_by(self.a) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
73 |
|
0 | 74 |
def test_owned_by_yams(self): |
75 |
self._test_owned_by(self.p) |
|
76 |
||
77 |
def _test_created_by(self, entity): |
|
78 |
self.assertEquals(len(entity.created_by), 1) |
|
79 |
creator = entity.created_by[0] |
|
80 |
self.assertIsInstance(creator, db.Model) |
|
81 |
self.assertIsInstance(entity.to_gae_model()['s_created_by'], datastore.Key) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
82 |
|
0 | 83 |
def test_created_by_dbmodel(self): |
84 |
self._test_created_by(self.a) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
85 |
|
0 | 86 |
def test_created_by_dbmodel(self): |
87 |
self._test_created_by(self.p) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
88 |
|
0 | 89 |
def test_user_owns_dbmodel(self): |
90 |
self.failUnless(self.req.user.owns(self.a.eid)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
91 |
|
0 | 92 |
def test_user_owns_yams(self): |
93 |
self.failUnless(self.req.user.owns(self.p.eid)) |
|
94 |
||
95 |
def test_is_relation(self): |
|
96 |
en = self.execute('Any EN WHERE E name EN, X is E, X eid %(x)s', {'x': self.a.eid}, 'x')[0][0] |
|
97 |
self.assertEquals(en, 'Article') |
|
98 |
en = self.execute('Any EN WHERE E name EN, X is E, X eid %(x)s', {'x': self.p.eid}, 'x')[0][0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
99 |
self.assertEquals(en, 'CWProperty') |
0 | 100 |
en = self.execute('Any EN WHERE E name EN, X is E, X eid %(x)s', {'x': self.req.user.eid}, 'x')[0][0] |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
101 |
self.assertEquals(en, 'CWUser') |
0 | 102 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
103 |
|
0 | 104 |
if __name__ == '__main__': |
105 |
from logilab.common.testlib import unittest_main |
|
106 |
unittest_main() |