File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed
Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change @@ -9,3 +9,6 @@ openfeature-provider-flagd==0.1.5
99openfeature-hooks-opentelemetry == 0.1.3
1010psutil == 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
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments