def test_can_insert_partial_udts(self):
        class UserGender(UserType):
            age = columns.Integer()
            name = columns.Text()
            gender = columns.Text()

        class UserModelGender(Model):
            id = columns.Integer(primary_key=True)
            info = columns.UserDefinedType(UserGender)

        sync_table(UserModelGender)
        self.addCleanup(drop_table, UserModelGender)

        user = UserGender(age=42, name="John")
        UserModelGender.create(id=0, info=user)

        john_info = UserModelGender.objects.first().info
        self.assertEqual(42, john_info.age)
        self.assertEqual("John", john_info.name)
        self.assertIsNone(john_info.gender)

        user = UserGender(age=42)
        UserModelGender.create(id=0, info=user)

        john_info = UserModelGender.objects.first().info
        self.assertEqual(42, john_info.age)
        self.assertIsNone(john_info.name)
        self.assertIsNone(john_info.gender)
Beispiel #2
0
    def test_can_insert_nested_udts(self):
        class Depth_0(UserType):
            age = columns.Integer()
            name = columns.Text()

        class Depth_1(UserType):
            value = columns.UserDefinedType(Depth_0)

        class Depth_2(UserType):
            value = columns.UserDefinedType(Depth_1)

        class Depth_3(UserType):
            value = columns.UserDefinedType(Depth_2)

        class DepthModel(Model):
            id = columns.Integer(primary_key=True)
            v_0 = columns.UserDefinedType(Depth_0)
            v_1 = columns.UserDefinedType(Depth_1)
            v_2 = columns.UserDefinedType(Depth_2)
            v_3 = columns.UserDefinedType(Depth_3)

        sync_table(DepthModel)

        udts = [Depth_0(age=42, name="John")]
        udts.append(Depth_1(value=udts[0]))
        udts.append(Depth_2(value=udts[1]))
        udts.append(Depth_3(value=udts[2]))

        DepthModel.create(id=0, v_0=udts[0], v_1=udts[1], v_2=udts[2], v_3=udts[3])
        output = DepthModel.objects().first()

        self.assertEqual(udts[0], output.v_0)
        self.assertEqual(udts[1], output.v_1)
        self.assertEqual(udts[2], output.v_2)
        self.assertEqual(udts[3], output.v_3)
    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 test_can_insert_udts_with_nones(self):
        """
        Test for inserting all column types as empty into a UserType as None's

        test_can_insert_udts_with_nones tests that each cqlengine column type can be inserted into a UserType as None's.
        It first creates a UserType that has each cqlengine column type, and a corresponding table/Model. It then creates
        a UserType instance where all the fields are None's and inserts the UserType as an instance of the Model. Finally,
        it verifies that each column read from the UserType from Cassandra is None.

        @since 2.5.0
        @jira_ticket PYTHON-251
        @expected_result The UserType is inserted with each column type, and the resulting read yields None's for each column.

        @test_category data_types:udt
        """
        sync_table(AllDatatypesModel)
        self.addCleanup(drop_table, AllDatatypesModel)

        input = AllDatatypes(a=None, b=None, c=None, d=None, e=None, f=None, g=None, h=None, i=None, j=None, k=None,
                             l=None, m=None, n=None)
        AllDatatypesModel.create(id=0, data=input)

        self.assertEqual(1, AllDatatypesModel.objects.count())

        output = AllDatatypesModel.objects.first().data
        self.assertEqual(input, output)
    def test_udts_with_unicode(self):
        """
        Test for inserting models with unicode and udt columns.

        test_udts_with_unicode constructs a model with a user defined type. It then attempts to insert that model with
        a unicode primary key. It will also attempt to upsert a udt that contains unicode text.

        @since 3.0.0
        @jira_ticket PYTHON-353
        @expected_result No exceptions thrown

        @test_category data_types:udt
        """
        ascii_name = 'normal name'
        unicode_name = u'Fran\u00E7ois'

        class UserModelText(Model):
            id = columns.Text(primary_key=True)
            info = columns.UserDefinedType(User)

        sync_table(UserModelText)
        self.addCleanup(drop_table, UserModelText)

        # Two udt instances one with a unicode one with ascii
        user_template_ascii = User(age=25, name=ascii_name)
        user_template_unicode = User(age=25, name=unicode_name)

        UserModelText.create(id=ascii_name, info=user_template_unicode)
        UserModelText.create(id=unicode_name, info=user_template_ascii)
        UserModelText.create(id=unicode_name, info=user_template_unicode)
    def test_can_insert_udts_with_all_datatypes(self):
        """
        Test for inserting all column types into a UserType

        test_can_insert_udts_with_all_datatypes tests that each cqlengine column type can be inserted into a UserType.
        It first creates a UserType that has each cqlengine column type, and a corresponding table/Model. It then creates
        a UserType instance where all the fields have corresponding data, and inserts the UserType as an instance of the Model.
        Finally, it verifies that each column read from the UserType from Cassandra is the same as the input parameters.

        @since 2.5.0
        @jira_ticket PYTHON-251
        @expected_result The UserType is inserted with each column type, and the resulting read yields proper data for each column.

        @test_category data_types:udt
        """
        sync_table(AllDatatypesModel)
        self.addCleanup(drop_table, AllDatatypesModel)

        input = AllDatatypes(a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
                             e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
                             h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
                             l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
                             m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'))
        AllDatatypesModel.create(id=0, data=input)

        self.assertEqual(1, AllDatatypesModel.objects.count())
        output = AllDatatypesModel.objects.first().data

        for i in range(ord('a'), ord('a') + 14):
            self.assertEqual(input[chr(i)], output[chr(i)])
    def setUpClass(cls):
        if PROTOCOL_VERSION < 4:
            return

        super(TestQuerying, cls).setUpClass()
        drop_table(TestQueryModel)
        sync_table(TestQueryModel)
    def test_default_values(self):
        """
        Test that default types are set on object creation for UDTs

        @since 3.7.0
        @jira_ticket PYTHON-606
        @expected_result Default values should be set.

        @test_category data_types:udt
        """

        class NestedUdt(UserType):

            test_id = columns.UUID(default=uuid4)
            something = columns.Text()
            default_text = columns.Text(default="default text")

        class OuterModel(Model):

            name = columns.Text(primary_key=True)
            first_name = columns.Text()
            nested = columns.List(columns.UserDefinedType(NestedUdt))
            simple = columns.UserDefinedType(NestedUdt)

        sync_table(OuterModel)
        self.addCleanup(drop_table, OuterModel)

        t = OuterModel.create(name='test1')
        t.nested = [NestedUdt(something='test')]
        t.simple = NestedUdt(something="")
        t.save()
        self.assertIsNotNone(t.nested[0].test_id)
        self.assertEqual(t.nested[0].default_text, "default text")
        self.assertIsNotNone(t.simple.test_id)
        self.assertEqual(t.simple.default_text, "default text")
    def test_can_insert_double_and_float(self):
        """
        Test for inserting single-precision and double-precision values into a Float and Double columns

        @since 2.6.0
        @changed 3.0.0 removed deprecated Float(double_precision) parameter
        @jira_ticket PYTHON-246
        @expected_result Each floating point column type is able to hold their respective precision values.

        @test_category data_types:primitive
        """
        class FloatingPointModel(Model):
            id = columns.Integer(primary_key=True)
            f = columns.Float()
            d = columns.Double()

        sync_table(FloatingPointModel)

        FloatingPointModel.create(id=0, f=2.39)
        output = FloatingPointModel.objects.first()
        self.assertEqual(2.390000104904175, output.f)  # float loses precision

        FloatingPointModel.create(id=0, f=3.4028234663852886e+38, d=2.39)
        output = FloatingPointModel.objects.first()
        self.assertEqual(3.4028234663852886e+38, output.f)
        self.assertEqual(2.39, output.d)  # double retains precision

        FloatingPointModel.create(id=0, d=3.4028234663852886e+38)
        output = FloatingPointModel.objects.first()
        self.assertEqual(3.4028234663852886e+38, output.d)
    def test_udt_validate_with_default(self):
        """
        Test to verify restrictions are honored and that validate is called
        on the default value

        @since 3.10
        @jira_ticket PYTHON-505
        @expected_result a validation error is arisen due to the name being
        too long

        @test_category data_types:object_mapper
        """
        class UserValidateDefault(UserType):
            age = columns.Integer()
            name = columns.Text(max_length=2, default="Robert")

        class UserModelValidateDefault(Model):
            id = columns.Integer(primary_key=True)
            info = columns.UserDefinedType(UserValidateDefault)

        sync_table(UserModelValidateDefault)
        self.addCleanup(drop_table, UserModelValidateDefault)

        user = UserValidateDefault(age=1)
        item = UserModelValidateDefault(id=1, info=user)
        with self.assertRaises(ValidationError):
            item.save()
    def setUpClass(cls):
        super(TestIndexedInheritanceQuery, cls).setUpClass()
        management.sync_table(IndexedInherit1)
        management.sync_table(IndexedInherit2)

        cls.p1 = IndexedInherit1.create(data1='pickle')
        cls.p2 = IndexedInherit2.create(partition=cls.p1.partition, data2='bacon')
Beispiel #12
0
    def _reset_data(self):

        for ks in self.keyspaces:
            drop_keyspace(ks, connections=self.conns)
        for ks in self.keyspaces:
            create_keyspace_simple(ks, 1, connections=self.conns)
        sync_table(TestModel, keyspaces=self.keyspaces, connections=self.conns)
    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)
