Add first type inferance tests

This slightly changes to code under test by:
* Adding __init__.py files to create modules
* Converts for a.b to a['b'] to allow object or dict types
This commit is contained in:
Guy Taylor
2018-10-20 20:36:46 +01:00
committed by Sebastian Thiel
parent a793af5586
commit 4faa5f6203
6 changed files with 1401 additions and 13 deletions

View File

@@ -2,3 +2,4 @@ mako
pyyaml
mkdocs==0.16.3
pytest
importlib_resources

0
src/mako/lib/__init__.py Normal file
View File

View File

File diff suppressed because it is too large Load Diff

View File

@@ -315,9 +315,9 @@ def _assure_unique_type_name(schemas, tn):
def to_rust_type(schemas, sn, pn, t, allow_optionals=True, _is_recursive=False):
def nested_type(nt):
if 'items' in nt:
nt = nt.items
elif nt.get('additionalProperties'):
nt = nt.additionalProperties
nt = nt['items']
elif 'additionalProperties' in nt:
nt = nt['additionalProperties']
else:
assert(is_nested_type_property(nt))
# It's a nested type - we take it literally like $ref, but generate a name for the type ourselves
@@ -339,18 +339,18 @@ def to_rust_type(schemas, sn, pn, t, allow_optionals=True, _is_recursive=False):
tn = 'Option<Box<%s>>' % unique_type_name(tn)
return wrap_type(tn)
try:
rust_type = TYPE_MAP[t.type]
if t.type == 'array':
rust_type = TYPE_MAP[t['type']]
if t['type'] == 'array':
return wrap_type("%s<%s>" % (rust_type, unique_type_name((nested_type(t)))))
elif t.type == 'object':
elif t['type'] == 'object':
if is_map_prop(t):
return wrap_type("%s<String, %s>" % (rust_type, nested_type(t)))
else:
return wrap_type(nested_type(t))
elif t.type == 'string' and 'Count' in pn:
elif t['type'] == 'string' and 'Count' in pn:
rust_type = 'i64'
elif rust_type == USE_FORMAT:
rust_type = TYPE_MAP[t.format]
rust_type = TYPE_MAP[t['format']]
if t.get('repeated', False):
rust_type = 'Vec<%s>' % rust_type
@@ -358,13 +358,13 @@ def to_rust_type(schemas, sn, pn, t, allow_optionals=True, _is_recursive=False):
rust_type = wrap_type(rust_type)
return rust_type
except KeyError as err:
raise AssertionError("%s: Property type '%s' unknown - add new type mapping: %s" % (str(err), t.type, str(t)))
raise AssertionError("%s: Property type '%s' unknown - add new type mapping: %s" % (str(err), t['type'], str(t)))
except AttributeError as err:
raise AssertionError("%s: unknown dict layout: %s" % (str(err), t))
# return True if this property is actually a nested type
def is_nested_type_property(t):
return 'type' in t and t.type == 'object' and 'properties' in t or ('items' in t and 'properties' in t.items)
return 'type' in t and t['type'] == 'object' and 'properties' in t or ('items' in t and 'properties' in t['items'])
# Return True if the schema is nested
def is_nested_type(s):

View File

@@ -1,10 +1,16 @@
#!/usr/bin/env python
import unittest
import json
import importlib_resources
from .util import to_api_version, library_name, re_find_replacements, to_rust_type, new_context
from . import test_data
from util import to_api_version, library_name, re_find_replacements
def read_test_json_file(resource):
data = importlib_resources.read_text(test_data, resource)
return json.loads(data)
class UtilsTest(unittest.TestCase):
@@ -58,10 +64,57 @@ class UtilsTest(unittest.TestCase):
self.assertEqual(len(ms), 1)
self.assertEqual(ms[0], '{+project}')
def test_to_rust_type(self):
full_api_schema = read_test_json_file('photoslibrary-api.json')
schemas = full_api_schema['schemas']
# Get class
class_name = None
property_name = None
property_value = {'$ref': 'Album'}
rust_type = to_rust_type(schemas, class_name, property_name, property_value, allow_optionals=True)
self.assertEqual(rust_type, 'Option<Album>')
# allow_optionals=False
class_name = None
property_name = None
property_value = {'$ref': 'Album'}
rust_type = to_rust_type(schemas, class_name, property_name, property_value, allow_optionals=False)
self.assertEqual(rust_type, 'Album')
# Get properties
test_properties = (
('Album', 'title', 'String'), # string
('Status', 'code', 'i32'), # numeric
('Album', 'mediaItemsCount', 'i64'), # numeric via "count" keyword
('Album', 'isWriteable', 'bool'), # boolean
('Album', 'shareInfo', 'ShareInfo'), # reference type
('SearchMediaItemsResponse', 'mediaItems', 'Vec<MediaItem>'), # array
)
for (class_name, property_name, expected) in test_properties:
property_value = schemas[class_name]['properties'][property_name]
rust_type = to_rust_type(schemas, class_name, property_name, property_value, allow_optionals=False)
self.assertEqual(rust_type, expected)
# items reference
class_name = 'SearchMediaItemsResponse'
property_name = 'mediaItems'
property_value = schemas[class_name]['properties'][property_name]
rust_type = to_rust_type(schemas, class_name, property_name, property_value, allow_optionals=True)
self.assertEqual(rust_type, 'Option<Vec<MediaItem>>')
# additionalProperties reference
class_name = 'Status'
property_name = 'details'
property_value = schemas[class_name]['properties'][property_name]
rust_type = to_rust_type(schemas, class_name, property_name, property_value, allow_optionals=True)
self.assertEqual(rust_type, 'Option<Vec<HashMap<String, String>>>')
def main():
unittest.main()
if __name__ == "__main__":
if __name__ == '__main__':
main()