Exemplo n.º 1
0
    def test_to_schema_patch__should_return_dictdiffer_diff(self, left_schema):
        right_schema = Schema({
            'Document1':
            Schema.Document(
                {
                    'field1': {
                        'param11': 'schemavalue11',
                        'param12': 'schemavalue12'
                    },
                    'field2': {
                        'param21': 'schemavalue21',
                        'param22': 'schemavalue22'
                    },
                },
                parameters={'collection': 'document1'},
                indexes={
                    'index1': {
                        'fields': [('field1', pymongo.ASCENDING)]
                    },
                    'index2': {
                        'fields': [('field1', pymongo.ASCENDING),
                                   ('field2', pymongo.GEOHAYSTACK)],
                        'name':
                        'index2',
                        'sparse':
                        True
                    },
                    'index3': {
                        'fields': [('field2', pymongo.DESCENDING)],
                        'name': 'index3',
                        'sparse': False
                    }
                }),
            '~EmbeddedDocument2':
            Schema.Document({
                'field1': {
                    'param3': 'schemavalue3'
                },
                'field2': {
                    'param4': 'schemavalue4'
                },
            })
        })
        fields = [('field2', pymongo.DESCENDING)]
        action = CreateIndex('Document1',
                             'index3',
                             fields=fields,
                             name='index3',
                             sparse=False)
        expect = [('change', 'Document1', (left_schema['Document1'],
                                           right_schema['Document1']))]

        res = action.to_schema_patch(left_schema)

        assert res == expect
Exemplo n.º 2
0
    def test_prepare__if_index_is_in_schema__should_raise_error(
            self, test_db, left_schema):
        fields = [('field2', pymongo.DESCENDING)]
        left_schema['Document1'].indexes['index3'] = {
            'fields': fields,
            'name': 'index3',
            'sparse': False
        }
        action = CreateIndex('Document1',
                             'index3',
                             fields=fields,
                             name='index3',
                             sparse=False)

        with pytest.raises(SchemaError):
            action.prepare(test_db, left_schema, MigrationPolicy.strict)
Exemplo n.º 3
0
    def test_build_object__if_index_creates__should_return_object(
            self, left_schema):
        right_schema = Schema({
            'Document1':
            Schema.Document(
                {
                    'field1': {
                        'param11': 'schemavalue11',
                        'param12': 'schemavalue12'
                    },
                    'field2': {
                        'param21': 'schemavalue21',
                        'param22': 'schemavalue22'
                    },
                },
                parameters={'collection': 'document1'},
                indexes={
                    'index3': {
                        'fields': [('field2', pymongo.DESCENDING)],
                        'name': 'index3',
                        'sparse': True
                    }
                }),
            '~EmbeddedDocument2':
            Schema.Document({
                'field1': {
                    'param3': 'schemavalue3'
                },
                'field2': {
                    'param4': 'schemavalue4'
                },
            })
        })

        res = CreateIndex.build_object('Document1', 'index3', left_schema,
                                       right_schema)

        assert isinstance(res, CreateIndex)
        assert res.document_type == 'Document1'
        assert res.index_name == 'index3'
        assert res.parameters == {
            'name': 'index3',
            'sparse': True,
            'fields': [('field2', pymongo.DESCENDING)]
        }
Exemplo n.º 4
0
    def test_forward__if_name_param_does_not_specified__should_create_with_autogenerated_name(
            self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        action = CreateIndex('Document1', 'test_index', fields=fields)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        action.run_forward()

        indexes = [
            x for x in test_db['document1'].list_indexes()
            if x['key'] == SON(fields)
        ]
        assert len(indexes) == 1
Exemplo n.º 5
0
    def test_build_object__if_document_not_in_both_schemas__should_return_none(
            self, left_schema, document_type):
        right_schema = Schema({
            'Document_new':
            Schema.Document(
                {
                    'field1': {
                        'param11': 'schemavalue11',
                        'param12': 'schemavalue12'
                    },
                    'field2': {
                        'param21': 'schemavalue21',
                        'param22': 'schemavalue22'
                    },
                    'field3': {
                        'param31': 'schemavalue31',
                        'param32': 'schemavalue32'
                    },
                },
                parameters={'collection': 'document1'},
                indexes={
                    'index3': {
                        'fields': [('field2', pymongo.DESCENDING)],
                        'name': 'index3',
                        'sparse': True
                    }
                }),
            '~EmbeddedDocument2':
            Schema.Document({
                'field1': {
                    'param3': 'schemavalue3'
                },
                'field2': {
                    'param4': 'schemavalue4'
                },
            })
        })

        res = CreateIndex.build_object(document_type, 'index3', left_schema,
                                       right_schema)

        assert res is None
Exemplo n.º 6
0
    def test_forward__if_index_with_such_fields_spec_and_other_parameters_already_exist__should_raise_error(
            self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        test_db['document1'].create_index(fields,
                                          name='old_index',
                                          sparse=False)
        action = CreateIndex('Document1',
                             'test_index',
                             fields=fields,
                             sparse=True)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        with pytest.raises(MigrationError):
            action.run_forward()
Exemplo n.º 7
0
    def test_build_object__if_index_in_embedded_document__should_return_none(
            self, left_schema):
        right_schema = Schema({
            'Document1':
            Schema.Document(
                {
                    'field1': {
                        'param11': 'schemavalue11',
                        'param12': 'schemavalue12'
                    },
                    'field3': {
                        'param31': 'schemavalue31',
                        'param32': 'schemavalue32'
                    },
                },
                parameters={'collection': 'document1'}),
            '~EmbeddedDocument2':
            Schema.Document(
                {
                    'field1': {
                        'param3': 'schemavalue3'
                    },
                    'field2': {
                        'param4': 'schemavalue4'
                    },
                },
                indexes={
                    'test_index': {
                        'fields': [('field1', pymongo.DESCENDING)]
                    }
                })
        })

        res = CreateIndex.build_object('~EmbeddedDocument2', 'test_index',
                                       left_schema, right_schema)

        assert res is None
Exemplo n.º 8
0
    def test_forward__should_create_index(self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        action = CreateIndex('Document1',
                             'test_index',
                             fields=fields,
                             name='test_index',
                             sparse=True)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        action.run_forward()

        indexes = [
            x for x in test_db['document1'].list_indexes()
            if x['key'] == SON(fields)
        ]
        assert len(indexes) == 1
        assert indexes[0]['name'] == 'test_index'
        assert indexes[0]['sparse'] is True
Exemplo n.º 9
0
    def test_forward__if_index_with_such_fields_spec_and_other_parameters_already_exist__should_ignore(
            self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        test_db['document1'].create_index(fields,
                                          name='test_index',
                                          sparse=True)
        action = CreateIndex('Document1',
                             'test_index',
                             fields=fields,
                             sparse=True)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        action.run_forward()

        indexes = [
            x for x in test_db['document1'].list_indexes()
            if x['key'] == SON(fields)
        ]
        assert len(indexes) == 1
Exemplo n.º 10
0
    def test_forward_backward__if_index_already_dropped__should_ignore_this(
            self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        action = CreateIndex('Document1',
                             'test_index',
                             fields=fields,
                             name='test_index',
                             sparse=False)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)
        action.run_forward()
        action.cleanup()
        test_db['document1'].drop_index('test_index')
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        action.run_backward()

        indexes = [
            x for x in test_db['document1'].list_indexes()
            if x['key'] == SON(fields)
        ]
        assert len(indexes) == 0
Exemplo n.º 11
0
    def test_forward_backward__if_name_param_does_not_specified__should_drop_by_fields_spec(
            self, test_db, left_schema):
        fields = [('field1', pymongo.ASCENDING),
                  ('field2', pymongo.DESCENDING)]
        action = CreateIndex('Document1',
                             'test_index',
                             fields=fields,
                             sparse=True)
        action.prepare(test_db, left_schema, MigrationPolicy.strict)
        action.run_forward()
        action.cleanup()
        action.prepare(test_db, left_schema, MigrationPolicy.strict)

        action.run_backward()

        indexes = [
            x for x in test_db['document1'].list_indexes()
            if x['key'] == SON(fields)
        ]
        assert len(indexes) == 0