def main():
    connection.default()

    # Management functions would normally be used in development, and possibly for deployments.
    # They are typically not part of a core application.
    log.info("### creating keyspace...")
    management.create_keyspace_simple(KEYSPACE, 1)
    log.info("### syncing model...")
    management.sync_table(FamilyMembers)

    # default uuid is assigned
    simmons = FamilyMembers.create(surname='Simmons', name='Gene', birth_year=1949, sex='m')

    # add members to his family later
    FamilyMembers.create(id=simmons.id, surname='Simmons', name='Nick', birth_year=1989, sex='m')
    sophie = FamilyMembers.create(id=simmons.id, surname='Simmons', name='Sophie', sex='f')

    nick = FamilyMembers.objects(id=simmons.id, surname='Simmons', name='Nick')
    try:
        nick.iff(birth_year=1988).update(birth_year=1989)
    except LWTException:
        print "precondition not met"

    # showing validation
    try:
        FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', birth_year=1957, sex='f')
    except ValidationError:
        log.exception('INTENTIONAL VALIDATION EXCEPTION; Failed creating instance:')
        FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', sex='f')

    log.info("### add multiple as part of a batch")
    # If creating many at one time, can use a batch to minimize round-trips
    hogan_id = uuid4()
    with BatchQuery() as b:
        FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Hulk', sex='m')
        FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Linda', sex='f')
        FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Nick', sex='m')
        FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Brooke', sex='f')

    log.info("### All members")
    for m in FamilyMembers.all():
        print m, m.birth_year, m.sex

    log.info("### Select by partition key")
    for m in FamilyMembers.objects(id=simmons.id):
        print m, m.birth_year, m.sex

    log.info("### Constrain on clustering key")
    for m in FamilyMembers.objects(id=simmons.id, surname=simmons.surname):
        print m, m.birth_year, m.sex

    log.info("### Constrain on clustering key")
    kids = FamilyMembers.objects(id=simmons.id, surname=simmons.surname, name__in=['Nick', 'Sophie'])

    log.info("### Delete a record")
    FamilyMembers(id=hogan_id, surname='Hogan', name='Linda').delete()
    for m in FamilyMembers.objects(id=hogan_id):
        print m, m.birth_year, m.sex

    management.drop_keyspace(KEYSPACE)
Exemplo n.º 2
0
 def test_connection_from_session_with_execution_profile(self):
     cluster = Cluster(
         execution_profiles={
             EXEC_PROFILE_DEFAULT: ExecutionProfile(
                 row_factory=dict_factory)
         })
     session = cluster.connect()
     connection.default()
     connection.set_session(session)
     conn = connection.get_connection()
     self.assertEqual(conn.cluster._config_mode, _ConfigMode.PROFILES)
Exemplo n.º 3
0
    def test_only_one_connection_is_created(self):
        """
        Test to ensure that only one new connection is created by
        connection.register_connection

        @since 3.12
        @jira_ticket PYTHON-814
        @expected_result Only one connection is created

        @test_category object_mapper
        """
        number_of_clusters_before = len(_clusters_for_shutdown)
        connection.default()
        number_of_clusters_after = len(_clusters_for_shutdown)
        self.assertEqual(number_of_clusters_after - number_of_clusters_before, 1)
Exemplo n.º 4
0
 def setUpClass(cls):
     """
     initialization logic for the test suite declared in the test module
     code that is executed before all tests in one test run
     :return:
     """
     # request Flak context
     cls.ctx = main.app.test_request_context()
     # cls.ctx = app_watchdog.test_request_context()
     cls.ctx.push()
     # propagate the exceptions to the test client
     main.app.config['TESTING'] = True
     main.app.config['DATABASE_KEYSPACE'] = 'tests'
     connection.default()
     create_keyspace_simple('tests', 1)
Exemplo n.º 5
0
    def test_only_one_connection_is_created(self):
        """
        Test to ensure that only one new connection is created by
        connection.register_connection

        @since 3.12
        @jira_ticket PYTHON-814
        @expected_result Only one connection is created

        @test_category object_mapper
        """
        number_of_clusters_before = len(_clusters_for_shutdown)
        connection.default()
        number_of_clusters_after = len(_clusters_for_shutdown)
        self.assertEqual(number_of_clusters_after - number_of_clusters_before,
                         1)
Exemplo n.º 6
0
    def test_execution_profile_insert_query(self):
        cluster = Cluster(
            execution_profiles={
                EXEC_PROFILE_DEFAULT: ExecutionProfile(
                    row_factory=dict_factory)
            })
        session = cluster.connect()
        connection.default()
        connection.set_session(session)
        self.assertEqual(connection.get_connection().cluster._config_mode,
                         _ConfigMode.PROFILES)

        sync_table(ConnectionModel)
        ConnectionModel.objects.create(key=0, some_data='text0')
        ConnectionModel.objects.create(key=1, some_data='text1')
        self.assertEqual(ConnectionModel.objects(key=0)[0].some_data, 'text0')
Exemplo n.º 7
0
 def test_connection_setup_with_default(self):
     connection.default()
     self.assertIsNotNone(
         connection.get_connection("default").cluster.metadata.get_host(
             "127.0.0.1"))
Exemplo n.º 8
0
 def test_default_connection_uses_legacy(self):
     connection.default()
     conn = connection.get_connection()
     self.assertEqual(conn.cluster._config_mode, _ConfigMode.LEGACY)
Exemplo n.º 9
0
 def test_connection_setup_with_default(self):
     connection.default()
     self.assertIsNotNone(connection.get_connection("default").cluster.metadata.get_host("127.0.0.1"))
Exemplo n.º 10
0
import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine import connection
from datetime import datetime
from cassandra.cqlengine.management import sync_table, create_keyspace
from cassandra.cqlengine.models import Model

##Configuration--------

keyspace="octalspace"
connection.default()
create_keyspace(keyspace, replication_factor=1, strategy_class="SimpleStrategy")
connection.setup(['127.0.0.1'], keyspace, protocol_version=3)

##/Configuration-------

class Account(Model):
    pk = columns.UUID(primary_key=True)
    created_at = columns.DateTime()
    username = columns.Text()

'''
So, how do you do tree data models in cassandra? It is a mystery

My initial thought is that the top level post object, (a self post or a link)
has a list of *all* it's children. That way we can do a batch read and get an entire page. 
Well mostly, one for the object, and once for all of it's descendents

That use case is going to be the vast majority of our data stuff, getting a top level post and *all* descendents.

For displaying it as a tree, we'd need to have each comment contain a list (well, unordered set) of it's immediate children.