This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate valid json if multiple log entries are generated on same line
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,24 @@ def single_log_file(): | |
return file | ||
|
||
|
||
@pytest.fixture | ||
def log_on_same_line(): | ||
"""Create a log file with multiple entries on same line.""" | ||
file = tempfile.NamedTemporaryFile(mode="w") | ||
file.write('{"message": 1}{"message": 2}{"message": 3}') | ||
file.seek(0) | ||
return file | ||
|
||
|
||
@pytest.fixture | ||
def log_comma_separation_same_line(): | ||
"""Create a log file with multiple entries on same line, comma separated.""" | ||
file = tempfile.NamedTemporaryFile(mode="w") | ||
file.write('{"message": 1},{"message": 2},{"message": 3}') | ||
file.seek(0) | ||
return file | ||
|
||
|
||
@pytest.fixture | ||
def empty_log_file(): | ||
"""Create a log file without any entries.""" | ||
|
@@ -110,3 +128,41 @@ def test_single_logfile(single_log_file): | |
else: | ||
assert len(json_log["logs"]) == 1 | ||
assert json_log["logs"][0]["from"] == "[email protected]" | ||
|
||
|
||
def test_logfile_log_entries_on_same_line(log_on_same_line): | ||
"""Test with single log.""" | ||
output = "" | ||
with open(log_on_same_line.name) as file: | ||
for line in wrap_json_output(file): | ||
output += line | ||
|
||
try: | ||
json_log = json.loads(output) | ||
except ValueError: | ||
assert False, "Invalid json generated." | ||
else: | ||
assert len(json_log["logs"]) == 3 | ||
assert json_log["logs"][0]["message"] == 1 | ||
assert json_log["logs"][1]["message"] == 2 | ||
assert json_log["logs"][2]["message"] == 3 | ||
|
||
|
||
def test_logfile_log_entries_on_same_line_with_comma_separation( | ||
log_comma_separation_same_line | ||
): | ||
"""Test with single log.""" | ||
output = "" | ||
with open(log_comma_separation_same_line.name) as file: | ||
for line in wrap_json_output(file): | ||
output += line | ||
|
||
try: | ||
json_log = json.loads(output) | ||
except ValueError: | ||
assert False, "Invalid json generated." | ||
else: | ||
assert len(json_log["logs"]) == 3 | ||
assert json_log["logs"][0]["message"] == 1 | ||
assert json_log["logs"][1]["message"] == 2 | ||
assert json_log["logs"][2]["message"] == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters