Esempio n. 1
0
    def test_cap_constraint_index(self):

        index = Index(self.test_1_col, CapConstraintIndex(size=5))

        index.save()

        index.delete()
Esempio n. 2
0
    def test_unique_hash_index(self):

        index = Index(self.test_1_col, HashIndex(fields=[
            'username'
        ]))

        index.save()

        index.delete()
Esempio n. 3
0
    def test_unique_skiptlist_index(self):

        index = Index(self.test_1_col, SkiplistIndex(fields=[
            'username'
        ]))

        index.save()

        index.delete()
Esempio n. 4
0
    def test_fulltext_index(self):

        index = Index(self.test_1_col, FulltextIndex(fields=[
            'description'
        ], minimum_length=5))

        index.save()

        index.delete()
Esempio n. 5
0
    def setUp(self):
        self.database_name = 'testcase_simple_index_query_123'
        self.db = Database.create(name=self.database_name)

        # Create test data
        self.test_1_col = self.db.create_collection('foo_1')

        self.hash_index = Index(self.test_1_col, HashIndex(fields=[
            'username'
        ]))

        self.geo_index = Index(self.test_1_col, GeoIndex(fields=['latitude', 'longitude'], geo_json=False))

        self.fulltext_index = Index(self.test_1_col, FulltextIndex(fields=['description'], minimum_length=4))

        self.skiplist_index = Index(self.test_1_col, SkiplistIndex(fields=['rated'], unique=False))

        self.hash_index.save()
        self.geo_index.save()
        self.fulltext_index.save()
        self.skiplist_index.save()

        self.col1_doc1 = self.test_1_col.create_document()
        self.col1_doc1.username='******'
        self.col1_doc1.description='Paris is such a beautiful city'
        self.col1_doc1.city= 'Paris'
        self.col1_doc1.latitude= 48.853333
        self.col1_doc1.longitude= 2.348611
        self.col1_doc1.rated= 4
        self.col1_doc1.save()

        self.col1_doc2 = self.test_1_col.create_document()
        self.col1_doc2.username='******'
        self.col1_doc2.description='The next time I will get myself some tickets for this event'
        self.col1_doc2.city='Koeln'
        self.col1_doc2.latitude= 50.933333
        self.col1_doc2.longitude=6.95
        self.col1_doc2.rated= 4
        self.col1_doc2.save()

        self.col1_doc3 = self.test_1_col.create_document()
        self.col1_doc3.username='******'
        self.col1_doc3.description='I have never seen such a big door in a city'
        self.col1_doc3.city='Berlin'
        self.col1_doc3.latitude=52.524167
        self.col1_doc3.longitude=13.410278
        self.col1_doc3.rated= 8
        self.col1_doc3.save()

        self.col1_doc4 = self.test_1_col.create_document()
        self.col1_doc4.username='******'
        self.col1_doc4.description='It one of the best cities in the world'
        self.col1_doc4.city='Rome'
        self.col1_doc4.latitude=41.891667
        self.col1_doc4.longitude=12.511111
        self.col1_doc4.rated= 10
        self.col1_doc4.save()
Esempio n. 6
0
    def test_unique_geo_index(self):

        index = Index(self.test_1_col, GeoIndex(fields=[
            'position'
        ], geo_json=True
        ))

        index.save()

        index.delete()
Esempio n. 7
0
    def init(cls):
        """
        """

        # TODO: Deal with super classes

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name, type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass # This is the case if init was called more than once

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        # Go through all fields
        fields_dict = cls.get_collection_fields_dict()
        for attribute_name in fields_dict:
            attribute = fields_dict[attribute_name]
            # Trigger init event
            attribute.on_init(cls, attribute_name)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance, index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Esempio n. 8
0
    def test_cap_constraint_index(self):

        index = Index(self.test_1_col, CapConstraintIndex(size=5))

        index.save()

        index.delete()
Esempio n. 9
0
    def test_unique_skiptlist_index(self):

        index = Index(self.test_1_col, SkiplistIndex(fields=['username']))

        index.save()

        index.delete()
Esempio n. 10
0
    def test_unique_hash_index(self):

        index = Index(self.test_1_col, HashIndex(fields=['username']))

        index.save()

        index.delete()
