"""Factories for testing models.""" import datetime from decimal import Decimal import factory from abacus_common_logic.test_utils.factories import ( BaseModelFactory, BaseModelFactoryWithTimestamps, ) from factory import fuzzy from abacus_contract.tests.utils.factories import ContractFactory from royalties.constants import constants from royalties.models.accounting_period import AccountingPeriod from royalties.models.accounting_period_report import AccountingPeriodReport from royalties.models.accounting_run import AccountingRun from royalties.models.earnings_transfer import EarningsTransfer from royalties.models.exchange_rate import ExchangeRate from royalties.models.project_transfer_term import ProjectTransferTerm from royalties.models.project_transfer_term_condition import ( ProjectTransferTermCondition, ) from royalties.models.run_controller import RunController from royalties.models.run_controller_contract import RunControllerContract from royalties.models.sales_file import SalesFile from royalties.models.statement_period import StatementPeriod from royalties.models.statement_period_adjustment_batch_criteria import ( StatementPeriodAdjustmentBatchCriteria, ) from royalties.models.statement_period_adjustment_file import ( StatementPeriodAdjustmentFile, ) from royalties.models.statement_period_payment_entity import ( StatementPeriodPaymentEntity, ) class StatementPeriodFactory(BaseModelFactory): """Factory to create Statement period models for testing.""" class Meta: """Meta definition for the factory.""" model = StatementPeriod statement_period_name = 'January 2020' statement_period_status = 'closed' closed_date = datetime.date(2020, 2, 5) closed_by = 'admin' class AccountingPeriodFactory(BaseModelFactoryWithTimestamps): """Factory to create accounting period models for testing.""" class Meta: """Meta definition for the factory.""" model = AccountingPeriod accounting_period_name = factory.Sequence(lambda n: f'Month 202{n}') accounting_period_status = constants.ACCOUNTING_PERIOD_STATUSES.OPEN contract_type = constants.CONTRACT_TYPES.DISTRIBUTION is_visible = True statement_period = factory.SubFactory(StatementPeriodFactory) class AccountingPeriodReportFactory(BaseModelFactoryWithTimestamps): """Factory to create Accounting Period Report for testing.""" class Meta: """Meta definition for the factory.""" model = AccountingPeriodReport accounting_period = factory.SubFactory(AccountingPeriodFactory) report_export_url = 's3://main/report_file/' report_type = constants.REPORT_TYPE.VAT_EXEMPT class EarningsTransferFactory(BaseModelFactory): """Factory to create earnings transfer models for testing.""" class Meta: """Meta definition for the factory.""" model = EarningsTransfer from_contract = factory.SubFactory(ContractFactory) to_contract = factory.SubFactory(ContractFactory) transfer_type = constants.EARNINGS_TRANSFER_TYPES.TRANSFER rate_type = constants.EARNINGS_TRANSFER_RATE_TYPES.PERCENT transfer_amount = 12.123000000000 input = constants.EARNINGS_TRANSFER_INPUT.NET_REVENUE negative = 0 use_static_balance = 0 comment = 'Test Earnings Transfer' class RunControllerFactory(BaseModelFactoryWithTimestamps): """Factory to create run controller models for testing.""" class Meta: """Meta definition for the factory.""" model = RunController run_controller_name = factory.Sequence(lambda n: f'Run Controller {n}') contract_type = constants.CONTRACT_TYPES.DISTRIBUTION class AccountingRunFactory(BaseModelFactoryWithTimestamps): """Factory to create Accounting Run models for testing.""" class Meta: """Meta definition for the factory.""" model = AccountingRun accounting_period = factory.SubFactory(AccountingPeriodFactory) run_controller = factory.SubFactory(RunControllerFactory) run_status = constants.ACCOUNTING_RUN_STATUSES.NO_ACTION_TAKEN start_date = datetime.datetime.today() summary_export_url = None class SalesFileFactory(BaseModelFactoryWithTimestamps): """Factory to create sales file models for testing.""" class Meta: """Meta definition for the factory.""" model = SalesFile accounting_period = factory.SubFactory(AccountingPeriodFactory) amount_usd = fuzzy.FuzzyDecimal(0) file_name = factory.Sequence(lambda n: f'March Sales {n}.txt') main_url = factory.Sequence(lambda n: f's3://main/sales_file_{n}/') row_count = factory.Sequence(lambda n: n + 1) class ExchangeRateFactory(BaseModelFactoryWithTimestamps): """Factory to create Exchange Rate models for testing.""" class Meta: """Meta definition for the factory.""" model = ExchangeRate statement_period = factory.SubFactory(StatementPeriodFactory) from_currency_code = factory.Sequence(lambda n: f'CC{n}') to_currency_code = factory.Sequence(lambda n: f'CC{n + 1}') rate = factory.Sequence(lambda n: Decimal('0.%06d' % n)) class RunControllerContractFactory(BaseModelFactory): """Factory to create Run controller contract models for testing.""" class Meta: """Meta definition for the factory.""" model = RunControllerContract contract = factory.SubFactory(ContractFactory) run_controller = factory.SubFactory(RunControllerFactory) class StatementPeriodAdjustmentFileFactory(BaseModelFactory): """Factory to create Statement period adjustment file models for testing.""" class Meta: """Meta definition for the factory.""" model = StatementPeriodAdjustmentFile file_name = 'test_file_name.xlsx' batch_type = constants.STATEMENT_PERIOD_ADJUSTMENT_FILE_BATCH_TYPES.UPLOAD statement_period = factory.SubFactory(StatementPeriodFactory) statement_period_adjustment_file_id = factory.Sequence(lambda n: n + 1) valid_file_location = None invalid_file_location = None class StatementPeriodAdjustmentBatchCriteriaFactory(BaseModelFactory): """Factory to create Statement period adjustment batch models for testing.""" class Meta: """Meta definition for the factory.""" model = StatementPeriodAdjustmentBatchCriteria batch_criteria = { 'payment_schedules': [ constants.PAYMENT_SCHEDULES.SCHEDULE_30_DAYS_MONTH, constants.PAYMENT_SCHEDULES.SCHEDULE_90_DAYS_HALFYEAR, ], 'reference_payment_entities': [1, 2, 3, 4], } statement_period_adjustment_file = factory.SubFactory( StatementPeriodAdjustmentFileFactory ) class StatementPeriodPaymentEntityFactory(BaseModelFactory): """Factory to create Statement period payment entity file models for testing.""" class Meta: """Meta definition for the factory.""" model = StatementPeriodPaymentEntity statement_period_payment_entity_id = factory.Sequence(lambda n: n + 1) statement_period = factory.SubFactory(StatementPeriodFactory) reference_payment_entity_id = 1 is_visible_to_customer = False class ProjectTransferTermFactory(BaseModelFactory): """Factory to create ProjectTransferTerm models for testing. Materializes a Contract via SubFactory so the FK to `contract.contract_id` is satisfied when the row is committed. """ class Meta: """Meta definition for the factory.""" model = ProjectTransferTerm project_transfer_term_id = factory.Sequence(lambda n: n + 1) job_id = 42 contract_id = factory.LazyFunction(lambda: ContractFactory.create().contract_id) name = None term_type = 'product' attachments = None attachment_relations = None created_by = 'test' created_at = factory.LazyFunction(datetime.datetime.utcnow) class ProjectTransferTermConditionFactory(BaseModelFactory): """Factory to create ProjectTransferTermCondition models for testing. Callers should pass `project_transfer_term_id` explicitly, set to the id of a term created with `ProjectTransferTermFactory`. The default value here is just a placeholder to satisfy `NOT NULL` and will fail the FK constraint if used directly. """ class Meta: """Meta definition for the factory.""" model = ProjectTransferTermCondition project_transfer_term_condition_id = factory.Sequence(lambda n: n + 1) project_transfer_term_id = 1 name = None priority = 1 term_rate = Decimal('80.00') commission = Decimal('0.00') conditions = factory.LazyFunction(dict) created_by = 'test' created_at = factory.LazyFunction(datetime.datetime.utcnow)