示例#1
0
def test_static_columns():
    class StaticModel(Model):
        id = columns.Integer(primary_key=True)
        c = columns.Integer(primary_key=True)
        name = columns.Text(static=True)

    drop_table(StaticModel)

    from mock import patch

    from cqlengine.connection import get_session
    session = get_session()

    with patch.object(session, "execute", wraps=session.execute) as m:
        sync_table(StaticModel)

    assert m.call_count > 0
    statement = m.call_args[0][0].query_string
    assert '"name" text static' in statement, statement

    # if we sync again, we should not apply an alter w/ a static
    sync_table(StaticModel)

    with patch.object(session, "execute", wraps=session.execute) as m2:
        sync_table(StaticModel)

    assert len(m2.call_args_list) == 1
    assert "ALTER" not in  m2.call_args[0][0].query_string
示例#2
0
def test_static_columns():
    class StaticModel(Model):
        id = columns.Integer(primary_key=True)
        c = columns.Integer(primary_key=True)
        name = columns.Text(static=True)

    drop_table(StaticModel)

    from mock import patch

    from cqlengine.connection import get_session
    session = get_session()

    with patch.object(session, "execute", wraps=session.execute) as m:
        sync_table(StaticModel)

    assert m.call_count > 0
    statement = m.call_args[0][0].query_string
    assert '"name" text static' in statement, statement

    # if we sync again, we should not apply an alter w/ a static
    sync_table(StaticModel)

    with patch.object(session, "execute", wraps=session.execute) as m2:
        sync_table(StaticModel)

    assert len(m2.call_args_list) == 1
    assert "ALTER" not in m2.call_args[0][0].query_string
示例#3
0
    def test_ttl_included_on_create(self):
        """ tests that ttls on models work as expected """
        session = get_session()

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.ttl(60).create(text="hello blake")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#4
0
    def test_update_includes_ttl(self):
        session = get_session()

        model = TestTTLModel.create(text="goodbye blake")
        with mock.patch.object(session, 'execute') as m:
            model.ttl(60).update(text="goodbye forever")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#5
0
    def test_ttl_included_on_create(self):
        """ tests that ttls on models work as expected """
        session = get_session()

        with mock.patch.object(session, "execute") as m:
            TestTTLModel.ttl(60).create(text="hello blake")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#6
0
    def test_update_includes_ttl(self):
        session = get_session()

        model = TestTTLModel.create(text="goodbye blake")
        with mock.patch.object(session, "execute") as m:
            model.ttl(60).update(text="goodbye forever")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
    def test_check_if_test_model2_saved_to_db2_keyspace(self):

        obj_id = 123456
        TestModel2.objects.create(id=obj_id)

        from cqlengine.connection import get_session
        session = get_session()
        session.set_keyspace('test_db2')
        self.assertEqual(
            session.execute('SELECT id FROM test_model2')[0]['id'], obj_id)
    def test_check_if_model_saved_to_test_keyspace(self):

        now = datetime(2010, 1, 1, 1, 1)
        obj_id = 123456
        ExampleModel.objects.create(id=obj_id, created_at=now)

        from cqlengine.connection import get_session
        session = get_session()
        session.set_keyspace('test_db')
        self.assertEqual(
            session.execute('SELECT id FROM example_model')[0]['id'], obj_id)
示例#9
0
    def test_ttl_included_with_blind_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        tid = o.id

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.objects(id=tid).ttl(60).update(text="bacon")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#10
0
    def test_check_if_model_saved_to_test_keyspace(self):

        now = datetime(2010, 1, 1, 1, 1)
        obj_id = 123456
        ExampleModel.objects.create(id=obj_id, created_at=now)

        from cqlengine.connection import get_session
        session = get_session()
        session.set_keyspace('test_db')
        self.assertEqual(
            session.execute('SELECT id FROM example_model')[0]['id'], obj_id)
示例#11
0
    def test_ttl_included_with_blind_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        tid = o.id

        with mock.patch.object(session, "execute") as m:
            TestTTLModel.objects(id=tid).ttl(60).update(text="bacon")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#12
0
    def test_delete_on_polymorphic_subclass_does_not_include_polymorphic_key(self):
        p1 = Poly1.create()
        session = get_session()
        with mock.patch.object(session, 'execute') as m:
            Poly1.objects(partition=p1.partition).delete()

        # make sure our polymorphic key isn't in the CQL
        # not sure how we would even get here if it was in there
        # since the CQL would fail.

        self.assertNotIn("row_type", m.call_args[0][0].query_string)
