Skip to content

Commit

Permalink
Merge pull request #32 from CodersOfTheNight/master
Browse files Browse the repository at this point in the history
Fix syntax error
  • Loading branch information
gianlucaborello committed Nov 28, 2018
2 parents 9190e4a + 28f69a6 commit 3fef05a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
40 changes: 27 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
language: python
install:
- echo "deb http://debian.datastax.com/community stable main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
- curl -L http://debian.datastax.com/debian/repo_key | sudo apt-key add -
- sudo apt-get update
- sudo apt-get -y -o Dpkg::Options::=--force-confnew install dsc21 cassandra=2.1.15
- sudo service cassandra stop
- sudo rm -rf /var/lib/cassandra/*
- sudo service cassandra start
- sleep 10
- sudo apt-get install libev4 libev-dev
- pip install --upgrade pip
- pip install blist
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"

services:
- docker

before_install:
- docker pull cassandra:2.1
- docker run --name cassandra -p 127.0.0.1:9042:9042 -p 127.0.0.1:9160:9160 -d cassandra:2.1

install:
- pip install six
- pip install cassandra-driver

script:
- ./tests/run_tests.sh
- cd tests
- make t1
- make t2
- make t3
- make t4
- make t5
- make t6
- make t7
- make t8
- make t9
- make t10
27 changes: 22 additions & 5 deletions cassandradump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
import sys
import itertools
import codecs

from ssl import PROTOCOL_TLSv1

try:
import six
except ImportError:
sys.exit('Please install six compatibility layer, via: \"pip install six\"')

try:
import cassandra
import cassandra.concurrent
Expand All @@ -27,7 +33,11 @@ def cql_type(val):
return val.cql_type

def to_utf8(string):
return codecs.decode(string, 'utf-8')
try:
return codecs.decode(string, 'utf-8')
except TypeError:
# Python3. We already have UTF-8 string.
return string

def log_quiet(msg):
if not args.quiet:
Expand Down Expand Up @@ -65,12 +75,19 @@ def make_value_encoder(typename):
return lambda v: session.encoder.cql_encode_all_types(v) if v is None else e(v)

def make_value_encoders(tableval):
return dict((to_utf8(k), make_value_encoder(cql_type(v))) for k, v in tableval.columns.iteritems())
return dict((to_utf8(k), make_value_encoder(cql_type(v))) for k, v in six.iteritems(tableval.columns))

def make_row_encoder():
def type_selector(*args):
if len(args) == 1:
(k, v) = args[0]
else:
(k, v) = args
return cql_type(v) == 'counter'

partitions = dict(
(has_counter, list(to_utf8(k) for k, v in columns))
for has_counter, columns in itertools.groupby(tableval.columns.iteritems(), lambda (k, v): cql_type(v) == 'counter')
for has_counter, columns in itertools.groupby(six.iteritems(tableval.columns), type_selector)
)

keyspace_utf8 = to_utf8(keyspace)
Expand Down Expand Up @@ -105,7 +122,7 @@ def row_encoder(values):
row_encoder = make_row_encoder()

for row in rows:
values = dict((to_utf8(k), to_utf8(value_encoders[k](v))) for k, v in row.iteritems())
values = dict((to_utf8(k), to_utf8(value_encoders[k](v))) for k, v in six.iteritems(row))
filep.write("%s;\n" % row_encoder(values))

cnt += 1
Expand Down Expand Up @@ -236,7 +253,7 @@ def export_data(session):
f.write('DROP KEYSPACE IF EXISTS "' + keyname + '";\n')
f.write(keyspace.export_as_string() + '\n')

for tablename, tableval in keyspace.tables.iteritems():
for tablename, tableval in six.iteritems(keyspace.tables):
if tablename in exclude_list:
log_quiet('Skipping data export for column family ' + keyname + '.' + tablename + '\n')
continue
Expand Down
41 changes: 41 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
TESTDIR=${PWD}
APPDIR=${TESTDIR}/..

t1:
python ${APPDIR}/cassandradump.py --import-file ${TESTDIR}/test_keyspace_init.cql --sync

t2:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --keyspace test_keyspace
bash compare.sh ${TESTDIR}/full_export.cql.expected /tmp/dump.cql

t3:
python ${APPDIR}/cassandradump.py --import-file ${TESTDIR}/full_export.cql.expected

t4:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --keyspace test_keyspace
bash compare.sh ${TESTDIR}/full_export.cql.expected /tmp/dump.cql

t5:
python ${APPDIR}/cassandradump.py --import-file ${TESTDIR}/full_export.cql.expected --sync

t6:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --keyspace test_keyspace
bash compare.sh ${TESTDIR}/full_export.cql.expected /tmp/dump.cql

t7:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --cf test_keyspace.twenty_rows_composite_table
bash compare.sh ${TESTDIR}/cf_export.cql.expected /tmp/dump.cql

t8:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --cf test_keyspace.twenty_rows_composite_table --no-create
bash compare.sh ${TESTDIR}/cf_export_nocreate.cql.expected /tmp/dump.cql

t9:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --cf test_keyspace.twenty_rows_composite_table --no-insert
bash compare.sh ${TESTDIR}/cf_export_noinsert.cql.expected /tmp/dump.cql

t10:
python ${APPDIR}/cassandradump.py --export-file /tmp/dump.cql --filter "test_keyspace.twenty_rows_composite_table WHERE a = 'A' AND b = '19'"
bash compare.sh ${TESTDIR}/filter.cql.expected /tmp/dump.cql

tests: t1 t2 t3 t4 t5 t6 t7 t8 t9 t10
2 changes: 2 additions & 0 deletions tests/compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
diff -a <(sort $1) <(sort $2)

0 comments on commit 3fef05a

Please sign in to comment.