Skip to content

Commit

Permalink
Feature/SK-1288 | Test all get (/) api endpoints (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman authored Jan 23, 2025
1 parent 68d946c commit a464c7a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ services:
- ME_CONFIG_BASICAUTH_PASSWORD=password
ports:
- 8081:8081

fedn_postgres:
image: postgres:15
environment:
POSTGRES_USER: fedn_admin
POSTGRES_PASSWORD: password
POSTGRES_DB: fedn_db
ports:
- "5432:5432"

api-server:
environment:
Expand All @@ -72,6 +81,7 @@ services:
depends_on:
- minio
- mongo
- fedn_postgres
command:
- controller
- start
Expand Down
69 changes: 68 additions & 1 deletion fedn/network/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
#

import unittest
from unittest.mock import patch
from unittest.mock import patch, MagicMock

import pymongo

import fedn # noqa: F401

entitites = ['clients', 'combiners', 'models', 'packages', 'rounds', 'sessions', 'statuses', 'validations']

class NetworkAPITests(unittest.TestCase):
""" Unittests for the Network API. """
Expand Down Expand Up @@ -59,6 +62,70 @@ def test_get_controller_status(self):
# Assert response
self.assertEqual(response.status_code, 200)

def test_get_endpoints(self):
""" Test allt get endpoints. """
return_value = {"count": 1, "results": [{"id": "test"}]}
fedn.network.api.shared.client_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.combiner_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.model_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.package_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.round_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.session_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.status_store.list = MagicMock(return_value=return_value)
fedn.network.api.shared.validation_store.list = MagicMock(return_value=return_value)

for entity in entitites:
response = self.app.get(f'/api/v1/{entity}/')
# Assert response
self.assertEqual(response.status_code, 200)

count = response.json['count']
expected_count = return_value['count']

self.assertEqual(count, expected_count)

id = response.json['results'][0]['id']
expected_id = return_value['results'][0]['id']

self.assertEqual(id, expected_id)

fedn.network.api.shared.client_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.client_store.list.assert_called_once()
fedn.network.api.shared.combiner_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.combiner_store.list.assert_called_once()
fedn.network.api.shared.model_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.model_store.list.assert_called_once()
fedn.network.api.shared.package_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.package_store.list.assert_called_once()
fedn.network.api.shared.round_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.round_store.list.assert_called_once()
fedn.network.api.shared.session_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.session_store.list.assert_called_once()
fedn.network.api.shared.status_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.status_store.list.assert_called_once()
fedn.network.api.shared.validation_store.list.assert_called_with(0, 0, None, pymongo.DESCENDING)
fedn.network.api.shared.validation_store.list.assert_called_once()

for entity in entitites:
headers = {
"X-Limit": 10,
"X-Skip": 10,
"X-Sort-Key": "test",
"X-Sort-Order": "asc"
}
response = self.app.get(f'/api/v1/{entity}/', headers=headers)
# Assert response
self.assertEqual(response.status_code, 200)

fedn.network.api.shared.client_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.combiner_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.model_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.package_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.round_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.session_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.status_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)
fedn.network.api.shared.validation_store.list.assert_called_with(10, 10, "test", pymongo.ASCENDING)


if __name__ == '__main__':
unittest.main()

0 comments on commit a464c7a

Please sign in to comment.