import repository from arizona
[raven.git] / apps / gacks / gacksaccount_test.py
1 import time
2 from gacksaccount import GacksAccount
3 from gacksaccountmanager import GacksAccountManager
4
5 gam = GacksAccountManager()
6
7 # remove 3 accounts
8 # if they do not exist, there will be no error
9 gam.delete_account("plc.arizona.test1", "user")
10 gam.delete_account("plc.arizona.test2", "user")
11 gam.delete_account("plc.arizona.test3", "user")
12 gam.delete_account("plc.arizona.test4", "user")
13
14 # make sure they are not here
15 assert(gam.get_account("plc.arizona.test1", "user") == None)
16 assert(gam.get_account("plc.arizona.test2", "user") == None)
17 assert(gam.get_account("plc.arizona.test3", "user") == None)
18 assert(gam.get_account("plc.arizona.test4", "user") == None)
19
20 # now make them
21 gac1 = gam.new_account("plc.arizona.test1", "user")
22 gac2 = gam.new_account("plc.arizona.test2", "user")
23 gac3 = gam.new_account("plc.arizona.test3", "user")
24
25 # make a few changes
26 gac1.set_balance(11)
27 #gac1.set_standing(0)
28 gac1.minBalance = 8;
29 gac2.set_balance(22)
30 gac2.level = "gold"
31 gac3.set_balance(33)
32 gac3.level = "silver"
33
34 # and commit them
35 gac1.commit()
36 gac2.commit()
37 gac3.commit()
38
39 gam.list_accounts()
40
41 def verify(acct, name, kind, level, balance, minBalance, maxBalance):
42     assert(acct.name == name)
43     assert(acct.kind == kind)
44     assert(acct.level == level)
45     assert(acct.balance == balance)
46     assert(acct.minBalance == minBalance)
47     assert(acct.maxBalance == maxBalance)
48
49 # make sure they are here
50 gac1 = gam.get_account("plc.arizona.test1", "user")
51 verify(gac1, "plc.arizona.test1", "user", "default", 11, 8, -1)
52
53 gac2 = gam.get_account("plc.arizona.test2", "user")
54 verify(gac2, "plc.arizona.test2", "user", "gold", 22, 0, -1)
55
56 gac3 = gam.get_account("plc.arizona.test3", "user")
57 verify(gac3, "plc.arizona.test3", "user", "silver", 33, 0, -1)
58
59 # now lets change some fields and verify they were saved
60 gac1.set_balance(111)
61 gac1.set_standing(2)
62 gac1.commit()
63 gac1 = gam.get_account("plc.arizona.test1", "user")
64 verify(gac1, "plc.arizona.test1", "user", "default", 111, 8, -1)
65
66 gac1.maxBalance=1111;
67 gac1.set_balance(1200);
68 assert(gac1.balance == 1111)
69 verify(gac1, "plc.arizona.test1", "user", "default", 1111, 8, 1111)
70
71 gac1.minBalance=123
72 gac1.set_balance(500)
73 assert(gac1.balance == 500)
74 gac1.set_balance(100)
75 assert(gac1.balance == 123)
76 verify(gac1, "plc.arizona.test1", "user", "default", 123, 123, 1111)
77
78 # check the conversion to/from dict
79 d = gac1.as_dict()
80 gac1b = GacksAccount(dict=d)
81 verify(gac1b, "plc.arizona.test1", "user", "default", 123, 123, 1111)
82
83 # lets make sure floats are working
84 gac4 = gam.new_account("plc.arizona.test4", "user")
85 verify(gac4, "plc.arizona.test4", "user", "default", 0, 0, -1)
86 gac4.adjust_balance(0.01)
87 verify(gac4, "plc.arizona.test4", "user", "default", 0.01, 0, -1)
88 gac4.commit()
89 gac4 = gam.get_account("plc.arizona.test4", "user")
90 verify(gac4, "plc.arizona.test4", "user", "default", 0.01, 0, -1)
91
92 # test in inRate
93 gac4.set_balance(0)
94 gac4.inRate = 15
95 gac4.lastInTime = (time.time() - 3600)  # 1 hour
96 gac4.apply_inRate()
97 assert(abs(gac4.balance - 15) < 0.0001)
98 time.sleep(1)
99 gac4.apply_inRate() # should accumulate 1 second worth of units
100 assert(abs(gac4.balance - 15.0042) < 0.0001)
101 gac4.apply_inRate() # should accumulate almost nothing
102 assert(abs(gac4.balance - 15.0042) < 0.0001)
103
104 # test multiplier
105 gac1 = gam.get_account("plc.arizona.test1", "user")
106 assert(gac1.multiplier==1)
107 gac1.multiplier=2
108 gac1.commit()
109 gac1 = gam.get_account("plc.arizona.test1", "user")
110 assert(gac1.multiplier==2)
111
112 print "complete!"
113
114
115
116
117
118