Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversions #356

Merged
merged 10 commits into from
Mar 8, 2024
105 changes: 87 additions & 18 deletions mangopay/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,45 +283,114 @@ class Meta:
verbose_name = 'conversion_rate'
verbose_name_plural = 'conversion_rates'
url = {
'GET_CONVERSION_RATE': '/conversion/rate/%(debited_currency)s/%(credited_currency)s'
'GET_CONVERSION_RATE': '/conversions/rate/%(debited_currency)s/%(credited_currency)s'
}


class ConversionQuote(BaseModel):
expiration_date = DateTimeField(api_name='ExpirationDate')
status = CharField(api_name='Status', choices=constants.STATUS_CHOICES, default=None)
duration = IntegerField(api_name='Duration')
debited_funds = MoneyField(api_name='DebitedFunds', required=True)
credited_funds = MoneyField(api_name='CreditedFunds', required=True)
conversion_rate = ConversionRateField(api_name='ConversionRateResponse')

def create_conversion_quote(self, **kwargs):
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
insert.identifier = 'CREATE_CONVERSION_QUOTE'
return insert.execute()

@staticmethod
def get_conversion_quote(id, *args, **kwargs):
kwargs['id'] = id
select = SelectQuery(ConversionQuote, *args, **kwargs)
select.identifier = 'GET_CONVERSION_QUOTE'
return select.all(*args, **kwargs)

class Meta:
verbose_name = 'conversion_quote'
verbose_name_plural = 'conversions'
url = {
'CREATE_CONVERSION_QUOTE': '/conversions/quote',
'GET_CONVERSION_QUOTE': '/conversions/quote/%(id)s'
}


@python_2_unicode_compatible
class InstantConversion(BaseModel):
class Conversion(BaseModel):
quoteId = ForeignKeyField(ConversionQuote, api_name='QuoteId', default=None)
type = CharField(api_name='Type', choices=constants.TRANSACTION_TYPE_CHOICES, default=None)
nature = CharField(api_name='Nature', choices=constants.NATURE_CHOICES, default=None)
status = CharField(api_name='Status', choices=constants.STATUS_CHOICES, default=None)
author = ForeignKeyField(User, api_name='AuthorId', required=True)
debited_wallet = ForeignKeyField(Wallet, api_name='DebitedWalletId', required=True)
credited_wallet = ForeignKeyField(Wallet, api_name='CreditedWalletId', required=True)
debited_funds = MoneyField(api_name='DebitedFunds', required=True)
credited_funds = MoneyField(api_name='CreditedFunds', required=True)
conversion_rate = ConversionRateField(api_name='ConversionRate')
type = CharField(api_name='Type', choices=constants.TRANSACTION_TYPE_CHOICES, default=None)
nature = CharField(api_name='Nature', choices=constants.NATURE_CHOICES, default=None)
creation_date = DateTimeField(api_name='CreationDate')
fees = MoneyField(api_name="Fees", required=True)
result_code = CharField(api_name='ResultCode')
result_message = CharField(api_name='ResultMessage')
status = CharField(api_name='Status', choices=constants.STATUS_CHOICES, default=None)
execution_date = DateTimeField(api_name='ExecutionDate')

def create_instant_conversion(self, **kwargs):
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
insert.identifier = 'CREATE_INSTANT_CONVERSION'
return insert.execute()
conversion_rate = ConversionRateField(api_name='ConversionRate')
creation_date = DateTimeField(api_name='CreationDate')

@staticmethod
def get_instant_conversion(id, *args, **kwargs):
def get_conversion(id, *args, **kwargs):
kwargs['id'] = id
select = SelectQuery(InstantConversion, *args, **kwargs)
select.identifier = 'GET_INSTANT_CONVERSION'
select = SelectQuery(Conversion, *args, **kwargs)
select.identifier = 'GET_CONVERSION'
return select.all(*args, **kwargs)

class Meta:
verbose_name = 'conversion'
verbose_name_plural = 'conversions'
url = {
'GET_CONVERSION': '/conversions/%(id)s'
}


class QuotedConversion(BaseModel):
quote = ForeignKeyField(ConversionQuote, api_name='QuoteId')
author = ForeignKeyField(User, api_name="AuthorId")
debited_wallet = ForeignKeyField(Wallet, api_name="DebitedWalletId")
credited_wallet = ForeignKeyField(Wallet, api_name="CreditedWalletId")
tag = CharField(api_name="Tag")

