-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AGENT-65 and AGENT-66 - Broken unit test (json encoding) #160
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17e142d
WIP fix and unit test
echee2 c1cae79
Refactor util.py to enable dynamic json lib selection. Custom encoder…
echee2 bdb6af9
Remove force json env variable
echee2 f4916b2
Eliminate custom encoding. Fix unit test
echee2 f905e65
Modify test_encode_decode to expect TypeError for json/ujson
echee2 be5320d
Remove asList/asDict
echee2 f4ccca3
Fix comment
echee2 7fa9870
Remove unused constants
echee2 740fd52
Catch invalid library choice
echee2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,112 @@ | ||
# Copyright 2014 Scalyr Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ------------------------------------------------------------------------ | ||
# | ||
# author: Edward Chee <echee@scalyr.com> | ||
|
||
__author__ = 'echee@scalyr.com' | ||
|
||
|
||
import unittest | ||
|
||
from scalyr_agent import util | ||
from scalyr_agent.json_lib import JsonObject | ||
from scalyr_agent.json_lib import JsonArray | ||
from scalyr_agent.test_base import ScalyrTestCase | ||
|
||
JSON = 1 | ||
UJSON = 2 | ||
FALLBACK = 3 | ||
|
||
|
||
class EncodeDecodeTest(ScalyrTestCase): | ||
"""This test ensures that JsonObject and JsonArray can be correctly encoded/decoded with different JSON libraries""" | ||
|
||
def _setlib(self, library): | ||
if library == JSON: | ||
util._set_json_lib('json') | ||
elif library == UJSON: | ||
util._set_json_lib('ujson') | ||
else: | ||
util._set_json_lib('json_lib') | ||
|
||
def test_invalid_lib(self): | ||
with self.assertRaises(ValueError): | ||
util._set_json_lib('BAD JSON LIBRARY NAME') | ||
|
||
def test_dict(self): | ||
self.__test_encode_decode('{"a":1,"b":2}', {u'a': 1, u'b': 2}) | ||
|
||
def test_dict2(self): | ||
self.__test_encode_decode('{"a":1,"b":{"c":2}}', {u'a': 1, u'b': {u'c': 2}}) | ||
|
||
def test_str(self): | ||
self.__test_encode_decode(r'"a"', u'a') | ||
|
||
def test_int(self): | ||
self.__test_encode_decode(r'1', 1) | ||
|
||
def test_bool(self): | ||
self.__test_encode_decode(r'false', False) | ||
self.__test_encode_decode(r'true', True) | ||
|
||
def test_float(self): | ||
self.__test_encode_decode(r'1.0003', 1.0003) | ||
|
||
def test_list(self): | ||
self.__test_encode_decode(r'[1,2,3]', [1, 2, 3]) | ||
|
||
def test_list2(self): | ||
self.__test_encode_decode(r'[1,2,"a"]', [1, 2, u'a']) | ||
|
||
def test_jsonarray(self): | ||
self.__test_encode_decode(r'[1,2,3]', JsonArray(1, 2, 3)) | ||
|
||
def test_jsonobject(self): | ||
self.__test_encode_decode(r'{"a":1,"b":2}', JsonObject({u'a': 1, u'b': 2})) | ||
|
||
def test_jsonobject_nested_dict(self): | ||
self.__test_encode_decode(r'{"a":{"b":{"c":3}}}', JsonObject({u'a': JsonObject({u'b': JsonObject({u'c': 3})})})) | ||
|
||
def test_jsonobject_nested_jsonarray(self): | ||
self.__test_encode_decode(r'{"a":[1,2,3]}', JsonObject({u'a': JsonArray(1, 2, 3)})) | ||
|
||
def test_jsonobject_nested_jsonarray2(self): | ||
self.__test_encode_decode(r'{"a":[1,2,3,[1,2,3]]}', JsonObject({u'a': JsonArray(1, 2, 3, JsonArray(1, 2, 3))})) | ||
|
||
def __test_encode_decode(self, text, obj): | ||
def __runtest(library): | ||
self._setlib(library) | ||
|
||
if library == FALLBACK or not isinstance(obj, (JsonArray, JsonObject)): | ||
text2 = util.json_encode(obj) | ||
self.assertEquals(text, text2) | ||
obj2 = util.json_decode(text2) | ||
text3 = util.json_encode(obj2) | ||
self.assertEquals(text, text3) | ||
else: | ||
with self.assertRaises(TypeError): | ||
util.json_encode(obj) | ||
|
||
__runtest(JSON) | ||
__runtest(UJSON) | ||
__runtest(FALLBACK) | ||
|
||
|
||
def main(): | ||
unittest.main() | ||
|
||
|
||
if __name__ == '__main__': | ||
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone passes in a
lib_name
value that is notjson
ujson
orjson_lib
(for whatever reason, maybe a spelling mistake, or maybe a new json library is added), then the function will reach the end and returnNone
, instead of an expected 3-value tuple.It's not a big issue, because it'll probably never happen and if it does we'll get a compile error, but maybe if we get an unrecognized name then we raise an exception with a message that the json
lib_name
is unsupported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, good suggestion. Change has been made & unit test installed to capture.