Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
5013d7d
1
Parent(s):
5aa5283
removed tests
Browse files- tests/__init__.py +0 -0
- tests/test_ai_agent_service.py +0 -202
- tests/test_analysis_agent_service.py +0 -88
- tests/test_auth_service.py +0 -56
- tests/test_ingredients_service.py +0 -80
- tests/test_scan_history_service.py +0 -43
tests/__init__.py
DELETED
|
File without changes
|
tests/test_ai_agent_service.py
DELETED
|
@@ -1,202 +0,0 @@
|
|
| 1 |
-
import unittest
|
| 2 |
-
from unittest.mock import patch, MagicMock
|
| 3 |
-
from sqlalchemy.orm import Session
|
| 4 |
-
from services.ai_agent import preprocess_data, validate_data, clean_data, standardize_data, enrich_data, process_data, integrate_hugging_face_transformers
|
| 5 |
-
from models.product import Product
|
| 6 |
-
from models.ingredient import Ingredient
|
| 7 |
-
|
| 8 |
-
class TestAIAgentService(unittest.TestCase):
|
| 9 |
-
|
| 10 |
-
@patch('services.ai_agent.fetch_product_data_from_api')
|
| 11 |
-
def test_preprocess_data(self, mock_fetch_product_data_from_api):
|
| 12 |
-
mock_fetch_product_data_from_api.return_value = {
|
| 13 |
-
'product': {
|
| 14 |
-
'product_name_en': 'Test Product',
|
| 15 |
-
'generic_name_en': 'Test Generic',
|
| 16 |
-
'brands': 'Test Brand',
|
| 17 |
-
'ingredients': [
|
| 18 |
-
{
|
| 19 |
-
'text': 'Test Ingredient',
|
| 20 |
-
'percent': 50,
|
| 21 |
-
'vegan': 'yes',
|
| 22 |
-
'vegetarian': 'yes',
|
| 23 |
-
'ingredients': [
|
| 24 |
-
{
|
| 25 |
-
'text': 'Sub Ingredient',
|
| 26 |
-
'percent': 25,
|
| 27 |
-
'vegan': 'yes',
|
| 28 |
-
'vegetarian': 'yes'
|
| 29 |
-
}
|
| 30 |
-
]
|
| 31 |
-
}
|
| 32 |
-
],
|
| 33 |
-
'ingredients_text_en': 'Test Ingredients Text',
|
| 34 |
-
'ingredients_analysis': {},
|
| 35 |
-
'nutriscore': {},
|
| 36 |
-
'nutrient_levels': {},
|
| 37 |
-
'nutriments': {},
|
| 38 |
-
'data_quality_warnings_tags': []
|
| 39 |
-
}
|
| 40 |
-
}
|
| 41 |
-
result = preprocess_data('test_barcode')
|
| 42 |
-
self.assertEqual(result['product_name'], 'Test Product')
|
| 43 |
-
self.assertEqual(result['generic_name'], 'Test Generic')
|
| 44 |
-
self.assertEqual(result['brands'], 'Test Brand')
|
| 45 |
-
self.assertEqual(result['ingredients'][0]['text'], 'Test Ingredient')
|
| 46 |
-
self.assertEqual(result['ingredients'][0]['sub_ingredients'][0]['text'], 'Sub Ingredient')
|
| 47 |
-
|
| 48 |
-
def test_validate_data(self):
|
| 49 |
-
valid_data = {
|
| 50 |
-
'product_name': 'Test Product',
|
| 51 |
-
'generic_name': 'Test Generic',
|
| 52 |
-
'brands': 'Test Brand',
|
| 53 |
-
'ingredients': [],
|
| 54 |
-
'nutriscore': {},
|
| 55 |
-
'nutrient_levels': {},
|
| 56 |
-
'nutriments': {}
|
| 57 |
-
}
|
| 58 |
-
invalid_data = {
|
| 59 |
-
'product_name': '',
|
| 60 |
-
'generic_name': '',
|
| 61 |
-
'brands': '',
|
| 62 |
-
'ingredients': [],
|
| 63 |
-
'nutriscore': {},
|
| 64 |
-
'nutrient_levels': {},
|
| 65 |
-
'nutriments': {}
|
| 66 |
-
}
|
| 67 |
-
self.assertTrue(validate_data(valid_data))
|
| 68 |
-
self.assertFalse(validate_data(invalid_data))
|
| 69 |
-
|
| 70 |
-
def test_clean_data(self):
|
| 71 |
-
data = {
|
| 72 |
-
'ingredients': [
|
| 73 |
-
{
|
| 74 |
-
'text': 'Test Ingredient',
|
| 75 |
-
'percent': 'N/A',
|
| 76 |
-
'sub_ingredients': [
|
| 77 |
-
{
|
| 78 |
-
'text': 'Sub Ingredient',
|
| 79 |
-
'percent': 'N/A'
|
| 80 |
-
}
|
| 81 |
-
]
|
| 82 |
-
}
|
| 83 |
-
]
|
| 84 |
-
}
|
| 85 |
-
cleaned_data = clean_data(data)
|
| 86 |
-
self.assertEqual(cleaned_data['ingredients'][0]['percent'], 0)
|
| 87 |
-
self.assertEqual(cleaned_data['ingredients'][0]['sub_ingredients'][0]['percent'], 0)
|
| 88 |
-
|
| 89 |
-
def test_standardize_data(self):
|
| 90 |
-
data = {
|
| 91 |
-
'ingredients': [
|
| 92 |
-
{
|
| 93 |
-
'text': 'Test Ingredient',
|
| 94 |
-
'sub_ingredients': [
|
| 95 |
-
{
|
| 96 |
-
'text': 'Sub Ingredient'
|
| 97 |
-
}
|
| 98 |
-
]
|
| 99 |
-
}
|
| 100 |
-
]
|
| 101 |
-
}
|
| 102 |
-
standardized_data = standardize_data(data)
|
| 103 |
-
self.assertEqual(standardized_data['ingredients'][0]['text'], 'test ingredient')
|
| 104 |
-
self.assertEqual(standardized_data['ingredients'][0]['sub_ingredients'][0]['text'], 'sub ingredient')
|
| 105 |
-
|
| 106 |
-
@patch('services.ai_agent.get_ingredient_by_name')
|
| 107 |
-
@patch('services.ai_agent.fetch_product_data_from_api')
|
| 108 |
-
@patch('services.ai_agent.save_ingredient_data')
|
| 109 |
-
def test_enrich_data(self, mock_save_ingredient_data, mock_fetch_product_data_from_api, mock_get_ingredient_by_name):
|
| 110 |
-
db = MagicMock(spec=Session)
|
| 111 |
-
data = {
|
| 112 |
-
'ingredients': [
|
| 113 |
-
{
|
| 114 |
-
'text': 'Test Ingredient',
|
| 115 |
-
'sub_ingredients': []
|
| 116 |
-
}
|
| 117 |
-
]
|
| 118 |
-
}
|
| 119 |
-
mock_get_ingredient_by_name.return_value = None
|
| 120 |
-
mock_fetch_product_data_from_api.return_value = {'nutritional_info': 'Test Info'}
|
| 121 |
-
enriched_data = enrich_data(db, data)
|
| 122 |
-
self.assertEqual(enriched_data['ingredients'][0]['nutritional_info'], {'nutritional_info': 'Test Info'})
|
| 123 |
-
mock_save_ingredient_data.assert_called_once_with(db, 'Test Ingredient', {'nutritional_info': 'Test Info'})
|
| 124 |
-
|
| 125 |
-
@patch('services.ai_agent.preprocess_data')
|
| 126 |
-
@patch('services.ai_agent.validate_data')
|
| 127 |
-
@patch('services.ai_agent.clean_data')
|
| 128 |
-
@patch('services.ai_agent.standardize_data')
|
| 129 |
-
@patch('services.ai_agent.enrich_data')
|
| 130 |
-
@patch('services.ai_agent.save_json_file')
|
| 131 |
-
def test_process_data(self, mock_save_json_file, mock_enrich_data, mock_standardize_data, mock_clean_data, mock_validate_data, mock_preprocess_data):
|
| 132 |
-
db = MagicMock(spec=Session)
|
| 133 |
-
mock_preprocess_data.return_value = {'product_name': 'Test Product'}
|
| 134 |
-
mock_validate_data.return_value = True
|
| 135 |
-
mock_clean_data.return_value = {'product_name': 'Test Product'}
|
| 136 |
-
mock_standardize_data.return_value = {'product_name': 'Test Product'}
|
| 137 |
-
mock_enrich_data.return_value = {'product_name': 'Test Product'}
|
| 138 |
-
result = process_data(db, 'test_barcode')
|
| 139 |
-
self.assertEqual(result['product_name'], 'Test Product')
|
| 140 |
-
mock_save_json_file.assert_called_once_with('test_barcode', {'product_name': 'Test Product'})
|
| 141 |
-
|
| 142 |
-
@patch('services.ai_agent.pipeline')
|
| 143 |
-
def test_integrate_hugging_face_transformers(self, mock_pipeline):
|
| 144 |
-
mock_pipeline.return_value = lambda text: [{'sequence': 'Test sequence'}]
|
| 145 |
-
result = integrate_hugging_face_transformers('test_model', 'Test text')
|
| 146 |
-
self.assertEqual(result, 'Test sequence')
|
| 147 |
-
|
| 148 |
-
@patch('services.ai_agent.get_ingredient_by_name')
|
| 149 |
-
@patch('services.ai_agent.fetch_ingredient_data_from_api')
|
| 150 |
-
@patch('services.ai_agent.save_ingredient_data')
|
| 151 |
-
def test_process_data_saves_ingredient_details(self, mock_save_ingredient_data, mock_fetch_ingredient_data_from_api, mock_get_ingredient_by_name):
|
| 152 |
-
db = MagicMock(spec=Session)
|
| 153 |
-
mock_get_ingredient_by_name.return_value = None
|
| 154 |
-
mock_fetch_ingredient_data_from_api.return_value = {'nutritional_info': 'Test Info'}
|
| 155 |
-
data = {
|
| 156 |
-
'product_name': 'Test Product',
|
| 157 |
-
'generic_name': 'Test Generic',
|
| 158 |
-
'brands': 'Test Brand',
|
| 159 |
-
'ingredients': [
|
| 160 |
-
{
|
| 161 |
-
'text': 'Test Ingredient',
|
| 162 |
-
'sub_ingredients': []
|
| 163 |
-
}
|
| 164 |
-
],
|
| 165 |
-
'ingredients_text': 'Test Ingredients Text',
|
| 166 |
-
'ingredients_analysis': {},
|
| 167 |
-
'nutriscore': {},
|
| 168 |
-
'nutrient_levels': {},
|
| 169 |
-
'nutriments': {},
|
| 170 |
-
'data_quality_warnings': []
|
| 171 |
-
}
|
| 172 |
-
with patch('services.ai_agent.preprocess_data', return_value=data), \
|
| 173 |
-
patch('services.ai_agent.validate_data', return_value=True), \
|
| 174 |
-
patch('services.ai_agent.clean_data', return_value=data), \
|
| 175 |
-
patch('services.ai_agent.standardize_data', return_value=data), \
|
| 176 |
-
patch('services.ai_agent.enrich_data', return_value=data):
|
| 177 |
-
result = process_data(db, 'test_barcode')
|
| 178 |
-
self.assertEqual(result['product_name'], 'Test Product')
|
| 179 |
-
mock_save_ingredient_data.assert_called_once_with(db, 'Test Ingredient', {'nutritional_info': 'Test Info'})
|
| 180 |
-
|
| 181 |
-
@patch('services.ai_agent.preprocess_data')
|
| 182 |
-
@patch('services.ai_agent.validate_data')
|
| 183 |
-
@patch('services.ai_agent.clean_data')
|
| 184 |
-
@patch('services.ai_agent.standardize_data')
|
| 185 |
-
@patch('services.ai_agent.enrich_data')
|
| 186 |
-
@patch('services.ai_agent.save_json_file')
|
| 187 |
-
def test_process_data_saves_product_details(self, mock_save_json_file, mock_enrich_data, mock_standardize_data, mock_clean_data, mock_validate_data, mock_preprocess_data):
|
| 188 |
-
db = MagicMock(spec=Session)
|
| 189 |
-
mock_preprocess_data.return_value = {'product_name': 'Test Product'}
|
| 190 |
-
mock_validate_data.return_value = True
|
| 191 |
-
mock_clean_data.return_value = {'product_name': 'Test Product'}
|
| 192 |
-
mock_standardize_data.return_value = {'product_name': 'Test Product'}
|
| 193 |
-
mock_enrich_data.return_value = {'product_name': 'Test Product'}
|
| 194 |
-
result = process_data(db, 'test_barcode')
|
| 195 |
-
self.assertEqual(result['product_name'], 'Test Product')
|
| 196 |
-
mock_save_json_file.assert_called_once_with('test_barcode', {'product_name': 'Test Product'})
|
| 197 |
-
db.add.assert_called_once()
|
| 198 |
-
db.commit.assert_called_once()
|
| 199 |
-
db.refresh.assert_called_once()
|
| 200 |
-
|
| 201 |
-
if __name__ == '__main__':
|
| 202 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_analysis_agent_service.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
import unittest
|
| 2 |
-
from unittest.mock import MagicMock
|
| 3 |
-
from sqlalchemy.orm import Session
|
| 4 |
-
from services.analysis_agent import analyze_ingredients, provide_personalized_recommendations
|
| 5 |
-
from models.user_preferences import UserPreferences
|
| 6 |
-
from models.ingredient import Ingredient
|
| 7 |
-
|
| 8 |
-
class TestAnalysisAgentService(unittest.TestCase):
|
| 9 |
-
|
| 10 |
-
def setUp(self):
|
| 11 |
-
self.db = MagicMock(spec=Session)
|
| 12 |
-
self.user_id = 1
|
| 13 |
-
self.ingredients = [
|
| 14 |
-
{"text": "sugar"},
|
| 15 |
-
{"text": "salt"},
|
| 16 |
-
{"text": "flour"}
|
| 17 |
-
]
|
| 18 |
-
self.preferences = UserPreferences(
|
| 19 |
-
user_id=self.user_id,
|
| 20 |
-
dietary_restrictions="low sugar",
|
| 21 |
-
allergens="",
|
| 22 |
-
preferred_ingredients="",
|
| 23 |
-
disliked_ingredients=""
|
| 24 |
-
)
|
| 25 |
-
self.db.query.return_value.filter.return_value.first.return_value = self.preferences
|
| 26 |
-
|
| 27 |
-
def test_analyze_ingredients(self):
|
| 28 |
-
self.db.query.return_value.filter.return_value.first.return_value = self.preferences
|
| 29 |
-
self.db.query.return_value.all.return_value = [
|
| 30 |
-
Ingredient(name="sugar", nutritional_info={"calories": 100}),
|
| 31 |
-
Ingredient(name="salt", nutritional_info={"sodium": 200}),
|
| 32 |
-
Ingredient(name="flour", nutritional_info={"carbs": 300})
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
result = analyze_ingredients(self.db, self.ingredients, self.user_id)
|
| 36 |
-
|
| 37 |
-
self.assertIn("safe_ingredients", result)
|
| 38 |
-
self.assertIn("unsafe_ingredients", result)
|
| 39 |
-
self.assertIn("additional_facts", result)
|
| 40 |
-
self.assertEqual(len(result["safe_ingredients"]), 2)
|
| 41 |
-
self.assertEqual(len(result["unsafe_ingredients"]), 1)
|
| 42 |
-
|
| 43 |
-
def test_provide_personalized_recommendations(self):
|
| 44 |
-
self.db.query.return_value.all.return_value = [
|
| 45 |
-
Ingredient(name="sugar", nutritional_info={"calories": 100}),
|
| 46 |
-
Ingredient(name="salt", nutritional_info={"sodium": 200}),
|
| 47 |
-
Ingredient(name="flour", nutritional_info={"carbs": 300})
|
| 48 |
-
]
|
| 49 |
-
|
| 50 |
-
result = provide_personalized_recommendations(self.db, self.user_id)
|
| 51 |
-
|
| 52 |
-
self.assertIn("recommended_ingredients", result)
|
| 53 |
-
self.assertEqual(len(result["recommended_ingredients"]), 3)
|
| 54 |
-
|
| 55 |
-
def test_analyze_ingredients_with_new_fields(self):
|
| 56 |
-
self.db.query.return_value.filter.return_value.first.return_value = self.preferences
|
| 57 |
-
self.db.query.return_value.all.return_value = [
|
| 58 |
-
Ingredient(name="sugar", nutritional_info={"calories": 100}, description="Sweetener", origin="USA", allergens="", vegan=True, vegetarian=True),
|
| 59 |
-
Ingredient(name="salt", nutritional_info={"sodium": 200}, description="Seasoning", origin="India", allergens="", vegan=True, vegetarian=True),
|
| 60 |
-
Ingredient(name="flour", nutritional_info={"carbs": 300}, description="Baking ingredient", origin="Canada", allergens="", vegan=True, vegetarian=True)
|
| 61 |
-
]
|
| 62 |
-
|
| 63 |
-
result = analyze_ingredients(self.db, self.ingredients, self.user_id)
|
| 64 |
-
|
| 65 |
-
self.assertIn("safe_ingredients", result)
|
| 66 |
-
self.assertIn("unsafe_ingredients", result)
|
| 67 |
-
self.assertIn("additional_facts", result)
|
| 68 |
-
self.assertEqual(len(result["safe_ingredients"]), 2)
|
| 69 |
-
self.assertEqual(len(result["unsafe_ingredients"]), 1)
|
| 70 |
-
self.assertEqual(result["safe_ingredients"][0]["description"], "Sweetener")
|
| 71 |
-
self.assertEqual(result["safe_ingredients"][1]["origin"], "India")
|
| 72 |
-
|
| 73 |
-
def test_provide_personalized_recommendations_with_new_fields(self):
|
| 74 |
-
self.db.query.return_value.all.return_value = [
|
| 75 |
-
Ingredient(name="sugar", nutritional_info={"calories": 100}, description="Sweetener", origin="USA", allergens="", vegan=True, vegetarian=True),
|
| 76 |
-
Ingredient(name="salt", nutritional_info={"sodium": 200}, description="Seasoning", origin="India", allergens="", vegan=True, vegetarian=True),
|
| 77 |
-
Ingredient(name="flour", nutritional_info={"carbs": 300}, description="Baking ingredient", origin="Canada", allergens="", vegan=True, vegetarian=True)
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
result = provide_personalized_recommendations(self.db, self.user_id)
|
| 81 |
-
|
| 82 |
-
self.assertIn("recommended_ingredients", result)
|
| 83 |
-
self.assertEqual(len(result["recommended_ingredients"]), 3)
|
| 84 |
-
self.assertEqual(result["recommended_ingredients"][0]["description"], "Sweetener")
|
| 85 |
-
self.assertEqual(result["recommended_ingredients"][1]["origin"], "India")
|
| 86 |
-
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_auth_service.py
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
import pytest
|
| 2 |
-
from fastapi.testclient import TestClient
|
| 3 |
-
from sqlalchemy import create_engine
|
| 4 |
-
from sqlalchemy.orm import sessionmaker
|
| 5 |
-
from database import get_db
|
| 6 |
-
from models.base import Base
|
| 7 |
-
from main import app
|
| 8 |
-
from services.auth_service import create_user, get_password_hash
|
| 9 |
-
from models.user import User
|
| 10 |
-
|
| 11 |
-
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
|
| 12 |
-
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
| 13 |
-
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 14 |
-
|
| 15 |
-
Base.metadata.create_all(bind=engine)
|
| 16 |
-
|
| 17 |
-
def override_get_db():
|
| 18 |
-
try:
|
| 19 |
-
db = TestingSessionLocal()
|
| 20 |
-
yield db
|
| 21 |
-
finally:
|
| 22 |
-
db.close()
|
| 23 |
-
|
| 24 |
-
app.dependency_overrides[get_db] = override_get_db
|
| 25 |
-
|
| 26 |
-
client = TestClient(app)
|
| 27 |
-
|
| 28 |
-
@pytest.fixture(scope="module")
|
| 29 |
-
def test_db():
|
| 30 |
-
Base.metadata.create_all(bind=engine)
|
| 31 |
-
db = TestingSessionLocal()
|
| 32 |
-
yield db
|
| 33 |
-
db.close()
|
| 34 |
-
Base.metadata.drop_all(bind=engine)
|
| 35 |
-
|
| 36 |
-
def test_register_user(test_db):
|
| 37 |
-
response = client.post("/api/auth/register", json={"username": "testuser", "email": "testuser@example.com", "password": "testpassword"})
|
| 38 |
-
assert response.status_code == 200
|
| 39 |
-
assert "access_token" in response.json()
|
| 40 |
-
assert response.json()["token_type"] == "bearer"
|
| 41 |
-
|
| 42 |
-
def test_login_user(test_db):
|
| 43 |
-
create_user(test_db, "testuser", "testuser@example.com", get_password_hash("testpassword"))
|
| 44 |
-
response = client.post("/api/auth/login", data={"username": "testuser", "password": "testpassword"})
|
| 45 |
-
assert response.status_code == 200
|
| 46 |
-
assert "access_token" in response.json()
|
| 47 |
-
assert response.json()["token_type"] == "bearer"
|
| 48 |
-
|
| 49 |
-
def test_get_current_user(test_db):
|
| 50 |
-
create_user(test_db, "testuser", "testuser@example.com", get_password_hash("testpassword"))
|
| 51 |
-
response = client.post("/api/auth/login", data={"username": "testuser", "password": "testpassword"})
|
| 52 |
-
token = response.json()["access_token"]
|
| 53 |
-
response = client.get("/api/auth/users/me", headers={"Authorization": f"Bearer {token}"})
|
| 54 |
-
assert response.status_code == 200
|
| 55 |
-
assert response.json()["username"] == "testuser"
|
| 56 |
-
assert response.json()["email"] == "testuser@example.com"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_ingredients_service.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
import unittest
|
| 2 |
-
from unittest.mock import patch, MagicMock
|
| 3 |
-
from sqlalchemy.orm import Session
|
| 4 |
-
from services.ingredients import get_ingredient_data, save_ingredient_data, filter_ingredients_by_preferences
|
| 5 |
-
|
| 6 |
-
class TestIngredientService(unittest.TestCase):
|
| 7 |
-
|
| 8 |
-
@patch('services.ingredients.get_ingredient_by_name')
|
| 9 |
-
@patch('services.ingredients.fetch_ingredient_data_from_api')
|
| 10 |
-
@patch('services.ingredients.save_ingredient_data')
|
| 11 |
-
def test_get_ingredient_data(self, mock_save_ingredient_data, mock_fetch_ingredient_data_from_api, mock_get_ingredient_by_name):
|
| 12 |
-
db = MagicMock(spec=Session)
|
| 13 |
-
mock_get_ingredient_by_name.return_value = None
|
| 14 |
-
mock_fetch_ingredient_data_from_api.return_value = {
|
| 15 |
-
"nutritional_info": "test_info",
|
| 16 |
-
"description": "test_description",
|
| 17 |
-
"origin": "test_origin",
|
| 18 |
-
"allergens": "test_allergens",
|
| 19 |
-
"vegan": True,
|
| 20 |
-
"vegetarian": True
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
result = get_ingredient_data(db, "test_ingredient")
|
| 24 |
-
|
| 25 |
-
mock_get_ingredient_by_name.assert_called_once_with(db, "test_ingredient")
|
| 26 |
-
mock_fetch_ingredient_data_from_api.assert_called_once_with("test_ingredient")
|
| 27 |
-
mock_save_ingredient_data.assert_called_once_with(db, "test_ingredient", {
|
| 28 |
-
"nutritional_info": "test_info",
|
| 29 |
-
"description": "test_description",
|
| 30 |
-
"origin": "test_origin",
|
| 31 |
-
"allergens": "test_allergens",
|
| 32 |
-
"vegan": True,
|
| 33 |
-
"vegetarian": True
|
| 34 |
-
})
|
| 35 |
-
self.assertEqual(result, {
|
| 36 |
-
"nutritional_info": "test_info",
|
| 37 |
-
"description": "test_description",
|
| 38 |
-
"origin": "test_origin",
|
| 39 |
-
"allergens": "test_allergens",
|
| 40 |
-
"vegan": True,
|
| 41 |
-
"vegetarian": True
|
| 42 |
-
})
|
| 43 |
-
|
| 44 |
-
def test_save_ingredient_data(self):
|
| 45 |
-
db = MagicMock(spec=Session)
|
| 46 |
-
name = "test_ingredient"
|
| 47 |
-
data = {
|
| 48 |
-
"nutritional_info": "test_info",
|
| 49 |
-
"description": "test_description",
|
| 50 |
-
"origin": "test_origin",
|
| 51 |
-
"allergens": "test_allergens",
|
| 52 |
-
"vegan": True,
|
| 53 |
-
"vegetarian": True
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
save_ingredient_data(db, name, data)
|
| 57 |
-
|
| 58 |
-
db.add.assert_called_once()
|
| 59 |
-
db.commit.assert_called_once()
|
| 60 |
-
db.refresh.assert_called_once()
|
| 61 |
-
|
| 62 |
-
def test_filter_ingredients_by_preferences(self):
|
| 63 |
-
ingredients = [
|
| 64 |
-
{"text": "ingredient1", "sugar": 10, "fat": 5, "allergens": ["allergen1"]},
|
| 65 |
-
{"text": "ingredient2", "sugar": 3, "fat": 2, "allergens": []},
|
| 66 |
-
{"text": "ingredient3", "sugar": 6, "fat": 1, "allergens": ["allergen2"]}
|
| 67 |
-
]
|
| 68 |
-
preferences = {
|
| 69 |
-
"low_sugar": True,
|
| 70 |
-
"low_fat": True,
|
| 71 |
-
"allergens": ["allergen1"]
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
result = filter_ingredients_by_preferences(ingredients, preferences)
|
| 75 |
-
|
| 76 |
-
self.assertEqual(len(result), 1)
|
| 77 |
-
self.assertEqual(result[0]["text"], "ingredient2")
|
| 78 |
-
|
| 79 |
-
if __name__ == '__main__':
|
| 80 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_scan_history_service.py
DELETED
|
@@ -1,43 +0,0 @@
|
|
| 1 |
-
import unittest
|
| 2 |
-
from unittest.mock import MagicMock
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from sqlalchemy.orm import Session
|
| 5 |
-
from models.scan_history import ScanHistory
|
| 6 |
-
from services.scan_history import record_scan, get_scan_history
|
| 7 |
-
|
| 8 |
-
class TestScanHistoryService(unittest.TestCase):
|
| 9 |
-
|
| 10 |
-
def setUp(self):
|
| 11 |
-
self.db = MagicMock(spec=Session)
|
| 12 |
-
self.user_id = 1
|
| 13 |
-
self.product_id = 123
|
| 14 |
-
self.scan_date = datetime.utcnow()
|
| 15 |
-
|
| 16 |
-
def test_record_scan(self):
|
| 17 |
-
scan_entry = ScanHistory(user_id=self.user_id, product_id=self.product_id, scan_date=self.scan_date)
|
| 18 |
-
self.db.add.return_value = None
|
| 19 |
-
self.db.commit.return_value = None
|
| 20 |
-
self.db.refresh.return_value = None
|
| 21 |
-
|
| 22 |
-
result = record_scan(self.db, self.user_id, self.product_id)
|
| 23 |
-
|
| 24 |
-
self.db.add.assert_called_once_with(scan_entry)
|
| 25 |
-
self.db.commit.assert_called_once()
|
| 26 |
-
self.db.refresh.assert_called_once_with(scan_entry)
|
| 27 |
-
self.assertEqual(result.user_id, self.user_id)
|
| 28 |
-
self.assertEqual(result.product_id, self.product_id)
|
| 29 |
-
self.assertEqual(result.scan_date, self.scan_date)
|
| 30 |
-
|
| 31 |
-
def test_get_scan_history(self):
|
| 32 |
-
scan_entry = ScanHistory(user_id=self.user_id, product_id=self.product_id, scan_date=self.scan_date)
|
| 33 |
-
self.db.query.return_value.filter.return_value.all.return_value = [scan_entry]
|
| 34 |
-
|
| 35 |
-
result = get_scan_history(self.db, self.user_id)
|
| 36 |
-
|
| 37 |
-
self.db.query.assert_called_once_with(ScanHistory)
|
| 38 |
-
self.db.query.return_value.filter.assert_called_once_with(ScanHistory.user_id == self.user_id)
|
| 39 |
-
self.db.query.return_value.filter.return_value.all.assert_called_once()
|
| 40 |
-
self.assertEqual(result, [scan_entry])
|
| 41 |
-
|
| 42 |
-
if __name__ == '__main__':
|
| 43 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|