def test_static_columns(self):
        class StaticModel(Model):
            id = columns.Integer(primary_key=True)
            c = columns.Integer(primary_key=True)
            name = columns.Text(static=True)

        drop_table(StaticModel)

        session = get_session()

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

        self.assertGreater(m.call_count, 0)
        statement = m.call_args[0][0].query_string
        self.assertIn('"name" text static', statement)

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

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

        self.assertEqual(len(m2.call_args_list), 0)
Ejemplo n.º 2
0
 def test_extra_field(self):
     drop_table(self.TestModel)
     sync_table(self.TestModel)
     self.TestModel.create()
     execute("ALTER TABLE {0} add blah int".format(
         self.TestModel.column_family_name(include_keyspace=True)))
     self.TestModel.objects().all()
    def test_table_definition(self):
        """ Tests that creating a table with capitalized column names succeeds """
        sync_table(LowercaseKeyModel)
        sync_table(CapitalizedKeyModel)

        drop_table(LowercaseKeyModel)
        drop_table(CapitalizedKeyModel)
Ejemplo n.º 4
0
    def test_model_over_write(self):
        """
        Test to ensure overwriting of primary keys in model inheritance is allowed

        This is currently only an issue in PyPy. When PYTHON-504 is introduced this should
        be updated error out and warn the user

        @since 3.6.0
        @jira_ticket PYTHON-576
        @expected_result primary keys can be overwritten via inheritance

        @test_category object_mapper
        """
        class TimeModelBase(Model):
            uuid = columns.TimeUUID(primary_key=True)

        class DerivedTimeModel(TimeModelBase):
            __table_name__ = 'derived_time'
            uuid = columns.TimeUUID(primary_key=True, partition_key=True)
            value = columns.Text(required=False)

        # In case the table already exists in keyspace
        drop_table(DerivedTimeModel)

        sync_table(DerivedTimeModel)
        uuid_value = uuid1()
        uuid_value2 = uuid1()
        DerivedTimeModel.create(uuid=uuid_value, value="first")
        DerivedTimeModel.create(uuid=uuid_value2, value="second")
        DerivedTimeModel.objects.filter(uuid=uuid_value)
    def setUpClass(cls):
        if PROTOCOL_VERSION < 4:
            return

        super(TestQuerying, cls).setUpClass()
        drop_table(TestQueryModel)
        sync_table(TestQueryModel)
Ejemplo n.º 6
0
    def tearDownClass(cls):
        super(ContextQueryConnectionTests, cls).tearDownClass()

        with ContextQuery(TestModel, connection='cluster') as tm:
            drop_table(tm)
        drop_keyspace('ks1', connections=['cluster'])

        # reset the default connection
        conn.unregister_connection('fake_cluster')
        conn.unregister_connection('cluster')
        setup_connection(DEFAULT_KEYSPACE)
Ejemplo n.º 7
0
    def test_keywords_as_names(self):
        """
        Test for CQL keywords as names

        test_keywords_as_names tests that CQL keywords are properly and automatically quoted in cqlengine. It creates
        a keyspace, keyspace, which should be automatically quoted to "keyspace" in CQL. It then creates a table, table,
        which should also be automatically quoted to "table". It then verfies that operations can be done on the
        "keyspace"."table" which has been created. It also verifies that table alternations work and operations can be
        performed on the altered table.

        @since 2.6.0
        @jira_ticket PYTHON-244
        @expected_result Cqlengine should quote CQL keywords properly when creating keyspaces and tables.

        @test_category schema:generation
        """

        # If the keyspace exists, it will not be re-created
        create_keyspace_simple('keyspace', 1)

        class table(Model):
            __keyspace__ = 'keyspace'
            select = columns.Integer(primary_key=True)
            table = columns.Text()

        # In case the table already exists in keyspace
        drop_table(table)

        # Create should work
        sync_table(table)

        created = table.create(select=0, table='table')
        selected = table.objects(select=0)[0]
        self.assertEqual(created.select, selected.select)
        self.assertEqual(created.table, selected.table)

        # Alter should work
        class table(Model):
            __keyspace__ = 'keyspace'
            select = columns.Integer(primary_key=True)
            table = columns.Text()
            where = columns.Text()

        sync_table(table)

        created = table.create(select=1, table='table')
        selected = table.objects(select=1)[0]
        self.assertEqual(created.select, selected.select)
        self.assertEqual(created.table, selected.table)
        self.assertEqual(created.where, selected.where)

        drop_keyspace('keyspace')
