OLD | NEW |
| (Empty) |
1 from boto.sdb.persist.object import SDBObject | |
2 from boto.sdb.persist.property import StringProperty, PositiveIntegerProperty, I
ntegerProperty | |
3 from boto.sdb.persist.property import BooleanProperty, DateTimeProperty, S3KeyPr
operty | |
4 from boto.sdb.persist.property import ObjectProperty, StringListProperty | |
5 from boto.sdb.persist.property import PositiveIntegerListProperty, BooleanListPr
operty, ObjectListProperty | |
6 from boto.sdb.persist import Manager | |
7 from datetime import datetime | |
8 import time | |
9 | |
10 # | |
11 # This will eventually be moved to the boto.tests module and become a real unit
test | |
12 # but for now it will live here. It shows examples of each of the Property type
s in | |
13 # use and tests the basic operations. | |
14 # | |
15 class TestScalar(SDBObject): | |
16 | |
17 name = StringProperty() | |
18 description = StringProperty() | |
19 size = PositiveIntegerProperty() | |
20 offset = IntegerProperty() | |
21 foo = BooleanProperty() | |
22 date = DateTimeProperty() | |
23 file = S3KeyProperty() | |
24 | |
25 class TestRef(SDBObject): | |
26 | |
27 name = StringProperty() | |
28 ref = ObjectProperty(ref_class=TestScalar) | |
29 | |
30 class TestSubClass1(TestRef): | |
31 | |
32 answer = PositiveIntegerProperty() | |
33 | |
34 class TestSubClass2(TestScalar): | |
35 | |
36 flag = BooleanProperty() | |
37 | |
38 class TestList(SDBObject): | |
39 | |
40 names = StringListProperty() | |
41 numbers = PositiveIntegerListProperty() | |
42 bools = BooleanListProperty() | |
43 objects = ObjectListProperty(ref_class=TestScalar) | |
44 | |
45 def test1(): | |
46 s = TestScalar() | |
47 s.name = 'foo' | |
48 s.description = 'This is foo' | |
49 s.size = 42 | |
50 s.offset = -100 | |
51 s.foo = True | |
52 s.date = datetime.now() | |
53 s.save() | |
54 return s | |
55 | |
56 def test2(ref_name): | |
57 s = TestRef() | |
58 s.name = 'testref' | |
59 rs = TestScalar.find(name=ref_name) | |
60 s.ref = rs.next() | |
61 s.save() | |
62 return s | |
63 | |
64 def test3(): | |
65 s = TestScalar() | |
66 s.name = 'bar' | |
67 s.description = 'This is bar' | |
68 s.size = 24 | |
69 s.foo = False | |
70 s.date = datetime.now() | |
71 s.save() | |
72 return s | |
73 | |
74 def test4(ref1, ref2): | |
75 s = TestList() | |
76 s.names.append(ref1.name) | |
77 s.names.append(ref2.name) | |
78 s.numbers.append(ref1.size) | |
79 s.numbers.append(ref2.size) | |
80 s.bools.append(ref1.foo) | |
81 s.bools.append(ref2.foo) | |
82 s.objects.append(ref1) | |
83 s.objects.append(ref2) | |
84 s.save() | |
85 return s | |
86 | |
87 def test5(ref): | |
88 s = TestSubClass1() | |
89 s.answer = 42 | |
90 s.ref = ref | |
91 s.save() | |
92 # test out free form attribute | |
93 s.fiddlefaddle = 'this is fiddlefaddle' | |
94 s._fiddlefaddle = 'this is not fiddlefaddle' | |
95 return s | |
96 | |
97 def test6(): | |
98 s = TestSubClass2() | |
99 s.name = 'fie' | |
100 s.description = 'This is fie' | |
101 s.size = 4200 | |
102 s.offset = -820 | |
103 s.foo = False | |
104 s.date = datetime.now() | |
105 s.flag = True | |
106 s.save() | |
107 return s | |
108 | |
109 def test(domain_name): | |
110 print 'Initialize the Persistance system' | |
111 Manager.DefaultDomainName = domain_name | |
112 print 'Call test1' | |
113 s1 = test1() | |
114 # now create a new instance and read the saved data from SDB | |
115 print 'Now sleep to wait for things to converge' | |
116 time.sleep(5) | |
117 print 'Now lookup the object and compare the fields' | |
118 s2 = TestScalar(s1.id) | |
119 assert s1.name == s2.name | |
120 assert s1.description == s2.description | |
121 assert s1.size == s2.size | |
122 assert s1.offset == s2.offset | |
123 assert s1.foo == s2.foo | |
124 #assert s1.date == s2.date | |
125 print 'Call test2' | |
126 s2 = test2(s1.name) | |
127 print 'Call test3' | |
128 s3 = test3() | |
129 print 'Call test4' | |
130 s4 = test4(s1, s3) | |
131 print 'Call test5' | |
132 s6 = test6() | |
133 s5 = test5(s6) | |
134 domain = s5._manager.domain | |
135 item1 = domain.get_item(s1.id) | |
136 item2 = domain.get_item(s2.id) | |
137 item3 = domain.get_item(s3.id) | |
138 item4 = domain.get_item(s4.id) | |
139 item5 = domain.get_item(s5.id) | |
140 item6 = domain.get_item(s6.id) | |
141 return [(s1, item1), (s2, item2), (s3, item3), (s4, item4), (s5, item5), (s6
, item6)] | |
OLD | NEW |