Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests fail, not print #312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Action on mismatch. One of 'Ignore', 'Report' or 'Fail'
# If 'Fail', the expected file will be saved in the appropriate temp directory
# NOTE: Before setting this back to Report or Ignore, you need to run cleartemp.sh in this directory
DEFAULT_MISMATCH_ACTION = eval(test_settings.get('DEFAULT_MISMATCH_ACTION', 'MismatchAction.Report'))
DEFAULT_MISMATCH_ACTION = eval(test_settings.get('DEFAULT_MISMATCH_ACTION', 'MismatchAction.Fail'))

# Use local import map. If True, tests/input/local_import_map.json is used to create the test files. Note that this
# will result in local path names being recorded in jsonld files. This should always be set to False before generating
Expand Down
11 changes: 7 additions & 4 deletions tests/support/clicktestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ def closein_comparison(expected_txt: str, actual_txt: str) -> None:
nt = nt[window:]
ot = ot[window:]
offset = max(offset-view, 0)
print(" - - EXPECTED - -")
print(ow[offset:offset+view+view])
print("\n - - ACTUAL - -")
print(nw[offset:offset+view+view])
msg = '\n'.join([
" - - EXPECTED - -",
str(ow[offset:offset+view+view]),
" - - ACTUAL - -",
str(nw[offset:offset+view+view])
])
raise ValueError(msg)

