OLD | NEW |
| (Empty) |
1 # -*- encoding: utf-8 -*- | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import json | |
7 import os | |
8 import unittest | |
9 | |
10 import infra_libs | |
11 | |
12 | |
13 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') | |
14 | |
15 | |
16 class _UtilTestException(Exception): | |
17 """Exception used inside tests.""" | |
18 | |
19 | |
20 class ReadJsonTest(unittest.TestCase): | |
21 def test_read_from_file(self): | |
22 new_dict = infra_libs.read_json_as_utf8( | |
23 filename=os.path.join(DATA_DIR, 'utils_test_dict.json')) | |
24 | |
25 # Make sure all keys can be decoded as utf8 | |
26 for key in new_dict.iterkeys(): | |
27 self.assertIsInstance(key, str) | |
28 key.decode('utf-8') # should raise no exceptions | |
29 | |
30 # Make sure all values contain only utf8 | |
31 self.assertIsInstance(new_dict['string'], str) | |
32 new_dict['string'].decode('utf-8') | |
33 | |
34 for value in new_dict['list_of_strings']: | |
35 self.assertIsInstance(value, str) | |
36 value.decode('utf-8') | |
37 | |
38 sub_dict = new_dict['clé accentuée'] | |
39 for key, value in sub_dict.iteritems(): | |
40 self.assertIsInstance(key, str) | |
41 self.assertIsInstance(value, str) | |
42 | |
43 key.decode('utf-8') | |
44 value.decode('utf-8') | |
45 | |
46 def test_read_from_string(self): | |
47 orig_dict = {"string": "prêt¿", | |
48 "list_of_strings": ["caractères", "accentués", "nous voilà"], | |
49 "clé accentuée": {"clé": "privée", "vie": "publique"}, | |
50 } | |
51 | |
52 json_data = json.dumps(orig_dict) | |
53 new_dict = infra_libs.read_json_as_utf8(text=json_data) | |
54 | |
55 self.assertEqual(orig_dict, new_dict) | |
56 | |
57 # Make sure all keys can be decoded as utf8 | |
58 for key in new_dict.iterkeys(): | |
59 self.assertIsInstance(key, str) | |
60 key.decode('utf-8') # should raise no exceptions | |
61 | |
62 # Make sure all values contain only utf8 | |
63 self.assertIsInstance(new_dict['string'], str) | |
64 new_dict['string'].decode('utf-8') | |
65 | |
66 for value in new_dict['list_of_strings']: | |
67 self.assertIsInstance(value, str) | |
68 value.decode('utf-8') | |
69 | |
70 sub_dict = new_dict['clé accentuée'] | |
71 for key, value in sub_dict.iteritems(): | |
72 self.assertIsInstance(key, str) | |
73 self.assertIsInstance(value, str) | |
74 | |
75 key.decode('utf-8') | |
76 value.decode('utf-8') | |
77 | |
78 def test_read_from_string_no_unicode(self): | |
79 # only numerical value. Keys have to be string in json. | |
80 orig_dict = {'1': 2, | |
81 '3': [4, 5., 7, None], | |
82 '7':{'8': 9, '10': 11}} | |
83 json_data = json.dumps(orig_dict) | |
84 new_dict = infra_libs.read_json_as_utf8(text=json_data) | |
85 self.assertEqual(orig_dict, new_dict) | |
86 | |
87 def test_dict_in_dict(self): | |
88 orig_dict = {'à': {'présent': {'ça': 'prétend marcher.'}}} | |
89 json_data = json.dumps(orig_dict) | |
90 new_dict = infra_libs.read_json_as_utf8(text=json_data) | |
91 self.assertEqual(orig_dict, new_dict) | |
92 | |
93 def test_dict_in_list(self): | |
94 orig_dict = ['à', {'présent': {'ça': 'prétend marcher.'}}] | |
95 json_data = json.dumps(orig_dict) | |
96 new_dict = infra_libs.read_json_as_utf8(text=json_data) | |
97 self.assertEqual(orig_dict, new_dict) | |
98 | |
99 def test_error_with_two_arguments(self): | |
100 with self.assertRaises(ValueError): | |
101 infra_libs.read_json_as_utf8(filename='filename', text='{}') | |
102 | |
103 def test_error_with_no_arguments(self): | |
104 with self.assertRaises(ValueError): | |
105 infra_libs.read_json_as_utf8() | |
106 | |
107 | |
108 class TemporaryDirectoryTest(unittest.TestCase): | |
109 def test_tempdir_no_error(self): | |
110 with infra_libs.temporary_directory() as tempdir: | |
111 self.assertTrue(os.path.isdir(tempdir)) | |
112 # This should work. | |
113 with open(os.path.join(tempdir, 'test_tempdir_no_error.txt'), 'w') as f: | |
114 f.write('nonsensical content') | |
115 # And everything should have been cleaned up afterward | |
116 self.assertFalse(os.path.isdir(tempdir)) | |
117 | |
118 def test_tempdir_with_exception(self): | |
119 with self.assertRaises(_UtilTestException): | |
120 with infra_libs.temporary_directory() as tempdir: | |
121 self.assertTrue(os.path.isdir(tempdir)) | |
122 # Create a non-empty file to check that tempdir deletion works. | |
123 with open(os.path.join(tempdir, 'test_tempdir_no_error.txt'), 'w') as f: | |
124 f.write('nonsensical content') | |
125 raise _UtilTestException() | |
126 | |
127 # And everything should have been cleaned up afterward | |
128 self.assertFalse(os.path.isdir(tempdir)) | |
OLD | NEW |