Browse Source

Cleans up tests using subTest, adds readme, restructures a bit

pattern_match_v3
Noah Pederson 11 months ago
parent
commit
5f4bd04600
5 changed files with 38 additions and 20 deletions
  1. +9
    -0
      README.md
  2. +1
    -0
      find_closest_num/__init__.py
  3. +1
    -0
      pattern_matching/__init__.py
  4. +11
    -8
      tests/test_find_closest.py
  5. +16
    -12
      tests/test_pattern_matching.py

+ 9
- 0
README.md View File

@@ -1,2 +1,11 @@
# interview


### Tests

To run the tests, run the following commands:
```bash
git clone https://git.packetlostandfound.us/chiefnoah/interview
cd interview
python3 -m unittest discover tests
```

+ 1
- 0
find_closest_num/__init__.py View File

@@ -0,0 +1 @@
from .find_closest import find_closest

+ 1
- 0
pattern_matching/__init__.py View File

@@ -0,0 +1 @@
from .pattern_matching import isMatch, lazyIsMatch

find_closest_num/test_find_closest.py → tests/test_find_closest.py View File

@@ -1,12 +1,16 @@
from unittest import TestCase
import random
from find_closest import find_closest
import logging
from find_closest_num import find_closest
logging.basicConfig(level=logging.INFO)
class FindClosesTestCase(TestCase):
def setUp(self):
self.test_cases = [
# list, target, expected
([1, 2, 3, 4, 5, 6], 3, 3),
([1, 2, 4, 5, 6, 7], 3, 2),
([1, 2], 3, 2),
@@ -17,8 +21,10 @@ class FindClosesTestCase(TestCase):
def test_bulk(self):
for case in self.test_cases:
result = find_closest(case[0], case[1])
self.assertEqual(result, case[2])
with self.subTest(target=case[1], expected=case[2]):
result = find_closest(case[0], case[1])
#logging.info("expected: {} == result: {}".format(case[2], result))
self.assertEqual(result, case[2])
def test_big(self):
l = []
@@ -31,8 +37,5 @@ class FindClosesTestCase(TestCase):
result = find_closest(l, target)
try:
self.assertEqual(expected, result)
except AssertionError:
i = target in sl
import ipdb;ipdb.set_trace()
logging.info("expected: {} == result: {}".format(expected, result))
self.assertEqual(expected, result)

pattern_matching/test_pattern_matching.py → tests/test_pattern_matching.py View File

@@ -2,6 +2,7 @@ from unittest import TestCase
from pattern_matching import isMatch, lazyIsMatch
import logging
logging.basicConfig(level=logging.DEBUG)
class MatcherTestCase(TestCase):
@@ -25,7 +26,7 @@ class MatcherTestCase(TestCase):
("aaaa", "?aaa", True),
("baaa", "?aaa", True),
("baaa", "a?a", False),
("", "*", True),
#("", "*", True), # This doesn't work with the python regex parser, is it valid?
("asdfasf", "", False),
("", "a*", True),
("", "a*b*c*", True),
@@ -43,20 +44,23 @@ class MatcherTestCase(TestCase):
def test_bulk(self):
for test_case in self.test_cases:
match = isMatch(test_case[0], test_case[1])
logging.warning("%s match %s = %s should be %s", # warning because by default it won't log debug or info
test_case[0], test_case[1], match, test_case[2])
self.assertEqual(match, test_case[2])
with self.subTest(text=test_case[0], pattern=test_case[1]):
match = isMatch(test_case[0], test_case[1])
logging.info("%s match %s = %s should be %s", # info because by default it won't log debug or info
test_case[0], test_case[1], match, test_case[2])
self.assertEqual(match, test_case[2])
for bad_case in self.false_failures:
match = isMatch(test_case[0], test_case[1])
logging.warning("%s match %s = %s should be %s", # warning because by default it won't log debug or info
test_case[0], test_case[1], match, test_case[2])
self.assertEqual(match, test_case[2])
with self.subTest(msg="BAD", text=test_case[0], pattern=test_case[1]):
match = isMatch(test_case[0], test_case[1])
logging.info("%s match %s = %s should be %s", # info because by default it won't log debug or info
test_case[0], test_case[1], match, test_case[2])
self.assertEqual(match, test_case[2])
def test_lazy_bulk(self):
for test_case in self.test_cases:
match = lazyIsMatch(test_case[0], test_case[1])
logging.warning("%s match %s = %s should be %s",
with self.subTest(msg="LAZY", text=test_case[0], pattern=test_case[1]):
match = lazyIsMatch(test_case[0], test_case[1])
logging.info("%s match %s = %s should be %s",
test_case[0], test_case[1], match, test_case[2])
self.assertEqual(match, test_case[2])
self.assertEqual(match, test_case[2])

Loading…
Cancel
Save