Spaces:
Running
Running
File size: 7,403 Bytes
c8477e1 3fba179 c8477e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
"""
Unit tests for FoodWiseNotionClient.
Tests cover all CRUD operations with mocked Notion API responses.
"""
import pytest
from unittest.mock import Mock, patch
from datetime import date
from pydantic import ValidationError
from src.foodwise.notion.notion_client import FoodWiseNotionClient
@pytest.fixture
def mock_notion_client():
"""Create a mock Notion client for testing."""
return Mock()
@pytest.fixture
def foodwise_client(mock_notion_client, monkeypatch):
"""Create FoodWiseNotionClient with mocked Notion client."""
# Mock environment variables
monkeypatch.setenv("NOTION_SECRET", "test-secret")
monkeypatch.setenv("NOTION_INVENTORY_DB_ID", "test-db-id")
with patch('src.foodwise.notion.notion_client.Client', return_value=mock_notion_client):
client = FoodWiseNotionClient()
client.client = mock_notion_client
return client
@pytest.fixture
def sample_item_data():
"""Sample item data for testing."""
return {
"name": "Test Apple",
"category": "Fruits",
"storage_type": "Pantry",
"quantity": 5.0,
"unit": "pieces",
"best_by_date": "2024-12-31",
"purchase_date": "2024-12-01"
}
@pytest.fixture
def mock_notion_page():
"""Mock Notion page response."""
return {
'id': 'test-page-id-123',
'url': 'https://notion.so/test-page',
'properties': {
'Name': {
'title': [{'text': {'content': 'Test Apple'}}]
},
'Category': {
'select': {'name': 'Fruits'}
},
'Storage Type': {
'select': {'name': 'Pantry'}
},
'Quantity': {
'number': 5.0
},
'Unit': {
'select': {'name': 'pieces'}
}
}
}
class TestFoodWiseNotionClient:
"""Test cases for FoodWiseNotionClient."""
def test_add_inventory_item_success(self, foodwise_client, sample_item_data, mock_notion_page):
"""Test successful item addition."""
foodwise_client.client.pages.create.return_value = mock_notion_page
result = foodwise_client.add_inventory_item(sample_item_data)
assert result['id'] == 'test-page-id-123'
assert result['name'] == 'Test Apple'
assert result['category'] == 'Fruits'
foodwise_client.client.pages.create.assert_called_once()
def test_add_inventory_item_failure(self, foodwise_client, sample_item_data):
"""Test item addition failure handling."""
foodwise_client.client.pages.create.side_effect = Exception("Notion API Error")
with pytest.raises(Exception, match="Failed to add item"):
foodwise_client.add_inventory_item(sample_item_data)
def test_query_inventory_success(self, foodwise_client, mock_notion_page):
"""Test successful inventory query."""
foodwise_client.client.databases.query.return_value = {
'results': [mock_notion_page]
}
result = foodwise_client.query_inventory()
assert len(result) == 1
assert result[0]['name'] == 'Test Apple'
foodwise_client.client.databases.query.assert_called_once()
def test_update_inventory_item_success(self, foodwise_client, mock_notion_page):
"""Test successful item update."""
foodwise_client.client.pages.update.return_value = mock_notion_page
update_data = {
"quantity": 3.0,
"best_by_date": date(2024, 12, 20)
}
result = foodwise_client.update_inventory_item("test-page-id", update_data)
assert result['id'] == 'test-page-id-123'
foodwise_client.client.pages.update.assert_called_once()
def test_update_inventory_item_validation_error(self, foodwise_client):
"""Test update with invalid data."""
invalid_data = {
"quantity": "not-a-number" # Invalid type
}
with pytest.raises(ValueError, match="Invalid update data"):
foodwise_client.update_inventory_item("test-page-id", invalid_data)
def test_update_inventory_item_empty_data(self, foodwise_client):
"""Test update with no valid fields."""
with pytest.raises(Exception, match="No valid fields provided"):
foodwise_client.update_inventory_item("test-page-id", {})
def test_remove_inventory_item_success(self, foodwise_client):
"""Test successful item removal."""
foodwise_client.client.pages.update.return_value = {"archived": True}
result = foodwise_client.remove_inventory_item("test-page-id")
assert result['success'] is True
assert "archived successfully" in result['message']
foodwise_client.client.pages.update.assert_called_once_with(
page_id="test-page-id",
archived=True
)
def test_remove_inventory_item_failure(self, foodwise_client):
"""Test item removal failure."""
foodwise_client.client.pages.update.side_effect = Exception("Notion API Error")
with pytest.raises(Exception, match="Failed to remove item"):
foodwise_client.remove_inventory_item("test-page-id")
def test_search_items_success(self, foodwise_client, mock_notion_page):
"""Test successful item search."""
foodwise_client.client.databases.query.return_value = {
'results': [mock_notion_page]
}
result = foodwise_client.search_items("apple")
assert len(result) == 1
assert result[0]['name'] == 'Test Apple'
def test_get_expiring_items_success(self, foodwise_client, mock_notion_page):
"""Test successful expiring items query."""
foodwise_client.client.databases.query.return_value = {
'results': [mock_notion_page]
}
result = foodwise_client.get_expiring_items(7)
assert len(result) == 1
foodwise_client.client.databases.query.assert_called_once()
class TestPartialFoodInventoryItem:
"""Test the PartialFoodInventoryItem validation."""
def test_valid_partial_data(self):
"""Test validation with valid partial data."""
from src.foodwise.database.db_models import PartialFoodInventoryItem
data = {
"name": "Test Item",
"quantity": 2.5
}
validated = PartialFoodInventoryItem.model_validate(data)
assert validated.name == "Test Item"
assert validated.quantity == 2.5
assert validated.category is None
def test_invalid_partial_data(self):
"""Test validation with invalid data types."""
from src.foodwise.database.db_models import PartialFoodInventoryItem
data = {
"quantity": "not-a-number"
}
with pytest.raises(ValidationError):
PartialFoodInventoryItem.model_validate(data)
def test_empty_partial_data(self):
"""Test validation with empty data."""
from src.foodwise.database.db_models import PartialFoodInventoryItem
validated = PartialFoodInventoryItem.model_validate({})
assert all(getattr(validated, field) is None for field in PartialFoodInventoryItem.model_fields) |