OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2013 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 from copy import deepcopy |
| 7 import unittest |
| 8 |
| 9 from features_utility import FeaturesFromJson, Filter, MergeFeatures |
| 10 |
| 11 class FeaturesUtilityTest(unittest.TestCase): |
| 12 def testFromJson(self): |
| 13 raw_features_json = { |
| 14 'doc1': { |
| 15 'extension_types': ['extension', 'platform_app'] |
| 16 }, |
| 17 'doc2': { |
| 18 'extension_types': ['hosted_app', 'packaged_app'] |
| 19 }, |
| 20 'doc3': { |
| 21 'whitelist': 'hashhashashhashashhashashhash' |
| 22 }, |
| 23 'doc4': [ |
| 24 { 'extension_types': 'all' }, |
| 25 { 'whitelist': 'hashhashashhashashhashashhash' } |
| 26 ], |
| 27 'doc5': { |
| 28 'extension_types': ['extension'] |
| 29 }, |
| 30 'doc1.sub1': { |
| 31 'extension_types': ['platform_app', 'hosted_app', 'packaged_app'] |
| 32 } |
| 33 } |
| 34 |
| 35 expected = { |
| 36 'doc1': { |
| 37 'platforms': ['app', 'extension'], |
| 38 'name': 'doc1' |
| 39 }, |
| 40 'doc2': { |
| 41 'platforms': [], |
| 42 'name': 'doc2' |
| 43 }, |
| 44 'doc4': { |
| 45 'platforms': ['app', 'extension'], |
| 46 'name': 'doc4' |
| 47 }, |
| 48 'doc5': { |
| 49 'platforms': ['extension'], |
| 50 'name': 'doc5' |
| 51 }, |
| 52 'doc1.sub1': { |
| 53 'platforms': ['app'], |
| 54 'name': 'doc1.sub1' |
| 55 } |
| 56 } |
| 57 |
| 58 self.assertEqual(expected, FeaturesFromJson(raw_features_json)) |
| 59 |
| 60 def testFilter(self): |
| 61 unfiltered = { |
| 62 'doc1': { 'platforms': ['app'] }, |
| 63 'doc2': { 'platforms': ['extension'] }, |
| 64 'doc3': { 'platforms': ['app', 'extension'] }, |
| 65 'doc4': { 'platforms': [] } |
| 66 } |
| 67 |
| 68 apps_names = set(('doc1', 'doc3')) |
| 69 extension_names = set(('doc2', 'doc3')) |
| 70 |
| 71 self.assertEqual( |
| 72 apps_names, set(Filter(deepcopy(unfiltered), 'app').keys())) |
| 73 self.assertEqual( |
| 74 extension_names, set(Filter(unfiltered, 'extension').keys())) |
| 75 |
| 76 def testMergeFeatures(self): |
| 77 features = { |
| 78 'doc1': { |
| 79 'platforms': ['app'] |
| 80 }, |
| 81 'doc3': { |
| 82 'name': 'doc3' |
| 83 } |
| 84 } |
| 85 |
| 86 other = { |
| 87 'doc1': { |
| 88 'name': 'doc1', |
| 89 'platforms': ['extension'] |
| 90 }, |
| 91 'doc2': { |
| 92 'name': 'doc2' |
| 93 }, |
| 94 'doc3': { |
| 95 'platforms': ['extension', 'app'] |
| 96 } |
| 97 } |
| 98 |
| 99 expected = { |
| 100 'doc1': { |
| 101 'name': 'doc1', |
| 102 'platforms': ['extension'] |
| 103 }, |
| 104 'doc2': { |
| 105 'name': 'doc2' |
| 106 }, |
| 107 'doc3': { |
| 108 'name': 'doc3', |
| 109 'platforms': ['extension', 'app'] |
| 110 } |
| 111 } |
| 112 |
| 113 self.assertEqual(expected, MergeFeatures(features, other)) |
| 114 |
| 115 if __name__ == '__main__': |
| 116 unittest.main() |
OLD | NEW |