Esempio n. 11
0
    def test_unique_geo_index(self):

        index = Index(self.test_1_col,
                      GeoIndex(fields=['position'], geo_json=True))

        index.save()

        index.delete()
Esempio n. 12
0
    def test_fulltext_index(self):

        index = Index(self.test_1_col,
                      FulltextIndex(fields=['description'], minimum_length=5))

        index.save()

        index.delete()
Esempio n. 13
0
    def init(cls):
        """
        """

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name,
                                                        type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(
                name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass  # This is the case if init was called more than once

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        # Go through all fields
        for attribute in cls.get_collection_fields():
            # Trigger init event
            attribute.on_init(cls)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance,
                                  index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Esempio n. 14
0
    def test_overwrite_index_definition(self):

        index = Index(self.test_1_col, HashIndex(fields=['username']))

        index.save()

        doc1 = self.test_1_col.create_document()
        doc1.username = '******'
        doc1.save()

        has_exception = False

        doc2 = self.test_1_col.create_document()
        doc2.username = '******'

        try:
            doc2.save()
        except:
            has_exception = True

        self.assertTrue(has_exception)

        index.index_type_obj.unique = False

        index.overwrite()

        has_exception = False

        try:
            doc2.save()
        except:
            has_exception = True

        self.assertFalse(has_exception)

        index.delete()
Esempio n. 15
0
    def test_overwrite_index_definition(self):

        index = Index(self.test_1_col, HashIndex(fields=[
            'username'
        ]))

        index.save()

        doc1 = self.test_1_col.create_document()
        doc1.username = '******'
        doc1.save()

        has_exception = False

        doc2 = self.test_1_col.create_document()
        doc2.username = '******'

        try:
            doc2.save()
        except:
            has_exception = True

        self.assertTrue(has_exception)

        index.index_type_obj.unique = False

        index.overwrite()

        has_exception = False

        try:
            doc2.save()
        except:
            has_exception = True

        self.assertFalse(has_exception)

        index.delete()
Esempio n. 16
0
    def init(cls):
        """
        """

        # TODO: Deal with super classes

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name,
                                                        type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(
                name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass  # This is the case if init was called more than once

        cls._default_manager = cls.objects

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        if hasattr(cls, 'Meta'):
            cls._meta = cls.Meta()
            cls._meta.model_name = name
            cls._meta.object_name = name

            # Giving other classes the chance to extend the meta data on init
            if hasattr(cls, 'extend_meta_data'):
                cls.extend_meta_data(cls, cls._meta)

        # Go through all fields
        fields_dict = cls.get_collection_fields_dict()
        for attribute_name in fields_dict:
            attribute = fields_dict[attribute_name]
            # Trigger init event
            attribute.on_init(cls, attribute_name)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance,
                                  index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Esempio n. 17
0
class SimpleIndexQueryTestCase(ExtendedTestCase):
    def setUp(self):
        self.database_name = 'testcase_simple_index_query_123'
        self.db = Database.create(name=self.database_name)

        # Create test data
        self.test_1_col = self.db.create_collection('foo_1')

        self.hash_index = Index(self.test_1_col, HashIndex(fields=[
            'username'
        ]))

        self.geo_index = Index(self.test_1_col, GeoIndex(fields=['latitude', 'longitude'], geo_json=False))

        self.fulltext_index = Index(self.test_1_col, FulltextIndex(fields=['description'], minimum_length=4))

        self.skiplist_index = Index(self.test_1_col, SkiplistIndex(fields=['rated'], unique=False))

        self.hash_index.save()
        self.geo_index.save()
        self.fulltext_index.save()
        self.skiplist_index.save()

        self.col1_doc1 = self.test_1_col.create_document()
        self.col1_doc1.username='******'
        self.col1_doc1.description='Paris is such a beautiful city'
        self.col1_doc1.city= 'Paris'
        self.col1_doc1.latitude= 48.853333
        self.col1_doc1.longitude= 2.348611
        self.col1_doc1.rated= 4
        self.col1_doc1.save()

        self.col1_doc2 = self.test_1_col.create_document()
        self.col1_doc2.username='******'
        self.col1_doc2.description='The next time I will get myself some tickets for this event'
        self.col1_doc2.city='Koeln'
        self.col1_doc2.latitude= 50.933333
        self.col1_doc2.longitude=6.95
        self.col1_doc2.rated= 4
        self.col1_doc2.save()

        self.col1_doc3 = self.test_1_col.create_document()
        self.col1_doc3.username='******'
        self.col1_doc3.description='I have never seen such a big door in a city'
        self.col1_doc3.city='Berlin'
        self.col1_doc3.latitude=52.524167
        self.col1_doc3.longitude=13.410278
        self.col1_doc3.rated= 8
        self.col1_doc3.save()

        self.col1_doc4 = self.test_1_col.create_document()
        self.col1_doc4.username='******'
        self.col1_doc4.description='It one of the best cities in the world'
        self.col1_doc4.city='Rome'
        self.col1_doc4.latitude=41.891667
        self.col1_doc4.longitude=12.511111
        self.col1_doc4.rated= 10
        self.col1_doc4.save()

    def tearDown(self):
        # Delete index
        self.geo_index.delete()
        self.hash_index.delete()
        self.fulltext_index.delete()
        self.skiplist_index.delete()

        # They need to be deleted
        Collection.remove(name=self.test_1_col.name)

        Database.remove(name=self.database_name)

    def test_get_only_by_hash_index(self):
        """
        """

        doc1 = SimpleIndexQuery.get_by_example_hash(collection=self.test_1_col,
                                                    index_id=self.hash_index.index_type_obj.id,
                                                    example_data={
            'username': self.col1_doc1.username
        })

        self.assertDocumentsEqual(doc1, self.col1_doc1)

    def test_within_position(self):
        """
        """

        # 52.370278, 9.733056 => Hannover (Germany)

        radius_kilometers = 400
        radius_meters = radius_kilometers * 1000

        in_radius_docs = SimpleIndexQuery.within(
            collection=self.test_1_col,
            latitude= 52.370278,
            longitude=9.733056,
            radius=radius_meters,
            index_id=self.geo_index.index_type_obj.id
        )

        self.assertEqual(len(in_radius_docs), 2)

        koeln_doc = in_radius_docs[0]
        berlin_doc = in_radius_docs[1]

        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)


    def test_near_position(self):
        """
        """

        # 50.850278, 4.348611 => Brussels

        near_docs = SimpleIndexQuery.near(
            collection=self.test_1_col,
            latitude=50.850278,
            longitude=4.348611,
            index_id=self.geo_index.index_type_obj.id,
            limit=2
        )

        self.assertEqual(len(near_docs), 2)

        koeln_doc = near_docs[0]
        paris_doc = near_docs[1]

        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
        self.assertDocumentsEqual(paris_doc, self.col1_doc1)

    def test_fulltext_search(self):
        """
        """

        docs_with_description = SimpleIndexQuery.fulltext(
            collection=self.test_1_col,
            attribute='description',
            example_text='city',
            index_id=self.fulltext_index.index_type_obj.id
        )

        self.assertNotEqual(docs_with_description, None)
        self.assertEqual(len(docs_with_description), 2)


        paris_doc = docs_with_description[0]
        berlin_doc = docs_with_description[1]

        self.assertDocumentsEqual(paris_doc, self.col1_doc1)
        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)

    def test_skiplist_by_example(self):
        """
        """

        docs_with_rating = SimpleIndexQuery.get_by_example_skiplist(
            collection=self.test_1_col,
            index_id=self.skiplist_index.index_type_obj.id,
            example_data={ 'rated': 4 }
        )

        self.assertNotEqual(docs_with_rating, None)
        self.assertEqual(len(docs_with_rating), 2)


        paris_doc = docs_with_rating[0]
        koeln_doc = docs_with_rating[1]

        self.assertDocumentsEqual(paris_doc, self.col1_doc1)
        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)

    def test_skiplist_range(self):
        """
        """

        docs_with_rating = SimpleIndexQuery.range(
            collection=self.test_1_col,
            attribute='rated',
            left=8,
            right=10,
            closed=True,
            index_id=self.skiplist_index.index_type_obj.id,
        )

        self.assertNotEqual(docs_with_rating, None)
        self.assertEqual(len(docs_with_rating), 2)

        berlin_doc = docs_with_rating[0]
        rome_doc = docs_with_rating[1]

        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
        self.assertDocumentsEqual(rome_doc, self.col1_doc4)
