Skip to content

Commit

Permalink
Add unit tests (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Lee authored Apr 27, 2021
1 parent 5e0caa7 commit 4dfc88d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ name: Python package

on:
push:
branches: [ main ]
branches:
- '**'
pull_request:
branches: [ main ]
branches:
- '**'

jobs:
build:
Expand All @@ -34,6 +36,7 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# - name: Test with pytest
# run: |
# pytest
- name: Test with unittest
run: |
python -m pip install -e .
python -m unittest discover test
21 changes: 21 additions & 0 deletions automint/account/Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,24 @@ def __add__(self, other_account):
for token_id in other_account.native_tokens.keys():
new_account = new_account.add_native_token(token_id, other_account.get_native_token(token_id)['quantity'])
return new_account

def __eq__(self, other_account):
# Check type
if type(other_account) != Account:
return False

# Check lovelace ammounts
if self.get_lovelace() != other_account.get_lovelace():
return False

# Check native token quantities
for token_id in self.native_tokens.keys():
if token_id not in other_account.native_tokens:
return False
for attr in self.native_tokens[token_id].keys():
if attr not in other_account.native_tokens[token_id][attr]:
return False
if self.native_tokens[token_id][attr] != other_account.native_tokens[token_id][attr]:
return False

return True
5 changes: 4 additions & 1 deletion automint/receivers/BasicReceiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ def set_native_token(self, token_id, quantity):
def duplicate(self):
return copy.deepcopy(self)

def transfer_all(self, other_receiver):
def transfer_to(self, other_receiver):
'''Transfer the convents of this receiver to another receiver,
emptying this one'''
other_receiver.account = self.account + other_receiver.account
self.account = Account()

def get_lovelace(self):
return self.account.get_lovelace()
Expand Down
1 change: 1 addition & 0 deletions automint/utxo/UTXO.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __str__(self):
return f'{self.get_utxo_identifier()}'

def convert_to_receiver(self, addr):
'''Converts UTXO to TxReceiver object, copying all contents over'''
new_receiver = TxReceiver(addr)
new_receiver.account = self.account.duplicate()
return new_receiver
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
install_requires = [
'requests'
],
license_files=('LICENSE',)
license_files=('LICENSE',),
)
Empty file added test/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions test/test_Account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest

from automint.account import Account


class AccountTests(unittest.TestCase):
def test_lovelace_addition(self):
account = Account().add_lovelace(1000)
self.assertEqual(account.get_lovelace(), 1000)
self.assertEqual(account.get_ada(), 0.001)

def test_account_addition(self):
account_a = Account().add_lovelace(1500000)
account_b = Account().add_lovelace(1500000)
account_c = account_a + account_b
self.assertEqual(account_c.get_lovelace(), 3000000)
self.assertEqual(account_c.get_ada(), 3)

def test_account_str(self):
account = Account().add_lovelace(1500000)
self.assertEqual(str(account), '1500000')

account = Account().add_lovelace(1500000).add_native_token('12345.tokenA', 2)
self.assertEqual(str(account), '1500000+"2 12345.tokenA"')


if __name__ == '__main__':
unittest.main()

0 comments on commit 4dfc88d

Please sign in to comment.