def test_init_save_fetch_delete(connection, assert_atomic, stub): t = Task(stub, ["foo"]) assert not connection.exists(t.key) with assert_atomic(): t._save() assert connection.exists(t.key) fetched = Task.fetch(t.id) assert fetched.id == t.id assert fetched.args == ["foo"] Task.delete_many([t.id]) assert not connection.exists(t.key) with pytest.raises(TaskDoesNotExist): Task.fetch(t.id)
def test_persistence(assert_atomic, connection, stub): fields = { 'func_name', 'args', 'kwargs', 'status', 'origin', 'description', 'error_message', 'enqueued_at', 'started_at', 'ended_at', 'meta', 'aborted_runs' } def randomize_data(task): string_fields = [ 'func_name', 'status', 'description', 'origin', 'error_message' ] date_fields = ['enqueued_at', 'started_at', 'ended_at'] for f in string_fields: setattr(task, f, str(uuid.uuid4())) for f in date_fields: setattr( task, f, datetime.datetime(random.randint(1000, 9999), 1, 1, tzinfo=datetime.timezone.utc)) task.args = tuple(str(uuid.uuid4()) for i in range(4)) task.kwargs = {str(uuid.uuid4()): ["d"]} task.meta = {"x": [str(uuid.uuid4())]} task.aborted_runs = ["foo", "bar", str(uuid.uuid4())] def as_dict(task): return {f: getattr(task, f) for f in fields} task = Task(stub) with assert_atomic(): task._save() assert set(decode_list(connection.hkeys(task.key))) <= fields assert as_dict(Task.fetch(task.id)) == as_dict(task) randomize_data(task) task._save() assert as_dict(Task.fetch(task.id)) == as_dict(task) # only deletes task.enqueued_at = None task.error_message = None task._save(['enqueued_at', 'error_message']) assert task.enqueued_at is None assert task.error_message is None for i in range(5): store = random.sample(fields, 7) copy = Task.fetch(task.id) randomize_data(copy) copy._save(store) for f in store: setattr(task, f, getattr(copy, f)) assert as_dict(Task.fetch(task.id)) == as_dict(task) copy = Task.fetch(task.id) randomize_data(copy) copy.meta = task.meta = {"new_meta": "here"} copy.save_meta() copy.refresh() assert as_dict(copy) == as_dict(task) assert as_dict(Task.fetch(task.id)) == as_dict(task) # save() and save_meta() in same pipeline with assert_atomic(): with connection.pipeline() as pipe: task = Task(stub) task._save(pipeline=pipe) task.meta["a"] = "b" task.save_meta(pipeline=pipe) pipe.execute() assert Task.fetch(task.id).meta == {"a": "b"} task = Task(stub) with pytest.raises(TaskDoesNotExist): task.refresh() with pytest.raises(TaskDoesNotExist): Task.fetch('nonexist')