Skip to content

Commit

Permalink
fix/change: migrate test cases for anyconfig.query.query from unittes…
Browse files Browse the repository at this point in the history
…t to pytest
  • Loading branch information
ssato committed Apr 27, 2024
1 parent a70b255 commit 2eda04c
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions tests/query/test_query.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
#
# Copyright (C) 2017 - 2021 Satoru SATOH <satoru.satoh@gmail.com>
# Copyright (C) 2017 - 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring, invalid-name
"""test cases for anyconfig.query.query.
"""
import os
import unittest
# pylint: disable=missing-docstring
"""test cases for anyconfig.query.query."""
from __future__ import annotations

import pytest

try:
import anyconfig.query.query as TT
except ImportError:
raise unittest.SkipTest('Needed library to query was not found')


class Test_00_Functions(unittest.TestCase):

def _assert_dicts_equal(self, dic, ref):
self.assertEqual(dic, ref,
"%r%s vs.%s%r" % (dic, os.linesep, os.linesep, ref))

def _assert_query(self, data_exp_ref_list, dicts=False):
_assert = self._assert_dicts_equal if dicts else self.assertEqual
for data, exp, ref in data_exp_ref_list:
try:
_assert(TT.query(data, exp)[0], ref)
except ValueError:
pass

def test_10_query(self):
self._assert_query([({"a": 1}, "a", 1),
({"a": {"b": 2}}, "a.b", 2)])

def test_12_invalid_query(self):
data = {"a": 1}
self._assert_query([(data, "b.", data)])

def test_14_empty_query(self):
data = {"a": 1}
self._assert_query([(data, None, data),
(data, '', data)])

# vim:sw=4:ts=4:et:
except ImportError as exc:
raise pytest.skip(
"Needed library to query was not found",
allow_module_leve=True
) from exc


@pytest.mark.parametrize(
("data", "query", "exp"),
(({"a": 1}, "a", 1),
({"a": {"b": 2}}, "a.b", 2),
({"a": 1}, "b.", {"a": 1}),
({"a": 1}, None, {"a": 1}),
({"a": 1}, "", {"a": 1}),
),
)
def test_query(data, query: str, exp):
(res, _exc) = TT.query(data, query)
assert res == exp

0 comments on commit 2eda04c

Please sign in to comment.