Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ |
| 7 | Unit tests for platform_json.py |
| 8 | |
| 9 | """ |
| 10 | |
| 11 | import os |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | |
| 15 | import platform_json |
| 16 | |
| 17 | MOCK_CONSOLIDATED_JSON_CONTENTS = \ |
| 18 | '''{ |
| 19 | "DEFAULTS": { |
| 20 | "platform": null, |
| 21 | "parent": null, |
| 22 | "field1": 5, |
| 23 | "field2": 5, |
| 24 | "field3": 5, |
| 25 | "field4": 5 |
| 26 | }, |
| 27 | "my_platform": { |
| 28 | "platform": "my_platform", |
| 29 | "parent": "my_parent", |
| 30 | "field1": 1, |
| 31 | "models": { |
| 32 | "my_model": { |
| 33 | "field1": 4 |
| 34 | } |
| 35 | } |
| 36 | }, |
| 37 | "my_parent": { |
| 38 | "platform": "my_parent", |
| 39 | "parent": "my_grandparent", |
| 40 | "field1": 2, |
| 41 | "field2": 2 |
| 42 | }, |
| 43 | "my_grandparent": { |
| 44 | "platform": "my_grandparent", |
| 45 | "field1": 3, |
| 46 | "field2": 3, |
| 47 | "field3": 3 |
| 48 | } |
| 49 | }''' |
| 50 | |
| 51 | class InheritanceTestCase(unittest.TestCase): |
| 52 | """Ensure that all levels of inheritance are handled correctly""" |
| 53 | |
| 54 | def setUp(self): |
| 55 | """Write mock JSON to a temporary file""" |
| 56 | _, self.mock_filepath = tempfile.mkstemp() |
| 57 | with open(self.mock_filepath, 'w') as mock_file: |
| 58 | mock_file.write(MOCK_CONSOLIDATED_JSON_CONTENTS) |
| 59 | |
| 60 | def runTest(self): # pylint:disable=invalid-name |
| 61 | """Load platform config and check that it looks correct""" |
| 62 | my_platform = platform_json.calculate_platform_json('my_platform', |
| 63 | None, |
| 64 | self.mock_filepath) |
| 65 | self.assertEqual(my_platform['field1'], 1) # No inheritance |
| 66 | self.assertEqual(my_platform['field2'], 2) # Direct inheritance |
| 67 | self.assertEqual(my_platform['field3'], 3) # Recursive inheritance |
| 68 | self.assertEqual(my_platform['field4'], 5) # Inherit from DEFAULTS |
| 69 | my_model = platform_json.calculate_platform_json('my_platform', |
| 70 | 'my_model', |
| 71 | self.mock_filepath) |
| 72 | self.assertEqual(my_model['field1'], 4) # Model override |
| 73 | self.assertEqual(my_model['field2'], 2) # Everything else is the same |
| 74 | self.assertEqual(my_model['field3'], 3) |
| 75 | self.assertEqual(my_model['field4'], 5) |
| 76 | |
| 77 | |
| 78 | def tearDown(self): |
| 79 | """Dsetroy the mock JSON file""" |
| 80 | os.remove(self.mock_filepath) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | unittest.main() |