Skip to content

Commit 940d0cf

Browse files
committed
VED-789: improving coverage: test for parsers
1 parent af54002 commit 940d0cf

File tree

5 files changed

+143
-189
lines changed

5 files changed

+143
-189
lines changed

lambdas/shared/tests/test_common/validator/schemas/Schema.json

Lines changed: 0 additions & 189 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
3+
from common.validator.parsers.csv_line_parser import CSVLineParser
4+
5+
6+
class TestCSVLineParser(unittest.TestCase):
7+
def test_parse_normal(self):
8+
p = CSVLineParser()
9+
p.parse_csv_line("9011011,Tom,32", "nhs_number,name,age")
10+
self.assertEqual(p.csv_file_data, {"nhs_number": "9011011", "name": "Tom", "age": "32"})
11+
self.assertEqual(p.get_key_value("name"), ["Tom"])
12+
13+
def test_extra_values_ignored(self):
14+
p = CSVLineParser()
15+
p.parse_csv_line("1,2,3", "a,b")
16+
self.assertEqual(p.csv_file_data, {"a": "1", "b": "2"})
17+
self.assertEqual(p.get_key_value("b"), ["2"])
18+
19+
def test_fewer_values_missing_key_raises(self):
20+
p = CSVLineParser()
21+
p.parse_csv_line("alpha,beta", "a,b,c")
22+
self.assertIn("a", p.csv_file_data)
23+
self.assertIn("b", p.csv_file_data)
24+
self.assertNotIn("c", p.csv_file_data)
25+
with self.assertRaises(KeyError):
26+
_ = p.get_key_value("c")
27+
28+
def test_get_missing_key_raises(self):
29+
p = CSVLineParser()
30+
p.parse_csv_line("1,2", "a,b")
31+
with self.assertRaises(KeyError):
32+
_ = p.get_key_value("z")
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import tempfile
2+
import unittest
3+
from pathlib import Path
4+
5+
from common.validator.parsers.csv_parser import CSVParser
6+
7+
8+
class TestCSVParser(unittest.TestCase):
9+
def _write_temp_csv(self, content: str) -> str:
10+
fd, path = tempfile.mkstemp(suffix=".csv")
11+
Path(path).write_text(content, encoding="utf-8")
12+
return path
13+
14+
def test_parse_file_with_pipe_delimiter(self):
15+
csv_content = "nhs|name|age\n9011011|Tom|32\n9011012|Ana|27\n"
16+
path = self._write_temp_csv(csv_content)
17+
try:
18+
p = CSVParser()
19+
p.parse_csv_file(path, delimiter="|")
20+
self.assertEqual(p.get_key_value("name"), ["Tom", "Ana"])
21+
self.assertEqual(p.get_key_value("nhs"), ["9011011", "9011012"])
22+
finally:
23+
Path(path).unlink(missing_ok=True)
24+
25+
def test_missing_column_raises_keyerror(self):
26+
csv_content = "a|b\n1|2\n"
27+
path = self._write_temp_csv(csv_content)
28+
try:
29+
p = CSVParser()
30+
p.parse_csv_file(path, delimiter="|")
31+
with self.assertRaises(KeyError):
32+
_ = p.get_key_value("c")
33+
finally:
34+
Path(path).unlink(missing_ok=True)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from common.validator.lookup_expressions.lookup_data import LookUpData
4+
5+
6+
class TestLookUpData(unittest.TestCase):
7+
def setUp(self):
8+
self.lookup = LookUpData()
9+
10+
def test_valid_lookup_returns_text(self):
11+
self.assertEqual(self.lookup.find_lookup("368208006"), "Left upper arm structure")
12+
13+
def test_unknown_code_returns_empty_string(self):
14+
self.assertEqual(self.lookup.find_lookup("not_a_code"), "")
15+
16+
def test_none_returns_empty_string(self):
17+
self.assertEqual(self.lookup.find_lookup(None), "")
18+
19+
def test_non_string_input_returns_empty_string(self):
20+
self.assertEqual(self.lookup.find_lookup(12345), "")
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unittest
2+
3+
from common.validator.parsers.schema_parser import SchemaParser
4+
5+
6+
class TestSchemaParser(unittest.TestCase):
7+
def test_parse_and_count_expressions(self):
8+
schema = {
9+
"expressions": [
10+
{"expression": "LOOKUP", "field": "route"},
11+
{"field": "no-op"}, # missing 'expression' should not be counted
12+
{"expression": "KEYCHECK", "field": "site"},
13+
]
14+
}
15+
p = SchemaParser()
16+
p.parse_schema(schema)
17+
self.assertEqual(p.expression_count(), 2)
18+
19+
def test_get_expression_by_index(self):
20+
schema = {
21+
"expressions": [
22+
{"expression": "A", "field": "x"},
23+
{"expression": "B", "field": "y"},
24+
]
25+
}
26+
p = SchemaParser()
27+
p.parse_schema(schema)
28+
self.assertEqual(p.get_expression(0), {"expression": "A", "field": "x"})
29+
self.assertEqual(p.get_expression(1), {"expression": "B", "field": "y"})
30+
with self.assertRaises(IndexError):
31+
_ = p.get_expression(2)
32+
33+
def test_get_expressions_returns_all(self):
34+
expressions = [
35+
{"expression": "A", "field": "x"},
36+
{"expression": "B", "field": "y"},
37+
{"field": "ignored"},
38+
]
39+
p = SchemaParser()
40+
p.parse_schema({"expressions": expressions})
41+
self.assertEqual(p.get_expressions(), expressions)
42+
43+
44+
if __name__ == "__main__":
45+
unittest.main()

0 commit comments

Comments
 (0)