File: test_kv.py

package info (click to toggle)
jc 1.25.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,104 kB
  • sloc: python: 70,400; sh: 724; xml: 278; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download | duplicates (3)
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
79
80
import os
import unittest
import json
import jc.parsers.kv

THIS_DIR = os.path.dirname(os.path.abspath(__file__))


class MyTests(unittest.TestCase):

    # input
    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/keyvalue.txt'), 'r', encoding='utf-8') as f:
        generic_ini_keyvalue = f.read()

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/keyvalue-ifcfg.txt'), 'r', encoding='utf-8') as f:
        generic_ini_keyvalue_ifcfg = f.read()

    # output
    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/keyvalue.json'), 'r', encoding='utf-8') as f:
        generic_ini_keyvalue_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/keyvalue-ifcfg.json'), 'r', encoding='utf-8') as f:
        generic_ini_keyvalue_ifcfg_json = json.loads(f.read())


    def test_kv_nodata(self):
        """
        Test the test kv file with no data
        """
        self.assertEqual(jc.parsers.kv.parse('', quiet=True), {})

    def test_kv_keyvalue(self):
        """
        Test a file that only includes key/value lines
        """
        self.assertEqual(jc.parsers.kv.parse(self.generic_ini_keyvalue, quiet=True), self.generic_ini_keyvalue_json)

    def test_kv_keyvalue_ifcfg(self):
        """
        Test a sample ifcfg key/value file that has quotation marks in the values
        """
        self.assertEqual(jc.parsers.kv.parse(self.generic_ini_keyvalue_ifcfg, quiet=True), self.generic_ini_keyvalue_ifcfg_json)

    def test_kv_duplicate_keys(self):
        """
        Test input that contains duplicate keys. Only the last value should be used.
        """
        data = '''
duplicate_key: value1
another_key = foo
duplicate_key = value2
'''
        expected = {'duplicate_key': 'value2', 'another_key': 'foo'}
        self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected)

    def test_kv_doublequote(self):
        """
        Test kv string with double quotes around a value
        """
        data = '''
key1: "value1"
key2: value2
        '''
        expected = {'key1': 'value1', 'key2': 'value2'}
        self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected)

    def test_kv_singlequote(self):
        """
        Test kv string with double quotes around a value
        """
        data = '''
key1: 'value1'
key2: value2
        '''
        expected = {'key1': 'value1', 'key2': 'value2'}
        self.assertEqual(jc.parsers.kv.parse(data, quiet=True), expected)


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