@@ -23,6 +23,117 @@ def output_path(filepath):
2323 dir_path = os .path .dirname (os .path .realpath (__file__ ))
2424 return os .path .join (dir_path , 'output/{}' .format (filepath ))
2525
26+ # BASE64
27+
28+ def test_from_base64_with_valid_data (self ):
29+ j = 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9'
30+ # j = '{"a": 1, "b": 2, "c": 3}'
31+ # static method
32+ d = IODict .from_base64 (j )
33+ self .assertTrue (isinstance (d , dict ))
34+ self .assertEqual (d , { 'a' : 1 , 'b' : 2 , 'c' : 3 , })
35+ # constructor
36+ d = IODict (j )
37+ self .assertTrue (isinstance (d , dict ))
38+ self .assertEqual (d , { 'a' : 1 , 'b' : 2 , 'c' : 3 , })
39+
40+ def test_from_base64_with_invalid_data (self ):
41+ j = 'Lorem ipsum est in ea occaecat nisi officia.'
42+ # static method
43+ with self .assertRaises (ValueError ):
44+ d = IODict .from_base64 (j )
45+ # constructor
46+ with self .assertRaises (ValueError ):
47+ d = IODict (j )
48+
49+ def test_from_base64_with_valid_file_valid_content (self ):
50+ filepath = self .input_path ('valid-content.base64' )
51+ # static method
52+ d = IODict .from_base64 (filepath )
53+ self .assertTrue (isinstance (d , dict ))
54+ # constructor
55+ d = IODict (filepath )
56+ self .assertTrue (isinstance (d , dict ))
57+
58+ def test_from_base64_with_valid_file_valid_content_invalid_format (self ):
59+ filepath = self .input_path ('valid-content.json' )
60+ with self .assertRaises (ValueError ):
61+ d = IODict .from_base64 (filepath )
62+ filepath = self .input_path ('valid-content.toml' )
63+ with self .assertRaises (ValueError ):
64+ d = IODict .from_base64 (filepath )
65+ filepath = self .input_path ('valid-content.xml' )
66+ with self .assertRaises (ValueError ):
67+ d = IODict .from_base64 (filepath )
68+ filepath = self .input_path ('valid-content.yml' )
69+ with self .assertRaises (ValueError ):
70+ d = IODict .from_base64 (filepath )
71+
72+ def test_from_base64_with_valid_file_invalid_content (self ):
73+ filepath = self .input_path ('invalid-content.base64' )
74+ # static method
75+ with self .assertRaises (ValueError ):
76+ d = IODict .from_base64 (filepath )
77+ # constructor
78+ with self .assertRaises (ValueError ):
79+ d = IODict (filepath )
80+
81+ def test_from_base64_with_invalid_file (self ):
82+ filepath = self .input_path ('invalid-file.base64' )
83+ # static method
84+ with self .assertRaises (ValueError ):
85+ d = IODict .from_base64 (filepath )
86+ # constructor
87+ with self .assertRaises (ValueError ):
88+ d = IODict (filepath )
89+
90+ # def test_from_base64_with_valid_url_valid_content(self):
91+ # url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.base64'
92+ # # static method
93+ # d = IODict.from_base64(url)
94+ # self.assertTrue(isinstance(d, dict))
95+ # # constructor
96+ # d = IODict(url)
97+ # self.assertTrue(isinstance(d, dict))
98+
99+ def test_from_base64_with_valid_url_invalid_content (self ):
100+ url = 'https://github.com/fabiocaccamo/python-benedict'
101+ # static method
102+ with self .assertRaises (ValueError ):
103+ d = IODict .from_base64 (url )
104+ # constructor
105+ with self .assertRaises (ValueError ):
106+ d = IODict (url )
107+
108+ def test_from_base64_with_invalid_url (self ):
109+ url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
110+ # static method
111+ with self .assertRaises (ValueError ):
112+ d = IODict .from_base64 (url )
113+ # constructor
114+ with self .assertRaises (ValueError ):
115+ d = IODict (url )
116+
117+ def test_to_base64 (self ):
118+ d = IODict ({
119+ 'a' : 1 ,
120+ 'b' : 2 ,
121+ 'c' : 3 ,
122+ })
123+ s = d .to_base64 (sort_keys = True )
124+ self .assertEqual (s , 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9' )
125+
126+ def test_to_base64_file (self ):
127+ d = IODict ({
128+ 'a' : 1 ,
129+ 'b' : 2 ,
130+ 'c' : 3 ,
131+ })
132+ filepath = self .output_path ('test_to_base64_file.base64' )
133+ s = d .to_base64 (filepath = filepath , sort_keys = True )
134+ self .assertTrue (d , os .path .isfile (filepath ))
135+ self .assertEqual (d , IODict .from_base64 (filepath ))
136+
26137# JSON
27138
28139 def test_from_json_with_valid_data (self ):
0 commit comments