python-benedict/tests/core/test_match.py

64 lines
1.8 KiB
Python
Raw Normal View History

2020-09-22 12:10:59 +00:00
# -*- coding: utf-8 -*-
from benedict.core import match as _match
import re
import unittest
class match_test_case(unittest.TestCase):
2022-02-13 10:35:43 +00:00
"""
This class describes a match test case.
"""
2020-09-22 12:10:59 +00:00
@staticmethod
def _get_dict():
return {
2022-02-13 10:35:43 +00:00
"DOC_0001.pdf": "DOC_0001.pdf",
"IMG_0001.jpg": "IMG_0001.jpg",
"IMG_0001.raw": "IMG_0001.raw",
"DOC_0002.pdf": "DOC_0002.pdf",
"IMG_0002.jpg": "IMG_0002.jpg",
"IMG_0002.raw": "IMG_0002.raw",
"DOC_0003.pdf": "DOC_0003.pdf",
"IMG_0003.jpg": "IMG_0003.jpg",
"IMG_0003.raw": "IMG_0003.raw",
"DOC_0004.pdf": "DOC_0004.pdf",
"IMG_0004.jpg": "IMG_0004.jpg",
"IMG_0004.raw": "IMG_0004.raw",
"DOC_0005.pdf": "DOC_0005.pdf",
"IMG_0005.jpg": "IMG_0005.jpg",
"IMG_0005.raw": "IMG_0005.raw",
2020-09-22 12:10:59 +00:00
}
def test_match_with_string_pattern(self):
d = self._get_dict()
2022-02-13 10:35:43 +00:00
values = _match(d, "IMG_*.jpg")
2020-09-22 12:10:59 +00:00
values.sort()
expected_values = [
2022-02-13 10:35:43 +00:00
"IMG_0001.jpg",
"IMG_0002.jpg",
"IMG_0003.jpg",
"IMG_0004.jpg",
"IMG_0005.jpg",
2020-09-22 12:10:59 +00:00
]
self.assertEqual(values, expected_values)
def test_match_with_regex_pattern(self):
d = self._get_dict()
2022-02-13 10:35:43 +00:00
values = _match(d, re.compile(r"^DOC\_"))
2020-09-22 12:10:59 +00:00
values.sort()
expected_values = [
2022-02-13 10:35:43 +00:00
"DOC_0001.pdf",
"DOC_0002.pdf",
"DOC_0003.pdf",
"DOC_0004.pdf",
"DOC_0005.pdf",
2020-09-22 12:10:59 +00:00
]
self.assertEqual(values, expected_values)
def test_match_with_invalid_pattern(self):
d = self._get_dict()
with self.assertRaises(ValueError):
values = _match(d, 100)