def save(self, **kwargs):
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
insert.identifier = InsertQuery.identifier
return insert.execute(model_klass=Conversion)

class Meta:
verbose_name = 'quoted_conversion'
verbose_name_plural = 'quoted_conversions'
url = {
InsertQuery.identifier: '/conversions/quoted-conversion'
}


class InstantConversion(BaseModel):
author = ForeignKeyField(User, api_name="AuthorId")
debited_wallet = ForeignKeyField(Wallet, api_name="DebitedWalletId")
credited_wallet = ForeignKeyField(Wallet, api_name="CreditedWalletId")
debited_funds = MoneyField(api_name="DebitedFunds")
credited_funds = MoneyField(api_name="CreditedFunds")
fees = MoneyField(api_name="Fees")
tag = CharField(api_name="Tag")

def save(self, **kwargs):
insert = InsertQuery(self, **kwargs)
insert.insert_query = self.get_field_dict()
insert.identifier = 'INSTANT_CONVERSION'
return insert.execute(model_klass=Conversion)

class Meta:
verbose_name = 'instant_conversion'
verbose_name_plural = 'instant_conversions'
url = {
'CREATE_INSTANT_CONVERSION': '/instant-conversion',
'GET_INSTANT_CONVERSION': '/instant-conversion/%(id)s'
'INSTANT_CONVERSION': '/conversions/instant-conversion'
}


Expand Down
175 changes: 175 additions & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
from mangopay.resources import ConversionQuote, QuotedConversion, InstantConversion
from mangopay.utils import Money
from tests.resources import ConversionRate, Conversion, Wallet
from tests.test_base import BaseTestLive


class ConversionsTest(BaseTestLive):

def test_get_conversion_rate(self):
conversion_rate = ConversionRate()
conversion_rate.debited_currency = 'EUR'
conversion_rate.credited_currency = 'GBP'

complete_conversion_rate = conversion_rate.get_conversion_rate()

self.assertIsNotNone(complete_conversion_rate)
self.assertIsNotNone(complete_conversion_rate.data[0].client_rate)
self.assertIsNotNone(complete_conversion_rate.data[0].market_rate)

def test_create_instant_conversion(self):
user = BaseTestLive.get_john()

credited_wallet = Wallet()
credited_wallet.owners = (user,)
credited_wallet.currency = 'GBP'
credited_wallet.description = 'WALLET IN GBP'
credited_wallet = Wallet(**credited_wallet.save())

credited_funds = Money()
credited_funds.currency = 'GBP'

debited_funds = Money()
debited_funds.currency = 'EUR'
debited_funds.amount = 79

instant_conversion = InstantConversion()
instant_conversion.author = user
instant_conversion.credited_wallet = credited_wallet
instant_conversion.debited_wallet = BaseTestLive.create_new_wallet_with_money()
instant_conversion.credited_funds = credited_funds
instant_conversion.debited_funds = debited_funds
instant_conversion.fees = Money(amount=9, currency='EUR')
instant_conversion.tag = "instant conversion test"

instant_conversion_response = instant_conversion.save()

self.assertIsNotNone(instant_conversion_response)
self.assertIsNotNone(instant_conversion_response['debited_funds'].amount)
self.assertIsNotNone(instant_conversion_response['credited_funds'].amount)
self.assertIsNotNone(instant_conversion_response['fees'].amount)
self.assertEqual(instant_conversion_response['status'], 'SUCCEEDED')

def test_get_instant_conversion(self):
user = BaseTestLive.get_john()

credited_wallet = Wallet()
credited_wallet.owners = (user,)
credited_wallet.currency = 'GBP'
credited_wallet.description = 'WALLET IN GBP'
credited_wallet = Wallet(**credited_wallet.save())

credited_funds = Money()
credited_funds.currency = 'GBP'

debited_funds = Money()
debited_funds.currency = 'EUR'
debited_funds.amount = 79

instant_conversion = InstantConversion()
instant_conversion.author = user
instant_conversion.credited_wallet = credited_wallet
instant_conversion.debited_wallet = BaseTestLive.create_new_wallet_with_money()
instant_conversion.credited_funds = credited_funds
instant_conversion.debited_funds = debited_funds
instant_conversion.tag = "instant conversion test"

instant_conversion_response = instant_conversion.save()
returned_conversion_response = Conversion.get_conversion(instant_conversion_response['id'])

