Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -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 query_by_field.py |
| 8 | |
| 9 | """ |
| 10 | |
| 11 | import io |
| 12 | import sys |
| 13 | import unittest |
| 14 | |
| 15 | import platform_json_unittest |
| 16 | import query_by_field |
| 17 | |
| 18 | |
| 19 | def _run_main(argv): |
| 20 | """Run platform_json.main(argv), capturing and returning stdout.""" |
| 21 | original_stdout = sys.stdout |
| 22 | capture_io = io.StringIO() |
| 23 | sys.stdout = capture_io |
| 24 | try: |
| 25 | query_by_field.main(argv) |
| 26 | return capture_io.getvalue() |
| 27 | finally: |
| 28 | capture_io.close() |
| 29 | sys.stdout = original_stdout |
| 30 | |
| 31 | |
| 32 | class SingleOperatorTestCase(platform_json_unittest.AbstractMockConfigTestCase, |
| 33 | unittest.TestCase): |
| 34 | """Test-case for using a single operator of each type.""" |
| 35 | def runTest(self): # pylint: disable=invalid-name |
| 36 | """Test each operator in isolation.""" |
| 37 | # Equality operator ('=') |
| 38 | argv = ['field1=5', '-c', self.mock_filepath] |
| 39 | expected = 'DEFAULTS\n' |
| 40 | self.assertEqual(_run_main(argv), expected) |
| 41 | |
| 42 | # Inequality operator ('!=') |
| 43 | argv = ['field1!=5', '-c', self.mock_filepath] |
| 44 | expected = 'my_platform\nmy_parent\nmy_grandparent\n' |
| 45 | self.assertEqual(_run_main(argv), expected) |
| 46 | |
Greg Edelston | b3efad2 | 2020-11-24 11:09:39 -0700 | [diff] [blame^] | 47 | # Array membership operator (':') |
| 48 | argv = ['fieldArray:elem2', '-c', self.mock_filepath] |
| 49 | expected = 'my_platform\n' |
| 50 | self.assertEqual(_run_main(argv), expected) |
| 51 | |
| 52 | # Array non-membership operator ('!:) |
| 53 | argv = ['fieldArray!:elem5', '-c', self.mock_filepath] |
| 54 | expected = 'my_platform\n' |
| 55 | self.assertEqual(_run_main(argv), expected) |
| 56 | |
| 57 | |
| 58 | class OperatorTypeMismatchTestCase(platform_json_unittest.AbstractMockConfigTestCase, |
| 59 | unittest.TestCase): |
| 60 | """Test-case for exceptions being raised for operator-type mismatches.""" |
| 61 | def runTest(self): # pylint: disable=invalid-name |
| 62 | """Test each operator which can have invalid types.""" |
| 63 | # Array operators only make sense with array fields. |
| 64 | for operator in (':', '!:'): |
| 65 | query = 'field1%s1' % operator |
| 66 | argv = [query, '-c', self.mock_filepath] |
| 67 | with self.assertRaises(query_by_field.NotIterableError): |
| 68 | _run_main(argv) |
| 69 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame] | 70 | |
| 71 | class MultipleOperatorsTestCase(platform_json_unittest.AbstractMockConfigTestCase, |
| 72 | unittest.TestCase): |
| 73 | """Test-case for using multiple operators.""" |
| 74 | def runTest(self): # pylint: disable=invalid-name |
| 75 | """Test using multiple operators.""" |
| 76 | argv = ['field1!=1', 'field1!=4', '-c', self.mock_filepath] |
| 77 | expected = 'DEFAULTS\nmy_parent\nmy_grandparent\n' |
| 78 | self.assertEqual(_run_main(argv), expected) |
| 79 | |
| 80 | |
| 81 | class ModelExceptionsTestCase(platform_json_unittest.AbstractMockConfigTestCase, |
| 82 | unittest.TestCase): |
| 83 | """Test-case for when model overrides change whether a platform matches.""" |
| 84 | def runTest(self): # pylint: disable=invalid-name |
| 85 | """Test all variants of model exceptions.""" |
| 86 | # Check for when a platform does not match, except for one model |
| 87 | argv = ['field1=4', '-c', self.mock_filepath] |
| 88 | expected = 'my_platform (only my_model)\n' |
| 89 | self.assertEqual(_run_main(argv), expected) |
| 90 | |
| 91 | # Check for when a platform matches, except for one model |
| 92 | argv = ['field1=1', '-c', self.mock_filepath] |
| 93 | expected = 'my_platform (except my_model)\n' |
| 94 | self.assertEqual(_run_main(argv), expected) |
| 95 | |
| 96 | # Check for multiple model exceptions |
| 97 | argv = ['field1=1', 'field2=2', '-c', self.mock_filepath] |
| 98 | expected = 'my_platform (except my_model, my_model2)\n' |
| 99 | self.assertEqual(_run_main(argv), expected) |
| 100 | |
| 101 | |
| 102 | class NoMatchesTestCase(platform_json_unittest.AbstractMockConfigTestCase, |
| 103 | unittest.TestCase): |
| 104 | """Test-case for when no platforms match the query.""" |
| 105 | def runTest(self): #pylint: disable=invalid-name |
| 106 | """Run script with a bogus query.""" |
| 107 | argv = ['field1=1337', '-c', self.mock_filepath] |
| 108 | expected = 'No platforms matched.\n' |
| 109 | self.assertEqual(_run_main(argv), expected) |
| 110 | |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | unittest.main() |