Commit 600e6aca authored by Vangelis Koukis's avatar Vangelis Koukis
Browse files

Fix one API test failing with PostgreSQL backend

One API test (test_servers_details) was too restrictive and failed
with the PostgreSQL backend. It assumed consistent, by-id ordering
between DB and API server list replies, and failed although there was
1-1 correspondence between the two replies.

To fix, the test takes care to order the two replies by id before
attempting to compare their contents.
parent c3fa31bd
......@@ -68,16 +68,22 @@ class APITestCase(TestCase):
self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
self.assertTrue(response.status_code in [200, 203])
def test_servers_details(self):
"""Test if the servers details are returned."""
response = self.client.get('/api/v1.1/servers/detail')
vms_from_db = VirtualMachine.objects.filter(deleted=False)
# Make sure both DB and API responses are sorted by id,
# to allow for 1-1 comparisons
vms_from_db = VirtualMachine.objects.filter(deleted=False).order_by('id')
vms_from_api = json.loads(response.content)['servers']['values']
vms_from_api = sorted(vms_from_api, key=lambda vm: vm['id'])
self.assertEqual(len(vms_from_db), len(vms_from_api))
id_list = [ for vm in vms_from_db]
number = 0
for vm_id in id_list:
vm_from_api = json.loads(response.content)['servers']['values'][number]
vm_from_api = vms_from_api[number]
vm_from_db = VirtualMachine.objects.get(id=vm_id)
self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
......@@ -86,7 +92,6 @@ class APITestCase(TestCase):
self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
number += 1
vms_from_api = json.loads(response.content)['servers']['values']
for vm_from_api in vms_from_api:
vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
......@@ -97,7 +102,6 @@ class APITestCase(TestCase):
self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
self.assertTrue(response.status_code in [200,203])
def test_wrong_server(self):
"""Test 404 response if server does not exist."""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment