Skip to content

Commit

Permalink
using yaml_snapshot from yamlns library
Browse files Browse the repository at this point in the history
  • Loading branch information
vokimon committed Mar 23, 2022
1 parent f3a9f62 commit 0b2a6ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
47 changes: 4 additions & 43 deletions heman/api/pvcalculator/pvcalculator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,6 @@
from ...config import create_app
from ...auth import APIUser
import os
try:
from pathlib2 import Path
except ImportError:
from pathlib import Path

@pytest.fixture
def snapshot(request):
def normalize(x):
"""Turns recursively all the dicts of a json like
structure into yamlns namespaces with their keys sorted
so that their dumps can be compared.
"""
if isinstance(x, dict):
return ns((
(k, normalize(v))
for k,v in sorted(x.items())
))
if isinstance(x, (list, tuple)):
return [i for i in x]
return x

def assertion(data, snapshot=None):
snapshotdir = Path('testdata/snapshots/')
snapshot = snapshot or '.'.join([request.node.module.__name__, request.node.name])
snapshotfile = snapshotdir / (snapshot+'.expected')
resultfile = snapshotdir / (snapshot+'.result')
ns(snapshot=normalize(data)).dump(resultfile)
assert snapshotfile.exists(), (
"First snapshot, check results and accept them with:\n"
"mv {} {}".format(resultfile, snapshotfile)
)
expected = ns.load(snapshotfile)
assert resultfile.read_text(encoding='utf8') == snapshotfile.read_text('utf8'), (
"Failed snapshot. Check the result and if it is ok accept it with:\n"
"mv {} {}".format(resultfile, snapshotfile)
)
# the result is keept if any of the former asserts fails
resultfile.unlink()
return assertion

@pytest.fixture
def api():
Expand Down Expand Up @@ -99,7 +60,7 @@ def scenario_data(mongodb):
})
yield contract, token

def test__scenario_report__with_power(api, scenario_data, snapshot):
def test__scenario_report__with_power(api, scenario_data, yaml_snapshot):
contract, token = scenario_data
r = api.get('/api/ScenarioReport/{}'.format(contract),
query_string=dict(
Expand All @@ -111,9 +72,9 @@ def test__scenario_report__with_power(api, scenario_data, snapshot):
Authorization = 'token {}'.format(token)
),
)
snapshot(r.get_json())
yaml_snapshot(r.get_json())

def test__scenario_report__optimal_payback(api, scenario_data, snapshot):
def test__scenario_report__optimal_payback(api, scenario_data, yaml_snapshot):
contract, token = scenario_data
r = api.get('/api/ScenarioReport/{}'.format(contract),
query_string=dict(
Expand All @@ -124,7 +85,7 @@ def test__scenario_report__optimal_payback(api, scenario_data, snapshot):
Authorization = 'token {}'.format(token)
),
)
snapshot(r.get_json())
yaml_snapshot(r.get_json())

def test__scenario_report__parameter_value_not_found(api, scenario_data):
contract, token = scenario_data
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ raven<5.2.0
blinker
flask-login<0.3.0
flask-cors
yamlns
yamlns>=0.10.0

0 comments on commit 0b2a6ca

Please sign in to comment.