self.assertIsNotNone(returned_conversion_response)
self.assertIsNotNone(returned_conversion_response.data[0])
self.assertIsNotNone(returned_conversion_response.data[0].debited_funds.amount)
self.assertIsNotNone(returned_conversion_response.data[0].credited_funds.amount)
self.assertEqual(returned_conversion_response.data[0].status, 'SUCCEEDED')

def test_create_conversion_quote(self):
conversion_quote = ConversionQuote()
conversion_quote.credited_funds = Money(currency='USD')
conversion_quote.debited_funds = Money(currency='GBP', amount=100)
conversion_quote.duration = 90
conversion_quote.tag = "Created using the Mangopay Python SDK"

created_conversion_quote = conversion_quote.create_conversion_quote()

self.assertIsNotNone(created_conversion_quote)
self.assertIsNotNone(created_conversion_quote['debited_funds'])
self.assertIsNotNone(created_conversion_quote['credited_funds'])
self.assertIsNotNone(created_conversion_quote['conversion_rate'])
self.assertEqual('ACTIVE', created_conversion_quote['status'])

def test_get_conversion_quote(self):
conversion_quote = ConversionQuote()
conversion_quote.credited_funds = Money(currency='USD')
conversion_quote.debited_funds = Money(currency='GBP', amount=100)
conversion_quote.duration = 90
conversion_quote.tag = "Created using the Mangopay Python SDK"

created_conversion_quote = conversion_quote.create_conversion_quote()
returned_conversion_quote = ConversionQuote.get_conversion_quote(created_conversion_quote['id'])
response = returned_conversion_quote.data[0]
self.assertIsNotNone(response)
self.assertIsNotNone(response.debited_funds)
self.assertIsNotNone(response.credited_funds)
self.assertIsNotNone(response.conversion_rate)
self.assertEqual('ACTIVE', response.status)

def test_create_quoted_conversion(self):
user = BaseTestLive.get_john()

credited_wallet = Wallet()
credited_wallet.owners = (user,)
credited_wallet.currency = 'GBP'
credited_wallet.description = 'WALLET IN GBP'
credited_wallet = Wallet(**credited_wallet.save())
debited_wallet = BaseTestLive.create_new_wallet_with_money()

conversion_quote = ConversionQuote()
conversion_quote.credited_funds = Money(currency='GBP')
conversion_quote.debited_funds = Money(currency='EUR', amount=50)
conversion_quote.duration = 90
conversion_quote.tag = "Created using the Mangopay Python SDK"
created_conversion_quote = conversion_quote.create_conversion_quote()

quoted_conversion = QuotedConversion()
quoted_conversion.quote_id = created_conversion_quote['id']
quoted_conversion.author_id = debited_wallet.owners_ids[0]
quoted_conversion.credited_wallet = credited_wallet
quoted_conversion.debited_wallet = debited_wallet

response = quoted_conversion.save()
self.assertIsNotNone(response)
self.assertIsNotNone(response['debited_funds'].amount)
self.assertIsNotNone(response['credited_funds'].amount)
self.assertEqual(response['status'], 'SUCCEEDED')

def test_get_quoted_conversion(self):
user = BaseTestLive.get_john()

credited_wallet = Wallet()
credited_wallet.owners = (user,)
credited_wallet.currency = 'GBP'
credited_wallet.description = 'WALLET IN GBP'
credited_wallet = Wallet(**credited_wallet.save())
debited_wallet = BaseTestLive.create_new_wallet_with_money()

conversion_quote = ConversionQuote()
conversion_quote.credited_funds = Money(currency='GBP')
conversion_quote.debited_funds = Money(currency='EUR', amount=50)
conversion_quote.duration = 90
conversion_quote.tag = "Created using the Mangopay Python SDK"
created_conversion_quote = conversion_quote.create_conversion_quote()

quoted_conversion = QuotedConversion()
quoted_conversion.quote_id = created_conversion_quote['id']
quoted_conversion.author_id = debited_wallet.owners_ids[0]
quoted_conversion.credited_wallet = credited_wallet
quoted_conversion.debited_wallet = debited_wallet

created_quoted_conversion = quoted_conversion.save()
response = Conversion.get_conversion(created_quoted_conversion['id'])
self.assertIsNotNone(response)
self.assertIsNotNone(response.data[0])
self.assertIsNotNone(response.data[0].debited_funds.amount)
self.assertIsNotNone(response.data[0].credited_funds.amount)
self.assertEqual(response.data[0].status, 'SUCCEEDED')
81 changes: 0 additions & 81 deletions tests/test_instant_conversion.py

This file was deleted.

Loading