-
Notifications
You must be signed in to change notification settings - Fork 21
/
test-db.rb
executable file
·179 lines (161 loc) · 7.14 KB
/
test-db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require_relative 'tester.rb'
class StoreTest < Tester
def test_unique_inv_item
err = assert_raises PG::UniqueViolation do
DB.exec("insert into lineitems(invoice_id, item_id) values(4, 4)")
end
end
#########################################
########################## TEST TRIGGERS:
#########################################
def test_lineitem_calc_price
DB.exec("update lineitems set quantity = 10 where id = 4")
res = DB.exec("select price from lineitems where id = 4")
assert_equal 20, res[0]['price'].to_i
DB.exec("update lineitems set quantity = 50 where id = 4")
res = DB.exec("select price from lineitems where id = 4")
assert_equal 100, res[0]['price'].to_i
DB.exec("update lineitems set quantity = 1, item_id = 2 where id = 4")
res = DB.exec("select price from lineitems where id = 4")
assert_equal 149.99, res[0]['price'].to_f
DB.exec("update lineitems set quantity = 2 where id = 4")
res = DB.exec("select price from lineitems where id = 4")
assert_equal 299.98, res[0]['price'].to_f
end
def test_lineitem_calc_invoice
DB.exec("update lineitems set quantity = 10 where id = 4")
res = DB.exec("select subtotal from invoices where id = 4")
assert_equal 20, res[0]['subtotal'].to_f
DB.exec("insert into lineitems values(default, 4, 2, 1, default)")
res = DB.exec("select subtotal from invoices where id = 4")
assert_equal 169.99, res[0]['subtotal'].to_f
end
def test_lineitem_calc_shipping
DB.exec("insert into lineitems(invoice_id, item_id, quantity) values (4, 2, 1)")
res = DB.exec("select shipping, total from invoices where id = 4")
assert_equal 7, res[0]['shipping'].to_f
assert_equal 158.99, res[0]['total'].to_f
DB.exec("insert into lineitems(invoice_id, item_id, quantity) values (4, 3, 1)")
res = DB.exec("select shipping, total from invoices where id = 4")
assert_equal 9, res[0]['shipping'].to_f
assert_equal 165.99, res[0]['total'].to_f
DB.exec("update lineitems set quantity = 100 where invoice_id = 4 and item_id = 3")
res = DB.exec("select shipping, total from invoices where id = 4")
assert_equal 14, res[0]['shipping'].to_f
assert_equal 665.99, res[0]['total'].to_f
DB.exec("delete from lineitems where invoice_id = 4 and id > 4")
res = DB.exec("select shipping, total from invoices where id = 4")
assert_equal 0, res[0]['shipping'].to_f
assert_equal 2, res[0]['total'].to_f
DB.exec("delete from lineitems where invoice_id = 4")
res = DB.exec("select shipping, total from invoices where id = 4")
assert_equal 0, res[0]['shipping'].to_f
assert_equal 0, res[0]['total'].to_f
end
def test_no_alter_paid_lineitem1
err = assert_raises PG::RaiseException do
DB.exec("delete from lineitems where id = 1")
end
assert err.message.include? 'no_alter_paid_lineitem'
end
def test_no_alter_paid_lineitem2
err = assert_raises PG::RaiseException do
DB.exec("update lineitems set quantity = 9 where id = 1")
end
assert err.message.include? 'no_alter_paid_lineitem'
end
def test_no_alter_shipped_invoice1
err = assert_raises PG::RaiseException do
DB.exec("delete from invoices where id = 1")
end
assert err.message.include? 'no_alter_shipped_invoice'
end
def test_no_alter_shipped_invoice2
err = assert_raises PG::RaiseException do
DB.exec("update invoices set total = 1 where id = 1")
end
assert err.message.include? 'no_alter_shipped_invoice'
end
def test_ok_alter_unshipped_invoice
res = DB.exec("update invoices set ship_date=now(), ship_info='FedEx' where id=2 returning *")
assert_equal Time.now.to_s[0,10], res[0]['ship_date']
assert_equal 'FedEx', res[0]['ship_info']
end
#########################################
######################### TEST FUNCTIONS:
#########################################
def test_invoice_needs_shipment
res = DB.exec("select * from store.invoice_needs_shipment(1)")
assert_equal 't', res[0]['invoice_needs_shipment']
res = DB.exec("select * from store.invoice_needs_shipment(2)")
assert_equal 't', res[0]['invoice_needs_shipment']
res = DB.exec("select * from store.invoice_needs_shipment(3)")
assert_equal 'f', res[0]['invoice_needs_shipment']
res = DB.exec("select * from store.invoice_needs_shipment(4)")
assert_equal 'f', res[0]['invoice_needs_shipment']
res = DB.exec("select * from store.invoice_needs_shipment(99)")
assert_equal 'f', res[0]['invoice_needs_shipment']
end
def test_shipcost
res = DB.exec("select * from store.shipcost('US', 0)")
assert_equal '0', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('CA', 0)")
assert_equal '0', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('ZH', 0)")
assert_equal '0', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('US', 0.1)")
assert_equal '3', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('US', 1)")
assert_equal '4', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('US', 1.5)")
assert_equal '5', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('US', 4)")
assert_equal '7', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('US', 4.01)")
assert_equal '12', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('CA', 0.3)")
assert_equal '5', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('CA', 1)")
assert_equal '6', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('CA', 400)")
assert_equal '13', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('RU', 0.5)")
assert_equal '7', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('IE', 1)")
assert_equal '8', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('IE', 1.01)")
assert_equal '9', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('SG', 400)")
assert_equal '14', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('XX', 1000)")
assert_equal '20', res[0]['shipcost']
res = DB.exec("select * from store.shipcost('XX', -1)")
assert_equal '20', res[0]['shipcost']
end
def test_invoice_shipcost
res = DB.exec("select * from store.invoice_shipcost(1)")
assert_equal '6', res[0]['cost']
res = DB.exec("select * from store.invoice_shipcost(2)")
assert_equal '10', res[0]['cost']
res = DB.exec("select * from store.invoice_shipcost(3)")
assert_equal '0', res[0]['cost']
res = DB.exec("select * from store.invoice_shipcost(4)")
assert_equal '0', res[0]['cost']
end
def test_cart_get
res = DB.exec("select * from store.cart_get_id(1)")
assert_nil res[0]['id']
res = DB.exec("select * from store.cart_get_id(6)")
assert_nil res[0]['id']
res = DB.exec("select * from store.cart_get_id(7)")
assert_equal '4', res[0]['id']
end
def test_cart_new
res = DB.exec("select * from store.cart_new_id(1)")
newid = res[0]['id'].to_i
assert newid > 4
res = DB.exec("select * from invoices where id = #{newid}")
assert_equal '1', res[0]['person_id']
assert_equal 'SG', res[0]['country']
end
end