Beispiel #14
0
    def test_lots_of_queries(self):
        import resource
        import objgraph

        class LoadTest(Model):
            k = columns.Integer(primary_key=True)
            v = columns.Integer()

        sync_table(LoadTest)
        gc.collect()
        objgraph.show_most_common_types()

        print("Starting...")

        for i in range(1000000):
            if i % 25000 == 0:
                # print memory statistic
                print("Memory usage: %s" %
                      (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss))

            LoadTest.create(k=i, v=i)

        objgraph.show_most_common_types()

        raise Exception("you shouldn't be here")
 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_bogus_option_update(self):
     sync_table(ModelWithTableProperties)
     option = 'no way will this ever be an option'
     try:
         ModelWithTableProperties.__options__[option] = 'what was I thinking?'
         self.assertRaisesRegexp(KeyError, "Invalid table option.*%s.*" % option, sync_table, ModelWithTableProperties)
     finally:
         ModelWithTableProperties.__options__.pop(option, None)
Beispiel #17
0
 def setUpClass(cls):
     super(BaseIfNotExistsTest, cls).setUpClass()
     """
     when receiving an insert statement with 'if not exist', cassandra would
     perform a read with QUORUM level. Unittest would be failed if replica_factor
     is 3 and one node only. Therefore I have create a new keyspace with
     replica_factor:1.
     """
     sync_table(TestIfNotExistsModel)
    def setUpClass(cls):
        if PROTOCOL_VERSION < 4 or CASSANDRA_VERSION < "3.0":
            return

        class DataTypeTest(Model):
            test_id = Integer(primary_key=True)
            class_param = cls.db_klass()

        cls.model_class = DataTypeTest
        sync_table(cls.model_class)
