Skip to content

Commit

Permalink
swift template_method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walkingtowork committed Oct 14, 2015
1 parent 3e967fe commit 246ecb8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 11 deletions.
2 changes: 1 addition & 1 deletion signals/generators/base/base_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BaseTemplate(object):
def __init__(self, project_name, schema, data_models_path, jinja2_environment):
# Command Flags
self.project_name = project_name
self.schema = schema # Is this right?
self.schema = schema
self.data_models_path = data_models_path
# Setup
self.jinja2_environment = jinja2_environment
Expand Down
71 changes: 63 additions & 8 deletions tests/generators/ios/test_template_methods.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import unittest
from signals.generators.ios.ios_template_methods import iOSTemplateMethods
from signals.generators.ios.objectivec_template import ObjectiveCTemplateMethods
from signals.generators.ios.parameters import ObjCParameter
from signals.generators.ios.parameters import ObjCParameter, SwiftParameter
from signals.generators.ios.swift_template import SwiftTemplateMethods
from signals.parser.schema import DataObject
from signals.parser.fields import Field
from signals.parser.api import GetAPI, API, PatchAPI
# from signals.generators.ios.template_methods import is_url, is_oauth, content_type, get_object_name, get_url_name, \
# get_media_fields, media_field_check, key_path, attribute_mappings, get_api_request_object, \
# create_parameter_signature, method_name, method_parameters
from tests.utils import create_dynamic_schema


Expand Down Expand Up @@ -45,7 +43,7 @@ def test_method_name(self):
schema = create_dynamic_schema(objects_json, urls_json)
self.assertEqual(iOSTemplateMethods.method_name(schema.urls[0].patch), "PostWithBody")

