Skip to content

Commit b010690

Browse files
updated the unit testing stage
1 parent 3c9b4c8 commit b010690

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.github/workflows/recommendation.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ jobs:
2424
cd src/recommendation
2525
python -m pip install --upgrade pip
2626
pip install -r requirements.txt
27-
pip install pytest
2827
2928
- name: Run unit tests
3029
working-directory: ./src/recommendation
31-
run: pytest
30+
run: |
31+
cd src/recommendation
32+
python3 -m unittest discover -s . -p "test_*.py"

src/recommendation/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ openfeature-provider-flagd==0.1.5
99
openfeature-hooks-opentelemetry==0.1.3
1010
psutil==5.9.6 # Importing this will also import opentelemetry-instrumentation-system-metrics when running opentelemetry-bootstrap
1111

12+
# Testing
13+
pytest==8.2.1
14+
unittest-xml-reporting==3.0.4
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock
3+
from demo_pb2 import ListRecommendationsRequest
4+
from recommendation import RecommendationService
5+
6+
7+
class TestRecommendationService(unittest.TestCase):
8+
@patch("recommendation.product_catalog_stub")
9+
def test_list_recommendations(self, mock_catalog_stub):
10+
# Arrange
11+
# Fake product catalog response
12+
fake_products = [
13+
MagicMock(id="prod1"),
14+
MagicMock(id="prod2"),
15+
MagicMock(id="prod3"),
16+
MagicMock(id="prod4"),
17+
MagicMock(id="prod5"),
18+
MagicMock(id="prod6"),
19+
]
20+
mock_catalog_stub.ListProducts.return_value.products = fake_products
21+
22+
service = RecommendationService()
23+
request = ListRecommendationsRequest(product_ids=["prod1", "prod2"])
24+
25+
# Act
26+
response = service.ListRecommendations(request, MagicMock())
27+
28+
# Assert
29+
self.assertTrue(len(response.product_ids) > 0)
30+
self.assertNotIn("prod1", response.product_ids)
31+
self.assertNotIn("prod2", response.product_ids)
32+
self.assertLessEqual(len(response.product_ids), 5)
33+
34+
35+
if __name__ == '__main__':
36+
unittest.main()

0 commit comments

Comments
 (0)