def test_forward_backward__if_name_is_set_and_changed_and_field_spec_is_the_same__should_undo_changes( self, test_db, left_schema): fields = [('field1', pymongo.ASCENDING), ('field2', pymongo.DESCENDING)] test_db['document1'].create_index(fields, name='index_old', sparse=True) action = AlterIndex('Document1', 'index2', fields=fields, name='index2', sparse=False) 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) == 1 assert indexes[0]['name'] == 'index2' assert indexes[0]['sparse'] is True # See index2 schema
def test_forward__if_name_is_set_and_changed_and_field_spec_also_changed__should_create_index( self, test_db, left_schema): fields1 = [('field2', pymongo.ASCENDING)] fields2 = [('field1', pymongo.ASCENDING), ('field2', pymongo.DESCENDING)] test_db['document1'].create_index(fields1, name='index_old', sparse=False) action = AlterIndex('Document1', 'index2', fields=fields2, name='index2', sparse=True) action.prepare(test_db, left_schema, MigrationPolicy.strict) action.run_forward() indexes1 = [ x for x in test_db['document1'].list_indexes() if x['key'] == SON(fields1) ] assert len(indexes1) == 1 assert indexes1[0]['sparse'] is False assert indexes1[0]['name'] == 'index_old' indexes2 = [ x for x in test_db['document1'].list_indexes() if x['key'] == SON(fields2) ] assert len(indexes2) == 1 assert indexes2[0]['name'] == 'index2' assert indexes2[0]['sparse'] is True
def test_prepare__if_index_is_not_in_schema__should_raise_error( self, test_db, left_schema): action = AlterIndex('Document1', 'unknown_index', fields=[('field1', pymongo.ASCENDING)]) with pytest.raises(SchemaError): action.prepare(test_db, left_schema, MigrationPolicy.strict)
def test_forward__if_name_is_not_set_and_field_spec_is_the_same__should_recreate_index( self, test_db, left_schema): fields = [('field1', pymongo.ASCENDING)] test_db['document1'].create_index(fields, sparse=False) action = AlterIndex('Document1', 'index1', 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 assert indexes[0]['sparse'] is True