-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out processing results for better testability.
Better date deserialization Added more unit tests
- Loading branch information
Showing
5 changed files
with
158 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
|
||
from mygeotab import api | ||
|
||
|
||
class TestProcessResults(unittest.TestCase): | ||
def setUp(self): | ||
self.api = api.API("test@example.com", session_id=123) | ||
|
||
def test_handle_server_exception(self): | ||
exception_response = dict(error=dict(errors=[dict( | ||
message=u'The method "Get" could not be found. Verify the method name and ensure all method parameters are ' | ||
u'included. Request Json: {"params": {"typeName": "Passwords", "credentials": {"userName": ' | ||
u'"test@example.com", "sessionId": "12345678901234567890", "database": "my_company"}}, "method": ' | ||
u'"Get", "id": -1}', | ||
name=u'MissingMethodException', | ||
stackTrace=u' at Geotab.Checkmate.Web.APIV1.ProcessRequest(IHttpRequest httpRequest, HttpResponse ' | ||
u'httpResponse, String methodName, Dictionary`2 parameters, Action`2 parametersJSONToTokens, ' | ||
u'Action`1 handleException, IProfiler profile, Credentials credentials, Int32 requestIndex, ' | ||
u'Object requestJsonOrHashMap, Boolean& isAsync) in ' | ||
u'c:\\ProgramData\\GEOTAB\\Checkmate\\BuildServer\\master\\WorkingDirectory\\Checkmate\\CheckmateServer\\Geotab\\Checkmate\\Web\\APIV1.cs:line 813\r\n ' | ||
u'at Geotab.Checkmate.Web.APIV1.<>c__DisplayClass13.<ProcessRequest>b__b() ' | ||
u'in c:\\ProgramData\\GEOTAB\\Checkmate\\BuildServer\\master\\WorkingDirectory\\Checkmate\\CheckmateServer\\Geotab\\Checkmate\\Web\\APIV1.cs:line 558\r\n ' | ||
u'at Geotab.Checkmate.Web.APIV1.ExecuteHandleException(Action action) in ' | ||
u'c:\\ProgramData\\GEOTAB\\Checkmate\\BuildServer\\master\\WorkingDirectory\\Checkmate\\CheckmateServer\\Geotab\\Checkmate\\Web\\APIV1.cs:line 632')], | ||
message=u'The method "Get" could not be found. Verify the method name and ensure all method parameters are ' | ||
u'included. Request Json: {"params": {"typeName": "Passwords", "credentials": {"userName": ' | ||
u'"test@example.com", "sessionId": "12345678901234567890", "database": "my_company"}}, "method": ' | ||
u'"Get", "id": -1}', | ||
name=u'JSONRPCError'), requestIndex=0) | ||
with self.assertRaises(api.MyGeotabException) as cm: | ||
self.api._process(exception_response) | ||
ex = cm.exception | ||
self.assertEqual(ex.name, 'MissingMethodException') | ||
self.assertEqual(ex.message, | ||
'The method "Get" could not be found. Verify the method name and ensure all method ' | ||
'parameters are included. Request Json: {"params": {"typeName": "Passwords", ' | ||
'"credentials": {"userName": "test@example.com", "sessionId": "12345678901234567890", ' | ||
'"database": "my_company"}}, "method": "Get", "id": -1}') | ||
|
||
def test_handle_server_results(self): | ||
results_response = {'result': [ | ||
dict( | ||
id='b123', | ||
name='test@example.com' | ||
) | ||
]} | ||
result = self.api._process(results_response) | ||
self.assertEqual(len(result), 1) | ||
self.assertEqual(result[0]['name'], 'test@example.com') | ||
self.assertEqual(result[0]['id'], 'b123') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
import json | ||
from datetime import datetime | ||
|
||
import pytz | ||
|
||
from mygeotab import serializers | ||
|
||
|
||
def json_serialize(data): | ||
return json.dumps(data, default=serializers.object_serializer) | ||
|
||
|
||
def json_deserialize(data_str): | ||
return json.loads(data_str, object_hook=serializers.object_deserializer) | ||
|
||
|
||
class TestSerialization(unittest.TestCase): | ||
def setUp(self): | ||
pass | ||
|
||
def test_top_level_utc_datetime(self): | ||
data = dict( | ||
dateTime=datetime(2015, 6, 5, 2, 3, 44) | ||
) | ||
expected_str = '{"dateTime": "2015-06-05T02:03:44Z"}' | ||
data_str = json_serialize(data) | ||
self.assertEqual(data_str, expected_str) | ||
|
||
def test_top_level_zoned_datetime(self): | ||
est = pytz.timezone('US/Eastern') | ||
data = dict( | ||
dateTime=est.localize(datetime(2015, 6, 4, 3, 3, 43)) | ||
) | ||
expected_str = '{"dateTime": "2015-06-04T07:03:43Z"}' | ||
data_str = json_serialize(data) | ||
self.assertEqual(data_str, expected_str) | ||
|
||
|
||
class TestDeserialization(unittest.TestCase): | ||
def setUp(self): | ||
pass | ||
|
||
def test_top_level_datetime(self): | ||
data_str = '{"dateTime": "2015-06-04T07:03:43Z"}' | ||
data = json_deserialize(data_str) | ||
utc_date = data.get('dateTime') | ||
self.assertIsNotNone(utc_date) | ||
check_date = datetime(2015, 6, 4, 7, 3, 43) | ||
self.assertEqual(utc_date.year, check_date.year) | ||
self.assertEqual(utc_date.month, check_date.month) | ||
self.assertEqual(utc_date.day, check_date.day) | ||
self.assertEqual(utc_date.hour, check_date.hour) | ||
self.assertEqual(utc_date.minute, check_date.minute) | ||
self.assertEqual(utc_date.second, check_date.second) | ||
|
||
def test_second_level_datetime(self): | ||
data_str = '[{"group": {"dateTime": "2015-06-04T07:03:43Z"}}]' | ||
data = json_deserialize(data_str) | ||
self.assertEqual(len(data), 1) | ||
group = data[0].get('group') | ||
self.assertIsNotNone(group) | ||
utc_date = group.get('dateTime') | ||
self.assertIsNotNone(utc_date) | ||
check_date = datetime(2015, 6, 4, 7, 3, 43) | ||
self.assertEqual(utc_date.year, check_date.year) | ||
self.assertEqual(utc_date.month, check_date.month) | ||
self.assertEqual(utc_date.day, check_date.day) | ||
self.assertEqual(utc_date.hour, check_date.hour) | ||
self.assertEqual(utc_date.minute, check_date.minute) | ||
self.assertEqual(utc_date.second, check_date.second) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters