I'm testing a function in my project. The function modifies some records in my test db. I'm interested to see if a specific record was updated. I call my method inside my test_function and after that looking for the record. It seems the record wasn't updated, but the function (with some logs) tells me it was!
I created my TestCase class using both TestCase and TransactionTestCase, but none of them worked. I'm usign Django 2.2
class HostTestCase(TransactionTestCase):
def setUp(self):
task = Task.objects.create(name='do_backup', description="")
for i in range(1, 17):
if i in [4, 7, 9, 11, 12, 14, 15]:
host = Host.objects.create(
name="test_sbb{}".format(i),
mac_address="asgsb{}".format(i),
remote_address="mock-server{}:4010".format(i)
)
if i in [11, 14, 15]:
host.last_seen_at = timezone.now()
host.save()
if i in [9, 12, 15]:
task_assigned = Host_Task.objects.create(host=host, task=task, status="IDLE")
elif i in [4, 7, 11, 14]:
task_assigned = Host_Task.objects.create(host=host, task=task, status="WORKING_CREATION")
else:
if i != 16:
host = Host.objects.create(
name="test_sbb{}".format(i),
mac_address="asgsb{}".format(i),
remote_address="mock-server{}:4010".format(i),
get_backup=False
)
if i in [8, 10, 13]:
host.last_seen_at = timezone.now()
host.save()
if i in [3, 6, 10, 13]:
task_assigned = Host_Task.objects.create(host=host, task=task, status="WORKING_CREATION")
else:
host = Host.objects.create(
name="test_sbb{}".format(i),
mac_address="asgsb{}".format(i),
remote_address="mock-server:4010"
)
host.last_seen_at = timezone.now()
host.save()
influx = InfluxDB(host)
# create new db
influx.create_database()
write_mock_data(influx, i)
def tearDown(self):
for i in range(1, 17):
host = Host.objects.filter(name="test_sbb{}".format(i)).first()
influx = InfluxDB(host)
influx.client.drop_database("{}_metrics".format(host.name))
print("DB {}_metrics dropped".format(host.name))
def test_do_backup_OK(self):
host = Host.objects.filter(name="test_sbb16").first()
task = Task.objects.filter(name="do_backup").first()
task_assigned = Host_Task.objects.create(host=host, task=task, status="IDLE")
task_manager = TaskManager()
task_manager.do_backup()
task_assigned.refresh_from_db()
self.assertEqual(task_assigned.status, "WORKING_CREATION")
The do_backup should modify only the Host_Task object with host = test_sbb16 and that task, updating the status to "WORKING_CREATION, instead the status remains "IDLE"
Aucun commentaire:
Enregistrer un commentaire