import pytest from faker import Faker from email_campaigns.automated.handlers import ( GetAutomatedEmailSalesLocationsAnalyticsHandler, ) from email_campaigns.automated.models import AutomatedEmail from email_campaigns.emails.exceptions import EmailNotFoundError from email_campaigns.emails.handlers import GetEmailSalesLocationsAnalyticsRequest from email_campaigns.emails.models import ShopifyEmailOrdersDbt from tests.unit.types import CreateModel class TestGetAutomatedEmailSalesLocationsAnalyticsHandler: @pytest.mark.db def test_get_automated_email_sales_locations_analytics( self, handler: GetAutomatedEmailSalesLocationsAnalyticsHandler, 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="Shop One", total_orders=5, items_sold=10, total_sales_usd=220.0, total_discounts_usd=10.0, total_taxes_usd=8.0, total_delivery_usd=5.0, total_net_sales_usd=200.0, ) create_model( ShopifyEmailOrdersDbt, email_id=automated_email.id, shop_id="shop2", currency="USD", country_code="CA", shop_name="Shop Two", total_orders=3, items_sold=6, total_sales_usd=115.0, total_discounts_usd=5.0, total_taxes_usd=4.0, total_delivery_usd=3.0, total_net_sales_usd=100.0, ) response = handler.handle( GetEmailSalesLocationsAnalyticsRequest( email_id=automated_email.id, identity_id=identity_id, ), ) assert response.data == [ { "country_code": "US", "items_sold": 10, "net_sales_usd": 200.0, "sales_rate": pytest.approx(200 / 300), }, { "country_code": "CA", "items_sold": 6, "net_sales_usd": 100.0, "sales_rate": pytest.approx(100 / 300), }, ] @pytest.mark.db def test_get_automated_email_sales_locations_analytics_no_data( self, handler: GetAutomatedEmailSalesLocationsAnalyticsHandler, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail, name="Test Automated Email") response = handler.handle( GetEmailSalesLocationsAnalyticsRequest( email_id=automated_email.id, identity_id=identity_id, ), ) assert response.data == [] @pytest.mark.db def test_get_automated_email_sales_locations_analytics_wrong_email_id( self, handler: GetAutomatedEmailSalesLocationsAnalyticsHandler, identity_id: str, faker: Faker, ) -> None: email_id = faker.uuid4() with pytest.raises(EmailNotFoundError): handler.handle( GetEmailSalesLocationsAnalyticsRequest( email_id=email_id, identity_id=identity_id, ), )