Skip to content

Commit

Permalink
stream_controller_test for streams list
Browse files Browse the repository at this point in the history
  • Loading branch information
wkrzywiec committed Feb 15, 2025
1 parent 7daba0e commit b28d3dc
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ tasks:
cmds:
- echo "Running tests for mankkoo core..."
- pytest -s -vv --cov=./mankkoo
# uncomment below step to run a single test file
# - pytest -s -vv --cov=./mankkoo tests/controller/stream_controller_test.py

update:
desc: Update dependencies for all services
Expand Down
2 changes: 1 addition & 1 deletion services/mankkoo/mankkoo/controller/stream_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from apiflask import APIBlueprint, Schema
from apiflask.fields import String, Boolean, Integer
from apiflask.fields import String, Boolean

from mankkoo.stream import stream_db as database
from mankkoo.stream.stream_db import Stream, StreamDetails
Expand Down
8 changes: 2 additions & 6 deletions services/mankkoo/mankkoo/stream/stream_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ def load_streams(active: bool, type: str) -> list[Stream]:
where_clause = ""

if len(conditions) > 0:
where_clause = f"WHERE {conditions[0]}"

if len(conditions) > 1:
conditions.pop(0)
and_cond = " AND ".join(conditions)
where_clause = where_clause + " AND " + and_cond
and_conditions = " AND ".join(conditions)
where_clause = f"WHERE {and_conditions}"

query = f"""
SELECT
Expand Down
6 changes: 2 additions & 4 deletions services/mankkoo/tests/controller/account_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import mankkoo.event_store as es


def test_load_all_accounts(test_client):
def test_all_accounts_are_loaded(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
Expand All @@ -23,8 +23,6 @@ def test_load_all_accounts(test_client):
assert response.status_code == 200

payload = response.get_json()
print(type(payload))
print(type(payload[0]))
assert len(payload) == 2

first_account = next(x for x in payload if x['id'] == str(streams[0].id))
Expand All @@ -38,7 +36,7 @@ def test_load_all_accounts(test_client):
assert first_account['importer'] == streams[0].metadata['importer']


def test_load_all_operations(test_client, account_with_two_operations):
def test_all_account_operations_are_laoded__if_valid_account_id_is_provided(test_client, account_with_two_operations):
# GIVEN
stream_type = 'account'
stream_id = uuid.uuid4()
Expand Down
128 changes: 128 additions & 0 deletions services/mankkoo/tests/controller/stream_controller_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import uuid

import mankkoo.event_store as es

def test_all_streams_are_listed__if_no_filters_are_provided__and_stream_names_are_correctly_set(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
{"active": True, "alias": "Bank account A", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-1", "accountName": "Super Personal account", "accountType": "checking", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'investment', 0,
{"active": True, "category": "treasury_bonds", "bankName": "Bank B", "investmentName": "2 years Treasury Bonds"}),
es.Stream(uuid.uuid4(), 'retirement', 0,
{"active": True, "alias": "PPK", "bank": "Bank PPK", "bankUrl": "https://www.ppk.com", "accountNumber": "ppk-bank-acc-11", "accountName": "'PPK", "accountType": "retirement", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'stocks', 0,
{"active": True, "type": "ETF", "broker": "Bank A", "etfUrl": "https://atlasetf.pl/etf-details/IE00B4L5Y983", "etfName": "ETF name", })
]
es.create(streams)

# WHEN
response = test_client.get('/api/streams')

# THEN
assert response.status_code == 200

payload = response.get_json()
assert len(payload) == 4

account_stream = next(x for x in payload if x['id'] == str(streams[0].id))
assert account_stream['type'] == 'account'
assert account_stream['name'] == streams[0].metadata['bankName'] + ' - ' + streams[0].metadata['alias']

investment_stream = next(x for x in payload if x['id'] == str(streams[1].id))
assert investment_stream['type'] == 'investment'
assert investment_stream['name'] == streams[1].metadata['investmentName']

retirement_stream = next(x for x in payload if x['id'] == str(streams[2].id))
assert retirement_stream['type'] == 'retirement'
assert retirement_stream['name'] == streams[2].metadata['alias']

etf_stream = next(x for x in payload if x['id'] == str(streams[3].id))
assert etf_stream['type'] == 'stocks'
assert etf_stream['name'] == streams[3].metadata['etfName']

def test_only_active_streams_are_listed__if_active_qparam_equals_true(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
{"active": True, "alias": "Bank account A", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-1", "accountName": "Super Personal account", "accountType": "checking", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'account', 0,
{"active": False, "alias": "Bank account B", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-2", "accountName": "Super Personal account 2", "accountType": "checking", "importer": "MANKKOO"}),
]
es.create(streams)

# WHEN
response = test_client.get('/api/streams?active=true')

# THEN
assert response.status_code == 200

payload = response.get_json()
assert len(payload) == 1

assert payload[0]['id'] == str(streams[0].id)

def test_only_inactive_streams_are_listed__if_active_qparam_equals_false(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
{"active": True, "alias": "Bank account A", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-1", "accountName": "Super Personal account", "accountType": "checking", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'account', 0,
{"active": False, "alias": "Bank account B", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-2", "accountName": "Super Personal account 2", "accountType": "checking", "importer": "MANKKOO"}),
]
es.create(streams)

# WHEN
response = test_client.get('/api/streams?active=false')

# THEN
assert response.status_code == 200

payload = response.get_json()
assert len(payload) == 1

assert payload[0]['id'] == str(streams[1].id)

def test_only_accounts_streams_are_listed__if_type_qparam_equals_account(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
{"active": True, "alias": "Bank account A", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-1", "accountName": "Super Personal account", "accountType": "checking", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'investment', 0,
{"active": True, "category": "treasury_bonds", "bankName": "Bank B", "investmentName": "2 years Treasury Bonds"}),
]
es.create(streams)

# WHEN
response = test_client.get('/api/streams?type=account')

# THEN
assert response.status_code == 200

payload = response.get_json()
assert len(payload) == 1

assert payload[0]['id'] == str(streams[0].id)

def test_only_inactive_investment_streams_are_listed__if_type_qparam_equals_investment_and_active_qparam_is_false(test_client):
# GIVEN
streams = [
es.Stream(uuid.uuid4(), 'account', 0,
{"active": True, "alias": "Bank account A", "bankName": "Bank A", "bankUrl": "https://www.bank-a.com", "accountNumber": "iban-1", "accountName": "Super Personal account", "accountType": "checking", "importer": "MANKKOO"}),
es.Stream(uuid.uuid4(), 'investment', 0,
{"active": True, "category": "treasury_bonds", "bankName": "Bank B", "investmentName": "2 years Treasury Bonds"}),
es.Stream(uuid.uuid4(), 'investment', 0,
{"active": False, "category": "treasury_bonds", "bankName": "Bank B", "investmentName": "1 year Treasury Bonds"}),
]
es.create(streams)

# WHEN
response = test_client.get('/api/streams?type=investment&active=false')

# THEN
assert response.status_code == 200

payload = response.get_json()
assert len(payload) == 1

assert payload[0]['id'] == str(streams[2].id)

0 comments on commit b28d3dc

Please sign in to comment.