Ejemplo n.º 8
0
    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.assertEqual([2] * 20, values)
        drop_table(TestClusteringComplexModel)
Ejemplo n.º 9
0
    def test_alter_actually_alters(self):
        tmp = copy.deepcopy(LeveledCompactionTestTable)
        drop_table(tmp)
        sync_table(tmp)
        tmp.__options__ = {
            'compaction': {
                'class':
                'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'
            }
        }
        sync_table(tmp)

        table_meta = _get_table_metadata(tmp)

        self.assertRegexpMatches(table_meta.export_as_string(),
                                 '.*SizeTieredCompactionStrategy.*')
    def test_concrete_class_table_creation_cycle(self):
        """ Tests that models with inherited abstract classes can be created, and have io performed """
        from dse.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)
Ejemplo n.º 11
0
    def test_all_leveled_options(self):
        class AllLeveledOptionsModel(Model):
            __options__ = {
                'compaction': {
                    'class':
                    'org.apache.cassandra.db.compaction.LeveledCompactionStrategy',
                    'sstable_size_in_mb': '64'
                }
            }

            cid = columns.UUID(primary_key=True)
            name = columns.Text()

        drop_table(AllLeveledOptionsModel)
        sync_table(AllLeveledOptionsModel)

        table_meta = _get_table_metadata(AllLeveledOptionsModel)
        self._verify_options(table_meta, AllLeveledOptionsModel.__options__)
Ejemplo n.º 12
0
    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))
Ejemplo n.º 13
0
    def test_compaction_not_altered_without_changes_leveled(self):
        class LeveledCompactionChangesDetectionTest(Model):

            __options__ = {
                'compaction': {
                    'class':
                    'org.apache.cassandra.db.compaction.LeveledCompactionStrategy',
                    'sstable_size_in_mb': '160',
                    'tombstone_threshold': '0.125',
                    'tombstone_compaction_interval': '3600'
                }
            }
            pk = columns.Integer(primary_key=True)

        drop_table(LeveledCompactionChangesDetectionTest)
        sync_table(LeveledCompactionChangesDetectionTest)

        self.assertFalse(
            _update_options(LeveledCompactionChangesDetectionTest))
Ejemplo n.º 14
0
    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))
Ejemplo n.º 15
0
    def test_all_size_tiered_options(self):
        class AllSizeTieredOptionsModel(Model):
            __options__ = {
                'compaction': {
                    'class':
                    'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy',
                    'bucket_low': '.3',
                    'bucket_high': '2',
                    'min_threshold': '2',
                    'max_threshold': '64',
                    'tombstone_compaction_interval': '86400'
                }
            }

            cid = columns.UUID(primary_key=True)
            name = columns.Text()

        drop_table(AllSizeTieredOptionsModel)
        sync_table(AllSizeTieredOptionsModel)

        table_meta = _get_table_metadata(AllSizeTieredOptionsModel)
        self._verify_options(table_meta, AllSizeTieredOptionsModel.__options__)
Ejemplo n.º 16
0
    def test_compaction_not_altered_without_changes_sizetiered(self):
        class SizeTieredCompactionChangesDetectionTest(Model):

            __options__ = {
                'compaction': {
                    'class':
                    'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy',
                    'bucket_high': '20',
                    'bucket_low': '10',
                    'max_threshold': '200',
                    'min_threshold': '100',
                    'min_sstable_size': '1000',
                    'tombstone_threshold': '0.125',
                    'tombstone_compaction_interval': '3600'
                }
            }
            pk = columns.Integer(primary_key=True)

        drop_table(SizeTieredCompactionChangesDetectionTest)
        sync_table(SizeTieredCompactionChangesDetectionTest)

        self.assertFalse(
            _update_options(SizeTieredCompactionChangesDetectionTest))
