Skip to content

Commit

Permalink
expand tests to include some flask/integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Jun 27, 2020
1 parent cb0b410 commit 28f0880
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 11 deletions.
Binary file modified 022-burette/magnum_opus/docker/storage/storage.db
Binary file not shown.
10 changes: 8 additions & 2 deletions 022-burette/magnum_opus/magnumopus/models/substance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import db
from sqlalchemy_utils.types.scalar_list import ScalarListType
from sqlalchemy_utils import ScalarListType

class SubstanceMustBeFreshToProcessException(Exception):
pass
Expand All @@ -18,7 +18,13 @@ def __init__(self, nature='Unknown'):
super(Substance, self).__init__()

def _process(self, process_name):
self.state.append(process_name)
# Example of leakage of persistence behaviour into
# domain, due to db.Model -- we must copy the state list to
# ensure it is seen to change...
state = self.state[:]
state.append(process_name)
self.state = state

return self

def cook(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ def post(self):

# Crude start at DI... see flask-injector
result = instruction_handler.handle(instruction, pantry)
print('y', result.state, result.id, self._substance_schema.dump(result))

pantry.add_substance(result)
print('z', result.state, result.id, self._substance_schema.dump(result))

pantry.commit()
print('a', result.state, result.id, self._substance_schema.dump(result))
print(result.state, result.id, self._substance_schema.dump(result))

return self._substance_schema.dump(result)
return self._substance_schema.dump(result), 201


def init_app(app, api):
Expand Down
2 changes: 1 addition & 1 deletion 022-burette/magnum_opus/magnumopus/resources/substance.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def post(self):

pantry.commit()

return self._substance_schema.dump(substance)
return self._substance_schema.dump(substance), 201


def init_app(app, api):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ class Meta:

id = ma.auto_field()
nature = ma.auto_field()
state = fields.Function(lambda model: model.state or [])
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ def handle(self, instruction: AlembicInstruction, pantry: Pantry):
else:
raise UnknownAlembicInstructionException(f'Unknown instruction: {action}')

print(result.state, result.id, 'x')
return result
4 changes: 4 additions & 0 deletions 022-burette/magnum_opus/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def is_dictionary_inside(d1, d2):
return d1.items() <= d2.items()

Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
from magnumopus import create_app
from .. import is_dictionary_inside
from .test_substance_resource import create_substance_request

def create_alembic_instruction_request(client, instruction_type, natures, action=None):
data = {
'instruction_type': instruction_type,
'natures': ','.join(natures)
}

if action is not None:
data['action'] = action

return client.post('/alembic_instruction', data=data)

def test_create_alembic_mix_instruction(client):
create_substance_request(client, ['Mercury'])
create_substance_request(client, ['Salt'])
create_substance_request(client, ['Sulphur'])

rv = create_alembic_instruction_request(client, 'mix', ['Mercury', 'Salt', 'Sulphur'])

assert rv.status_code == 201

content = rv.get_json()

assert is_dictionary_inside({
'nature': 'Gloop',
'state': [],
'is_philosophers_stone': False
}, content)

def test_create_alembic_process_instruction(client):
create_substance_request(client, ['Mercury'])

rv = create_alembic_instruction_request(client, 'process', ['Mercury'], 'cook')

assert rv.status_code == 201

content = rv.get_json()

assert is_dictionary_inside({
'nature': 'Mercury',
'state': ['cooked'],
'is_philosophers_stone': False
}, content)
7 changes: 0 additions & 7 deletions 022-burette/magnum_opus/tests/application/test_app.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
from magnumopus import create_app
from .. import is_dictionary_inside


def test_create_philosophers_stone(client):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from magnumopus import create_app
from .. import is_dictionary_inside

def create_substance_request(client, nature):
return client.post('/substance', data={
'nature': nature
})

def index_substances_request(client, nature):
return client.get(f'/substance?nature={nature}')

def test_create_substance(client):
rv = create_substance_request(client, 'Sulphur')

assert rv.status_code == 201

content = rv.get_json()

assert type(content['id']) is int

assert is_dictionary_inside({
'nature': 'Sulphur',
'state': [],
'is_philosophers_stone': False
}, content)


def test_get_substances(client):
create_substance_request(client, 'Sulphur')

rv = index_substances_request(client, 'Sulphur')

assert rv.status_code == 200

content = rv.get_json()

assert type(content) is list

assert len(content) == 1

assert type(content[0]['id']) is int

assert is_dictionary_inside({
'nature': 'Sulphur',
'state': [],
'is_philosophers_stone': False
}, content[0])

# We also need non-happy paths!
8 changes: 8 additions & 0 deletions 022-burette/magnum_opus/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ def _db(app):
with app.app_context():
db.create_all()
return db

@pytest.fixture
def client(app):
with app.test_client() as client:
with app.app_context():
db.create_all()
yield client
db.drop_all()

0 comments on commit 28f0880

Please sign in to comment.