Libraries refactor and add some sinks #4

Closed
m wants to merge 136 commits from library-refactor into master
1 changed files with 111 additions and 36 deletions
Showing only changes of commit ee93a91c04 - Show all commits

View File

@ -40,51 +40,84 @@ class TestTransactions(TestCase):
transactions.r.keys = self.mock_keys transactions.r.keys = self.mock_keys
transactions.r.get = self.mock_get transactions.r.get = self.mock_get
# Mock some callbacks
self.transactions.irc = MagicMock() self.transactions.irc = MagicMock()
self.transactions.irc.sendmsg = MagicMock() self.transactions.irc.sendmsg = MagicMock()
self.transactions.release_funds = MagicMock() self.transactions.release_funds = MagicMock()
self.transactions.notify = MagicMock() self.transactions.notify = MagicMock()
self.transactions.notify.notify_complete_trade = MagicMock() self.transactions.notify.notify_complete_trade = MagicMock()
self.trade_1 = { # Mock the rates
"id": "uuid1", self.transactions.money = MagicMock()
"buyer": "test_buyer_1", self.transactions.money.get_rates_all = MagicMock()
"currency": "GBP", self.transactions.money.get_rates_all.return_value = {"GBP": 0.8}
"amount": "1",
"amount_xmr": "0.3", self.trades = {
"reference": "TEST-1", 1: {
"id": "uuid1",
"buyer": "test_buyer_1",
"currency": "GBP",
"amount": "1",
"amount_xmr": "0.3",
"reference": "TEST-1",
},
2: {
"id": "uuid2",
"buyer": "test_buyer_2",
"currency": "GBP",
"amount": "1",
"amount_xmr": "0.3",
"reference": "TEST-2",
},
3: {
"id": "uuid3",
"buyer": "test_buyer_3",
"currency": "GBP",
"amount": "1000",
"amount_xmr": "3",
"reference": "TEST-3",
},
4: {
"id": "uuid4",
"buyer": "test_buyer_4",
"currency": "GBP",
"amount": "10",
"amount_xmr": "0.5",
"reference": "TEST-4",
},
} }
self.trade_2 = { self.return_trades = [1, 2, 3]
"id": "uuid2",
"buyer": "test_buyer_2",
"currency": "GBP",
"amount": "1",
"amount_xmr": "0.3",
"reference": "TEST-2",
}
@property @property
def test_data_copy(self): def test_data_copy(self):
return deepcopy(self.test_data) return deepcopy(self.test_data)
def data_custom(self, amount, currency, reference):
test_data = self.test_data_copy
test_data["data"]["reference"] = reference
test_data["data"]["legs"][0]["amount"] = amount
test_data["data"]["legs"][0]["currency"] = currency
return test_data
def mock_hgetall(self, string): def mock_hgetall(self, string):
if string == "trade.TEST-1": print("MOCK HGERALL CALLED WITH", string)
return self.trade_1 ref = string.split(".")[1]
elif string == "trade.TEST-2": for num, trade in self.trades.items():
return self.trade_2 if trade["reference"] == ref:
return trade
def mock_hmset(self, string, data): def mock_hmset(self, string, data):
print("HMSET", string, data) print("HMSET", string, data)
def mock_keys(self, string): def mock_keys(self, string):
return ["uuid1", "uuid2"] return [v["id"] for k, v in self.trades.items() if k in self.return_trades]
def mock_get(self, string): def mock_get(self, string):
if string == "uuid1": print("MOCK GET", string)
return "TEST-1" for num, trade in self.trades.items():
elif string == "uuid2": if trade["id"] == string:
return "TEST-2" return trade["reference"]
def test_transaction(self): def test_transaction(self):
self.transactions.transaction(self.test_data) self.transactions.transaction(self.test_data)
@ -98,38 +131,80 @@ class TestTransactions(TestCase):
def test_transaction_invalid(self): def test_transaction_invalid(self):
invalid_data = self.test_data_copy invalid_data = self.test_data_copy
invalid_data["data"]["reference"] = "sss" invalid_data = self.data_custom(2000, "SEK", "sss")
invalid_data["data"]["amount"] = 2000
invalid_data["data"]["currency"] = "SEK"
self.transactions.transaction(invalid_data) self.transactions.transaction(invalid_data)
self.transactions.release_funds.assert_not_called() self.transactions.release_funds.assert_not_called()
def test_transaction_malformed(self): def test_transaction_malformed(self):
pass malformed_data = self.test_data_copy
del malformed_data["data"]
self.transactions.transaction(malformed_data)
self.transactions.release_funds.assert_not_called()
malformed_data = self.test_data_copy
del malformed_data["data"]["type"]
self.transactions.transaction(malformed_data)
self.transactions.release_funds.assert_not_called()
def test_transaction_no_reference_fail(self): def test_transaction_no_reference_fail(self):
pass no_reference_fail = self.data_custom(1, "GBP", "none")
no_reference_fail["data"]["reference"] = "none"
self.transactions.transaction(no_reference_fail)
self.transactions.release_funds.assert_not_called()
def test_transaction_no_reference_pass(self): def test_transaction_no_reference_pass(self):
pass no_reference_pass = self.data_custom(1, "GBP", "none")
no_reference_pass["data"]["reference"] = "none"
self.return_trades = [1]
self.transactions.transaction(no_reference_pass)
self.transactions.release_funds.assert_called_with("uuid1", "TEST-1")
def test_transaction_large(self):
exceeds_max = self.data_custom(1000, "GBP", "TEST-3")
self.transactions.transaction(exceeds_max)
self.transactions.release_funds.assert_called_once_with("uuid3", "TEST-3")
def test_transaction_no_reference_exceeds_max(self): def test_transaction_no_reference_exceeds_max(self):
pass exceeds_max = self.data_custom(1000, "GBP", "noref")
self.transactions.transaction(exceeds_max)
self.transactions.release_funds.assert_not_called()
def test_transaction_wrong_currency(self): def test_transaction_wrong_currency(self):
pass wrong_currency = self.data_custom(1, "EUR", "TEST-1")
self.transactions.transaction(wrong_currency)
self.transactions.release_funds.assert_not_called()
wrong_currency = self.data_custom(1, "EUR", "none")
self.transactions.transaction(wrong_currency)
self.transactions.release_funds.assert_not_called()
def test_transaction_wrong_amount(self): def test_transaction_wrong_amount(self):
pass self.transactions.money.get_acceptable_margins = MagicMock()
self.transactions.money.get_acceptable_margins.return_value = (0.8, 1.8)
wrong_amount = self.data_custom(10, "GBP", "TEST-1")
self.transactions.transaction(wrong_amount)
self.transactions.release_funds.assert_not_called()
wrong_amount = self.data_custom(10, "GBP", "none")
self.transactions.transaction(wrong_amount)
self.transactions.release_funds.assert_not_called()
def test_transaction_pending(self): def test_transaction_pending(self):
pass pending_tx = self.test_data_copy
pending_tx["data"]["state"] = "pending"
self.transactions.transaction(pending_tx)
self.transactions.release_funds.assert_not_called()
def test_transaction_too_low(self): def test_transaction_too_low(self):
pass too_low = self.data_custom(5, "GBP", "TEST-1")
self.transactions.transaction(too_low)
self.transactions.release_funds.assert_not_called()
def test_transaction_too_high(self): def test_transaction_too_high(self):
pass too_high = self.data_custom(15, "GBP", "TEST-1")
self.transactions.transaction(too_high)
self.transactions.release_funds.assert_not_called()
# def test_transaction_pending_then_completed(self): # def test_transaction_pending_then_completed(self):
# pass # pass