Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed May 2, 2024
1 parent 79be44d commit 36af017
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 83 deletions.
151 changes: 77 additions & 74 deletions breathecode/monitoring/tests/management/commands/tests_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def log(self, module, name):
def alog(self, module, name):
return self.log(module, name)

def id(self, module, name):
s = SupervisorModel.objects.filter(task_module=module, task_name=name).first()
return s.id

@sync_to_async
def aid(self, module, name):
return self.id(module, name)


@pytest.fixture
def supervisor(db, bc: Breathecode):
Expand Down Expand Up @@ -112,25 +120,24 @@ def tests_older_issues_are_removed(self, database: dfx.Database, supervisor: Sup
model = database.create(**extra)

run_supervisor_mock, fix_issue_mock = patch
run_supervisor_mock.call_args_list = []
fix_issue_mock.call_args_list = []
command = Command()

assert command.handle() == None
assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
},
]
assert {
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
} in supervisor.list()
assert supervisor.log('breathecode.payments.supervisors', 'supervise_all_consumption_sessions') == []
assert database.list_of('monitoring.Supervisor') == [
db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}),
]
assert db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}) in database.list_of('monitoring.Supervisor')
assert database.list_of('monitoring.SupervisorIssue') == []
assert run_supervisor_mock.call_args_list == [call(1)]
assert call(supervisor.id('breathecode.payments.supervisors',
'supervise_all_consumption_sessions')) in run_supervisor_mock.call_args_list
assert fix_issue_mock.call_args_list == []

def tests_recent_issues_keeps__available_attempts(self, bc: Breathecode, database: dfx.Database,
Expand All @@ -147,26 +154,25 @@ def tests_recent_issues_keeps__available_attempts(self, bc: Breathecode, databas
}))

run_supervisor_mock, fix_issue_mock = patch
run_supervisor_mock.call_args_list = []
fix_issue_mock.call_args_list = []
command = Command()

assert command.handle() == None
assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
},
]
assert {
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
} in supervisor.list()
assert supervisor.log('breathecode.payments.supervisors',
'supervise_all_consumption_sessions') == [x.error for x in model.supervisor_issue]
assert database.list_of('monitoring.Supervisor') == [
db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}),
]
assert db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}) in database.list_of('monitoring.Supervisor')
assert database.list_of('monitoring.SupervisorIssue') == bc.format.to_dict(model.supervisor_issue)
assert run_supervisor_mock.call_args_list == [call(1)]
assert call(supervisor.id('breathecode.payments.supervisors',
'supervise_all_consumption_sessions')) in run_supervisor_mock.call_args_list
assert fix_issue_mock.call_args_list == [call(1), call(2)]

def tests_recent_issues_keeps__no_available_attempts(self, bc: Breathecode, database: dfx.Database,
Expand All @@ -183,26 +189,25 @@ def tests_recent_issues_keeps__no_available_attempts(self, bc: Breathecode, data
}))

run_supervisor_mock, fix_issue_mock = patch
run_supervisor_mock.call_args_list = []
fix_issue_mock.call_args_list = []
command = Command()

assert command.handle() == None
assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
},
]
assert {
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
} in supervisor.list()
assert supervisor.log('breathecode.payments.supervisors',
'supervise_all_consumption_sessions') == [x.error for x in model.supervisor_issue]
assert database.list_of('monitoring.Supervisor') == [
db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}),
]
assert db({
'id': 1,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}) in database.list_of('monitoring.Supervisor')
assert database.list_of('monitoring.SupervisorIssue') == bc.format.to_dict(model.supervisor_issue)
assert run_supervisor_mock.call_args_list == [call(1)]
assert call(supervisor.id('breathecode.payments.supervisors',
'supervise_all_consumption_sessions')) in run_supervisor_mock.call_args_list
assert fix_issue_mock.call_args_list == []


