Home | History | Annotate | Download | only in server2
      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 import unittest
      7 from copy import deepcopy
      8 
      9 from schema_util import RemoveNoDocs, DetectInlineableTypes, InlineDocs
     10 
     11 
     12 class SchemaUtilTest(unittest.TestCase):
     13 
     14   def testRemoveNoDocs(self):
     15     expected_nodoc = [
     16       {
     17         'name': 'B',
     18         'list': [
     19           {
     20             'name': 'B2'
     21           }
     22         ]
     23       },
     24       {
     25         'name': 'D',
     26         'nodoc': False
     27       },
     28       {
     29         'name': 'E',
     30         'items1': [
     31           {
     32             'name': 'E1',
     33             'items': [
     34               {
     35                 'name': 'E1.3'
     36               }
     37             ]
     38           },
     39           {
     40             'name': 'E2'
     41           }
     42         ]
     43       }
     44     ]
     45 
     46     nodoc_data = [
     47       {
     48         'name': 'A',
     49         'nodoc': True
     50       },
     51       {
     52         'name': 'B',
     53         'list': [
     54           {
     55             'name': 'B1',
     56             'nodoc': True
     57           },
     58           {
     59             'name': 'B2'
     60           },
     61           {
     62             'name': 'B3',
     63             'nodoc': True
     64           }
     65         ]
     66       },
     67       {
     68         'name': 'C',
     69         'nodoc': True
     70       },
     71       {
     72         'name': 'D',
     73         'nodoc': False
     74       },
     75       {
     76         'name': 'E',
     77         'dict': {
     78           'name': 'Ed',
     79           'nodoc': True
     80         },
     81         'items1': [
     82           {
     83             'name': 'E1',
     84             'items': [
     85               {
     86                 'name': 'E1.1',
     87                 'nodoc': True
     88               },
     89               {
     90                 'name': 'E1.2',
     91                 'nodoc': True
     92               },
     93               {
     94                 'name': 'E1.3'
     95               }
     96             ]
     97           },
     98           {
     99             'name': 'E2'
    100           },
    101           {
    102             'name': 'E3',
    103             'nodoc': True
    104           }
    105         ]
    106       }
    107     ]
    108 
    109     RemoveNoDocs(nodoc_data)
    110     self.assertEquals(expected_nodoc, nodoc_data)
    111 
    112   def testInlineDocs(self):
    113     schema = {
    114       'namespace': 'storage',
    115       'properties': {
    116         'key2': {
    117           'description': 'second key',
    118           '$ref': 'Key'
    119         },
    120         'key1': {
    121           'description': 'first key',
    122           '$ref': 'Key'
    123         }
    124       },
    125       'types': [
    126         {
    127           'inline_doc': True,
    128           'type': 'string',
    129           'id': 'Key',  # Should be inlined into both properties and be removed
    130                         # from types.
    131           'description': 'This is a key.',  # This description should disappear.
    132           'marker': True  # This should appear three times in the output.
    133         },
    134         {
    135           'items': {
    136             '$ref': 'Key'
    137           },
    138           'type': 'array',
    139           'id': 'KeyList',
    140           'description': 'A list of keys'
    141         }
    142       ]
    143     }
    144 
    145     expected_schema = {
    146       'namespace': 'storage',
    147       'properties': {
    148         'key2': {
    149           'marker': True,
    150           'type': 'string',
    151           'description': 'second key'
    152         },
    153         'key1': {
    154           'marker': True,
    155           'type': 'string',
    156           'description': 'first key'
    157         }
    158       },
    159       'types': [
    160         {
    161           'items': {
    162             'marker': True,
    163             'type': 'string'
    164           },
    165           'type': 'array',
    166           'id': 'KeyList',
    167           'description': 'A list of keys'
    168         }
    169       ]
    170     }
    171 
    172     inlined_schema = deepcopy(schema)
    173     InlineDocs(inlined_schema)
    174     self.assertEqual(expected_schema, inlined_schema)
    175 
    176   def testDetectInline(self):
    177     schema = {
    178       'types': [
    179         {
    180           'id': 'Key',
    181           'items': {
    182             '$ref': 'Value'
    183           }
    184         },
    185         {
    186           'id': 'Value',
    187           'marker': True
    188         }
    189       ]
    190     }
    191 
    192     expected_schema = {
    193       'types': [
    194         {
    195           'id': 'Key',
    196           'items': {
    197             'marker': True,
    198           }
    199         }
    200       ]
    201     }
    202 
    203     DetectInlineableTypes(schema)
    204     InlineDocs(schema)
    205     self.assertEqual(expected_schema, schema)
    206 
    207 
    208 if __name__ == '__main__':
    209   unittest.main()
    210