Esempio n. 18
0
class SimpleIndexQueryTestCase(ExtendedTestCase):
    def setUp(self):
        self.database_name = 'testcase_simple_index_query_123'
        self.db = Database.create(name=self.database_name)

        # Create test data
        self.test_1_col = self.db.create_collection('foo_1')

        self.hash_index = Index(self.test_1_col,
                                HashIndex(fields=['username']))

        self.geo_index = Index(
            self.test_1_col,
            GeoIndex(fields=['latitude', 'longitude'], geo_json=False))

        self.fulltext_index = Index(
            self.test_1_col,
            FulltextIndex(fields=['description'], minimum_length=4))

        self.skiplist_index = Index(
            self.test_1_col, SkiplistIndex(fields=['rated'], unique=False))

        self.hash_index.save()
        self.geo_index.save()
        self.fulltext_index.save()
        self.skiplist_index.save()

        self.col1_doc1 = self.test_1_col.create_document()
        self.col1_doc1.username = '******'
        self.col1_doc1.description = 'Paris is such a beautiful city'
        self.col1_doc1.city = 'Paris'
        self.col1_doc1.latitude = 48.853333
        self.col1_doc1.longitude = 2.348611
        self.col1_doc1.rated = 4
        self.col1_doc1.save()

        self.col1_doc2 = self.test_1_col.create_document()
        self.col1_doc2.username = '******'
        self.col1_doc2.description = 'The next time I will get myself some tickets for this event'
        self.col1_doc2.city = 'Koeln'
        self.col1_doc2.latitude = 50.933333
        self.col1_doc2.longitude = 6.95
        self.col1_doc2.rated = 4
        self.col1_doc2.save()

        self.col1_doc3 = self.test_1_col.create_document()
        self.col1_doc3.username = '******'
        self.col1_doc3.description = 'I have never seen such a big door in a city'
        self.col1_doc3.city = 'Berlin'
        self.col1_doc3.latitude = 52.524167
        self.col1_doc3.longitude = 13.410278
        self.col1_doc3.rated = 8
        self.col1_doc3.save()

        self.col1_doc4 = self.test_1_col.create_document()
        self.col1_doc4.username = '******'
        self.col1_doc4.description = 'It one of the best cities in the world'
        self.col1_doc4.city = 'Rome'
        self.col1_doc4.latitude = 41.891667
        self.col1_doc4.longitude = 12.511111
        self.col1_doc4.rated = 10
        self.col1_doc4.save()

    def tearDown(self):
        # Delete index
        self.geo_index.delete()
        self.hash_index.delete()
        self.fulltext_index.delete()
        self.skiplist_index.delete()

        # They need to be deleted
        Collection.remove(name=self.test_1_col.name)

        Database.remove(name=self.database_name)

    def test_get_only_by_hash_index(self):
        """
        """

        doc1 = SimpleIndexQuery.get_by_example_hash(
            collection=self.test_1_col,
            index_id=self.hash_index.index_type_obj.id,
            example_data={'username': self.col1_doc1.username})

        self.assertDocumentsEqual(doc1, self.col1_doc1)

    def test_within_position(self):
        """
        """

        # 52.370278, 9.733056 => Hannover (Germany)

        radius_kilometers = 400
        radius_meters = radius_kilometers * 1000

        in_radius_docs = SimpleIndexQuery.within(
            collection=self.test_1_col,
            latitude=52.370278,
            longitude=9.733056,
            radius=radius_meters,
            index_id=self.geo_index.index_type_obj.id)

        self.assertEqual(len(in_radius_docs), 2)

        koeln_doc = in_radius_docs[0]
        berlin_doc = in_radius_docs[1]

        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)

    def test_near_position(self):
        """
        """

        # 50.850278, 4.348611 => Brussels

        near_docs = SimpleIndexQuery.near(
            collection=self.test_1_col,
            latitude=50.850278,
            longitude=4.348611,
            index_id=self.geo_index.index_type_obj.id,
            limit=2)

        self.assertEqual(len(near_docs), 2)

        koeln_doc = near_docs[0]
        paris_doc = near_docs[1]

        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)
        self.assertDocumentsEqual(paris_doc, self.col1_doc1)

    def test_fulltext_search(self):
        """
        """

        docs_with_description = SimpleIndexQuery.fulltext(
            collection=self.test_1_col,
            attribute='description',
            example_text='city',
            index_id=self.fulltext_index.index_type_obj.id)

        self.assertNotEqual(docs_with_description, None)
        self.assertEqual(len(docs_with_description), 2)

        paris_doc = docs_with_description[0]
        berlin_doc = docs_with_description[1]

        self.assertDocumentsEqual(paris_doc, self.col1_doc1)
        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)

    def test_skiplist_by_example(self):
        """
        """

        docs_with_rating = SimpleIndexQuery.get_by_example_skiplist(
            collection=self.test_1_col,
            index_id=self.skiplist_index.index_type_obj.id,
            example_data={'rated': 4})

        self.assertNotEqual(docs_with_rating, None)
        self.assertEqual(len(docs_with_rating), 2)

        paris_doc = docs_with_rating[0]
        koeln_doc = docs_with_rating[1]

        self.assertDocumentsEqual(paris_doc, self.col1_doc1)
        self.assertDocumentsEqual(koeln_doc, self.col1_doc2)

    def test_skiplist_range(self):
        """
        """

        docs_with_rating = SimpleIndexQuery.range(
            collection=self.test_1_col,
            attribute='rated',
            left=8,
            right=10,
            closed=True,
            index_id=self.skiplist_index.index_type_obj.id,
        )

        self.assertNotEqual(docs_with_rating, None)
        self.assertEqual(len(docs_with_rating), 2)

        berlin_doc = docs_with_rating[0]
        rome_doc = docs_with_rating[1]

        self.assertDocumentsEqual(berlin_doc, self.col1_doc3)
        self.assertDocumentsEqual(rome_doc, self.col1_doc4)
