def test_file_reporter_publish_invalid_namespace(namespace, tmpdir): tmpdir.chdir() reporter = FileReporter(os.getcwd()) with pytest.raises(ValueError) as err_info: reporter.publish(namespace, "") assert "Namespace contains path separators" in str(err_info.value)
def test_file_reporter_publish_invalid_json(tmpdir): tmpdir.chdir() namespace = "data" data = json # The json module is not JSON serializable... reporter = FileReporter(os.getcwd()) with pytest.raises(TypeError): reporter.publish(namespace, data)
def test_file_reporter_publish_valid_json(data, tmpdir): tmpdir.chdir() namespace = "data" reporter = FileReporter(os.getcwd()) reporter.publish(namespace, data) with open(namespace + ".json") as f: loaded_data = json.load(f) assert loaded_data == data
def test_file_reporter_publish_invalid_output_dir(tmpdir): tmpdir.chdir() output_dir = os.path.join(os.getcwd(), "output") with open(output_dir, "w") as f: f.write("You shall not be a directory") reporter = FileReporter(output_dir) with pytest.raises(ValueError) as err_info: reporter.publish("namespace", "") assert "Expected output_dir to be a directory" in str(err_info.value)
def test_file_reporter_publish_multiple_json(tmpdir): tmpdir.chdir() namespace = "some_data" reporter = FileReporter(os.getcwd()) for data in [0, [0, 12], "Dear JSON"]: reporter.publish(namespace, data) with open(namespace + ".json") as f: loaded_data = json.load(f) assert loaded_data == data
def test_file_reporter_publisg_output_location(folder, namespace, tmpdir): tmpdir.chdir() reporter = FileReporter(os.path.join(os.getcwd(), folder)) data = [1, 2, 1, 2, "Testing the most arbitrary data there is"] reporter.publish(namespace, data) expected_output_file = os.path.join(os.getcwd(), folder, namespace) + ".json" with open(expected_output_file) as f: loaded_data = json.load(f) assert data == loaded_data
def test_file_reporter_publish_multiple_json(tmpdir): tmpdir.chdir() namespace = "some_data" reporter = FileReporter(os.getcwd()) data = [0, [0, 12], "Dear JSON"] for idx, data_elem in enumerate(data): reporter.publish(namespace, data_elem) with open(namespace + ".json") as f: loaded_data = json.load(f) assert loaded_data == data[:idx + 1]
def test_file_reporter_publish_multiple_namespaces(tmpdir): tmpdir.chdir() namespace1 = "namespace1" namespace2 = "namespace2" data1 = "This is message number 1" data2 = "This is yet another message" reporter = FileReporter(os.getcwd()) reporter.publish(namespace1, data1) reporter.publish(namespace2, data2) with open(namespace1 + ".json") as f: loaded_data1 = json.load(f) assert loaded_data1 == data1 with open(namespace2 + ".json") as f: loaded_data2 = json.load(f) assert loaded_data2 == data2