From 14fac0996709fab96a7da3f3bb3e1c23958ac561 Mon Sep 17 00:00:00 2001 From: kurupn Date: Fri, 21 Oct 2022 20:29:34 +0100 Subject: [PATCH 1/3] Added fees calculation for wallet and bundle if sold after the holding period. --- controllers/BundleController.py | 4 ++-- controllers/WalletController.py | 4 ++-- models/Utility.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/controllers/BundleController.py b/controllers/BundleController.py index b812ffb..ce94163 100644 --- a/controllers/BundleController.py +++ b/controllers/BundleController.py @@ -335,9 +335,9 @@ def sellBundle(self, jsonReqData): bundleTransactionFE.setBundleAddress(bundleFE.getBundleAddress()) bundleTransactionFE.setTransactionDatetime(int(time.time())) if Utility.isWithinHoldingPeriod(bundleTransactionDA.getTransactionDatetime(), bundleDA.getHoldingPeriod()): - bundleTransactionFE.setChargeApplied(Utility.calculateChargesApplied(bundleTransactionFE.getAmount())) + bundleTransactionFE.setChargeApplied(Utility.calculateDeduction(bundleTransactionFE.getAmount(), float(0.10))) else: - bundleTransactionFE.setChargeApplied(Utility.roundDecimals(0.0)) + bundleTransactionFE.setChargeApplied(Utility.calculateDeduction(bundleTransactionFE.getAmount(), float(0.01))) bundleTransactionFE.setAction("SELL") self.BDA.updateBundleStatus(bundleFE) diff --git a/controllers/WalletController.py b/controllers/WalletController.py index 209408e..c9a59e5 100644 --- a/controllers/WalletController.py +++ b/controllers/WalletController.py @@ -308,9 +308,9 @@ def sellWallet(self, jsonReqData): walletTransactionFE.setWalletAddress(walletFE.getWalletAddress()) walletTransactionFE.setTransactionDatetime(int(time.time())) if Utility.isWithinHoldingPeriod(walletTransactionDA.getTransactionDatetime(), walletDA.getHoldingPeriod()): - walletTransactionFE.setChargeApplied(Utility.calculateChargesApplied(walletTransactionFE.getAmount())) + walletTransactionFE.setChargeApplied(Utility.calculateDeduction(walletTransactionFE.getAmount(), float(0.10))) else: - walletTransactionFE.setChargeApplied(Utility.roundDecimals(0.0)) + walletTransactionFE.setChargeApplied(Utility.calculateDeduction(walletTransactionFE.getAmount(), float(0.01))) walletTransactionFE.setAction("SELL") self.WDA.updateWalletCurrentBalance(walletFE) diff --git a/models/Utility.py b/models/Utility.py index 3a9feda..8c0907a 100644 --- a/models/Utility.py +++ b/models/Utility.py @@ -79,8 +79,8 @@ def roundDecimals(decimalValue): return round(float(decimalValue), 4) @staticmethod - def calculateChargesApplied(amount): - return round(float(amount) * 0.10, 4) + def calculateDeduction(amount, charge): + return round(float(amount) * charge, 4) @staticmethod def isWithinHoldingPeriod(startDateTime, holdingPeriod): From 63534350b94fe7075249fc739b546b0b5d6ba90c Mon Sep 17 00:00:00 2001 From: kurupn Date: Sat, 22 Oct 2022 13:17:12 +0100 Subject: [PATCH 2/3] Added unit test cases for fees and charge applied calculation for wallet and bundle if sold after the holding period. --- data_access/BundleDataAccess.py | 21 +++++++++++++++++++ data_access/WalletDataAccess.py | 22 ++++++++++++++++++++ models/Utility.py | 2 +- tests/test_BundleController.py | 37 +++++++++++++++++++++++++++++++++ tests/test_WalletController.py | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/data_access/BundleDataAccess.py b/data_access/BundleDataAccess.py index 90ff773..a07c90e 100644 --- a/data_access/BundleDataAccess.py +++ b/data_access/BundleDataAccess.py @@ -102,9 +102,18 @@ def insertDayZeroData(self): bundleThree.setPurchaseDatetime(1664645865) bundleThree.setStatus("ACTIVE") + bundleFour = Bundle() + bundleFour.setBundleAddress("1G6ftEHoxst84caX2EUd") + bundleFour.setBundleID("5") + bundleFour.setCustomerID("1WNJKpBpYfWwKIlvbaz0") + bundleFour.setHoldingPeriod(3) + bundleFour.setPurchaseDatetime(1654645865) + bundleFour.setStatus("ACTIVE") + self.insertBundle(bundleOne) self.insertBundle(bundleTwo) self.insertBundle(bundleThree) + self.insertBundle(bundleFour) cur.execute("SELECT EXISTS (SELECT * FROM BundleTransactionHistory)") if not cur.fetchone()[0]: @@ -142,9 +151,21 @@ def insertDayZeroData(self): bundleTransactionObjThree.setExpiry("03/23") bundleTransactionObjThree.setInitialRate(735.23) + bundleTransactionObjFour = BundleTransactionHistory() + bundleTransactionObjFour.setTransactionID("h0YrN10TzimnB05aYkCc") + bundleTransactionObjFour.setBundleAddress("1G6ftEHoxst84caX2EUd") + bundleTransactionObjFour.setAction("BUY") + bundleTransactionObjFour.setTransactionDatetime(1654645865) + bundleTransactionObjFour.setChargeApplied(0.00) + bundleTransactionObjFour.setAmount(89046.3456) + bundleTransactionObjFour.setCardNumber("4857037529348105") + bundleTransactionObjFour.setExpiry("03/23") + bundleTransactionObjFour.setInitialRate(56748.3421) + self.insertBundleTransactionHistory(bundleTransactionObj) self.insertBundleTransactionHistory(bundleTransactionObjTwo) self.insertBundleTransactionHistory(bundleTransactionObjThree) + self.insertBundleTransactionHistory(bundleTransactionObjFour) cur.close() con.commit() diff --git a/data_access/WalletDataAccess.py b/data_access/WalletDataAccess.py index 89013bf..c3b1e26 100644 --- a/data_access/WalletDataAccess.py +++ b/data_access/WalletDataAccess.py @@ -109,6 +109,15 @@ def insertDayZeroData(self): walletOne.setHoldingPeriod(12) self.insertWallet(walletOne) + walletTwo = Wallet() + walletTwo.setCustomerID("1WNJKpBpYfWwKIlvbaz0") + walletTwo.setWalletAddress("t5vumA246YH7dX6k7Hfn") + walletTwo.setInitialBalance(1.4326) + walletTwo.setCurrentBalance(1.4326) + walletTwo.setCryptocurrencyCode('dogecoin') + walletTwo.setHoldingPeriod(5) + self.insertWallet(walletTwo) + cur.execute("select exists (SELECT * FROM Wallet)") if not cur.fetchone()[0]: walletTransZero = WalletTransactionHistory() @@ -135,8 +144,21 @@ def insertDayZeroData(self): walletTransOne.setWalletAddress("hrD3IxwVUWloVP0nrIct") walletTransOne.setUnitsSold(0.0) + walletTransTwo = WalletTransactionHistory() + walletTransTwo.setTransactionID("0ueLmcfLy8DPtMObBKEv") + walletTransTwo.setTransactionDatetime(1554541119) + walletTransTwo.setChargeApplied(0.0) + walletTransTwo.setAmount(36487.2188) + walletTransTwo.setAction("BUY") + walletTransTwo.setCardNumber("7436281956743846") + walletTransTwo.setExpiry("07/25") + walletTransTwo.setInitialRate(25469.23) + walletTransTwo.setWalletAddress("t5vumA246YH7dX6k7Hfn") + walletTransTwo.setUnitsSold(0.0) + self.insertWalletTransactionHistory(walletTransZero) self.insertWalletTransactionHistory(walletTransOne) + self.insertWalletTransactionHistory(walletTransTwo) cur.close() con.commit() diff --git a/models/Utility.py b/models/Utility.py index 8c0907a..40579d8 100644 --- a/models/Utility.py +++ b/models/Utility.py @@ -88,7 +88,7 @@ def isWithinHoldingPeriod(startDateTime, holdingPeriod): endTime = startTime + relativedelta(months=holdingPeriod) currentTime = datetime.fromtimestamp(int(time.time())) - if currentTime > startTime and currentTime < endTime: + if currentTime >= startTime and currentTime <= endTime: return True else: return False diff --git a/tests/test_BundleController.py b/tests/test_BundleController.py index badffb6..6aeaced 100644 --- a/tests/test_BundleController.py +++ b/tests/test_BundleController.py @@ -2529,6 +2529,43 @@ def test_success_response_key_initialRate_correct(self): .get("initialRate"), expected) + def test_success_response_key_chargeApplied_correct(self): + reqData = { + "customerID": "1WNJKpBpYfWwKIlvbaz0", + "bundleID": "1", + "holdingPeriod": 6, + "initialRate": 22000.00, + "amount": 1000.00, + "cardNumber": "1234567890123456", + "expiry": "12/24" + } + response = self.BController.purchaseBundle(reqData) + + reqData = { + "bundleAddress": response.get("bundle").get("bundleAddress"), + "customerID": "1WNJKpBpYfWwKIlvbaz0", + "bundleID": "5", + "initialRate": 56748.3421, + "amount": 89046.3456, + "cardNumber": "1234567890123456", + "expiry": "12/24" + } + response = self.BController.sellBundle(reqData) + self.assertEqual(8904.6346, response.get("bundleTransaction").get("chargeApplied")) + + def test_success_response_key_fees_correct(self): + reqData = { + "bundleAddress": "1G6ftEHoxst84caX2EUd", + "customerID": "1WNJKpBpYfWwKIlvbaz0", + "bundleID": "5", + "initialRate": 56748.3421, + "amount": 89046.3456, + "cardNumber": "1234567890123456", + "expiry": "12/24" + } + response = self.BController.sellBundle(reqData) + self.assertEqual(890.4635, response.get("bundleTransaction").get("chargeApplied")) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_WalletController.py b/tests/test_WalletController.py index 23619c3..4ddcb42 100644 --- a/tests/test_WalletController.py +++ b/tests/test_WalletController.py @@ -1971,6 +1971,43 @@ def test_failure_message_unitsSold_exceeding_currentBalance(self): expected = "Units to sell must not be greater than the Current Balance" self.assertEqual(expected, response.get("status")["statusMessage"]) + def test_success_response_chargeApplied_value_correct(self): + jsonReqData = { + "customerID": "Debo32tKqJBeZwHHgkvx", + "initialBalance": 1.9492, + "cryptocurrencyCode": "bitcoin", + "holdingPeriod": 4, + "initialRate": 38291.34, + "amount": 74638.99, + "cardNumber": "7281726537281963", + "expiry": "12/23" + } + response = self.wController.purchaseWallet(jsonReqData) + + jsonReqData = { + "customerID": "Debo32tKqJBeZwHHgkvx", + "walletAddress": response.get("wallet").get("walletAddress"), + "unitsSold": 1.9492, + "initialRate": 42710.34, + "amount": 83250.9947, + "cardNumber": "7281726537281963", + "expiry": "12/23" + } + response = self.wController.sellWallet(jsonReqData) + self.assertEqual(8325.0995, response.get("walletTransaction")["chargeApplied"]) + + def test_success_response_fees_value_correct(self): + jsonReqData = { + "customerID": "1WNJKpBpYfWwKIlvbaz0", + "walletAddress": "t5vumA246YH7dX6k7Hfn", + "unitsSold": 1.4326, + "initialRate": 42710.34, + "amount": 61186.8330, + "cardNumber": "7281726537281963", + "expiry": "12/25" + } + response = self.wController.sellWallet(jsonReqData) + self.assertEqual(611.8683, response.get("walletTransaction")["chargeApplied"]) if __name__ == '__main__': unittest.main() From 8959606bd7960fce0d8885d29a7fbe0515c23a1f Mon Sep 17 00:00:00 2001 From: kurupn Date: Sat, 22 Oct 2022 14:19:06 +0100 Subject: [PATCH 3/3] Modified the unit test names --- tests/test_BundleController.py | 16 ++-------------- tests/test_WalletController.py | 2 +- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/tests/test_BundleController.py b/tests/test_BundleController.py index 6aeaced..a1048bb 100644 --- a/tests/test_BundleController.py +++ b/tests/test_BundleController.py @@ -995,18 +995,6 @@ def test_success_reponse_key_bundleCryptocurrencies_is_exists(self): response = self.BController.getAllBundlesFromCustomerID(reqData) self.assertTrue("bundleCryptocurrencies" in response.get("bundles")[0].keys()) - def test_success_reponse_cryptocurrencies_for_bundles(self): - reqData = { - "customerID": "1WNJKpBpYfWwKIlvbaz0" - } - response = self.BController.getAllBundlesFromCustomerID(reqData) - - def test_success_reponse_cryptocurrencies_for_bundles(self): - reqData = { - "customerID": "Debo32tKqJBeZwHHgkvx" - } - response = self.BController.getAllBundlesFromCustomerID(reqData) - class TestPurchaseBundle(unittest.TestCase): @@ -1602,7 +1590,7 @@ def test_success_response_key_chargeApplied_exists(self): self.assertTrue("chargeApplied" in response.get("bundleTransaction").keys()) - def test_success_response_key_chargeApplied_correct(self): + def test_success_response_key_chargeApplied_correct_value_zero(self): reqData = { "customerID": "1WNJKpBpYfWwKIlvbaz0", "bundleID": "1", @@ -2529,7 +2517,7 @@ def test_success_response_key_initialRate_correct(self): .get("initialRate"), expected) - def test_success_response_key_chargeApplied_correct(self): + def test_success_response_key_chargeApplied_calculated_correct(self): reqData = { "customerID": "1WNJKpBpYfWwKIlvbaz0", "bundleID": "1", diff --git a/tests/test_WalletController.py b/tests/test_WalletController.py index 4ddcb42..8dd7bb6 100644 --- a/tests/test_WalletController.py +++ b/tests/test_WalletController.py @@ -21,7 +21,7 @@ def test_success_response_cryptocurrencyName_key_present_in_res(self): response = self.wController.getAllAvailableCryptocurrencies() self.assertTrue("cryptocurrencyName" in response.get("availableCryptocurrencies")[0].keys()) - def test_success_response_cryptocurrencyCode_key_present_in_res(self): + def test_success_response_symbol_key_present_in_res(self): response = self.wController.getAllAvailableCryptocurrencies() self.assertTrue("symbol" in response.get("availableCryptocurrencies")[0].keys())