Skip to content

Commit

Permalink
Merge pull request #2 from thegeorgeous/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
thegeorgeous committed Dec 13, 2015
2 parents 8bebc7d + 67e52d2 commit fb6fd76
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ dist/

# Sphinx documentation
docs/_build/

# Emacs files
.org
tags

# Vagrant files
Vagrantfile
.vagrant/
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
- cassandra

language: python

python:
- 2.7

env:
- CASSANDRA_DRIVER_VERSION=2.6.0
- CASSANDRA_DRIVER_VERSION=2.7.2

install:
- pip install -q cassandra-driver==$CASSANDRA_DRIVER_VERSION
- pip install -q flask
- python setup.py -q install

script: py.test
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,3 @@ CQLAlchemy provides all the option available in the cqlengine connection.setup()

## Tutorial
For a tutorial on how to use Flask-CQLAlchemy check this [post](http://thegeorgeous.com/2015/06/17/creating-a-tumblelog-with-flask-and-flask-cqlalchemy-I.html)

## TODO/Help Wanted
* Write tests to check compatibility of different cassandra driver versions
5 changes: 2 additions & 3 deletions flask_cqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, app=None):
self.Model = models.Model
self.app = app
self.sync_table = sync_table
self.create_keyspace_simple = create_keyspace_simple
if app is not None:
self.init_app(app)

Expand All @@ -46,8 +47,6 @@ def init_app(self, app):
retry_connect = app.config.get('CASSANDRA_RETRY_CONNECT', False)
setup_kwargs = app.config.get('CASSANDRA_SETUP_KWARGS', {})

# Create a keyspace with a replication factor of 2
# If the keyspace already exists, it will not be modified
if not self._hosts_ and self._keyspace_:
raise NoConfig("No Configuration options defined. At least CASSANDRA_HOSTS and CASSANDRA_CONSISTENCY must be supplied")
connection.setup(self._hosts_,
Expand All @@ -72,7 +71,7 @@ def set_keyspace(self, keyspace_name):
this method must be called again without any arguments
"""
if not keyspace_name:
keyspace_name = self._default_keyspace_
keyspace_name = self._keyspace_
models.DEFAULT_KEYSPACE = keyspace_name
self._keyspace_ = keyspace_name

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
include_package_data=True,
platforms='any',
install_requires=[
'cassandra-driver>=2.5',
'cassandra-driver>=2.6',
'blist'
],
classifiers=[
Expand Down
Empty file added tests/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions tests/test_cqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
import uuid
from flask import Flask
from flask.ext.cqlalchemy import CQLAlchemy
from cassandra.cqlengine.management import drop_keyspace, create_keyspace_simple
from cassandra.cqlengine import models

def make_user_model(db):
class User(db.Model):
uuid = db.columns.UUID(primary_key=True, default=uuid.uuid4)
username = db.columns.Text(index=True, required=False)

return User

class BasicTestCase(unittest.TestCase):

def setUp(self):
app = Flask(__name__)
app.config['CASSANDRA_HOSTS'] = ['127.0.0.1']
app.config['CASSANDRA_KEYSPACE'] = "test1"
app.config['CASSANDRA_SETUP_KWARGS'] = {'protocol_version': 3}
db = CQLAlchemy(app)
self.User = make_user_model(db)
create_keyspace_simple("test1", 1)

self.app = app
self.db = db

def tearDown(self):
drop_keyspace("cqlengine")


def test_sync_db(self):
self.db.sync_db()
user = self.User.create(username="JohnDoe")
self.assertTrue(isinstance(user, self.User))
self.assertEqual(user.username, "JohnDoe")

def test_set_keyspace(self):
create_keyspace_simple("test2", 1)
self.db.set_keyspace('test2')
self.assertEqual(models.DEFAULT_KEYSPACE, "test2")
self.assertEqual(self.db._keyspace_, "test2")

if __name__ == '__main__':
unittest.main()

0 comments on commit fb6fd76

Please sign in to comment.