-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdump_test.py
250 lines (196 loc) · 6.46 KB
/
dump_test.py
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
# -*- coding: utf-8 -*-
"""
test dump
to run on the command line:
python -m unittest test_pout[.ClassTest[.test_method]]
"""
from __future__ import unicode_literals, division, print_function, absolute_import
import unittest
import subprocess
import os
import random
import testdata
import dsnparse
import psycopg2
import psycopg2.extras
class Connection(object):
instance = None
@classmethod
def get_instance(cls):
if not cls.instance:
cls.instance = cls(os.environ["DUMP_DSN"])
return cls.instance
def __init__(self, dsn):
self.dsn = dsnparse.parse(dsn)
self.dbname = self.dsn.dbname
self.user = self.dsn.username
self.password = self.dsn.password
self.host = self.dsn.hostname
self.port = self.dsn.port
if self.dsn.scheme.startswith("postgres"):
self.conn = psycopg2.connect(
dbname=self.dbname,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
cursor_factory=psycopg2.extras.RealDictCursor,
)
self.conn.autocommit = True
else:
raise ValueError("{} is unsupported".format(self.dsn.scheme))
def close(self):
self.conn.close()
type(self).instance = None
class Foo(object):
table_name = "foo"
fields = [
("_id", "BIGSERIAL PRIMARY KEY"),
("bar", "INTEGER"),
]
def __init__(self, **fields):
for k, v in fields.items():
setattr(self, k, v)
def delete(self):
query_str = 'DROP TABLE IF EXISTS "{}" CASCADE'.format(self.table_name)
ret = self.query(query_str, ignore_result=True)
def install(self):
self.delete()
query_str = []
query_str.append('CREATE TABLE "{}" ('.format(self.table_name))
query_fields = []
for field_name, field in self.fields:
query_fields.append(' {} {}'.format(field_name, field))
query_str.append(",\n".join(query_fields))
query_str.append(')')
query_str = "\n".join(query_str)
self.query(query_str, ignore_result=True)
def count(self):
ret = self.query('SELECT COUNT(*) FROM "{}"'.format(self.table_name))
return int(ret[0]['count'])
def save(self):
field_names = []
field_formats = []
query_vals = []
for k, _ in self.fields:
try:
v = getattr(self, k)
query_vals.append(v)
field_names.append(k)
except AttributeError: pass
#query_str = 'INSERT INTO {} ({}) VALUES ({}) RETURNING {}'.format(
query_str = 'INSERT INTO "{}" ("{}") VALUES ({}) RETURNING "_id"'.format(
self.table_name,
'", "'.join(field_names),
', '.join(['%s'] * len(field_names)),
)
return self.query(query_str, query_vals)[0]["_id"]
def query(self, query_str, query_vals=[], **kwargs):
ret = None
cur = Connection.get_instance().conn.cursor()
if query_vals:
cur.execute(query_str, query_vals)
else:
cur.execute(query_str)
q = query_str.lower()
if not kwargs.get("ignore_result", False):
ret = cur.fetchall()
return ret
def close(self):
Connection.get_instance().close()
class Bar(Foo):
table_name = "bar"
fields = [
("_id", "BIGSERIAL PRIMARY KEY"),
("foo", "INTEGER"),
]
class Client(object):
"""makes running a command nice and easy for easy peasy testing"""
@property
def files(self):
for path, dirs, files in os.walk(self.directory):
return [os.path.join(path, f) for f in files]
#return files
def __init__(self):
self.code = 0
self.output = ""
self.directory = testdata.create_dir()
conn = Connection.get_instance()
self.arg_str = " ".join([
"--dbname={}".format(conn.dbname),
"--username={}".format(conn.user),
"--password={}".format(conn.password),
"--host={}".format(conn.host),
"--port={}".format(conn.port),
"--dir={}".format(self.directory),
"--debug",
])
def run(self, arg_str):
cmd = "python -m dump {}".format(arg_str)
try:
self.output = subprocess.check_output(
cmd,
shell=True,
stderr=subprocess.STDOUT,
cwd=os.curdir
)
except subprocess.CalledProcessError as e:
self.code = e.returncode
self.output = e.output
def backup(self, *tables):
subcommand = "backup"
arg_str = "{} {} {}".format(subcommand, self.arg_str, " ".join(tables))
return self.run(arg_str)
def restore(self):
subcommand = "restore"
arg_str = "{} {}".format(subcommand, self.arg_str)
return self.run(arg_str)
class DumpTest(unittest.TestCase):
def setUp(self):
Foo().install()
Bar().install()
def test_default_help(self):
c = Client()
c.run("")
self.assertLess(0, c.code)
def test_table_not_exist(self):
Foo().delete()
c = Client()
c.backup(Foo.table_name)
self.assertEqual(1, c.code, c.output)
def test_full_table_backup_and_restore(self):
for x in range(100):
f = Foo(bar=x)
_id = f.save()
self.assertLess(0, _id)
self.assertEqual(100, Foo().count())
c = Client()
c.backup(Foo.table_name)
self.assertEqual(1, len(c.files))
for path in c.files:
self.assertLess(0, os.path.getsize(path))
c.restore()
self.assertEqual(100, Foo().count())
f = Foo(bar=101)
pk = f.save()
self.assertLess(100, pk)
self.setUp()
c.restore()
self.assertEqual(100, Foo().count())
def test_multi_table_backup(self):
count = 10
for x in range(count):
f = Foo(bar=x)
f.save()
b = Bar(foo=x)
b.save()
self.assertEqual(count, Foo().count())
self.assertEqual(count, Bar().count())
c = Client()
c.backup(Foo.table_name, Bar.table_name)
self.setUp()
c.restore()
self.assertEqual(count, Foo().count())
self.assertEqual(count, Bar().count())
if __name__ == '__main__':
unittest.main()