示例#13
0
    def test_default_ttl_set(self):
        session = get_session()
        o = TestDefaultTTLModel.create(text="some text on ttl")
        tid = o.id

        self.assertEqual(o._ttl, TestDefaultTTLModel.__default_ttl__)

        with mock.patch.object(session, "execute") as m:
            TestDefaultTTLModel.objects(id=tid).update(text="aligators expired")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#14
0
    def test_ttl_is_include_with_query_on_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        o.text = "new stuff"
        o = o.ttl(60)

        with mock.patch.object(session, 'execute') as m:
            o.save()

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#15
0
    def test_ttl_is_include_with_query_on_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        o.text = "new stuff"
        o = o.ttl(60)

        with mock.patch.object(session, "execute") as m:
            o.save()

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#16
0
    def test_default_ttl_not_set(self):
        session = get_session()

        o = TestTTLModel.create(text="some text")
        tid = o.id

        self.assertIsNone(o._ttl)

        with mock.patch.object(session, "execute") as m:
            TestTTLModel.objects(id=tid).update(text="aligators")

        query = m.call_args[0][0].query_string
        self.assertNotIn("USING TTL", query)
示例#17
0
    def test_default_ttl_set(self):
        session = get_session()
        o = TestDefaultTTLModel.create(text="some text on ttl")
        tid = o.id

        self.assertEqual(o._ttl, TestDefaultTTLModel.__default_ttl__)

        with mock.patch.object(session, 'execute') as m:
            TestDefaultTTLModel.objects(id=tid).update(
                text="aligators expired")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
示例#18
0
    def test_default_ttl_not_set(self):
        session = get_session()

        o = TestTTLModel.create(text="some text")
        tid = o.id

        self.assertIsNone(o._ttl)

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.objects(id=tid).update(text="aligators")

        query = m.call_args[0][0].query_string
        self.assertNotIn("USING TTL", query)
    def execute(self):
        promises = []
        session = get_session()
        for instance in self.instances:
            query = instance.__dmlquery__(instance.__class__, instance)
            query.batch(self._batch)
            query.save()

        for query in self._batch.queries:
            statement = SimpleStatement(str(query))
            params = query.get_context()
            promises.append(session.execute_async(statement, params))

        return [r.result() for r in promises]
示例#20
0
    def execute(self):
        promises = []
        session = get_session()
        for instance in self.instances:
            query = instance.__dmlquery__(instance.__class__, instance)
            query.batch(self._batch)
            query.save()

        for query in self._batch.queries:
            statement = SimpleStatement(str(query))
            params = query.get_context()
            promises.append(session.execute_async(statement, params))

        return [r.result() for r in promises]
示例#21
0
def test_paged_result_handling():
    # addresses #225
    class PagingTest(Model):
        id = columns.Integer(primary_key=True)
        val = columns.Integer()
    sync_table(PagingTest)

    PagingTest.create(id=1, val=1)
    PagingTest.create(id=2, val=2)

    session = get_session()
    with mock.patch.object(session, 'default_fetch_size', 1):
        results = PagingTest.objects()[:]

    assert len(results) == 2
示例#22
0
def test_paged_result_handling():
    # addresses #225
    class PagingTest(Model):
        id = columns.Integer(primary_key=True)
        val = columns.Integer()
    sync_table(PagingTest)

    PagingTest.create(id=1, val=1)
    PagingTest.create(id=2, val=2)

    session = get_session()
    with mock.patch.object(session, 'default_fetch_size', 1):
        results = PagingTest.objects()[:]

    assert len(results) == 2
def test_static_columns():
    class StaticModel(Model):
        id = columns.Integer(primary_key=True)
        c = columns.Integer(primary_key=True)
        name = columns.Text(static=True)

    drop_table(StaticModel)

    from mock import patch

    from cqlengine.connection import get_session
    session = get_session()

    with patch.object(session, "execute", side_effect=Exception) as m:
        try:
            sync_table(StaticModel)
        except:
            pass

    assert m.call_count > 0
    statement = m.call_args[0][0].query_string
    assert '"name" text static' in statement, statement
 def session(self):
     return connection.get_session()
示例#25
0
 def setUp(self):
     self.session = get_session()
     super(BaseCassEngTestCase, self).setUp()
 def session(self):
     return connection.get_session()
示例#27
0
 def setUp(self):
     self.session = get_session()
     super(BaseCassEngTestCase, self).setUp()