def test_get_next_payment_date(generated_subscription, start_date, frequency, expected): """Check that get next payment date works correctly""" test_subscription = Subscription( owner=generated_subscription["owner"], name=generated_subscription["name"], frequency=frequency, start_date=start_date, price=generated_subscription["price"], currency=generated_subscription["currency"], comment=generated_subscription["comment"], ) assert test_subscription.get_next_payment_date() == expected
def subscription() -> Subscription: """Return Subscription class instance""" return Subscription( owner="Lena", name="Amazon Prime", frequency="monthly", start_date=date(2020, 6, 18), price=8.99, currency="GBP", comment="Prime membership for faster delivery", )
def get_subscription() -> Subscription: """Returns Subscription object""" return Subscription( owner="Mary", name="Sky Store", frequency="monthly", start_date=date(2019, 4, 13), price=12.97, currency="CNY", comment="Generation date: 23/06/2020, 10:46:41", )
def create_subscription(**params) -> Subscription: """ Take subscription, validate its every field and return Subscription object Args: **params: subscription to create The format for a parameter is: owner (str): name of the person, who owns this subscription. name (str): name of the subscription, e.g. 'Spotify music' frequency (str): payment period for subscription from the list: daily, weekly, monthly, yearly start_date (date): date when subscription started price (str): amount of money needed to pay for subscription per one payment period currency (str): currency of payment comment (str): not-mandatory comment for subscription Returns: class <Subscription>: subscription object in case of valid input Raises: MissingFieldsException: If some of mandatory fields is missing InvalidValueException: If some of taken fields have invalid values WrongTypeException: If some of taken fields have wrong types """ # Check that there is no missed fields in the subscription expected_fields = { field.name: field.type for field in fields(Subscription) } if params.keys() != expected_fields.keys(): raise MissingFieldsException(MISSING_FIELDS_MSG) # Check that types of all fields are correct for key, value in params.items(): if type(value) != expected_fields[key]: raise WrongTypeException( WRONG_TYPE_MSG.format( expected=expected_fields[key], recieved_type=type(value), field=value, )) # Check that given subscription currency is supported if params["currency"] not in CURRENCIES: raise InvalidValueException( UNEXPECTED_CURRENCY_MSG.format(currency=params["currency"])) # Check that given subscription frequency is supported if params["frequency"] not in FREQUENCIES: raise InvalidValueException( UNEXPECTED_FREQUENCY_MSG.format(frequency=params["frequency"])) # Check that start_date is less than today if params["start_date"] > date.today(): raise InvalidValueException(FUTURE_START_DATE_MSG) return Subscription(**params)
def get_subscription_by_name(self, subscription_name: str) -> Subscription: """ Return subscription by name Returns: <class Subscription>: found subscription like Subscription object Raises: SubsNotFoundException: when subscription with this id doesn't exist in database """ try: utils.validate_str_field(field=subscription_name) except SubscriptionException: raise received_subscription: dict = self.dbhelper.get_subscription( subscription_name) return Subscription(**received_subscription)
def get_subscriptions_list(self, owner: str = None) -> List[Subscription]: """ Return list of subscriptions Args: owner (str): not mandatory, if not None returns all subscriptions of specified owner Returns: List[Subscription]: list of all subscriptions or all user's subscription if owner is specified """ try: utils.validate_str_field(field=owner, none_allowed=True) except SubscriptionException: raise subscription_list = self.dbhelper.get_all_subscriptions(owner) # Convert start_date type from datetime to date, convert every dict to Subscription return [ Subscription(**subscription) for subscription in subscription_list ]
def test_get_subscription_by_name(controller: Controller): """Check correct format of found subscription: _id from database is not in the dict type(result) == Subscription type(start_date) == date, not datetime other fields values and types have no changes """ actual_result = controller.get_subscription_by_name("Sky Store") expected_result = Subscription( owner="Mary", name="Sky Store", frequency="monthly", start_date=date(2019, 4, 13), price=12.97, currency="CNY", comment="Generation date: 23/06/2020, 10:46:41", ) assert actual_result == expected_result
"currency": "EUR", "frequency": "weekly", "name": "Spotify", "owner": "Robin", "price": 30.23, "start_date": date(2019, 10, 19), }, ] subscriptions_obj_list = [ Subscription( owner="Kevin", name="Youtube Music", frequency="daily", start_date=date(2019, 5, 11), price=8.3, currency="USD", comment="Generation date: 23/06/2020, 10:46:11", ), Subscription( owner="Mary", name="Sky Store", frequency="monthly", start_date=date(2019, 4, 13), price=12.97, currency="CNY", comment="Generation date: 23/06/2020, 10:46:41", ), Subscription( owner="Robin",