def test_pre_commit_should_be_called_before_on_commit(self): numbers_list = [] with transaction.atomic(): on_commit(lambda: add_number(numbers_list, 2)) pre_commit(lambda: add_number(numbers_list, 1)) assert_equal(len(numbers_list), 0) assert_equal(numbers_list, [1, 2])
def test_pre_commit_should_not_be_called_for_rollback(self): numbers_list = [] with assert_raises(RuntimeError): with transaction.atomic(): on_commit(lambda: add_number(numbers_list, 2)) pre_commit(lambda: add_number(numbers_list, 1)) assert_equal(len(numbers_list), 0) raise RuntimeError assert_equal(numbers_list, [])
def test_pre_commit_should_call_one_time_callable_only_once(self): numbers_list = [] class AddNumberOneTimePreCommitCallable(UniquePreCommitCallable): def handle(self): self.kwargs_list[-1]['numbers_list'].append(3) with transaction.atomic(): for i in range(5): pre_commit( AddNumberOneTimePreCommitCallable( numbers_list=numbers_list, number=i)) assert_equal(len(numbers_list), 0) assert_equal(numbers_list, [3])
def on_invocation_apply(self, invocation_id, args, kwargs, options, result): _super = super() def _on_invocation_apply(): _super.on_invocation_apply(invocation_id, args, kwargs, options, result) if options.get('is_on_commit') and in_atomic_block(): if settings.TASK_USE_PRE_COMMIT: pre_commit(_on_invocation_apply, using=options.get('using')) else: transaction.on_commit(_on_invocation_apply, using=options.get('using')) else: _on_invocation_apply()
def test_pre_commit_should_called_with_the_right_order(self): data = [] def pre_commit_fn_a(): pre_commit(pre_commit_fn_c) data.append('a') def pre_commit_fn_b(): data.append('b') def pre_commit_fn_c(): data.append('c') with transaction.atomic(): pre_commit(pre_commit_fn_a) with transaction.atomic(): pre_commit(pre_commit_fn_b) assert_equal(data, ['a', 'b', 'c'])
def test_pre_commit_should_call_one_time_callable_only_once_for_not_failed_blocks( self): numbers_list = [] class AddNumberOneTimePreCommitCallable(UniquePreCommitCallable): def handle(self): for kwargs in self.kwargs_list: self.kwargs_list[-1]['numbers_list'].append( kwargs['number']) with transaction.atomic(): pre_commit( AddNumberOneTimePreCommitCallable(numbers_list=numbers_list, number=1)) with assert_raises(RuntimeError): with transaction.atomic(): pre_commit( AddNumberOneTimePreCommitCallable( numbers_list=numbers_list, number=2)) assert_equal(len(numbers_list), 0) raise RuntimeError with transaction.atomic(): pre_commit( AddNumberOneTimePreCommitCallable( numbers_list=numbers_list, number=3)) assert_equal(len(numbers_list), 0) assert_equal(len(numbers_list), 0) assert_equal(numbers_list, [1])
def test_pre_commit_should_call_only_not_failed_pre_commit_hooks(self): numbers_list = [] with transaction.atomic(): with transaction.atomic(): pre_commit(lambda: add_number(numbers_list, 0)) assert_equal(len(numbers_list), 0) assert_equal(len(numbers_list), 0) with assert_raises(RuntimeError): with transaction.atomic(): pre_commit(lambda: add_number(numbers_list, 1)) assert_equal(len(numbers_list), 0) raise RuntimeError pre_commit(lambda: add_number(numbers_list, 2)) assert_equal(len(numbers_list), 0) assert_equal(numbers_list, [0, 2])
def test_pre_commit_without_atomic_should_be_called_immediately(self): numbers_list = [] pre_commit(lambda: add_number(numbers_list, 1)) assert_equal(numbers_list, [1])
def pre_commit_fn_a(): pre_commit(pre_commit_fn_c) data.append('a')
def _handle(self, instance, **kwargs): pre_commit(InstanceOneTimePreCommitHandlerCallable(self, instance), using=self.using)
def _handle(self, instance, **kwargs): pre_commit(lambda: self.handle(instance, **kwargs), using=self.using)