def test_save_and_load(): path = "save_and_load_test.json" z1 = Zipf({"one": 0.2, "two": 0.25, "three": 0.6}) z1.save(path) z2 = Zipf.load(path) os.remove(path) assert z1 == z2
def test_ksequence_factory(): errors = [] try: ZipfFromKSequence(-3) errors.append("ZipfFromKSequence should fail with k less than zero") except Exception as e: pass k = 5 factory = ZipfFromKSequence(k) current_path = os.path.dirname(__file__)+"/factory_utils" zipf = Zipf.load( current_path+"/expected_results/sequence.json").sort().round() with open(current_path+"/sequence/sequence.txt", "r") as f: sequence = f.read() factory_run = factory.run(sequence).round() if factory_run != zipf: errors.append( "Sequence zipf run is different than expected: %s != %s" % (zipf, factory_run)) assert not errors, "errors occured:\n{}".format("\n".join(errors))
def factory_fails(Factory, path, prepare=None, run=None): if prepare is None: prepare = Factory if run is None: def run(factory, data): return factory.run(data) current_path = os.path.dirname(__file__) errors = factory_break_options(Factory) global _options_for_tests tests = ["default", "empty"] + list(_options_for_tests.keys()) for test in tests: data_path_name = map_test_to_data(test) data_path_json = os.path.join(current_path, "%s/%s.json" % (path, data_path_name)) data_path_text = os.path.join(current_path, "%s/%s.txt" % (path, data_path_name)) result_path = os.path.join(current_path, "expected_results/%s.json" % test) if os.path.isfile(data_path_json): with open(data_path_json, "r") as f: data = json.load(f) elif os.path.isfile(data_path_text): with open(data_path_text, "r") as f: data = f.read() else: pytest.skip( "While testing %s, data for testing '%s' was not found in %s or %s." % (Factory.__name__, test, data_path_text, data_path_json)) if not os.path.isfile(result_path): pytest.skip( "While testing %s, result for testing '%s' was not found in %s." % (Factory.__name__, test, result_path)) result = Zipf.load(result_path).sort().round() factory = prepare(options=_get_options_for(test)) factory_run = run(factory, data).sort().round() if result != factory_run: errors.append( "%s has not expected result on run test '%s': %s != %s" % (Factory.__name__, test, result, factory_run)) return errors