def test_method_parameters(self):
def test_objective_c_method_parameters(self):
objects_json = {
'$postRequest': {"body": "string", "title": "string"},
'$postResponse': {"body": "string", "title": "string"}
Expand All @@ -68,7 +66,30 @@ def test_method_parameters(self):
"failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure"
self.assertEqual(parameter_signature, expected)

def test_key_path(self):
def test_swift_method_parameters(self):
objects_json = {
'$postRequest': {"body": "string", "title": "string"},
'$postResponse': {"body": "string", "title": "string"}
}
urls_json = [
{
"url": "post/:id/",
"patch": {
"request": "$postRequest",
"response": {
"200+": "$postResponse"
}
}
}
]
schema = create_dynamic_schema(objects_json, urls_json)
parameter_signature = SwiftTemplateMethods.method_parameters(schema.urls[0].patch)
expected = "body: String, title: String, theID: Int, " \
"success: RestKitSuccess, " \
"failure: RestKitError"
self.assertEqual(parameter_signature, expected)

def test_objective_c_key_path(self):
api = GetAPI("post/", {
"response": {
"200+": "$postResponse"
Expand All @@ -86,6 +107,24 @@ def test_key_path(self):
})
self.assertEqual(ObjectiveCTemplateMethods.key_path(api), 'nil')

def test_swift_key_path(self):
api = GetAPI("post/", {
"response": {
"200+": "$postResponse"
}
})
self.assertEqual(SwiftTemplateMethods.key_path(api), '"results"')

api.resource_type = GetAPI.RESOURCE_DETAIL
self.assertEqual(SwiftTemplateMethods.key_path(api), 'nil')

api = GetAPI("post/:id/favorite/", {
"response": {
"200+": "$postResponse"
}
})
self.assertEqual(SwiftTemplateMethods.key_path(api), 'nil')

def test_get_object_name(self):
data_object = DataObject("$postRequest", {})
self.assertEqual(iOSTemplateMethods.get_object_name(data_object), "postRequest")
Expand All @@ -94,14 +133,22 @@ def test_get_object_name(self):
def test_get_url_name(self):
self.assertEqual(iOSTemplateMethods.get_url_name("/post/:id/favorite/"), "PostWithIdFavorite")

def test_attribute_mappings(self):
def test_objective_c_attribute_mappings(self):
fields = [
Field("first_title", ["string"]),
Field("user_id", ["int"])
]
attribute_mapping_string = ObjectiveCTemplateMethods.attribute_mappings(fields)
self.assertEqual(attribute_mapping_string, '@"first_title": @"firstTitle", @"user_id": @"userId"')

def test_swift_attribute_mappings(self):
fields = [
Field("first_title", ["string"]),
Field("user_id", ["int"])
]
attribute_mapping_string = SwiftTemplateMethods.attribute_mappings(fields)
self.assertEqual(attribute_mapping_string, '"first_title": "firstTitle", "user_id": "userId"')

def test_is_oauth(self):
api = GetAPI("post/", {
"response": {
Expand Down Expand Up @@ -145,14 +192,22 @@ def test_is_url(self):
self.assertTrue(iOSTemplateMethods.is_url("http://test.com"))
self.assertFalse(iOSTemplateMethods.is_url("Constants.getBaseURL()"))

def test_create_parameter_signature(self):
def test_create_objc_parameter_signature(self):
parameters = [
ObjCParameter("title", "NSString*"),
ObjCParameter("userId", "NSNumber*")
]
parameter_signature = ObjectiveCTemplateMethods.create_parameter_signature(parameters)
self.assertEqual(parameter_signature, "(NSString*)title userId:(NSNumber*)userId")

def test_create_swift_parameter_signature(self):
parameters = [
SwiftParameter("title", "String"),
SwiftParameter("userId", "Int")
]
parameter_signature = SwiftTemplateMethods.create_parameter_signature(parameters)
self.assertEqual(parameter_signature, "title: String, userId: Int")

def test_get_api_request_object(self):
objects_json = {
'$postRequest': {"body": "string", "title": "string"},
Expand Down
45 changes: 43 additions & 2 deletions tests/generators/ios/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jinja2 import Environment
from signals.generators.ios.conversion import sanitize_field_name, get_proper_name
from signals.generators.ios.objectivec_template import ObjectiveCTemplateMethods, ObjectiveCTemplate
from signals.generators.ios.swift_template import SwiftTemplate, SwiftTemplateMethods
from signals.parser.fields import Relationship, Field
from signals.parser.schema import DataObject, Schema, URL
from signals.parser.api import GetAPI, PatchAPI, PostAPI
Expand Down Expand Up @@ -241,7 +242,7 @@ def test_relationship_mapping_template(self):
'relationship': relationship
})

def test_data_model_header_template(self):
def test_objective_c_data_model_header_template(self):
schema = Schema("./tests/files/test_schema.json")
self.assertTemplateEqual('data_model.h.j2', 'DataModel.h', {
'schema': schema,
Expand All @@ -253,7 +254,7 @@ def test_data_model_header_template(self):
datetime.today().strftime('%m/%d/%Y'),
))

def test_data_model_implementation_template(self):
def test_objective_c_data_model_implementation_template(self):
schema = Schema("./tests/files/test_schema.json")
self.assertTemplateEqual('data_model.m.j2', 'DataModel.m', {
'project_name': "TestProject",
Expand All @@ -267,3 +268,43 @@ def test_data_model_implementation_template(self):
}, expected_context=(
datetime.today().strftime('%m/%d/%Y'),
))


class SwiftTemplateTestCase(unittest.TestCase):
def setUp(self):
self.jinja2_environment = Environment(loader=PackageLoader('signals.generators.ios', 'templates/swift/'),
extensions=['jinja2.ext.with_'],
trim_blocks=True,
lstrip_blocks=True)

def assertTemplateEqual(self, template_name, expected_template, context, expected_context=None):
current_directory = os.path.dirname(__file__)
expected_file_path = os.path.join(current_directory, "files", expected_template)
with open(expected_file_path, "r") as expected_template_file:
template = self.jinja2_environment.get_template(template_name)
# Registers all methods in template_methods.py with jinja2 for use
context.update({name: method for name, method in getmembers(SwiftTemplateMethods, isfunction)})
context.update({'get_proper_name': get_proper_name,
'sanitize_field_name': sanitize_field_name
})
template_output = template.render(**context)
expected_template_out = expected_template_file.read()
if expected_context:
# Using %s formatting syntax, since format uses {'s and the outputted code contains many
expected_template_out = expected_template_out % expected_context
self.assertEqual(template_output, expected_template_out)

# def test_swift_data_model_implementation_template(self):
# schema = Schema("./tests/files/test_schema.json")
# self.assertTemplateEqual('data_model.swift.j2', 'DataModel.swift', {
# 'project_name': "TestProject",
# 'schema': schema,
# 'VIDEO_FIELD': Field.VIDEO,
# 'IMAGE_FIELD': Field.IMAGE,
# 'today': datetime.today(),
# 'endpoints': URL.URL_ENDPOINTS.keys(),
# 'request_objects': SwiftTemplate.get_request_objects(schema.data_objects),
# 'sanitize_field_name': sanitize_field_name
# }, expected_context=(
# datetime.today().strftime('%m/%d/%Y'),
# ))

0 comments on commit 246ecb8

Please sign in to comment.