Skip to content

Commit 4e70bc0

Browse files
committed
Test package setup and teardown
1 parent e973649 commit 4e70bc0

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

tests/__init__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import logging
2+
import unittest
3+
4+
from cassandra.cluster import Cluster
5+
from cassandra.connection import _loop
6+
from cassandra.policies import HostDistance
7+
8+
log = logging.getLogger()
9+
log.setLevel('DEBUG')
10+
log.addHandler(logging.StreamHandler())
11+
12+
existing_keyspaces = None
13+
14+
def setup_package():
15+
try:
16+
cluster = Cluster()
17+
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
18+
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
19+
session = cluster.connect()
20+
except Exception, exc:
21+
log.error('Failed to connect to cluster:')
22+
log.error(exc)
23+
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
24+
25+
try:
26+
global existing_keyspaces
27+
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
28+
existing_keyspaces = set([row.values()[0] for row in results])
29+
finally:
30+
try:
31+
cluster.shutdown()
32+
except Exception, exc:
33+
log.error('Failed to connect to cluster:')
34+
log.error(exc)
35+
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
36+
37+
38+
def teardown_package():
39+
try:
40+
cluster = Cluster()
41+
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
42+
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
43+
session = cluster.connect()
44+
except Exception, exc:
45+
log.error('Failed to connect to cluster:')
46+
log.error(exc)
47+
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
48+
49+
try:
50+
if existing_keyspaces:
51+
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
52+
current_keyspaces = set([row.values()[0] for row in results])
53+
for keyspace in current_keyspaces - existing_keyspaces:
54+
session.execute("DROP KEYSPACE %s" % (keyspace,))
55+
56+
finally:
57+
try:
58+
cluster.shutdown()
59+
_loop.stop()
60+
except Exception, exc:
61+
log.error('Failed to connect to cluster:')
62+
log.error(exc)

0 commit comments

Comments
 (0)