caellion-python-commons: tests/test_stringutil_unixpassword.py Source File

caellion-python-commons

test_stringutil_unixpassword.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 import pytest
4
5 # package
6 from caellion.pycommons.stringutil.unixpassword import UnixPasswordText as upt
7 from caellion.pycommons.stringutil.unixpassword import NewlineEmptyException
8 from caellion.pycommons.stringutil.unixpassword import DuplicateFieldException
9 from caellion.pycommons.stringutil.unixpassword import EmptyFieldNameException
10 from caellion.pycommons.stringutil.unixpassword import SeparatorEmptyException
11 from caellion.pycommons.stringutil.unixpassword import InvalidFileFormatException
12
13
14 # test cases
15 class TestStringUtilUnixPasswordText:
16 # @params((0, 0, "0"), (0, 3, "0.000"), (1000, 0, "1k"), (1000000, 0, "1M"), (123.456, 3, "123.456"), (1234.56, 3, "1.235k"), (123456, 3, "123.456k"), (123456789, 3, "123.457M"), (999999, 3, "999.999k"), (12345678901234567890123456789, 0, "12346Y"), (12345678901234567890123456789000, 0, "12345679Y"))
17 # def test_fmt_si_plus(self, value, decimals, expected):
18 # assert (fmt.formatSI(value, decimals), expected)
19
20 def test_upt_duplicate_keynames(self):
21 with pytest.raises(DuplicateFieldException):
22 upto = upt(["a", "b", "a"], ":", "\n") # noqa
23
24 def test_upt_empty_keyname(self):
25 with pytest.raises(EmptyFieldNameException):
26 upto = upt(["a", "", "a"], ":", "\n") # noqa
27
28 def test_upt_none_keyname(self):
29 with pytest.raises(EmptyFieldNameException):
30 upto = upt(["a", None, "a"], ":", "\n") # noqa
31
32 def test_upt_empty_separator(self):
33 with pytest.raises(SeparatorEmptyException):
34 upto = upt(["a", "b", "c"], "", "\n") # noqa
35
36 def test_upt_none_separator(self):
37 with pytest.raises(SeparatorEmptyException):
38 upto = upt(["a", "b", "c"], None, "\n") # noqa
39
40 def test_upt_empty_newline(self):
41 with pytest.raises(NewlineEmptyException):
42 upto = upt(["a", "b", "c"], ":", "") # noqa
43
44 def test_upt_none_newline(self):
45 with pytest.raises(NewlineEmptyException):
46 upto = upt(["a", "b", "c"], ":", None) # noqa
47
48 def test_upt_invalid_line(self):
49 with pytest.raises(InvalidFileFormatException):
50 upto = upt(["a", "b", "c"], ":", "\n") # noqa
51 upto.add_line_from_text("a:b:c:d")
52
53 def test_upt_invalid_block(self):
54 with pytest.raises(InvalidFileFormatException):
55 upto = upt(["a", "b", "c"], ":", "\n") # noqa
56 upto.load_text("a:b:c\nd:e:f\ng:h:i\ny:\na:b:d:c")
57
58 @pytest.mark.parametrize( "arg,expected", [ ("1:1:1", {"a": "1", "b": "1", "c": "1"}),
59 ("1:2:3", {"a": "1", "b": "2", "c": "3"}),
60 ("a:b:c", {"a": "a", "b": "b", "c": "c"}),
61 ("f:e:d", {"a": "f", "b": "e", "c": "d"}),
62 (
63 "some longer string:with more data:exe",
64 {"a": "some longer string", "b": "with more data", "c": "exe"},
65 ),
66 ],
67 )
68 def test_upt_parse_line_ok(self, arg, expected):
69 a = upt(["a", "b", "c"])
70 result = a.parse_line(arg)
71 assert result == expected
72
73 @pytest.mark.parametrize( "arg,expected", [ ({"a": "1", "b": "1", "c": "1"}, "1:1:1"),
74 ({"a": "1", "b": "2", "c": "3"}, "1:2:3"),
75 ({"a": "a", "b": "b", "c": "c"}, "a:b:c"),
76 ({"a": "f", "b": "e", "c": "d"}, "f:e:d"),
77 (
78 {"a": "some longer string", "b": "with more data", "c": "exe"},
79 "some longer string:with more data:exe",
80 ),
81 ],
82 )
83 def test_upt_create_line_ok(self, arg, expected):
84 a = upt(["a", "b", "c"])
85 result = a.create_line(arg)
86 assert result == expected
87
88 @pytest.mark.parametrize( "arg", [ ({"a": "1", "b": "1", "c": "1"}),
89 ({"a": "1", "b": "2", "c": "3"}),
90 ({"a": "a", "b": "b", "c": "c"}),
91 ({"a": "f", "b": "e", "c": "d"}),
92 ({"a": "some longer string", "b": "with more data", "c": "exe"}),
93 ],
94 )
95 def test_upt_add_new_line_ok(self, arg):
96 a = upt(["a", "b", "c"])
97 before = len(a.text_lines)
98 a.add_new_line(arg)
99 after = len(a.text_lines)
100 assert after > before
101
102 @pytest.mark.parametrize( "arg", [ ("1:1:1"),
103 ("1:2:3"),
104 ("a:b:c"),
105 ("f:e:d"),
106 ("some longer string:with more data:exe"),
107 ],
108 )
109 def test_upt_add_line_from_text_ok(self, arg):
110 a = upt(["a", "b", "c"])
111 before = len(a.text_lines)
112 a.add_line_from_text(arg)
113 after = len(a.text_lines)
114 assert after > before
115
116 def test_dump_text_ok(self):
117 a = upt(["a", "b", "c"])
118 a.add_line_from_text("a:b:C")
119 a.add_new_line({"a": 1, "b": 2, "c": 3})
120 assert a.dump_text() == "a:b:C\n1:2:3\n"
121
122 def test_load_then_dump_text_ok(self):
123 a = upt(["a", "b", "c"])
124 a.load_text("a:b:C\n1:2:3\n")
125 a.add_line_from_text("a:b:C")
126 a.add_new_line({"a": 1, "b": 2, "c": 3})
127 assert a.dump_text() == "a:b:C\n1:2:3\na:b:C\n1:2:3\n"
128
129
130 if __name__ == "__main__":
131 unittest.main()
132
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText
Definition: test_stringutil_unixpassword.py:15
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_load_then_dump_text_ok
def test_load_then_dump_text_ok(self)
Definition: test_stringutil_unixpassword.py:122
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_none_keyname
def test_upt_none_keyname(self)
Definition: test_stringutil_unixpassword.py:28
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_dump_text_ok
def test_dump_text_ok(self)
Definition: test_stringutil_unixpassword.py:116
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_none_newline
def test_upt_none_newline(self)
Definition: test_stringutil_unixpassword.py:44
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_duplicate_keynames
def test_upt_duplicate_keynames(self)
Definition: test_stringutil_unixpassword.py:20
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_add_new_line_ok
def test_upt_add_new_line_ok(self, arg)
Definition: test_stringutil_unixpassword.py:95
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_invalid_line
def test_upt_invalid_line(self)
Definition: test_stringutil_unixpassword.py:48
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_create_line_ok
def test_upt_create_line_ok(self, arg, expected)
Definition: test_stringutil_unixpassword.py:83
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_invalid_block
def test_upt_invalid_block(self)
Definition: test_stringutil_unixpassword.py:53
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_parse_line_ok
def test_upt_parse_line_ok(self, arg, expected)
Definition: test_stringutil_unixpassword.py:68
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_none_separator
def test_upt_none_separator(self)
Definition: test_stringutil_unixpassword.py:36
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_empty_newline
def test_upt_empty_newline(self)
Definition: test_stringutil_unixpassword.py:40
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_add_line_from_text_ok
def test_upt_add_line_from_text_ok(self, arg)
Definition: test_stringutil_unixpassword.py:109
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_empty_keyname
def test_upt_empty_keyname(self)
Definition: test_stringutil_unixpassword.py:24
tests.test_stringutil_unixpassword.TestStringUtilUnixPasswordText.test_upt_empty_separator
def test_upt_empty_separator(self)
Definition: test_stringutil_unixpassword.py:32
caellion.pycommons.stringutil.unixpassword
This module provides utlilities related to creating, reading, writing and parsing of linux-style pass...
Definition: unixpassword.py:1