Skip to content

Commit

Permalink
Closing open files in finally
Browse files Browse the repository at this point in the history
  • Loading branch information
boffman committed Oct 23, 2024
1 parent 63cb0a4 commit d365038
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions tests/unit/test_grizzly/testdata/variables/test_csv_writer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Unit tests for grizzly.testdata.variable.csv_writer."""
from __future__ import annotations

from contextlib import suppress
from typing import TYPE_CHECKING

import pytest

from grizzly.testdata.variables.csv_writer import AtomicCsvWriter, atomiccsvwriter__base_type__, atomiccsvwriter_message_handler
from grizzly.testdata.variables.csv_writer import AtomicCsvWriter, atomiccsvwriter__base_type__, atomiccsvwriter_message_handler, open_files
from grizzly.types.locust import Message

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -39,37 +40,41 @@ def test_atomiccsvwriter__base_type__(grizzly_fixture: GrizzlyFixture) -> None:


def test_atomiccsvwriter_message_handler(grizzly_fixture: GrizzlyFixture) -> None:
parent = grizzly_fixture()
try:
parent = grizzly_fixture()

destination_file = grizzly_fixture.test_context / 'requests' / 'foobar.csv'
destination_file = grizzly_fixture.test_context / 'requests' / 'foobar.csv'

assert not destination_file.exists()
assert not destination_file.exists()

message = Message('atomiccsvwriter', data={
'destination': 'foobar.csv',
'row': {
'foo': 'hello',
'bar': 'world!',
},
}, node_id=None)
message = Message('atomiccsvwriter', data={
'destination': 'foobar.csv',
'row': {
'foo': 'hello',
'bar': 'world!',
},
}, node_id=None)

atomiccsvwriter_message_handler(parent.user.environment, message)
atomiccsvwriter_message_handler(parent.user.environment, message)

assert destination_file.exists()
assert destination_file.read_text() == 'foo,bar\nhello,world!\n'
assert destination_file.exists()
assert destination_file.read_text() == 'foo,bar\nhello,world!\n'

message = Message('atomiccsvwriter', data={
'destination': 'foobar.csv',
'row': {
'foo': 'bar',
'bar': 'foo',
},
}, node_id=None)
message = Message('atomiccsvwriter', data={
'destination': 'foobar.csv',
'row': {
'foo': 'bar',
'bar': 'foo',
},
}, node_id=None)

atomiccsvwriter_message_handler(parent.user.environment, message)

assert destination_file.read_text() == 'foo,bar\nhello,world!\nbar,foo\n'
atomiccsvwriter_message_handler(parent.user.environment, message)

assert destination_file.read_text() == 'foo,bar\nhello,world!\nbar,foo\n'
finally:
for open_file in open_files.values():
with suppress(Exception):
open_file.close()

class TestAtomicCsvWriter:
def test___init__(self, grizzly_fixture: GrizzlyFixture, cleanup: AtomicVariableCleanupFixture) -> None:
Expand Down

0 comments on commit d365038

Please sign in to comment.