-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/change: migrate test cases for anyconfig.utils from unittest to p…
…ytest
- Loading branch information
Showing
4 changed files
with
66 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |