File: test_abstraction.py

package info (click to toggle)
python-clevercsv 0.8.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,076 kB
  • sloc: python: 6,184; ansic: 870; makefile: 90
file content (78 lines) | stat: -rw-r--r-- 2,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-

"""
Unit tests for making abstractions

"""

import gzip
import json
import unittest

from pathlib import Path

from typing import Any
from typing import Dict
from typing import List

from clevercsv.cabstraction import c_merge_with_quotechar
from clevercsv.detect_pattern import base_abstraction
from clevercsv.detect_pattern import fill_empties
from clevercsv.detect_pattern import strip_trailing


class AbstractionTestCase(unittest.TestCase):
    def setUp(self) -> None:
        here = Path(__file__)
        this_dir = here.parent
        data_dir = this_dir / "data"
        testcases_file = data_dir / "abstraction_testcases.json.gz"
        if not testcases_file.exists():
            self._cases = []
        else:
            self._cases = self._load_cases(testcases_file)

    @staticmethod
    def _load_cases(filename: Path) -> List[Dict[str, Any]]:
        cases = []
        with gzip.open(filename, "rt", newline="", encoding="utf-8") as fp:
            for line in fp:
                cases.append(json.loads(line))
        return cases

    def test_abstraction_multi(self) -> None:
        if not self._cases:
            self.skipTest("no abstraction test cases found")

        for case in self._cases:
            content = case["content"]
            dialect = case["dialect"]

            exp_base = case["base_abstraction"]
            exp_merge = case["after_merge_with_quotechar"]
            exp_empties = case["after_fill_empties"]
            exp_trailing = case["after_strip_trailing"]
            with self.subTest(name=case["name"], kind="base"):
                base = base_abstraction(
                    content,
                    dialect["delimiter"],
                    dialect["quotechar"],
                    dialect["escapechar"],
                )
                self.assertEqual(base, exp_base)

            with self.subTest(name=case["name"], kind="merge"):
                merge = c_merge_with_quotechar(base)
                self.assertEqual(merge, exp_merge)

            with self.subTest(name=case["name"], kind="empties"):
                empties = fill_empties(merge)
                self.assertEqual(empties, exp_empties)

            with self.subTest(name=case["name"], kind="trailing"):
                trailing = strip_trailing(empties)
                self.assertEqual(trailing, exp_trailing)


if __name__ == "__main__":
    unittest.main()