| Alexei Nicoara | 7d69b1d | 2022-07-11 12:38:50 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2022 The Android Open Source Project | 
|  | 4 | # | 
|  | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | # you may not use this file except in compliance with the License. | 
|  | 7 | # You may obtain a copy of the License at | 
|  | 8 | # | 
|  | 9 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | # Unless required by applicable law or agreed to in writing, software | 
|  | 12 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | # See the License for the specific language governing permissions and | 
|  | 15 | # limitations under the License. | 
|  | 16 | # | 
|  | 17 | """Tests for jsonmodify.""" | 
|  | 18 |  | 
|  | 19 | import json | 
|  | 20 | import jsonmodify | 
|  | 21 | import unittest | 
|  | 22 |  | 
|  | 23 |  | 
|  | 24 | class JsonmodifyTest(unittest.TestCase): | 
|  | 25 |  | 
|  | 26 | def test_set_value(self): | 
|  | 27 | obj = json.loads('{"field1": 111}') | 
|  | 28 | field1 = jsonmodify.SetValue("field1") | 
|  | 29 | field1.apply(obj, 222) | 
|  | 30 | field2 = jsonmodify.SetValue("field2") | 
|  | 31 | field2.apply(obj, 333) | 
|  | 32 | expected = json.loads('{"field1": 222, "field2": 333}') | 
|  | 33 | self.assertEqual(obj, expected) | 
|  | 34 |  | 
|  | 35 | def test_replace(self): | 
|  | 36 | obj = json.loads('{"field1": 111}') | 
|  | 37 | field1 = jsonmodify.Replace("field1") | 
|  | 38 | field1.apply(obj, 222) | 
|  | 39 | field2 = jsonmodify.Replace("field2") | 
|  | 40 | field2.apply(obj, 333) | 
|  | 41 | expected = json.loads('{"field1": 222}') | 
|  | 42 | self.assertEqual(obj, expected) | 
|  | 43 |  | 
|  | 44 | def test_replace_if_equal(self): | 
|  | 45 | obj = json.loads('{"field1": 111, "field2": 222}') | 
|  | 46 | field1 = jsonmodify.ReplaceIfEqual("field1") | 
|  | 47 | field1.apply(obj, 111, 333) | 
|  | 48 | field2 = jsonmodify.ReplaceIfEqual("field2") | 
|  | 49 | field2.apply(obj, 444, 555) | 
|  | 50 | field3 = jsonmodify.ReplaceIfEqual("field3") | 
|  | 51 | field3.apply(obj, 666, 777) | 
|  | 52 | expected = json.loads('{"field1": 333, "field2": 222}') | 
|  | 53 | self.assertEqual(obj, expected) | 
|  | 54 |  | 
|  | 55 | def test_remove(self): | 
|  | 56 | obj = json.loads('{"field1": 111, "field2": 222}') | 
|  | 57 | field2 = jsonmodify.Remove("field2") | 
|  | 58 | field2.apply(obj) | 
|  | 59 | field3 = jsonmodify.Remove("field3") | 
|  | 60 | field3.apply(obj) | 
|  | 61 | expected = json.loads('{"field1": 111}') | 
|  | 62 | self.assertEqual(obj, expected) | 
|  | 63 |  | 
|  | 64 | def test_append_list(self): | 
|  | 65 | obj = json.loads('{"field1": [111]}') | 
|  | 66 | field1 = jsonmodify.AppendList("field1") | 
|  | 67 | field1.apply(obj, 222, 333) | 
|  | 68 | field2 = jsonmodify.AppendList("field2") | 
|  | 69 | field2.apply(obj, 444, 555, 666) | 
|  | 70 | expected = json.loads('{"field1": [111, 222, 333], "field2": [444, 555, 666]}') | 
|  | 71 | self.assertEqual(obj, expected) | 
|  | 72 |  | 
|  | 73 |  | 
|  | 74 | if __name__ == '__main__': | 
|  | 75 | unittest.main(verbosity=2) |