-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.lua
277 lines (243 loc) · 10.1 KB
/
test.lua
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
-- make sure you setup a .pgpass file and HYPER_PG_CONN env variable
-- (see README.md#install for instructions on how to setup postgreSQL)
local mytester
local testSchema = 'hypero_test'
local htest = {}
function htest.Postgres()
local dbconn = hypero.Postgres()
local res = dbconn:execute([[
DROP SCHEMA IF EXISTS %s CASCADE;
CREATE SCHEMA %s;
]], {testSchema, testSchema})
mytester:assert(testSchema, "Postgres schema err")
local res, err = dbconn:execute([[
CREATE TABLE %s.test5464 ( n INT4, v FLOAT4, s TEXT );
]], {testSchema})
mytester:assert(res, "Postgres create table err")
res, err = dbconn:fetchOne("SELECT * FROM %s.test5464", testSchema)
mytester:assert(_.isEmpty(res), "Postgres select empty table err")
local param_list = {
{5, 4.1, 'asdfasdf'},
{6, 3.5, 'asdfashhd'},
{6, 3.7, 'asdfashhd2'}
}
res, err = dbconn:executeMany(
string.format([[
INSERT INTO %s.test5464 VALUES (%s, %s, '%s');]],
testSchema, '%s', '%s', '%s'), param_list)
mytester:assertTableEq(res, {1,1,1}, "Postgres insert many err")
res, err = dbconn:fetch([[
SELECT * FROM %s.test5464 WHERE n = 6
]], {testSchema})
mytester:assert(torch.type(res) == 'table')
mytester:assert(#res == 2, "Postgres select serialize err")
mytester:assert(#res[1] == 3, "Postgres missing columns err")
-- test serialization/deserialization of postgres object
local dbconn_str = torch.serialize(dbconn)
dbconn = torch.deserialize(dbconn_str)
local res, err = dbconn:execute([[
CREATE TABLE %s.test5464 ( n INT4, v FLOAT4, s TEXT );
]], {testSchema})
mytester:assert(res == nil and err, "Postgres serialize table exist err")
res, err = dbconn:fetchOne("SELECT * FROM %s.test5464", testSchema)
mytester:assert(torch.type(res) == 'table', "Postgres serialize select table err")
mytester:assert(#res == 3, "Postgres serialize select table err")
res, err = dbconn:executeMany(
string.format([[
INSERT INTO %s.test5464 VALUES (%s, %s, '%s');]],
testSchema, '%s', '%s', '%s'), param_list)
mytester:assertTableEq(res, {1,1,1}, "Postgres serialize insert many err")
res, err = dbconn:fetch([[
SELECT * FROM %s.test5464 WHERE n = 6
]], {testSchema})
mytester:assert(torch.type(res) == 'table')
mytester:assert(#res == 4, "Postgres select serialize err")
mytester:assert(#res[1] == 3, "Postgres missing columns err")
dbconn:close()
end
function htest.Connect()
local dbconn = hypero.Postgres()
local res, err = dbconn:execute("DROP SCHEMA IF EXISTS %s CASCADE", testSchema)
mytester:assert(res, "DROP SCHEMA error")
res, err = dbconn:execute("SELECT * FROM %s.battery", testSchema)
mytester:assert(not res, "Connect DROP SCHEMA not dropped err")
dbconn:close()
local conn = hypero.connect{schema=testSchema}
res, err = conn:execute("SELECT * FROM %s.battery", testSchema)
mytester:assert(res, "Connect battery TABLE err")
res, err = conn:execute("SELECT * FROM %s.version", testSchema)
mytester:assert(res, "Connect version TABLE err")
res, err = conn:execute("SELECT * FROM %s.experiment", testSchema)
mytester:assert(res, "Connect experiment TABLE err")
res, err = conn:execute("SELECT * FROM %s.param", testSchema)
mytester:assert(res, "Connect param TABLE err")
res, err = conn:execute("SELECT * FROM %s.meta", testSchema)
mytester:assert(res, "Connect metadata TABLE err")
res, err = conn:execute("SELECT * FROM %s.result", testSchema)
mytester:assert(res, "Connect result TABLE err")
conn:close()
end
function htest.Battery()
local verbose = false
local dbconn = hypero.Postgres()
local res, err = dbconn:execute("DROP SCHEMA IF EXISTS %s CASCADE", testSchema)
mytester:assert(res, "DROP SCHEMA error")
local conn = hypero.connect{schema=testSchema,dbconn=dbconn}
local batName = "Test 22"
local bat = conn:battery(batName, nil, verbose)
mytester:assert(bat.id == 1, "Battery id err")
mytester:assert(bat.verId == 1, "Battery verId err")
mytester:assert(bat.verDesc == "Initial battery version")
local verDesc = "Version 33"
local verId2, verDesc2 = bat:version(verDesc)
mytester:assert(verId2 == 2, "Battery version() id err")
mytester:assert(verDesc2 == verDesc, "Battery version() desc err")
local bat = conn:battery(batName, verDesc, verbose)
mytester:assert(bat.id == 1, "Battery id err")
mytester:assert(bat.verId == 2, "Battery verId err")
res, err = conn:fetchOne("SELECT COUNT(*) FROM %s.battery", testSchema)
mytester:assert(res, err)
mytester:assert(res[1] == '1')
res, err = conn:fetchOne("SELECT COUNT(*) FROM %s.version", testSchema)
mytester:assert(res, err)
mytester:assert(res[1] == '2')
local verIds = bat:fetchVersions()
mytester:assert(#verIds == 2)
mytester:assertTableEq(verIds, {1, 2})
bat:version("Version 34")
local verIds = bat:fetchVersions("Version 33")
mytester:assert(#verIds == 2)
mytester:assertTableEq(verIds, {2, 3})
local verId = bat:getVerId("Version 33")
mytester:assert(verId == 2)
for j, verDesc in ipairs{'Version 35', 'Version 36'} do
bat:version(verDesc)
-- create some dummy experiments
for i=1,10 do
local hex = bat:experiment()
hex:setParam{lr=0.0001,mom=0.9, i=i, v=verDesc}
hex:setMeta{hostname='bobby', screen=3463, i=i, v=verDesc, d=3}
hex:setResult{valid_acc = 0.0001, i=i, v=verDesc}
end
local res, err = conn:fetch([[
SELECT hex_id FROM %s.experiment
WHERE (bat_id, ver_id) = (%s, %s)
]], {testSchema, bat.id, bat.verId})
mytester:assert(res, err)
mytester:assert(#res == 10)
end
local verIds = bat:fetchVersions("Version 33")
local hexIds = bat:fetchExperiments(verIds)
mytester:assert(#hexIds == 20)
mytester:assert(torch.type(hexIds[1]) == 'number')
local param, colNames = bat:getParam(hexIds, {'i', 'v'})
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 2 and #param[20] == 2)
mytester:assertTableEq(colNames, {'i', 'v'})
local param, colNames = bat:getParam(hexIds, '*')
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 4 and #param[20] == 4)
mytester:assert(#colNames == 4)
local param, colNames = bat:getMeta(hexIds, {'i', 'v'})
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 2 and #param[20] == 2)
mytester:assertTableEq(colNames, {'i', 'v'})
local param, colNames = bat:getMeta(hexIds, '*')
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 5 and #param[20] == 5)
mytester:assert(#colNames == 5)
local param, colNames = bat:getResult(hexIds, {'i', 'v'})
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 2 and #param[20] == 2)
mytester:assertTableEq(colNames, {'i', 'v'})
local param, colNames = bat:getResult(hexIds, '*')
mytester:assert(table.length(param) == 20)
mytester:assert(#param[1] == 3 and #param[20] == 3)
mytester:assert(#colNames == 3)
local tbl, colNames = bat:exportTable()
mytester:assert(table.length(tbl) == 10)
mytester:assert(table.length(tbl[1]) == 13)
local prevHexId = -1
local sorted = true
for i,row in ipairs(tbl) do
sorted = sorted and row[1] > prevHexId
prevHexId = row[1]
end
mytester:assert(sorted)
conn:close()
end
function htest.Experiment()
local dbconn = hypero.Postgres()
local res, err = dbconn:execute("DROP SCHEMA IF EXISTS %s CASCADE", testSchema)
mytester:assert(res, "DROP SCHEMA error")
local conn = hypero.connect{schema=testSchema,dbconn=dbconn}
local batName = "Test 23"
local bat = conn:battery(batName, verDesc, false)
local hex = bat:experiment()
mytester:assert(hex.id == 1)
res, err = conn:fetchOne("SELECT COUNT(*) FROM %s.experiment", testSchema)
mytester:assert(res, err)
mytester:assert(res[1] == '1')
local hex = hypero.Experiment(conn, 1)
mytester:assert(hex.id == 1)
res, err = conn:fetchOne("SELECT COUNT(*) FROM %s.experiment", testSchema)
mytester:assert(res, err)
mytester:assert(res[1] == '1')
local success = pcall(function() return hypero.Experiment(2) end)
mytester:assert(not success)
-- hyperParam
local hp = {lr=0.0001,mom=0.9}
hex:setParam(hp)
local hp2 = hex:getParam()
mytester:assert(hp2.lr == 0.0001)
mytester:assert(hp2.mom == 0.9)
hp.lr = 0.01
mytester:assert(not pcall(function() return hex:setParam(hp) end))
hex:setParam(hp, true)
local hp2 = hex:getParam()
mytester:assert(hp2.lr == 0.01)
-- metaData
local md = {hostname='bobby', screen=3463}
hex:setMeta(md)
local md2 = hex:getMeta()
mytester:assert(md2.hostname == 'bobby')
mytester:assert(md2.screen == 3463)
md.hostname = 'sonny'
mytester:assert(not pcall(function() return hex:setMeta(md) end))
hex:setMeta(md, true)
local md2 = hex:getMeta()
mytester:assert(md2.hostname == 'sonny')
-- result
local res = {valid_acc = 0.0001, test_acc = 0.02}
hex:setResult(res)
local res2 = hex:getResult()
mytester:assert(res2.valid_acc == 0.0001)
mytester:assert(res2.test_acc == 0.02)
res.valid_acc = 0.01
mytester:assert(not pcall(function() return hex:setResult(es) end))
hex:setResult(res, true)
local res2 = hex:getResult()
mytester:assert(res2.valid_acc == 0.01)
conn:close()
end
function htest.Sampler()
local hs = hypero.Sampler()
local val = hs:categorical({0.001, 0.0001, 0.0001, 10000}, {1,2,3,4})
mytester:assert(val == 4, "Sampler err")
local val = hs:normal(0, 1)
mytester:assert(torch.type(val) == 'number')
local val = hs:uniform(0, 1)
mytester:assert(val >= 0 and val <= 1)
local val = hs:logUniform(0, 1)
mytester:assert(val >= math.exp(0) and val <= math.exp(1))
local val = hs:randint(1,100)
mytester:assert(math.floor(val) == val)
mytester:assert(val >= 1 and val <= 100)
end
function hypero.test(tests)
math.randomseed(os.time())
mytester = torch.Tester()
mytester:add(htest)
mytester:run(tests)
return mytester
end