1
from boto.sdb.db.model import Model
2
from boto.sdb.db.property import StringProperty, IntegerProperty, BooleanProperty
3
from boto.sdb.db.property import DateTimeProperty, FloatProperty, ReferenceProperty
4
from boto.sdb.db.property import PasswordProperty, ListProperty, MapProperty
5
from datetime import datetime
7
from boto.exception import SDBPersistenceError
12
# This will eventually be moved to the boto.tests module and become a real unit test
13
# but for now it will live here. It shows examples of each of the Property types in
14
# use and tests the basic operations.
16
class TestBasic(Model):
18
name = StringProperty()
19
size = IntegerProperty()
20
foo = BooleanProperty()
21
date = DateTimeProperty()
23
class TestFloat(Model):
25
name = StringProperty()
26
value = FloatProperty()
28
class TestRequired(Model):
30
req = StringProperty(required=True, default='foo')
32
class TestReference(Model):
34
ref = ReferenceProperty(reference_class=TestBasic, collection_name='refs')
36
class TestSubClass(TestBasic):
38
answer = IntegerProperty()
40
class TestPassword(Model):
41
password = PasswordProperty()
43
class TestList(Model):
45
name = StringProperty()
46
nums = ListProperty(int)
50
name = StringProperty()
53
class TestListReference(Model):
55
name = StringProperty()
56
basics = ListProperty(TestBasic)
58
class TestAutoNow(Model):
60
create_date = DateTimeProperty(auto_now_add=True)
61
modified_date = DateTimeProperty(auto_now=True)
63
class TestUnique(Model):
64
name = StringProperty(unique=True)
72
t.date = datetime.now()
75
_objects['test_basic_t'] = t
77
print 'now try retrieving it'
78
tt = TestBasic.get_by_id(t.id)
79
_objects['test_basic_tt'] = tt
81
l = TestBasic.get_by_id([t.id])
83
assert l[0].id == t.id
84
assert t.size == tt.size
85
assert t.foo == tt.foo
86
assert t.name == tt.name
87
#assert t.date == tt.date
93
t.name = 'float object'
97
_objects['test_float_t'] = t
99
print 'now try retrieving it'
100
tt = TestFloat.get_by_id(t.id)
101
_objects['test_float_tt'] = tt
103
assert tt.name == t.name
104
assert tt.value == t.value
110
_objects['test_required_t'] = t
114
def test_reference(t=None):
122
tt = TestReference.get_by_id(tt.id)
123
_objects['test_reference_tt'] = tt
124
assert tt.ref.id == t.id
131
_objects['test_subclass_t'] = t
132
t.name = 'a subclass'
139
_objects['test_password_t'] = t
143
# Make sure it stored ok
144
tt = TestPassword.get_by_id(t.id)
145
_objects['test_password_tt'] = tt
146
#Testing password equality
147
assert tt.password == "foo"
148
#Testing password not stored as string
149
assert str(tt.password) != "foo"
154
_objects['test_list_t'] = t
155
t.name = 'a list of ints'
158
tt = TestList.get_by_id(t.id)
159
_objects['test_list_tt'] = tt
160
assert tt.name == t.name
162
assert isinstance(n, int)
164
def test_list_reference():
168
_objects['test_list_ref_t'] = t
169
tt = TestListReference()
174
_objects['test_list_ref_tt'] = tt
175
ttt = TestListReference.get_by_id(tt.id)
176
assert ttt.basics[0].id == t.id
181
name = 'foo' + str(int(time.time()))
184
_objects['test_unique_t'] = t
187
_objects['test_unique_tt'] = tt
192
except(SDBPersistenceError):
199
_objects['test_datetime_t'] = t
201
tt = TestAutoNow.get_by_id(t.id)
202
assert tt.create_date.timetuple() == t.create_date.timetuple()
207
print 'test_required'
209
print 'test_reference'
211
print 'test_subclass'
213
print 'test_password'
217
print 'test_list_reference'
218
test_list_reference()
219
print "test_datetime"
224
if __name__ == "__main__":