Skip to content

Commit

Permalink
issue #21: update tests with versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
k-allagbe committed Dec 22, 2023
1 parent f9742f0 commit d9f48b3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 143 deletions.
36 changes: 0 additions & 36 deletions tests/test_ailab_search.py

This file was deleted.

98 changes: 98 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import unittest
from unittest.mock import patch

from app.api.common.ailab_db import DBError
from app.app_creator import create_app
from tests.common import TestConfig


class TestApiV1(unittest.TestCase):
def setUp(self):
self.config = TestConfig()
self.app = create_app(self.config)
self.client = self.app.test_client()
self.static_search_url = "/api/v1/search/static"
self.azure_search_url = "/api/v1/search/azure"
self.ailab_search_url = "/api/v1/search/ailab"
self.health_check_url = "/api/v1/health"

### Static ###
def test_search_static_success(self):
with patch("app.api.v1.blueprints.search.fetch_data") as mock_fetch:
mock_fetch.return_value = {"some": "data"}
json = {"query": "test query"}
response = self.client.post(self.static_search_url, json=json)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"some": "data"})

def test_search_static_no_query(self):
response = self.client.post(self.static_search_url, json={})
self.assertEqual(response.status_code, 400)

def test_search_static_no_match(self):
with patch("app.api.v1.blueprints.search.fetch_data") as mock_fetch:
mock_fetch.return_value = None
json = {"query": "test query"}
response = self.client.post(self.static_search_url, json=json)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, None)

def test_search_static_error(self):
with patch("app.api.v1.blueprints.search.fetch_data") as mock_fetch:
mock_fetch.side_effect = Exception("API request failed")
json = {"query": "test query"}
response = self.client.post(self.static_search_url, json=json)
self.assertEqual(response.status_code, 500)

### Azure ###
def test_search_azure_success(self):
with patch("app.api.v1.blueprints.search.search") as mock_search:
mock_search.return_value = {"some": "azure data"}
json = {"query": "azure query"}
response = self.client.post(self.azure_search_url, json=json)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"some": "azure data"})

def test_search_azure_no_query(self):
response = self.client.post(self.azure_search_url, json={})
self.assertEqual(response.status_code, 400)

def test_search_azure_error(self):
with patch("app.api.v1.blueprints.search.search") as mock_search:
mock_search.side_effect = Exception("Azure search failed")
json = {"query": "azure query"}
response = self.client.post(self.azure_search_url, json=json)
self.assertEqual(response.status_code, 500)

### Ailab ###
def test_search_ailab_success(self):
with patch("app.api.v1.blueprints.search.ailab_db_search") as mock_search:
mock_search.return_value = {"some": "ailab data"}
json = {"query": "ailab query"}
response = self.client.post(self.ailab_search_url, json=json)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"some": "ailab data"})

def test_search_ailab_no_query(self):
response = self.client.post(self.ailab_search_url, json={})
self.assertEqual(response.status_code, 400)

def test_search_ailab_db_error(self):
with patch("app.api.v1.blueprints.search.ailab_db_search") as mock_search:
mock_search.side_effect = DBError("Ailab DB failed")
json = {"query": "ailab query"}
response = self.client.post(self.ailab_search_url, json=json)
self.assertEqual(response.status_code, 500)

def test_search_ailab_unexpected_error(self):
with patch("app.api.v1.blueprints.search.ailab_db_search") as mock_search:
mock_search.side_effect = Exception("Unexpected error")
json = {"query": "ailab query"}
response = self.client.post(self.ailab_search_url, json=json)
self.assertEqual(response.status_code, 500)

### Health ###
def test_health_route(self):
response = self.client.get(self.health_check_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode(), "ok")
29 changes: 0 additions & 29 deletions tests/test_azure_search.py

This file was deleted.

10 changes: 5 additions & 5 deletions tests/test_finesse_data_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import requests

from app.finesse_data import (
from app.api.common.finesse_data import (
EmptyQueryError,
FinesseDataFetchException,
fetch_data,
Expand All @@ -26,18 +26,18 @@ def setUp(self):
"Product Catalog",
]

@patch("app.finesse_data.requests.get")
@patch("app.api.common.finesse_data.requests.get")
def test_fetch_data_empty_query(self, mock_get):
with self.assertRaises(EmptyQueryError):
fetch_data(self.finesse_data_url, "", self.match_threshold)

@patch("app.finesse_data.requests.get")
@patch("app.api.common.finesse_data.requests.get")
def test_fetch_data_no_match_found(self, mock_get):
mock_get.return_value = Mock(status_code=200, json=lambda: self.files)
result = fetch_data(self.finesse_data_url, "bad query", self.match_threshold)
self.assertIsNone(result)

@patch("app.finesse_data.requests.get")
@patch("app.api.common.finesse_data.requests.get")
def test_fetch_data_success(self, mock_get):
mock_get.side_effect = [
Mock(status_code=200, json=lambda: self.files),
Expand All @@ -46,7 +46,7 @@ def test_fetch_data_success(self, mock_get):
result = fetch_data(self.finesse_data_url, "file1", self.match_threshold)
self.assertEqual(result, {"data": "content"})

@patch("app.finesse_data.requests.get")
@patch("app.api.common.finesse_data.requests.get")
def test_fetch_data_request_exception(self, mock_get):
mock_get.side_effect = requests.RequestException()
with self.assertRaises(FinesseDataFetchException):
Expand Down
20 changes: 0 additions & 20 deletions tests/test_monitor.py

This file was deleted.

53 changes: 0 additions & 53 deletions tests/test_search.py

This file was deleted.

0 comments on commit d9f48b3

Please sign in to comment.