Skip to content

Commit

Permalink
Tests: fix type issues with dialect kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
MKuranowski committed Apr 28, 2024
1 parent 0f937c5 commit 23968d5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions tests/test_dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aiocsv import AsyncReader, AsyncWriter

FILENAME = "tests/eu_cities_unix.csv"
PARAMS = {"dialect": "unix"}
DIALECT = "unix"
VALUES = [
["Berlin", "Germany"],
["Madrid", "Spain"],
Expand All @@ -20,7 +20,7 @@
@pytest.mark.asyncio
async def test_dialect_read():
async with aiofiles.open(FILENAME, mode="r", encoding="ascii", newline="") as afp:
read_rows = [i async for i in AsyncReader(afp, **PARAMS)]
read_rows = [i async for i in AsyncReader(afp, dialect=DIALECT)]
assert read_rows == VALUES


Expand All @@ -33,7 +33,7 @@ async def test_dialect_write():
try:
# Write rows
async with aiofiles.open(target_name, mode="w", encoding="ascii", newline="") as afp:
writer = AsyncWriter(afp, **PARAMS)
writer = AsyncWriter(afp, dialect=DIALECT)
await writer.writerow(VALUES[0])
await writer.writerows(VALUES[1:])

Expand Down
11 changes: 6 additions & 5 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import pytest

from aiocsv import AsyncDictReader, AsyncDictWriter
from aiocsv.protocols import CsvDialectKwargs

FILENAME = "tests/metro_systems.tsv"
PARAMS = {"delimiter": "\t", "quotechar": "'", "quoting": csv.QUOTE_ALL}
DIALECT_PARAMS: CsvDialectKwargs = {"delimiter": "\t", "quotechar": "'", "quoting": csv.QUOTE_ALL}
HEADER = ["City", "Stations", "System Length"]
VALUES = [
dict(zip(HEADER, i))
Expand All @@ -26,22 +27,22 @@
@pytest.mark.asyncio
async def test_dict_read():
async with aiofiles.open(FILENAME, mode="r", encoding="ascii", newline="") as afp:
read_rows = [i async for i in AsyncDictReader(afp, **PARAMS)]
read_rows = [i async for i in AsyncDictReader(afp, **DIALECT_PARAMS)]
assert read_rows == VALUES


@pytest.mark.asyncio
async def test_dict_read_line_nums():
async with aiofiles.open(FILENAME, mode="r", encoding="ascii", newline="") as afp:
r = AsyncDictReader(afp, **PARAMS)
r = AsyncDictReader(afp, **DIALECT_PARAMS)
read_rows = [(row, r.line_num) async for row in r]
assert read_rows == [(row, i) for i, row in enumerate(VALUES, start=2)]


@pytest.mark.asyncio
async def test_dict_read_get_fieldnames():
async with aiofiles.open(FILENAME, mode="r", encoding="ascii", newline="") as afp:
reader = AsyncDictReader(afp, **PARAMS)
reader = AsyncDictReader(afp, **DIALECT_PARAMS)

assert reader.fieldnames is None
assert await reader.get_fieldnames() == ["City", "Stations", "System Length"]
Expand All @@ -57,7 +58,7 @@ async def test_dict_write():
try:
# Write rows
async with aiofiles.open(target_name, mode="w", encoding="ascii", newline="") as afp:
writer = AsyncDictWriter(afp, HEADER, **PARAMS)
writer = AsyncDictWriter(afp, HEADER, **DIALECT_PARAMS)
await writer.writeheader()
await writer.writerow(VALUES[0])
await writer.writerows(VALUES[1:])
Expand Down
3 changes: 2 additions & 1 deletion tests/test_newlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import pytest

from aiocsv import AsyncDictReader, AsyncReader
from aiocsv.protocols import CsvDialectKwargs

DIALECT_PARAMS = {"escapechar": "$", "lineterminator": "\n"}
DIALECT_PARAMS: CsvDialectKwargs = {"escapechar": "$", "lineterminator": "\n"}
FILENAME = "tests/newlines.csv"
HEADER = ["field1", "field2", "field3"]
READ_VALUES = [
Expand Down

0 comments on commit 23968d5

Please sign in to comment.