Ejemplo n.º 1
0
def test_schedule_executes_in_order():
    """A MigrationSchedule is used internally to schedule calls in different phases. The calls 
       scheduled in each phase are executed in the order the phases have been set up on the MigrationSchedule.
       Within a phase, the calls are executed in the order they were registered in that phase.
    """

    schedule_names = ['a', 'b', 'c']
    migration_schedule = MigrationSchedule(*schedule_names)

    class SomeObject(object):
        def do_something(self, arg):
            pass

    some_object = SomeObject()

    # schedule calls not in registered order
    with CallMonitor(some_object.do_something) as monitor:
        migration_schedule.schedule('c', some_object.do_something, 'c1')
        migration_schedule.schedule('a', some_object.do_something, 'a1')
        migration_schedule.schedule('b', some_object.do_something, 'b')
        migration_schedule.schedule('a', some_object.do_something, 'a2')
        migration_schedule.schedule('c', some_object.do_something, 'c2')

    migration_schedule.execute_all()

    actual_order = [call.args[0] for call in monitor.calls]
    expected_order = ['a1', 'a2', 'b', 'c1', 'c2']
    assert actual_order == expected_order
Ejemplo n.º 2
0
def test_invalid_schedule_name_raises():
    """A useful error is raised when an attempt is made to schedule a call in a phase that is not defined."""
    
    migration_schedule = MigrationSchedule(EmptyStub(), EmptyStub(), [])

    with expected(ProgrammerError, test=r'A phase with name<wrong_name> does not exist\.'):
        migration_schedule.schedule('wrong_name', EmptyStub(), EmptyStub(), EmptyStub())
Ejemplo n.º 3
0
def test_invalid_schedule_name_raises():
    """A useful error is raised when an attempt is made to schedule a call in a phase that is not defined."""

    valid_schedule_names = ['a', 'b']
    migration_schedule = MigrationSchedule(*valid_schedule_names)

    with expected(ProgrammerError,
                  test='A phase with name<wrong_name> does not exist\.'):
        migration_schedule.schedule('wrong_name', None)
Ejemplo n.º 4
0
def invalid_schedule_name_raises(fixture):
    """A useful error is raised when an attempt is made to schedule a call in a phase that is not defined."""

    valid_schedule_names = ['a', 'b']
    migration_schedule = MigrationSchedule(*valid_schedule_names)

    def check_exception(ex):
        vassert(
            six.text_type(ex) ==
            'A phase with name<wrong_name> does not exist.')

    with expected(ProgrammerError, test=check_exception):
        migration_schedule.schedule('wrong_name', None)
Ejemplo n.º 5
0
def test_error_reporting_on_breaking_migrations():
    """When there is an error during execution of a Migration, the code where it was scheduled is reported."""

    class SomeObject:
        def please_call_me(self):
            raise Exception('breaking intentionally')
    some_object = SomeObject()

    migration_schedule = MigrationSchedule(EmptyStub(), EmptyStub(), [])
    migration = Migration(migration_schedule)

    migration.schedule('alter', some_object.please_call_me)

    def is_please_call_me_on_stack(ex):
        return re.match(".*The above Exception happened for the migration that was scheduled here:.*    migration.schedule\('alter', some_object.please_call_me\)", str(ex), re.MULTILINE|re.S)
    with expected(ExceptionDuringMigration, test=is_please_call_me_on_stack):
        migration_schedule.execute_all()
Ejemplo n.º 6
0
def test_schedule_executes_phases_with_parameters():
    """When a MigrationSchedule executes the calls that were scheduled from a Migration, 
       the methods are actually called, and passed the correct arguments."""

    class SomeObject:
        def please_call_me(self, arg, kwarg=None):
            pass
    some_object = SomeObject()
    
    migration_schedule = MigrationSchedule(EmptyStub(), EmptyStub(), [])
    migration = Migration(migration_schedule)

    with CallMonitor(some_object.please_call_me) as monitor:
        migration.schedule('alter', some_object.please_call_me, 'myarg', kwarg='mykwarg')

    migration_schedule.execute_all()

    assert monitor.calls[0].args == ('myarg',)
    assert monitor.calls[0].kwargs == dict(kwarg='mykwarg')
Ejemplo n.º 7
0
def test_schedule_executes_in_order():
    """A MigrationSchedule is used internally to schedule calls in different phases. The calls 
       scheduled in each phase are executed in the order the phases have been set up on the MigrationSchedule.
       Within a phase, the calls are executed in the order they were registered in that phase.
    """
    
    migration_schedule = MigrationSchedule(EmptyStub(), EmptyStub(), [])

    class SomeObject:
        def do_something(self, arg):
            pass
    some_object = SomeObject()

    # schedule calls not in registered order
    with CallMonitor(some_object.do_something) as monitor:
        migration_schedule.schedule('cleanup', EmptyStub(), EmptyStub(), some_object.do_something, '1')
        migration_schedule.schedule('create_fk', EmptyStub(), EmptyStub(), some_object.do_something, '2')
        migration_schedule.schedule('data', EmptyStub(), EmptyStub(), some_object.do_something, '3')
        migration_schedule.schedule('indexes', EmptyStub(), EmptyStub(), some_object.do_something, '4')
        migration_schedule.schedule('create_pk', EmptyStub(), EmptyStub(), some_object.do_something, '5')

        migration_schedule.schedule('alter', EmptyStub(), EmptyStub(), some_object.do_something, 'c1')
        migration_schedule.schedule('drop_pk', EmptyStub(), EmptyStub(),some_object.do_something, 'a1')
        migration_schedule.schedule('pre_alter', EmptyStub(), EmptyStub(),some_object.do_something, 'b')
        migration_schedule.schedule('drop_pk', EmptyStub(), EmptyStub(),some_object.do_something, 'a2')
        migration_schedule.schedule('alter', EmptyStub(), EmptyStub(),some_object.do_something, 'c2')

    migration_schedule.execute_all()

    actual_order = [call.args[0] for call in monitor.calls]
    expected_order = ['a1', 'a2', 'b', 'c1', 'c2', '5', '4', '3', '2', '1']
    assert actual_order == expected_order