Beispiel #19
0
    def setUpClass(cls):
        super(TestDateTimeQueries, cls).setUpClass()
        sync_table(DateTimeQueryTestModel)

        cls.base_date = datetime.now() - timedelta(days=10)
        for x in range(7):
            for y in range(10):
                DateTimeQueryTestModel.create(user=x,
                                              day=(cls.base_date +
                                                   timedelta(days=y)),
                                              data=str(uuid4()))
    def test_reserved_cql_words_can_be_used_as_column_names(self):
        """
        """
        sync_table(ReservedWordModel)

        model1 = ReservedWordModel.create(token='1', insert=5)

        model2 = ReservedWordModel.filter(token='1')

        self.assertTrue(len(model2) == 1)
        self.assertTrue(model1.token == model2[0].token)
        self.assertTrue(model1.insert == model2[0].insert)
    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')
    def test_table_property_update(self):
        ModelWithTableProperties.__options__['bloom_filter_fp_chance'] = 0.66778
        ModelWithTableProperties.__options__['comment'] = 'xirAkRWZVVvsmzRvXamiEcQkshkUIDINVJZgLYSdnGHweiBrAiJdLJkVohdRy'
        ModelWithTableProperties.__options__['gc_grace_seconds'] = 96362

        ModelWithTableProperties.__options__['read_repair_chance'] = 0.2989
        ModelWithTableProperties.__options__['dclocal_read_repair_chance'] = 0.12732

        sync_table(ModelWithTableProperties)

        table_options = management._get_table_metadata(ModelWithTableProperties).options

        self.assertDictContainsSubset(ModelWithTableProperties.__options__, table_options)
Beispiel #23
0
    def setUpClass(cls):
        super(ContextQueryConnectionTests, cls).setUpClass()
        create_keyspace_simple('ks1', 1)

        conn.unregister_connection('default')
        conn.register_connection('fake_cluster', ['127.0.0.100'],
                                 lazy_connect=True,
                                 retry_connect=True,
                                 default=True)
        conn.register_connection('cluster', [DSE_IP])

        with ContextQuery(TestModel, connection='cluster') as tm:
            sync_table(tm)
Beispiel #24
0
    def setUpClass(cls):
        super(BatchQueryConnectionTests, cls).setUpClass()

        create_keyspace_simple('ks1', 1)
        sync_table(TestModel)
        sync_table(AnotherTestModel)

        conn.unregister_connection('default')
        conn.register_connection('fake_cluster', ['127.0.0.100'],
                                 lazy_connect=True,
                                 retry_connect=True,
                                 default=True)
        conn.register_connection('cluster', [DSE_IP])
