def test_extra_field(self): drop_table(self.TestModel) sync_table(self.TestModel) self.TestModel.create() execute("ALTER TABLE {} add blah int".format( self.TestModel.column_family_name(include_keyspace=True))) self.TestModel.objects().all()
def test_all_size_tiered_options(self): class AllSizeTieredOptionsModel(Model): __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_low__ = .3 __compaction_bucket_high__ = 2 __compaction_min_threshold__ = 2 __compaction_max_threshold__ = 64 __compaction_tombstone_compaction_interval__ = 86400 cid = columns.UUID(primary_key=True) name = columns.Text() drop_table(AllSizeTieredOptionsModel) sync_table(AllSizeTieredOptionsModel) options = get_table_settings(AllSizeTieredOptionsModel).options['compaction_strategy_options'] options = json.loads(options) expected = {u'min_threshold': u'2', u'bucket_low': u'0.3', u'tombstone_compaction_interval': u'86400', u'bucket_high': u'2', u'max_threshold': u'64'} self.assertDictEqual(options, expected)
def test_table_definition(self): """ Tests that creating a table with capitalized column names succeedso """ sync_table(LowercaseKeyModel) sync_table(CapitalizedKeyModel) drop_table(LowercaseKeyModel) drop_table(CapitalizedKeyModel)
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
def test_all_size_tiered_options(self): class AllSizeTieredOptionsModel(Model): __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_low__ = 0.3 __compaction_bucket_high__ = 2 __compaction_min_threshold__ = 2 __compaction_max_threshold__ = 64 __compaction_tombstone_compaction_interval__ = 86400 cid = columns.UUID(primary_key=True) name = columns.Text() drop_table(AllSizeTieredOptionsModel) sync_table(AllSizeTieredOptionsModel) options = get_table_settings(AllSizeTieredOptionsModel).options["compaction_strategy_options"] options = json.loads(options) expected = { u"min_threshold": u"2", u"bucket_low": u"0.3", u"tombstone_compaction_interval": u"86400", u"bucket_high": u"2", u"max_threshold": u"64", } self.assertDictEqual(options, expected)
def test_all_size_tiered_options(self): class AllSizeTieredOptionsModel(Model): __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_low__ = .3 __compaction_bucket_high__ = 2 __compaction_min_threshold__ = 2 __compaction_max_threshold__ = 64 __compaction_tombstone_compaction_interval__ = 86400 cid = columns.UUID(primary_key=True) name = columns.Text() drop_table(AllSizeTieredOptionsModel) sync_table(AllSizeTieredOptionsModel) settings = get_table_settings(AllSizeTieredOptionsModel) options = json.loads(settings['compaction_strategy_options']) expected = { u'min_threshold': u'2', u'bucket_low': u'0.3', u'tombstone_compaction_interval': u'86400', u'bucket_high': u'2', u'max_threshold': u'64' } self.assertDictEqual(options, expected)
def test_non_quality_filtering(): class NonEqualityFilteringModel(Model): __keyspace__ = 'test' example_id = columns.UUID(primary_key=True, default=uuid.uuid4) sequence_id = columns.Integer( primary_key=True) # sequence_id is a clustering key example_type = columns.Integer(index=True) created_at = columns.DateTime() drop_table(NonEqualityFilteringModel) sync_table(NonEqualityFilteringModel) # setup table, etc. NonEqualityFilteringModel.create(sequence_id=1, example_type=0, created_at=datetime.now()) NonEqualityFilteringModel.create(sequence_id=3, example_type=0, created_at=datetime.now()) NonEqualityFilteringModel.create(sequence_id=5, example_type=1, created_at=datetime.now()) qA = NonEqualityFilteringModel.objects( NonEqualityFilteringModel.sequence_id > 3).allow_filtering() num = qA.count() assert num == 1, num
def test_multiple_deletes_dont_fail(self): """ """ sync_table(TestModel) drop_table(TestModel) drop_table(TestModel)
def run(): from cqlengine import connection connection.setup(['127.0.0.1'], "cqlengine") from cqlengine import management management.drop_table(Stock) management.sync_table(Stock) Stock.create(name="WPRO", prices={ datetime.date(2014, 12, 1): 200 , datetime.date(2014, 12, 2): 220.45 , datetime.date(2014, 12, 3): 250.67 , datetime.date(2014, 12, 4): 246.86 , datetime.date(2014, 12, 5): 201 , datetime.date(2014, 12, 6): 233 , datetime.date(2014, 12, 7): 245 , datetime.date(2014, 12, 8): 300 , datetime.date(2014, 12, 9): 307 , datetime.date(2014, 12, 10): 180 , datetime.date(2014, 12, 11): 405 , datetime.date(2014, 12, 12): 400 , datetime.date(2014, 12, 13): 670 , datetime.date(2014, 12, 14): 260 , datetime.date(2014, 12, 15): 250 , datetime.date(2014, 12, 16): 251 , datetime.date(2014, 12, 17): 254 , datetime.date(2014, 12, 18): 267 , datetime.date(2014, 12, 19): 270 }, events={ datetime.date(2014, 12, 13): "Something happened over here", datetime.date(2014, 12, 19): "The bears are playing" }) Stock.create(name="INFY", prices={ datetime.date(2014, 8, 1): 3200 , datetime.date(2014, 8, 2): 3220.45 , datetime.date(2014, 8, 3): 3250.67 , datetime.date(2014, 8, 4): 3246.86 , datetime.date(2014, 8, 5): 3201 , datetime.date(2014, 8, 6): 3233 , datetime.date(2014, 8, 7): 3245 , datetime.date(2014, 8, 8): 3300 , datetime.date(2014, 8, 9): 3307 , datetime.date(2014, 8, 10): 3180 , datetime.date(2014, 8, 11): 3405 , datetime.date(2014, 8, 12): 3400 , datetime.date(2014, 8, 13): 3670 , datetime.date(2014, 8, 14): 3260 , datetime.date(2014, 8, 15): 3250 , datetime.date(2014, 8, 16): 3251 , datetime.date(2014, 8, 17): 3254 , datetime.date(2014, 8, 18): 3267 , datetime.date(2014, 8, 19): 3270 })
def test_alter_actually_alters(self): tmp = copy.deepcopy(LeveledcompactionTestTable) drop_table(tmp) sync_table(tmp) tmp.__compaction__ = SizeTieredCompactionStrategy tmp.__compaction_sstable_size_in_mb__ = None sync_table(tmp) table_settings = get_table_settings(tmp) self.assertRegexpMatches(table_settings['compaction_strategy_class'], '.*SizeTieredCompactionStrategy$')
def test_alter_options(self): class AlterTable(Model): __compaction__ = LeveledCompactionStrategy __compaction_sstable_size_in_mb__ = 64 user_id = columns.UUID(primary_key=True) name = columns.Text() drop_table(AlterTable) sync_table(AlterTable) AlterTable.__compaction_sstable_size_in_mb__ = 128 sync_table(AlterTable)
def test_all_leveled_options(self): class AllLeveledOptionsModel(Model): __compaction__ = LeveledCompactionStrategy __compaction_sstable_size_in_mb__ = 64 cid = columns.UUID(primary_key=True) name = columns.Text() drop_table(AllLeveledOptionsModel) sync_table(AllLeveledOptionsModel) settings = get_table_settings(AllLeveledOptionsModel) options = json.loads(settings['compaction_strategy_options']) self.assertDictEqual(options, {u'sstable_size_in_mb': u'64'})
def test_clustering_order_more_complex(self): """ Tests that models can be saved and retrieved """ sync_table(TestClusteringComplexModel) items = list(range(20)) random.shuffle(items) for i in items: TestClusteringComplexModel.create(id=1, clustering_key=i, some_value=2) values = list(TestClusteringComplexModel.objects.values_list('some_value', flat=True)) self.assertEquals([2] * 20, values) drop_table(TestClusteringComplexModel)
def test_compaction_not_altered_without_changes_leveled(self): from cqlengine.management import update_compaction class LeveledCompactionChangesDetectionTest(Model): __compaction__ = LeveledCompactionStrategy __compaction_sstable_size_in_mb__ = 160 __compaction_tombstone_threshold__ = 0.125 __compaction_tombstone_compaction_interval__ = 3600 pk = columns.Integer(primary_key=True) drop_table(LeveledCompactionChangesDetectionTest) sync_table(LeveledCompactionChangesDetectionTest) assert not update_compaction(LeveledCompactionChangesDetectionTest)
def test_compaction_not_altered_without_changes_leveled(self): from cqlengine.management import update_compaction class LeveledCompactionChangesDetectionTest(Model): __keyspace__ = 'test' __compaction__ = LeveledCompactionStrategy __compaction_sstable_size_in_mb__ = 160 __compaction_tombstone_threshold__ = 0.125 __compaction_tombstone_compaction_interval__ = 3600 pk = columns.Integer(primary_key=True) drop_table(LeveledCompactionChangesDetectionTest) sync_table(LeveledCompactionChangesDetectionTest) assert not update_compaction(LeveledCompactionChangesDetectionTest)
def test_concrete_class_table_creation_cycle(self): """ Tests that models with inherited abstract classes can be created, and have io performed """ from cqlengine.management import sync_table, drop_table sync_table(ConcreteModelWithCol) w1 = ConcreteModelWithCol.create(pkey=5, data=6) w2 = ConcreteModelWithCol.create(pkey=6, data=7) r1 = ConcreteModelWithCol.get(pkey=5) r2 = ConcreteModelWithCol.get(pkey=6) assert w1.pkey == r1.pkey assert w1.data == r1.data assert w2.pkey == r2.pkey assert w2.data == r2.data drop_table(ConcreteModelWithCol)
def test_batch_execute_on_exception_succeeds(self): # makes sure if execute_on_exception == True we still apply the batch drop_table(BatchQueryLogModel) sync_table(BatchQueryLogModel) obj = BatchQueryLogModel.objects(k=1) self.assertEqual(0, len(obj)) try: with BatchQuery(execute_on_exception=True) as b: BatchQueryLogModel.batch(b).create(k=1, v=1) raise Exception("Blah") except: pass obj = BatchQueryLogModel.objects(k=1) # should be 1 because the batch should execute self.assertEqual(1, len(obj))
def test_batch_execute_on_exception_skips_if_not_specified(self): # makes sure if execute_on_exception == True we still apply the batch drop_table(BatchQueryLogModel) sync_table(BatchQueryLogModel) obj = BatchQueryLogModel.objects(k=2) self.assertEqual(0, len(obj)) try: with BatchQuery() as b: BatchQueryLogModel.batch(b).create(k=2, v=2) raise Exception("Blah") except: pass obj = BatchQueryLogModel.objects(k=2) # should be 0 because the batch should not execute self.assertEqual(0, len(obj))
def test_clustering_order_more_complex(self): """ Tests that models can be saved and retrieved """ sync_table(TestClusteringComplexModel) items = list(range(20)) random.shuffle(items) for i in items: TestClusteringComplexModel.create(id=1, clustering_key=i, some_value=2) values = list( TestClusteringComplexModel.objects.values_list('some_value', flat=True)) self.assertEquals([2] * 20, values) drop_table(TestClusteringComplexModel)
def setUpClass(cls): super(BaseQuerySetUsage, cls).setUpClass() drop_table(TestModel) drop_table(IndexedTestModel) sync_table(TestModel) sync_table(IndexedTestModel) sync_table(TestMultiClusteringModel) TestModel.objects.create(test_id=0, attempt_id=0, description="try1", expected_result=5, test_result=30) TestModel.objects.create(test_id=0, attempt_id=1, description="try2", expected_result=10, test_result=30) TestModel.objects.create(test_id=0, attempt_id=2, description="try3", expected_result=15, test_result=30) TestModel.objects.create(test_id=0, attempt_id=3, description="try4", expected_result=20, test_result=25) TestModel.objects.create(test_id=1, attempt_id=0, description="try5", expected_result=5, test_result=25) TestModel.objects.create(test_id=1, attempt_id=1, description="try6", expected_result=10, test_result=25) TestModel.objects.create(test_id=1, attempt_id=2, description="try7", expected_result=15, test_result=25) TestModel.objects.create(test_id=1, attempt_id=3, description="try8", expected_result=20, test_result=20) TestModel.objects.create(test_id=2, attempt_id=0, description="try9", expected_result=50, test_result=40) TestModel.objects.create(test_id=2, attempt_id=1, description="try10", expected_result=60, test_result=40) TestModel.objects.create(test_id=2, attempt_id=2, description="try11", expected_result=70, test_result=45) TestModel.objects.create(test_id=2, attempt_id=3, description="try12", expected_result=75, test_result=45) IndexedTestModel.objects.create(test_id=0, attempt_id=0, description="try1", expected_result=5, test_result=30) IndexedTestModel.objects.create(test_id=1, attempt_id=1, description="try2", expected_result=10, test_result=30) IndexedTestModel.objects.create(test_id=2, attempt_id=2, description="try3", expected_result=15, test_result=30) IndexedTestModel.objects.create(test_id=3, attempt_id=3, description="try4", expected_result=20, test_result=25) IndexedTestModel.objects.create(test_id=4, attempt_id=0, description="try5", expected_result=5, test_result=25) IndexedTestModel.objects.create(test_id=5, attempt_id=1, description="try6", expected_result=10, test_result=25) IndexedTestModel.objects.create(test_id=6, attempt_id=2, description="try7", expected_result=15, test_result=25) IndexedTestModel.objects.create(test_id=7, attempt_id=3, description="try8", expected_result=20, test_result=20) IndexedTestModel.objects.create(test_id=8, attempt_id=0, description="try9", expected_result=50, test_result=40) IndexedTestModel.objects.create( test_id=9, attempt_id=1, description="try10", expected_result=60, test_result=40 ) IndexedTestModel.objects.create( test_id=10, attempt_id=2, description="try11", expected_result=70, test_result=45 ) IndexedTestModel.objects.create( test_id=11, attempt_id=3, description="try12", expected_result=75, test_result=45 )
def test_compaction_not_altered_without_changes_sizetiered(self): from cqlengine.management import update_compaction class SizeTieredCompactionChangesDetectionTest(Model): __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_high__ = 20 __compaction_bucket_low__ = 10 __compaction_max_threshold__ = 200 __compaction_min_threshold__ = 100 __compaction_min_sstable_size__ = 1000 __compaction_tombstone_threshold__ = 0.125 __compaction_tombstone_compaction_interval__ = 3600 pk = columns.Integer(primary_key=True) drop_table(SizeTieredCompactionChangesDetectionTest) sync_table(SizeTieredCompactionChangesDetectionTest) assert not update_compaction(SizeTieredCompactionChangesDetectionTest)
def test_compaction_not_altered_without_changes_sizetiered(self): from cqlengine.management import update_compaction class SizeTieredCompactionChangesDetectionTest(Model): __keyspace__ = 'test' __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_high__ = 20 __compaction_bucket_low__ = 10 __compaction_max_threshold__ = 200 __compaction_min_threshold__ = 100 __compaction_min_sstable_size__ = 1000 __compaction_tombstone_threshold__ = 0.125 __compaction_tombstone_compaction_interval__ = 3600 pk = columns.Integer(primary_key=True) drop_table(SizeTieredCompactionChangesDetectionTest) sync_table(SizeTieredCompactionChangesDetectionTest) assert not update_compaction(SizeTieredCompactionChangesDetectionTest)
def test_non_quality_filtering(): class NonEqualityFilteringModel(Model): __keyspace__ = 'test' example_id = columns.UUID(primary_key=True, default=uuid.uuid4) sequence_id = columns.Integer(primary_key=True) # sequence_id is a clustering key example_type = columns.Integer(index=True) created_at = columns.DateTime() drop_table(NonEqualityFilteringModel) sync_table(NonEqualityFilteringModel) # setup table, etc. NonEqualityFilteringModel.create(sequence_id=1, example_type=0, created_at=datetime.now()) NonEqualityFilteringModel.create(sequence_id=3, example_type=0, created_at=datetime.now()) NonEqualityFilteringModel.create(sequence_id=5, example_type=1, created_at=datetime.now()) qA = NonEqualityFilteringModel.objects(NonEqualityFilteringModel.sequence_id > 3).allow_filtering() num = qA.count() assert num == 1, num
def setUpClass(cls): super(BaseQuerySetUsage, cls).setUpClass() drop_table(TestModel) drop_table(IndexedTestModel) sync_table(TestModel) sync_table(IndexedTestModel) sync_table(TestMultiClusteringModel) TestModel.objects.create(test_id=0, attempt_id=0, description='try1', expected_result=5, test_result=30) TestModel.objects.create(test_id=0, attempt_id=1, description='try2', expected_result=10, test_result=30) TestModel.objects.create(test_id=0, attempt_id=2, description='try3', expected_result=15, test_result=30) TestModel.objects.create(test_id=0, attempt_id=3, description='try4', expected_result=20, test_result=25) TestModel.objects.create(test_id=1, attempt_id=0, description='try5', expected_result=5, test_result=25) TestModel.objects.create(test_id=1, attempt_id=1, description='try6', expected_result=10, test_result=25) TestModel.objects.create(test_id=1, attempt_id=2, description='try7', expected_result=15, test_result=25) TestModel.objects.create(test_id=1, attempt_id=3, description='try8', expected_result=20, test_result=20) TestModel.objects.create(test_id=2, attempt_id=0, description='try9', expected_result=50, test_result=40) TestModel.objects.create(test_id=2, attempt_id=1, description='try10', expected_result=60, test_result=40) TestModel.objects.create(test_id=2, attempt_id=2, description='try11', expected_result=70, test_result=45) TestModel.objects.create(test_id=2, attempt_id=3, description='try12', expected_result=75, test_result=45) IndexedTestModel.objects.create(test_id=0, attempt_id=0, description='try1', expected_result=5, test_result=30) IndexedTestModel.objects.create(test_id=1, attempt_id=1, description='try2', expected_result=10, test_result=30) IndexedTestModel.objects.create(test_id=2, attempt_id=2, description='try3', expected_result=15, test_result=30) IndexedTestModel.objects.create(test_id=3, attempt_id=3, description='try4', expected_result=20, test_result=25) IndexedTestModel.objects.create(test_id=4, attempt_id=0, description='try5', expected_result=5, test_result=25) IndexedTestModel.objects.create(test_id=5, attempt_id=1, description='try6', expected_result=10, test_result=25) IndexedTestModel.objects.create(test_id=6, attempt_id=2, description='try7', expected_result=15, test_result=25) IndexedTestModel.objects.create(test_id=7, attempt_id=3, description='try8', expected_result=20, test_result=20) IndexedTestModel.objects.create(test_id=8, attempt_id=0, description='try9', expected_result=50, test_result=40) IndexedTestModel.objects.create(test_id=9, attempt_id=1, description='try10', expected_result=60, test_result=40) IndexedTestModel.objects.create(test_id=10, attempt_id=2, description='try11', expected_result=70, test_result=45) IndexedTestModel.objects.create(test_id=11, attempt_id=3, description='try12', expected_result=75, test_result=45)
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 _update_current_rev(self, old, new): if old == new: return if new is None: if 'cqlengine' == self.dialect.name: from cqlengine import management management.drop_table(self._version) else: self.impl._exec(self._version.delete()) elif old is None: if 'cqlengine' == self.dialect.name: self._version.create(key='alembic', version_num=unicode(new)) else: self.impl._exec(self._version.insert(). values(version_num=literal_column("'%s'" % new)) ) else: if 'cqlengine' == self.dialect.name: row = self._version(key='alembic') row.update(version_num=unicode(new)) else: self.impl._exec(self._version.update(). values(version_num=literal_column("'%s'" % new)) )
def tearDownClass(cls): super(TestCamelMapColumn, cls).tearDownClass() drop_table(TestCamelMapModel)
def setUpClass(cls): super(TestCamelMapColumn, cls).setUpClass() drop_table(TestCamelMapModel) sync_table(TestCamelMapModel)
def tearDownClass(cls): super(TestDatetime, cls).tearDownClass() drop_table(cls.DatetimeTest)
def setUpClass(cls): super(TestSetColumn, cls).setUpClass() drop_table(TestSetModel) sync_table(TestSetModel)
def setUpClass(cls): super(TestStaticColumn, cls).setUpClass() drop_table(TestStaticModel) if CASSANDRA_VERSION >= 20: sync_table(TestStaticModel)
def test_alter_is_called_table(self): drop_table(LeveledcompactionTestTable) sync_table(LeveledcompactionTestTable) with patch('cqlengine.management.update_compaction') as mock: sync_table(LeveledcompactionTestTable) assert mock.called == 1
def tearDownClass(cls): super(TestTimeUUID, cls).tearDownClass() drop_table(cls.TimeUUIDTest)
def tearDownClass(cls): super(ModelUpdateTests, cls).tearDownClass() drop_table(TestUpdateModel)
def tearDown(self): super(TestTokenFunction, self).tearDown() drop_table(TokenTestModel)
def tearDownClass(cls): super(TestPolymorphicModel, cls).tearDownClass() management.drop_table(Poly1) management.drop_table(Poly2)
def tearDownClass(cls): super(TestIndexedPolymorphicQuery, cls).tearDownClass() management.drop_table(IndexedPoly1) management.drop_table(IndexedPoly2)
def setUpClass(cls): super(TestCounterColumn, cls).setUpClass() drop_table(TestCounterModel) sync_table(TestCounterModel)
def tearDownClass(cls): super(TestStaticColumn, cls).tearDownClass() drop_table(TestStaticModel)
def tearDownClass(cls): super(TestCounterColumn, cls).tearDownClass() drop_table(TestCounterModel)
def tearDownClass(cls): super(TestSetColumn, cls).tearDownClass() drop_table(TestSetModel)
def tearDownClass(cls): super(BaseDefaultTTLTest, cls).tearDownClass() drop_table(TestDefaultTTLModel) drop_table(TestTTLModel)
def setUpClass(cls): super(BatchQueryTests, cls).setUpClass() drop_table(TestMultiKeyModel) sync_table(TestMultiKeyModel)
def tearDownClass(cls): super(BatchQueryTests, cls).tearDownClass() drop_table(TestMultiKeyModel)
def setUp(self): drop_table(self.InetTestModel) sync_table(self.InetTestModel)