def do_test(self,
args: Union[str, List[str]],
Expand Down
13 changes: 8 additions & 5 deletions tests/support/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, filedir: str) -> None:
@staticmethod
def _check_changed(test_file: str, runtime_file: str) -> None:
if not filecmp.cmp(test_file, runtime_file):
print(
raise Exception(
f"WARNING: Test file {test_file} does not match {runtime_file}. "
f"You may want to update the test version and rerun")
from tests import USE_LOCAL_IMPORT_MAP
Expand Down Expand Up @@ -112,7 +112,7 @@ def fail_on_error(self) -> bool:

@property
def report_errors(self) -> bool:
return self.mismatch_action != MismatchAction.Ignore
return self.mismatch_action == MismatchAction.Report

def __str__(self):
""" Return the current state of the log file """
Expand Down Expand Up @@ -175,10 +175,12 @@ def generate_directory(self, dirname: Union[str, List[str]], generator: Callable

diffs = are_dir_trees_equal(expected_output_directory, temp_output_directory)
if diffs:
self.log(expected_output_directory, diffs)
if not self.fail_on_error:
self.log(expected_output_directory, diffs)
shutil.rmtree(expected_output_directory)
os.rename(temp_output_directory, expected_output_directory)
else:
raise Exception(f'Diffs found in {expected_output_directory}:\n{diffs}')
else:
shutil.rmtree(temp_output_directory)

Expand Down Expand Up @@ -237,12 +239,13 @@ def eval_single_file(self, expected_file_path: str, actual_text: str, filtr: Cal
cmsg = comparator(actual_text, actual_text)
if cmsg:
msg = msg + '\n' + cmsg
if msg:
self.log(expected_file_path, msg)

if msg and not self.fail_on_error:
self.make_temp_dir(os.path.dirname(expected_file_path), clear=False)
with open(expected_file_path, 'w', encoding='UTF-8') as outf:
outf.write(actual_text)
elif msg is not None:
raise Exception(msg)
return not msg


Expand Down
3 changes: 0 additions & 3 deletions tests/test_issues/test_issue_368_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def test_issue_368_enums(self):
def dump_and_load(dumper: Callable, sfx: str) -> None:
fname = env.actual_path(f'issue_368_1.{sfx}')
dumper(example, fname)
with open(fname) as f:
print(f'\n----- {sfx} -----')
print(f.read())

dump_and_load(json_dumper.dump, 'json')
dump_and_load(yaml_dumper.dump, 'yaml')
Expand Down
4 changes: 0 additions & 4 deletions tests/test_issues/test_linkml_issue_478.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ class Issue478TestCase(TestCase):

def test_issue_478(self):
view = SchemaView(env.input_path('linkml_issue_478.yaml'))
for c in view.all_classes():
print(f'c={c}')
cnames = list(view.class_name_mappings().keys())
snames = list(view.slot_name_mappings().keys())
print(cnames)
print(snames)
cnames.remove("5'Sequencing") ## TODO
self.assertCountEqual(['FooBar', 'NamedThing', 'BiosampleProcessing', 'TooMuchWhitespace'], cnames)
snames.remove("5'_sequence") ## TODO
Expand Down
2 changes: 0 additions & 2 deletions tests/test_issues/test_linkml_issue_576.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def test_issue_no_namespace(self):
s = rdflib_dumper.dumps(inst, view, 'turtle', prefix_map={"@base": "http://example.org/default/"})
self.assertIn("@base <http://example.org/default/> .", s)
g = rdflib.Graph().parse(data=s, format='turtle')
for t in g.triples((None, None, None)):
print(t)
cases = [
(None,
rdflib.term.URIRef('https://w3id.org/linkml/personinfo/source'),
Expand Down
7 changes: 0 additions & 7 deletions tests/test_loaders_dumpers/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ def test_enum(self):
* https://github.com/linkml/linkml/issues/119
"""
i = Organism(state='LIVING')
print(i)
print(i.state)
print(i.state.code)
print(i.state.code.text)
print(type(i.state))
print(StateEnum.LIVING)
assert str(i.state) == 'LIVING'
assert i.state.code == StateEnum.LIVING
obj = json.loads(json_dumper.dumps(i))
assert obj['state'] == 'LIVING'
obj = yaml.safe_load(yaml_dumper.dumps(i))
assert obj['state'] == 'LIVING'
reconstituted = json_loader.loads(json_dumper.dumps(i), target_class=Organism)
print(f'RECONSTITUTED = {reconstituted}')
assert reconstituted.state.code == StateEnum.LIVING


Expand Down
5 changes: 0 additions & 5 deletions tests/test_loaders_dumpers/test_rdflib_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,13 @@ def test_enums(self):
assert not org1type1.meaning
assert org1type2.meaning
org1 = Organization('ROR:1', categories=[org1type1, org1type2])
print(org1.categories)
g = rdflib_dumper.as_rdf_graph(org1, schemaview=view, prefix_map=self.prefix_map)
print(g)
cats = list(g.objects(ROR['1'], INFO['categories']))
print(cats)
self.assertCountEqual([Literal('non profit'), URIRef('https://example.org/bizcodes/001')], cats)
orgs = rdflib_loader.from_rdf_graph(g, target_class=Organization, schemaview=view)
assert len(orgs) == 1
[org1x] = orgs
catsx = org1x.categories
print(catsx)
self.assertCountEqual([org1type1, org1type2], catsx)

def test_undeclared_prefix(self):
Expand Down Expand Up @@ -245,7 +241,6 @@ def test_blank_node(self):
ignore_unmapped_predicates=True)
self.assertEqual(address.city, 'foo city')
ttl = rdflib_dumper.dumps(address, schemaview=view)
print(ttl)
g = Graph()
g.parse(data=ttl, format='ttl')
INFO = Namespace('https://w3id.org/linkml/examples/personinfo/')
Expand Down
5 changes: 0 additions & 5 deletions tests/test_processing/test_referencevalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def setUpClass(cls):

Sets up a document object that each test will contribute to.
"""
print("Setting up class...")
doc = MarkdownDocument()
doc.h1("Validation Suite")
doc.text("This document describes the validation suite for the LinkML model.")
Expand Down Expand Up @@ -1154,8 +1153,6 @@ def test_line_number(self):
"""
obj = yaml.load(s, DupCheckYamlLoader)
report = validator.validate(obj)
for r in report.results_excluding_normalized():
print(yaml_dumper.dumps(r))
self.assertEqual(1, len(report.results_excluding_normalized()))
r = report.results_excluding_normalized()[0]
self.assertEqual(3, r.source_line_number)
Expand Down Expand Up @@ -1188,8 +1185,6 @@ def test_examples_against_metamodel(self):
report = Report()
schema_norm = validator.normalize(schema_dict, target=sdc.name, report=report)
self.assertEqual(dict, type(schema_norm["prefixes"]))
for r in report.results_excluding_normalized():
print(yaml_dumper.dumps(r))
self.assertEqual(len(report.errors()), 0)
# for r in report.repaired():
# print(r)
Expand Down
8 changes: 0 additions & 8 deletions tests/test_utils/test_dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,26 @@ def test_as_dict(self):
assert d['name'] == 'test class'
assert d['id_prefixes'] == []
assert d['description'] is None
for x in _signature(d):
if not _is_python_type(x):
print(f' ****={x} {type(x)}')
assert all(_is_basic_type(x) for x in _signature(d))

d2 = yutils.as_dict(obj2)
print(d2)
assert d2['slot_usage']['foo']['range'] == 'bar'
assert all(_is_basic_type(x) for x in _signature(d2))

# as_simple_dict removes nones and empty lists
d = as_simple_dict(obj)
print(d)
assert isinstance(d, dict)
assert all(_is_python_type(x) for x in _signature(d))
assert d == {'name': 'test class'}

d2 = as_simple_dict(obj2)
print(d2)
assert isinstance(d2, dict)
assert d2 == {'name': 'test class', 'slot_usage': {'foo': {'name': 'foo', 'range': 'bar'}}}

s = yutils.as_yaml(obj)
print(s)
assert(s.strip() == 'name: test class')

s2 = yutils.as_yaml(obj2)
print(s2)
assert(s2.strip().startswith('name: test class'))


Expand Down
6 changes: 0 additions & 6 deletions tests/test_utils/test_ruleutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@ def test_disjunction(self):
disj = get_range_as_disjunction(analyte)
#print(disj)
self.assertCountEqual(disj, {'MissingValueEnum', 'AnalyteEnum'})
for s in view.all_slots().values():
disj = get_range_as_disjunction(s)
print(f'{s.name} DISJ: {disj}')

def test_roll_up(self):
# no import schema
view = SchemaView(SCHEMA)
c = view.get_class('ProteinCodingGene')
rules = subclass_to_rules(view, 'ProteinCodingGene', 'SeqFeature')
rule = rules[0]
print(f'IF: {rule.preconditions}')
print(f'THEN: {rule.postconditions}')
print(yaml_dumper.dumps(rule))



Expand Down