def test_none_filter_fails():
    class NoneFilterModel(Model):

        pk = columns.Integer(primary_key=True)
        v = columns.Integer()

    sync_table(NoneFilterModel)

    try:
        NoneFilterModel.objects(pk=None)
        raise Exception("fail")
    except CQLEngineException as e:
        pass
    def test_add_column(self):
        sync_table(FirstModel)
        meta_columns = _get_table_metadata(FirstModel).columns
        self.assertEqual(set(meta_columns), set(FirstModel._columns))

        sync_table(SecondModel)
        meta_columns = _get_table_metadata(FirstModel).columns
        self.assertEqual(set(meta_columns), set(SecondModel._columns))

        sync_table(ThirdModel)
        meta_columns = _get_table_metadata(FirstModel).columns
        self.assertEqual(len(meta_columns), 5)
        self.assertEqual(len(ThirdModel._columns), 4)
        self.assertIn('fourth_key', meta_columns)
        self.assertNotIn('fourth_key', ThirdModel._columns)
        self.assertIn('blah', ThirdModel._columns)
        self.assertIn('blah', meta_columns)

        sync_table(FourthModel)
        meta_columns = _get_table_metadata(FirstModel).columns
        self.assertEqual(len(meta_columns), 5)
        self.assertEqual(len(ThirdModel._columns), 4)
        self.assertIn('fourth_key', meta_columns)
        self.assertNotIn('fourth_key', FourthModel._columns)
        self.assertIn('renamed', FourthModel._columns)
        self.assertNotIn('renamed', meta_columns)
        self.assertIn('blah', meta_columns)
    def test_sync_table_works_with_primary_keys_only_tables(self):

        sync_table(PrimaryKeysOnlyModel)
        # blows up with DoesNotExist if table does not exist
        table_meta = management._get_table_metadata(PrimaryKeysOnlyModel)

        self.assertIn('LeveledCompactionStrategy', table_meta.as_cql_query())

        PrimaryKeysOnlyModel.__options__['compaction']['class'] = 'SizeTieredCompactionStrategy'

        sync_table(PrimaryKeysOnlyModel)

        table_meta = management._get_table_metadata(PrimaryKeysOnlyModel)
        self.assertIn('SizeTieredCompactionStrategy', table_meta.as_cql_query())
    def test_primary_key_validation(self):
        """
        Test to ensure that changes to primary keys throw CQLEngineExceptions

        @since 3.2
        @jira_ticket PYTHON-532
        @expected_result Attempts to modify primary keys throw an exception

        @test_category object_mapper
        """
        sync_table(PrimaryKeysOnlyModel)
        self.assertRaises(CQLEngineException, sync_table, PrimaryKeysModelChanged)
        self.assertRaises(CQLEngineException, sync_table, PrimaryKeysAddedClusteringKey)
        self.assertRaises(CQLEngineException, sync_table, PrimaryKeysRemovedPk)
Beispiel #29
0
def create_tables():
    """Create Cassandra tables for the different models"""
    tables = (
        DataObject,
        Group,
        #         IDSearch,
        Notification,
        #         SearchIndex,
        User,
        TreeNode,
    )
    for table in tables:
        cfg.logger.info('Syncing table "{0}"'.format(table.__name__))
        sync_table(table)
    def test_db_field_override(self):
        """
        Tests for db_field override

        Tests to ensure that udt's in models can specify db_field for a particular field and that it will be honored.

        @since 3.1.0
        @jira_ticket PYTHON-346
        @expected_result The actual cassandra column will use the db_field specified.

        @test_category data_types:udt
        """
        class db_field_different(UserType):
            age = columns.Integer(db_field='a')
            name = columns.Text(db_field='n')

        class TheModel(Model):
            id = columns.Integer(primary_key=True)
            info = columns.UserDefinedType(db_field_different)

        sync_table(TheModel)
        self.addCleanup(drop_table, TheModel)

        cluster = connection.get_cluster()
        type_meta = cluster.metadata.keyspaces[TheModel._get_keyspace()].user_types[db_field_different.type_name()]

        type_fields = (db_field_different.age.column, db_field_different.name.column)

        self.assertEqual(len(type_meta.field_names), len(type_fields))
        for f in type_fields:
            self.assertIn(f.db_field_name, type_meta.field_names)

        id = 0
        age = 42
        name = 'John'
        info = db_field_different(age=age, name=name)
        TheModel.create(id=id, info=info)

        self.assertEqual(1, TheModel.objects.count())

        john = TheModel.objects.first()
        self.assertEqual(john.id, id)
        info = john.info
        self.assertIsInstance(info, db_field_different)
        self.assertEqual(info.age, age)
        self.assertEqual(info.name, name)
        # also excercise the db_Field mapping
        self.assertEqual(info.a, age)
        self.assertEqual(info.n, name)