Skip to content

Commit

Permalink
chore: More tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Jun 29, 2024
1 parent 11b9af0 commit aebe122
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/harfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _write_entry(
connection: str | None = None,
comment: str | None = None,
) -> None:
separator = "" if self._is_first_entry else ",\n"
separator = "\n" if self._is_first_entry else ",\n"
self._is_first_entry = False
self._fd.write(f"{separator} {{")
self._fd.write(f'\n "startedDateTime": "{startedDateTime.isoformat()}",')
Expand All @@ -170,26 +170,24 @@ def _write_entry(
def _write_preamble(self) -> None:
creator = f"""{{
"name": "{self._creator.name}",
"version": "{self._creator.version}"
"""
"version": "{self._creator.version}\""""
if self._creator.comment:
creator += f',\n "comment": "{self._creator.comment}"\n}}'
creator += "}"
creator += f',\n "comment": "{self._creator.comment}"'
creator += "\n }"
browser = f"""{{
"name": "{self._browser.name}",
"version": "{self._browser.version}"
"""
"version": "{self._browser.version}\""""
if self._browser.comment:
browser += f',\n "comment": "{self._browser.comment}"\n}}'
browser += "}"
browser += f',\n "comment": "{self._browser.comment}"'
browser += "\n }"
self._fd.write(f"""{{
"log": {{
"version": "{HAR_VERSION}",
"creator": {creator},
"browser": {browser}""")
if self._comment:
self._fd.write(f' "comment": "{self._comment}"')
self._fd.write(',\n "entries": [\n')
self._fd.write(',\n "entries": [')

def _write_postscript(self) -> None:
if self._is_first_entry:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_har.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

import jsonschema
import pytest
from hypothesis import HealthCheck, Phase, given, settings
from hypothesis import strategies as st

Expand Down Expand Up @@ -424,3 +425,46 @@ def test_write_har(entries, tmp_path):
write_har(path, entries)
with open(path) as fd:
HAR_VALIDATOR.validate(json.load(fd))


def test_close():
buffer = io.StringIO()
with harfile.open(buffer) as har:
har.close()


def test_exception():
buffer = io.StringIO()
with pytest.raises(ZeroDivisionError):
with harfile.open(buffer):
raise ZeroDivisionError
assert buffer.getvalue() == ""


def test_with_comments():
buffer = io.StringIO()
with harfile.open(
buffer,
creator=harfile.Creator(name="test", version="0.1", comment="EXAMPLE-1"),
browser=harfile.Browser(name="test", version="0.2", comment="EXAMPLE-2"),
):
pass
assert (
buffer.getvalue()
== """{
"log": {
"version": "1.2",
"creator": {
"name": "test",
"version": "0.1",
"comment": "EXAMPLE-1"
},
"browser": {
"name": "test",
"version": "0.2",
"comment": "EXAMPLE-2"
},
"entries": []
}
}"""
)

0 comments on commit aebe122

Please sign in to comment.