Skip to content

Commit

Permalink
Updated tests in modules.raw to check both variants of 'dynamically_c…
Browse files Browse the repository at this point in the history
…onvert_elements_to_models'
  • Loading branch information
sveinugu committed Aug 13, 2024
1 parent cbcc52d commit 2ffecd6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tests/helpers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def assert_model(model: object, target_type: TypeForm, contents: object):
def assert_val(value: object, target_type: TypeForm, contents: object):
assert not isinstance(value, Model)
assert isinstance(value, ensure_plain_type(target_type))
assert value == contents
assert value == contents, f'{value} != {contents}'


def assert_model_or_val(dyn_convert: bool,
Expand Down
31 changes: 23 additions & 8 deletions tests/modules/raw/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_split_to_and_join_lines_dataset(
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data = """\
Alas, poor Yorick! I knew him, Horatio: a fellow
Expand Down Expand Up @@ -119,8 +120,15 @@ def test_split_to_and_join_lines_dataset(
}


@pytest.mark.parametrize('dyn_convert', [False, True])
@pytest.mark.parametrize('use_str_model', [False, True], ids=['str', 'Model[str]'])
def test_split_to_and_join_items_dataset(use_str_model: bool) -> None:
def test_split_to_and_join_items_dataset(
use_str_model: bool,
runtime: Annotated[IsRuntime, pytest.fixture],
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data_tab_start = 'abc\t def \tghi\tjkl'
data_tab_start = Model[str](raw_data_tab_start) if use_str_model else raw_data_tab_start

Expand All @@ -129,13 +137,13 @@ def test_split_to_and_join_items_dataset(use_str_model: bool) -> None:

items_stripped_tab = SplitToItemsDataset(dict(start=data_tab_start, end=data_tab_end))
assert items_stripped_tab['start'].contents == ['abc', 'def', 'ghi', 'jkl']
assert items_stripped_tab['start'][1].contents == 'def'
assert_model_or_val(dyn_convert, items_stripped_tab['start'][1], str, 'def')
assert items_stripped_tab['end'][-2:].contents == ['vwx', 'yz']

items_unstripped_tab = SplitToItemsDataset(
dict(start=data_tab_start, end=data_tab_end), strip=False)
assert items_unstripped_tab['start'].contents == ['abc', ' def ', 'ghi', 'jkl']
assert items_unstripped_tab['start'][1].contents == ' def '
assert_model_or_val(dyn_convert, items_unstripped_tab['start'][1], str, ' def ')
assert items_unstripped_tab['end'][-2:].contents == ['vwx', 'yz ']

data_comma_start = 'abc, def, ghi, jkl'
Expand All @@ -144,7 +152,7 @@ def test_split_to_and_join_items_dataset(use_str_model: bool) -> None:
items_stripped_comma = SplitToItemsDataset(
dict(start=data_comma_start, end=data_comma_end), delimiter=',')
assert items_stripped_comma['start'].contents == ['abc', 'def', 'ghi', 'jkl']
assert items_stripped_comma['start'][1].contents == 'def'
assert_model_or_val(dyn_convert, items_stripped_comma['start'][1], str, 'def')
assert items_stripped_comma['end'][-2:].contents == ['vwx', 'yz']

for data_file, items in items_stripped_comma.items():
Expand All @@ -162,8 +170,15 @@ def test_split_to_and_join_items_dataset(use_str_model: bool) -> None:
assert comma_space_joined_items['end'][1:-1].contents == 'qr, st'


@pytest.mark.parametrize('dyn_convert', [False, True])
@pytest.mark.parametrize('use_str_model', [False, True], ids=['str', 'Model[str]'])
def test_split_lines_to_columns_and_join_columns_to_lines_dataset(use_str_model: bool) -> None:
def test_split_lines_to_columns_and_join_columns_to_lines_dataset(
use_str_model: bool,
runtime: Annotated[IsRuntime, pytest.fixture],
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data_tab_fw = ['abc\t def \tghi\t jkl', 'mno\t pqr\tstu\t vwx', 'yz']
data_tab_fw = [Model[str](_) for _ in raw_data_tab_fw] if use_str_model else raw_data_tab_fw

Expand All @@ -172,7 +187,7 @@ def test_split_lines_to_columns_and_join_columns_to_lines_dataset(use_str_model:

cols_stripped_tab = SplitLinesToColumnsDataset(dict(forward=data_tab_fw, reverse=data_tab_rev))
assert cols_stripped_tab['forward'][0].contents == ['abc', 'def', 'ghi', 'jkl']
assert cols_stripped_tab['forward'][0][1].contents == 'def'
assert_model_or_val(dyn_convert, cols_stripped_tab['forward'][0][1], str, 'def')
assert cols_stripped_tab['reverse'][1:2].contents \
== [SplitToItemsModel(' nml\t kji\thgf\t edc')]
assert cols_stripped_tab['reverse'][1:].to_data() \
Expand All @@ -181,7 +196,7 @@ def test_split_lines_to_columns_and_join_columns_to_lines_dataset(use_str_model:
cols_unstripped_tab = SplitLinesToColumnsDataset(
dict(forward=data_tab_fw, reverse=data_tab_rev), strip=False)
assert cols_unstripped_tab['forward'][0].contents == ['abc', ' def ', 'ghi', ' jkl']
assert cols_unstripped_tab['forward'][0][1].contents == ' def '
assert_model_or_val(dyn_convert, cols_unstripped_tab['forward'][0][1], str, ' def ')
assert cols_unstripped_tab['reverse'][1:2].contents \
== [SplitToItemsModel(' nml\t kji\thgf\t edc', strip=False)]
assert cols_unstripped_tab['reverse'][1:].to_data() \
Expand All @@ -198,7 +213,7 @@ def test_split_lines_to_columns_and_join_columns_to_lines_dataset(use_str_model:
cols_stripped_comma = SplitLinesToColumnsDataset(
dict(forward=data_comma_fw, reverse=data_comma_rev), delimiter=',')
assert cols_stripped_comma['forward'][0].contents == ['abc', 'def', 'ghi', 'jkl']
assert cols_stripped_comma['forward'][0][1].contents == 'def'
assert_model_or_val(dyn_convert, cols_stripped_comma['forward'][0][1], str, 'def')
assert cols_stripped_comma['reverse'][1:2].contents == [
SplitToItemsModel('nml, kji, hgf, edc', delimiter=',')
]
Expand Down
67 changes: 52 additions & 15 deletions tests/modules/raw/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
from textwrap import dedent
from typing import Annotated

from pydantic import ValidationError
import pytest

from omnipy.api.protocols.public.hub import IsRuntime
from omnipy.data.model import Model
from omnipy.modules.raw.models import (BytesModel,
JoinColumnsToLinesModel,
Expand All @@ -14,6 +16,8 @@
SplitToLinesModel,
StrModel)

from ...helpers.functions import assert_model_or_val # type: ignore[misc]


def test_bytes_model():
assert BytesModel(b'').contents == b''
Expand Down Expand Up @@ -43,8 +47,15 @@ def test_str_model():
StrModel(b'\xe6\xf8\xe5', encoding='my-encoding')


@pytest.mark.parametrize('dyn_convert', [False, True])
@pytest.mark.parametrize('use_str_model', [False, True], ids=['str', 'Model[str]'])
def test_split_to_and_join_lines_model(use_str_model: bool) -> None:
def test_split_to_and_join_lines_model(
use_str_model: bool,
runtime: Annotated[IsRuntime, pytest.fixture],
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data = """\
Alas, poor Yorick! I knew him, Horatio: a fellow
Expand All @@ -61,48 +72,66 @@ def test_split_to_and_join_lines_model(use_str_model: bool) -> None:
data = Model[str](raw_data) if use_str_model else raw_data

lines_stripped = SplitToLinesModel(data)
assert lines_stripped[0].contents == ( # type: ignore[index]
assert_model_or_val(
dyn_convert,
lines_stripped[0], # type: ignore[index]
str,
'Alas, poor Yorick! I knew him, Horatio: a fellow')

lines_unstripped = SplitToLinesModel(data, strip=False)
assert lines_unstripped[0].contents == ' ' # type: ignore[index]
assert lines_unstripped[1].contents == ( # type: ignore
assert_model_or_val(dyn_convert, lines_unstripped[0], str, ' ') # type: ignore[index]
assert_model_or_val(
dyn_convert,
lines_unstripped[1], # type: ignore[index]
str,
' Alas, poor Yorick! I knew him, Horatio: a fellow')
assert lines_unstripped[-1].contents == ' ' # type: ignore[index]
assert_model_or_val(dyn_convert, lines_unstripped[-1], str, ' ') # type: ignore[index]

last_lines = lines_stripped[3:] # type: ignore[index]
assert last_lines[0:2].contents == [
'abhorred in my imagination it is! my gorge rises at',
'it. Here hung those lips that I have kissed I know'
]
assert last_lines[-1].contents == 'now, to mock your own grinning? quite chap-fallen.'
assert_model_or_val(
dyn_convert,
last_lines[-1], # type: ignore[index]
str,
'now, to mock your own grinning? quite chap-fallen.')

joined_lines = JoinLinesModel(last_lines[0:2])
assert joined_lines.contents == dedent("""\
abhorred in my imagination it is! my gorge rises at
it. Here hung those lips that I have kissed I know""")

assert joined_lines[:joined_lines.index(' ')].contents == 'abhorred' # type: ignore[index]

assert JoinLinesModel(SplitToLinesModel(data)).contents == os.linesep.join(
[line.strip() for line in data.strip().split(os.linesep)]) # type: ignore[attr-defined]

assert JoinLinesModel(SplitToLinesModel(data, strip=False)).contents == raw_data


@pytest.mark.parametrize('dyn_convert', [False, True])
@pytest.mark.parametrize('use_str_model', [False, True], ids=['str', 'Model[str]'])
def test_split_to_and_join_items_model(use_str_model: bool) -> None:
def test_split_to_and_join_items_model(
use_str_model: bool,
runtime: Annotated[IsRuntime, pytest.fixture],
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data_tab = 'abc\t def \tghi\tjkl'

data_tab = Model[str](raw_data_tab) if use_str_model else raw_data_tab

items_stripped_tab = SplitToItemsModel(data_tab)
assert items_stripped_tab.contents == ['abc', 'def', 'ghi', 'jkl']
assert items_stripped_tab[1].contents == 'def' # type: ignore[index]

assert_model_or_val(dyn_convert, items_stripped_tab[1], str, 'def') # type: ignore[index]
assert items_stripped_tab[-2:].contents == ['ghi', 'jkl'] # type: ignore[index]

items_unstripped_tab = SplitToItemsModel(data_tab, strip=False)
assert items_unstripped_tab.contents == ['abc', ' def ', 'ghi', 'jkl']
assert items_unstripped_tab[1].contents == ' def ' # type: ignore[index]
assert_model_or_val(dyn_convert, items_unstripped_tab[1], str, ' def ') # type: ignore[index]
assert items_unstripped_tab[-2:].contents == ['ghi', 'jkl'] # type: ignore[index]

raw_data_comma = 'abc, def, ghi, jkl'
Expand All @@ -111,7 +140,7 @@ def test_split_to_and_join_items_model(use_str_model: bool) -> None:

items_stripped_comma = SplitToItemsModel(data_comma, delimiter=',')
assert items_stripped_comma.contents == ['abc', 'def', 'ghi', 'jkl']
assert items_stripped_comma[1].contents == 'def' # type: ignore[index]
assert_model_or_val(dyn_convert, items_stripped_comma[1], str, 'def') # type: ignore[index]
assert items_stripped_comma[-2:].contents == ['ghi', 'jkl'] # type: ignore[index]

tab_joined_items = JoinItemsModel(items_stripped_tab[1:3]) # type: ignore[index]
Expand All @@ -124,14 +153,21 @@ def test_split_to_and_join_items_model(use_str_model: bool) -> None:
assert comma_space_joined_items[1:-1].contents == 'ef, gh' # type: ignore[index]


@pytest.mark.parametrize('dyn_convert', [False, True])
@pytest.mark.parametrize('use_str_model', [False, True], ids=['str', 'Model[str]'])
def test_split_lines_to_columns_and_join_columns_to_lines_model(use_str_model: bool) -> None:
def test_split_lines_to_columns_and_join_columns_to_lines_model(
use_str_model: bool,
runtime: Annotated[IsRuntime, pytest.fixture],
dyn_convert: bool,
) -> None:
runtime.config.data.dynamically_convert_elements_to_models = dyn_convert

raw_data_tab = ['abc\t def \tghi\t jkl', 'mno\t pqr\tstu\t vwx', 'yz']
data_tab = [Model[str](line) for line in raw_data_tab] if use_str_model else raw_data_tab

cols_stripped_tab = SplitLinesToColumnsModel(data_tab)
assert cols_stripped_tab[0].contents == ['abc', 'def', 'ghi', 'jkl'] # type: ignore[index]
assert cols_stripped_tab[0][1].contents == 'def' # type: ignore[index]
assert_model_or_val(dyn_convert, cols_stripped_tab[0][1], str, 'def') # type: ignore[index]
assert cols_stripped_tab[1:2].contents == [ # type: ignore[index]
SplitToItemsModel('mno\t pqr\tstu\t vwx')
]
Expand All @@ -141,7 +177,7 @@ def test_split_lines_to_columns_and_join_columns_to_lines_model(use_str_model: b

cols_unstripped_tab = SplitLinesToColumnsModel(data_tab, strip=False)
assert cols_unstripped_tab[0].contents == ['abc', ' def ', 'ghi', ' jkl'] # type: ignore[index]
assert cols_unstripped_tab[0][1].contents == ' def ' # type: ignore[index]
assert_model_or_val(dyn_convert, cols_unstripped_tab[0][1], str, ' def ') # type: ignore[index]
assert cols_unstripped_tab[1:2].contents == [ # type: ignore[index]
SplitToItemsModel('mno\t pqr\tstu\t vwx', strip=False)
]
Expand All @@ -157,7 +193,8 @@ def test_split_lines_to_columns_and_join_columns_to_lines_model(use_str_model: b

cols_stripped_comma = SplitLinesToColumnsModel(data_comma, delimiter=',')
assert cols_stripped_comma[0].contents == ['abc', 'def', 'ghi', 'jkl'] # type: ignore[index]
assert cols_stripped_comma[0][1].contents == 'def' # type: ignore[index]
assert_model_or_val(dyn_convert, cols_stripped_comma[0][1], str, 'def') # type: ignore[index]

assert cols_stripped_comma[1:2].contents == [ # type: ignore[index]
SplitToItemsModel('mno, pqr, stu, vwx', delimiter=',')
]
Expand Down

0 comments on commit 2ffecd6

Please sign in to comment.