def test_forward__if_required_and_default_is_set__should_create_field_and_set_a_value(
            self, load_fixture, test_db, dump_db):
        schema = load_fixture('schema1').get_schema()
        dump = dump_db()
        default = 'test!'
        expect = deepcopy(dump)
        parser = jsonpath_rw.parse('schema1_doc1[*]')
        for rec in parser.find(expect):
            rec.value['test_field'] = default

        action = CreateField('Schema1Doc1',
                             'test_field',
                             choices=None,
                             db_field='test_field',
                             default=default,
                             max_length=None,
                             min_length=None,
                             null=False,
                             primary_key=False,
                             regex=None,
                             required=True,
                             sparse=False,
                             type_key='StringField',
                             unique=False,
                             unique_with=None)
        action.prepare(test_db, schema, MigrationPolicy.strict)

        action.run_forward()

        assert expect == dump_db()
    def test_forward__if_required_and_default_is_set_and_field_in_db__should_not_touch_field(
            self, load_fixture, test_db, dump_db):
        schema = load_fixture('schema1').get_schema()
        default = 'test!'
        ids = set()
        for doc in test_db['schema1_doc1'].find({}, limit=2):
            test_db['schema1_doc1'].update_one(
                {'_id': doc['_id']}, {'$set': {
                    'test_field': 'old_value'
                }})
            ids.add(doc['_id'])

        action = CreateField('Schema1Doc1',
                             'test_field',
                             choices=None,
                             db_field='test_field',
                             default=default,
                             max_length=None,
                             min_length=None,
                             null=False,
                             primary_key=False,
                             regex=None,
                             required=True,
                             sparse=False,
                             type_key='StringField',
                             unique=False,
                             unique_with=None)
        action.prepare(test_db, schema, MigrationPolicy.strict)

        action.run_forward()

        assert all(d['test_field'] == 'old_value'
                   for d in test_db['schema1_doc1'].find() if d['_id'] in ids)
    def test_forward__if_default_is_not_set__should_do_nothing(
            self, load_fixture, test_db, dump_db):
        schema = load_fixture('schema1').get_schema()
        dump = dump_db()

        action = CreateField('~Schema1EmbDoc1',
                             'test_field',
                             choices=None,
                             db_field='test_field',
                             default=None,
                             max_length=None,
                             min_length=None,
                             null=False,
                             primary_key=False,
                             regex=None,
                             required=False,
                             sparse=False,
                             type_key='StringField',
                             unique=False,
                             unique_with=None)
        action.prepare(test_db, schema, MigrationPolicy.strict)

        action.run_forward()

        assert dump == dump_db()