Expand All @@ -229,27 +234,26 @@ def tests_pending_to_be_scheduled(self, database: dfx.Database, supervisor: Supe
model = database.create(supervisor=s, supervisor_issue=supervisor_issues)

run_supervisor_mock, fix_issue_mock = patch
run_supervisor_mock.call_args_list = []
fix_issue_mock.call_args_list = []
command = Command()

assert command.handle() == None
assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
},
]
assert {
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
} in supervisor.list()
assert supervisor.log('breathecode.payments.supervisors', 'supervise_all_consumption_sessions') == []
assert database.list_of('monitoring.Supervisor') == [
db({
'id': 1,
'delta': delta,
'ran_at': ran_at,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}),
]
assert db({
'id': 1,
'delta': delta,
'ran_at': ran_at,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}) in database.list_of('monitoring.Supervisor')
assert database.list_of('monitoring.SupervisorIssue') == []
assert run_supervisor_mock.call_args_list == [call(1)]
assert call(supervisor.id('breathecode.payments.supervisors',
'supervise_all_consumption_sessions')) in run_supervisor_mock.call_args_list
assert fix_issue_mock.call_args_list == []

def tests_in_cooldown(self, database: dfx.Database, supervisor: Supervisor, patch, utc_now):
Expand All @@ -268,25 +272,24 @@ def tests_in_cooldown(self, database: dfx.Database, supervisor: Supervisor, patc
model = database.create(supervisor=s, supervisor_issue=supervisor_issues)

run_supervisor_mock, fix_issue_mock = patch
run_supervisor_mock.call_args_list = []
fix_issue_mock.call_args_list = []
command = Command()

assert command.handle() == None
assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
},
]
assert {
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
} in supervisor.list()
assert supervisor.log('breathecode.payments.supervisors', 'supervise_all_consumption_sessions') == []
assert database.list_of('monitoring.Supervisor') == [
db({
'id': 1,
'delta': delta,
'ran_at': ran_at,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}),
]
assert db({
'id': 1,
'delta': delta,
'ran_at': ran_at,
'task_module': 'breathecode.payments.supervisors',
'task_name': 'supervise_all_consumption_sessions',
}) in database.list_of('monitoring.Supervisor')
assert database.list_of('monitoring.SupervisorIssue') == []
assert run_supervisor_mock.call_args_list == []
assert call(supervisor.id('breathecode.payments.supervisors',
'supervise_all_consumption_sessions')) not in run_supervisor_mock.call_args_list
assert fix_issue_mock.call_args_list == []
2 changes: 1 addition & 1 deletion breathecode/payments/supervisors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def supervise_all_consumption_sessions():

users = User.objects.filter(consumptionsession__status='CANCELLED',
consumptionsession__eta__lte=utc_now,
consumptionsession__eta__gte=utc_now - timedelta(days=1))
consumptionsession__eta__gte=utc_now - timedelta(days=1)).distinct()

for user in users:
done_sessions = ConsumptionSession.objects.filter(user=user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ def tests_so_much_pending_sessions(database: dfx.Database, supervisor: Superviso

supervise_all_consumption_sessions()

# run_supervisor.delay(1)

assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
Expand All @@ -131,8 +129,6 @@ def tests_so_much_cancelled_sessions__no_unsafe_sessions(database: dfx.Database,

supervise_all_consumption_sessions()

# run_supervisor.delay(1)

assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
Expand All @@ -145,20 +141,18 @@ def tests_so_much_cancelled_sessions__no_unsafe_sessions(database: dfx.Database,
def tests_so_much_cancelled_sessions__unsafe_sessions(database: dfx.Database, supervisor: Supervisor, utc_now: datetime,
random: cfx.Random):
eta = utc_now - timedelta(seconds=(3600 * random.int(1, 24)) - 1)
x = {'eta': eta, 'operation_code': 'unsafe-consume-service-set'}
x = {'eta': eta, 'operation_code': 'unsafe-consume-service-set', 'user_id': 1}
consumption_sessions = [{
'status': 'CANCELLED',
**x
} for _ in range(4)] + [{
'status': 'DONE',
**x
} for _ in range(6)]
model = database.create(consumption_session=consumption_sessions, service_set=1)
model = database.create(consumption_session=consumption_sessions, service_set=1, user=1)

supervise_all_consumption_sessions()

# run_supervisor.delay(1)

assert supervisor.list() == [
{
'task_module': 'breathecode.payments.supervisors',
Expand Down

0 comments on commit 36af017

Please sign in to comment.