Skip to content

Commit

Permalink
fix/change: migrate test cases for anyconfig.utils from unittest to p…
Browse files Browse the repository at this point in the history
…ytest
  • Loading branch information
ssato committed Apr 24, 2024
1 parent e8e744e commit e3db551
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
2 changes: 2 additions & 0 deletions tests/utils/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pylint: disable=missing-docstring,invalid-name
"""test cases for anyconfig.utils."""
from __future__ import annotations

import collections

import pytest
Expand Down
51 changes: 26 additions & 25 deletions tests/utils/test_files.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#
# Copyright (C) 2012 - 2021 Satoru SATOH <satoru.satoh@gmail.com>
# Copyright (C) 2012 - 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring
r"""Test cases for anyconfig.utils.files.
"""
r"""Test cases for anyconfig.utils.files."""
from __future__ import annotations

import pathlib
import unittest
import typing

import anyconfig.utils.files as TT
import pytest

import anyconfig.utils.files as TT

class TestCase(unittest.TestCase):

def test_is_io_stream(self):
ies = (
(open(__file__), True),
(__file__, False),
([__file__], False),
(pathlib.Path(__file__), False),
([pathlib.Path(__file__)], False),
)
for inp, exp in ies:
res = TT.is_io_stream(inp)
(self.assertTrue if exp else self.assertFalse)(res)
@pytest.mark.parametrize(
("obj", "exp"),
((open(__file__, encoding="utf-8"), True),
(__file__, False),
([__file__], False),
(pathlib.Path(__file__), False),
([pathlib.Path(__file__)], False),
),
)
def test_is_io_stream(obj: typing.Any, exp: bool) -> None:
res = TT.is_io_stream(obj)
assert res if exp else not res

def test_get_path_from_stream(self):
this = __file__

with pathlib.Path(this).open() as strm:
self.assertEqual(TT.get_path_from_stream(strm), this)
def test_get_path_from_stream() -> None:
this = __file__

with self.assertRaises(ValueError):
TT.get_path_from_stream(this)
with pathlib.Path(this).open(encoding="utf-8") as strm:
assert TT.get_path_from_stream(strm) == this

self.assertEqual(TT.get_path_from_stream(this, safe=True), '')
with pytest.raises(ValueError):
TT.get_path_from_stream(this)

# vim:sw=4:ts=4:et:
assert TT.get_path_from_stream(this, safe=True) == ""
51 changes: 25 additions & 26 deletions tests/utils/test_lists.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
#
# Copyright (C) 2012 - 2021 Satoru SATOH <satoru.satoh@gmail.com>
# Copyright (C) 2012 - 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring,invalid-name
r"""test cases for anyconfig.utils.lists.
"""
# pylint: disable=missing-docstring
r"""test cases for anyconfig.utils.lists."""
from __future__ import annotations

import operator
import unittest

import anyconfig.utils.lists as TT
import pytest

import anyconfig.utils.lists as TT

class TestCase(unittest.TestCase):

def test_groupby(self):
items = (("a", 1), ("b", -1), ("c", 1))
res = TT.groupby(items, operator.itemgetter(1))
self.assertEqual(
[(key, tuple(grp)) for key, grp in res],
[(-1, (("b", -1),)), (1, (("a", 1), ("c", 1)))]
)
def test_groupby() -> None:
items = (("a", 1), ("b", -1), ("c", 1))
res = TT.groupby(items, operator.itemgetter(1))
assert [
(key, tuple(grp)) for key, grp in res
] == [(-1, (("b", -1),)), (1, (("a", 1), ("c", 1)))]

def test_concat(self):
ies = (
([[]], []),
((()), []),
([[1, 2, 3], [4, 5]], [1, 2, 3, 4, 5]),
([[1, 2, 3], [4, 5, [6, 7]]], [1, 2, 3, 4, 5, [6, 7]]),
(((1, 2, 3), (4, 5, (6, 7))), [1, 2, 3, 4, 5, (6, 7)]),
(((i, i * 2) for i in range(3)), [0, 0, 1, 2, 2, 4]),
)
for inp, exp in ies:
self.assertEqual(TT.concat(inp), exp)

# vim:sw=4:ts=4:et:
@pytest.mark.parametrize(
("xss", "exp"),
(([[]], []),
((()), []),
([[1, 2, 3], [4, 5]], [1, 2, 3, 4, 5]),
([[1, 2, 3], [4, 5, [6, 7]]], [1, 2, 3, 4, 5, [6, 7]]),
(((1, 2, 3), (4, 5, (6, 7))), [1, 2, 3, 4, 5, (6, 7)]),
(((i, i * 2) for i in range(3)), [0, 0, 1, 2, 2, 4])
),
)
def test_concat(xss, exp):
assert TT.concat(xss) == exp
28 changes: 13 additions & 15 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#
# Copyright (C) 2012 - 2021 Satoru SATOH <satoru.satoh@gmail.com>
# Copyright (C) 2012 - 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring
r"""test cases for anyconfig.utils.lists.
"""
import unittest

import anyconfig.utils.utils as TT
r"""test cases for anyconfig.utils.lists."""
from __future__ import annotations

import pytest

class TestCase(unittest.TestCase):
import anyconfig.utils.utils as TT

def test_filter_options(self):
data = (
(('aaa', ), dict(aaa=1, bbb=2), dict(aaa=1)),
(('aaa', ), dict(bbb=2), dict()),
)
for keys, inp, exp in data:
self.assertEqual(TT.filter_options(keys, inp), exp)

# vim:sw=4:ts=4:et:
@pytest.mark.parametrize(
("keys", "opts", "exp"),
((('aaa', ), {"aaa": 1, "bbb": 2}, {"aaa": 1}),
(('aaa', ), {"bbb": 2}, {}),
)
)
def test_filter_options(keys, opts, exp) -> None:
assert TT.filter_options(keys, opts) == exp

0 comments on commit e3db551

Please sign in to comment.