import pytest from faker import Faker from email_campaigns.automated.handlers import ( GetAutomatedEmailSalesShopsAnalyticsHandler, ) from email_campaigns.automated.models import AutomatedEmail from email_campaigns.emails.exceptions import EmailNotFoundError from email_campaigns.emails.handlers import GetEmailSalesShopsAnalyticsRequest from email_campaigns.emails.models import ShopifyEmailOrdersDbt from tests.unit.types import CreateModel class TestGetAutomatedEmailSalesShopsAnalyticsHandler: @pytest.mark.db def test_get_automated_email_sales_shops_analytics( self, handler: GetAutomatedEmailSalesShopsAnalyticsHandler, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail, name="Test Automated Email") create_model( ShopifyEmailOrdersDbt, email_id=automated_email.id, shop_id="shop1", currency="USD", country_code="US", shop_name="Big Shop", total_orders=10, items_sold=20, total_sales_usd=320.0, total_discounts_usd=10.0, total_taxes_usd=8.0, total_delivery_usd=5.0, total_net_sales_usd=300.0, ) create_model( ShopifyEmailOrdersDbt, email_id=automated_email.id, shop_id="shop2", currency="USD", country_code="CA", shop_name="Small Shop", total_orders=2, items_sold=4, total_sales_usd=60.0, total_discounts_usd=5.0, total_taxes_usd=3.0, total_delivery_usd=2.0, total_net_sales_usd=50.0, ) response = handler.handle( GetEmailSalesShopsAnalyticsRequest( email_id=automated_email.id, identity_id=identity_id, ), ) assert response.data == [ {"shop_name": "Big Shop", "items_sold": 20, "net_sales_usd": 300.0}, {"shop_name": "Small Shop", "items_sold": 4, "net_sales_usd": 50.0}, ] @pytest.mark.db def test_get_automated_email_sales_shops_analytics_no_data( self, handler: GetAutomatedEmailSalesShopsAnalyticsHandler, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail, name="Test Automated Email") response = handler.handle( GetEmailSalesShopsAnalyticsRequest( email_id=automated_email.id, identity_id=identity_id, ), ) assert response.data == [] @pytest.mark.db def test_get_automated_email_sales_shops_analytics_wrong_email_id( self, handler: GetAutomatedEmailSalesShopsAnalyticsHandler, identity_id: str, faker: Faker, ) -> None: email_id = faker.uuid4() with pytest.raises(EmailNotFoundError): handler.handle( GetEmailSalesShopsAnalyticsRequest( email_id=email_id, identity_id=identity_id, ), )