Ejemplo n.º 17
0
    def test_alter_options(self):
        class AlterTable(Model):

            __options__ = {
                'compaction': {
                    'class':
                    'org.apache.cassandra.db.compaction.LeveledCompactionStrategy',
                    'sstable_size_in_mb': '64'
                }
            }
            user_id = columns.UUID(primary_key=True)
            name = columns.Text()

        drop_table(AlterTable)
        sync_table(AlterTable)
        table_meta = _get_table_metadata(AlterTable)
        self.assertRegexpMatches(table_meta.export_as_string(),
                                 ".*'sstable_size_in_mb': '64'.*")
        AlterTable.__options__['compaction']['sstable_size_in_mb'] = '128'
        sync_table(AlterTable)
        table_meta = _get_table_metadata(AlterTable)
        self.assertRegexpMatches(table_meta.export_as_string(),
                                 ".*'sstable_size_in_mb': '128'.*")
Ejemplo n.º 18
0
    def test_create_drop_table(self):
        """
        Tests drop and create Table with connections explicitly set

        @since 3.7
        @jira_ticket PYTHON-613
        @expected_result Tables should be created and dropped

        @test_category object_mapper
        """
        for ks in self.keyspaces:
            create_keyspace_simple(ks, 1, connections=self.conns)

        # No connection (default is fake)
        with self.assertRaises(NoHostAvailable):
            sync_table(TestModel)

        # Explicit connections
        sync_table(TestModel, connections=self.conns)

        # Explicit drop
        drop_table(TestModel, connections=self.conns)

        # Model connection
        TestModel.__connection__ = 'cluster'
        sync_table(TestModel)
        TestModel.__connection__ = None

        # No connection (default is fake)
        with self.assertRaises(NoHostAvailable):
            drop_table(TestModel)

        # Model connection
        TestModel.__connection__ = 'cluster'
        drop_table(TestModel)
        TestModel.__connection__ = None

        # Model connection
        for ks in self.keyspaces:
            drop_keyspace(ks, connections=self.conns)
 def setUp(self):
     drop_table(ModelWithTableProperties)
Ejemplo n.º 20
0
 def test_alter_is_called_table(self):
     drop_table(LeveledCompactionTestTable)
     sync_table(LeveledCompactionTestTable)
     with patch('dse.cqlengine.management._update_options') as mock:
         sync_table(LeveledCompactionTestTable)
     assert mock.called == 1
Ejemplo n.º 21
0
 def tearDownClass(cls):
     super(ModelUpdateTests, cls).tearDownClass()
     drop_table(TestUpdateModel)
Ejemplo n.º 22
0
 def tearDownClass(cls):
     super(BaseColumnIOTest, cls).tearDownClass()
     if not cls.column:
         return
     drop_table(cls._generated_model)
Ejemplo n.º 23
0
 def tearDownClass(cls):
     super(BaseIfNotExistsWithCounterTest, cls).tearDownClass()
     drop_table(TestIfNotExistsWithCounterModel)
Ejemplo n.º 24
0
 def tearDownClass(cls):
     super(BaseIfNotExistsTest, cls).tearDownClass()
     drop_table(TestIfNotExistsModel)
    def test_multiple_deletes_dont_fail(self):
        sync_table(TestModel)

        drop_table(TestModel)
        drop_table(TestModel)
 def setUp(self):
     drop_table(FirstModel)
 def setUp(self):
     drop_table(IndexModel)
     drop_table(IndexCaseSensitiveModel)
 def setUp(self):
     drop_table(IndexModel)
Ejemplo n.º 29
0
 def tearDownClass(cls):
     super(TestClusteringOrder, cls).tearDownClass()
     drop_table(TestModel)
 def setUp(self):
     drop_table(PrimaryKeysOnlyModel)