forked from thepaul/cassandra-dtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_tools_test.py
101 lines (77 loc) · 3.23 KB
/
json_tools_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
import os
import tempfile
from dtest import Tester, debug, create_ks
from tools.data import rows_to_list
from tools.decorators import since
@since('0', '2.2.X')
class TestJson(Tester):
def json_tools_test(self):
debug("Starting cluster...")
cluster = self.cluster
cluster.set_batch_commitlog(enabled=True)
cluster.populate(1).start()
debug("Version: " + cluster.version().vstring)
debug("Getting CQLSH...")
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
debug("Inserting data...")
create_ks(session, 'Test', 1)
session.execute("""
CREATE TABLE users (
user_name varchar PRIMARY KEY,
password varchar,
gender varchar,
state varchar,
birth_year bigint
);
""")
session.execute("INSERT INTO Test. users (user_name, password, gender, state, birth_year) VALUES('frodo', 'pass@', 'male', 'CA', 1985);")
session.execute("INSERT INTO Test. users (user_name, password, gender, state, birth_year) VALUES('sam', '@pass', 'male', 'NY', 1980);")
res = session.execute("SELECT * FROM Test. users")
self.assertItemsEqual(rows_to_list(res),
[[u'frodo', 1985, u'male', u'pass@', u'CA'],
[u'sam', 1980, u'male', u'@pass', u'NY']])
debug("Flushing and stopping cluster...")
node1.flush()
cluster.stop()
debug("Exporting to JSON file...")
json_path = tempfile.mktemp(suffix='.schema.json')
with open(json_path, 'w') as f:
node1.run_sstable2json(f)
with open(json_path, 'r') as fin:
data = fin.read().splitlines(True)
if data[0][0] == 'W':
with open(json_path, 'w') as fout:
fout.writelines(data[1:])
debug("Deleting cluster and creating new...")
cluster.clear()
cluster.start()
debug("Inserting data...")
session = self.patient_cql_connection(node1)
create_ks(session, 'Test', 1)
session.execute("""
CREATE TABLE users (
user_name varchar PRIMARY KEY,
password varchar,
gender varchar,
state varchar,
birth_year bigint
);
""")
session.execute("INSERT INTO Test. users (user_name, password, gender, state, birth_year) VALUES('gandalf', 'p@$$', 'male', 'WA', 1955);")
node1.flush()
cluster.stop()
debug("Importing JSON file...")
with open(json_path) as f:
node1.run_json2sstable(f, "test", "users")
os.remove(json_path)
debug("Verifying import...")
cluster.start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
res = session.execute("SELECT * FROM Test. users")
debug("data: " + str(res))
self.assertItemsEqual(rows_to_list(res),
[[u'frodo', 1985, u'male', u'pass@', u'CA'],
[u'sam', 1980, u'male', u'@pass', u'NY'],
[u'gandalf', 1955, u'male', u'p@$$', u'WA']])