Esempio n. 19
0
    def setUp(self):
        self.database_name = 'testcase_simple_index_query_123'
        self.db = Database.create(name=self.database_name)

        # Create test data
        self.test_1_col = self.db.create_collection('foo_1')

        self.hash_index = Index(self.test_1_col,
                                HashIndex(fields=['username']))

        self.geo_index = Index(
            self.test_1_col,
            GeoIndex(fields=['latitude', 'longitude'], geo_json=False))

        self.fulltext_index = Index(
            self.test_1_col,
            FulltextIndex(fields=['description'], minimum_length=4))

        self.skiplist_index = Index(
            self.test_1_col, SkiplistIndex(fields=['rated'], unique=False))

        self.hash_index.save()
        self.geo_index.save()
        self.fulltext_index.save()
        self.skiplist_index.save()

        self.col1_doc1 = self.test_1_col.create_document()
        self.col1_doc1.username = '******'
        self.col1_doc1.description = 'Paris is such a beautiful city'
        self.col1_doc1.city = 'Paris'
        self.col1_doc1.latitude = 48.853333
        self.col1_doc1.longitude = 2.348611
        self.col1_doc1.rated = 4
        self.col1_doc1.save()

        self.col1_doc2 = self.test_1_col.create_document()
        self.col1_doc2.username = '******'
        self.col1_doc2.description = 'The next time I will get myself some tickets for this event'
        self.col1_doc2.city = 'Koeln'
        self.col1_doc2.latitude = 50.933333
        self.col1_doc2.longitude = 6.95
        self.col1_doc2.rated = 4
        self.col1_doc2.save()

        self.col1_doc3 = self.test_1_col.create_document()
        self.col1_doc3.username = '******'
        self.col1_doc3.description = 'I have never seen such a big door in a city'
        self.col1_doc3.city = 'Berlin'
        self.col1_doc3.latitude = 52.524167
        self.col1_doc3.longitude = 13.410278
        self.col1_doc3.rated = 8
        self.col1_doc3.save()

        self.col1_doc4 = self.test_1_col.create_document()
        self.col1_doc4.username = '******'
        self.col1_doc4.description = 'It one of the best cities in the world'
        self.col1_doc4.city = 'Rome'
        self.col1_doc4.latitude = 41.891667
        self.col1_doc4.longitude = 12.511111
        self.col1_doc4.rated = 10
        self.col1_doc4.save()