Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50664 views
1
###
2
Test suite for PostgreSQL interface and functionality.
3
4
WARNING: The server timezone **MUST BE** UTC everywhere, or tests will fail!
5
6
COPYRIGHT : (c) 2017 SageMath, Inc.
7
LICENSE : AGPLv3
8
###
9
10
require('coffee-cache')
11
12
DEBUG = !!(process.env['SMC_DEBUG'] ? false)
13
#DEBUG = true
14
# if true, completely deletes database before running tests -- do on schema change for now.
15
RESET = !!(process.env['SMC_DB_RESET'] ? false)
16
#RESET = true
17
DATABASE = 'test-fubar'
18
19
async = require('async')
20
postgres = require('../../postgres')
21
22
if DEBUG
23
log = (args...) -> console.log('pgtest: ', args...)
24
else
25
log = ->
26
27
exports.log = log
28
29
exports.db = undefined
30
exports.setup = (cb) ->
31
async.series([
32
(cb) ->
33
if exports.db?
34
log("db already defined")
35
cb()
36
else
37
exports.db = postgres.db(database:DATABASE, debug:DEBUG, connect:false)
38
if RESET
39
log("delete the entire database (e.g., since schema could have changed from previous runs)")
40
dropdb(cb)
41
else
42
cb()
43
(cb) ->
44
log("ensure db defined and we are connected")
45
exports.db.connect(cb:cb)
46
(cb) ->
47
log("connected")
48
exports.db.update_schema(cb:cb)
49
(cb) ->
50
log("drop contents of tables")
51
exports.teardown(cb)
52
], (err) ->
53
if err
54
log("ERROR running setup", err)
55
cb(err)
56
)
57
58
exports.teardown = (cb) ->
59
# just deletes contents of tables, not schema.
60
exports.db?.delete_all(cb:cb, confirm:'yes')
61
62
# create n accounts
63
exports.create_accounts = (n, m, cb) ->
64
if typeof(m) == 'function'
65
cb = m
66
m = 0
67
f = (i, cb) ->
68
exports.db.create_account
69
first_name : "Firstname#{i}"
70
last_name : "Lastname#{i}"
71
email_address : "sage+#{i}@sagemath.com"
72
cb : cb
73
async.map([m...n+m], f, cb)
74
75
# create n projects owned by the account_id's in the array account_ids (or string account_id)
76
exports.create_projects = (n, account_ids, cb) ->
77
if typeof(account_ids) == "string"
78
account_id = account_ids
79
collabs = []
80
else
81
account_id = account_ids[0]
82
collabs = account_ids.slice(1)
83
f = (i, cb) ->
84
project_id = undefined
85
async.series([
86
(cb) ->
87
exports.db.create_project
88
title : "Project #{i}"
89
description: "Description #{i}"
90
account_id : account_id
91
cb : (err, _project_id) ->
92
project_id = _project_id; cb(err)
93
(cb) ->
94
g = (id, cb) ->
95
exports.db.add_user_to_project
96
account_id: id
97
project_id: project_id
98
cb : cb
99
async.map(collabs, g, cb)
100
], (err) -> cb(err, project_id))
101
async.map([0...n], f, cb)
102
103
# Used to test a sequence of results from a changefeed (see usage below)
104
exports.changefeed_series = (v, cb) ->
105
n = -1
106
done = (err) ->
107
cb?(err)
108
cb = undefined
109
f = (err, x) ->
110
if DEBUG
111
if err
112
console.log("changefeed_series: err=",err)
113
else
114
console.log("changefeed_series: x=#{JSON.stringify(x)}")
115
n += 1
116
if err
117
done(err)
118
return
119
h = v[n]
120
if not h?
121
done()
122
return
123
if typeof(h) != 'function'
124
throw Error("each element of v must be a function, but v[#{n}]='#{h}' is not!")
125
h x, (err) ->
126
if err
127
done(err)
128
else
129
if n+1 >= v.length
130
# success
131
done()
132
return f
133
134
# Start with a clean slate -- delete the test database
135
dropdb = (cb) ->
136
misc_node = require('smc-util-node/misc_node')
137
log 'delete the test database'
138
if not exports.db?
139
console.log "ERROR -- exports.db is not defined!"
140
cb()
141
return
142
port = exports.db._port
143
host = exports.db._host
144
misc_node.execute_code
145
command : 'dropdb'
146
args : ['--port', port, '--host', host, DATABASE]
147
cb : (err) ->
148
exports.db.disconnect()
149
log 'done deleting test database'
150
if err
151
log('WARNING: dropdb error', err)
152
cb() # non-fatal -- would give error if db doesn't exist
153
154
155