afscgap.test.test_flat_cursor
Tests for flat records cursor and its decorators.
(c) 2025 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.
This file is part of afscgap released under the BSD 3-Clause License. See LICENSE.md.
1""" 2Tests for flat records cursor and its decorators. 3 4(c) 2025 Regents of University of California / The Eric and Wendy Schmidt Center 5for Data Science and the Environment at UC Berkeley. 6 7This file is part of afscgap released under the BSD 3-Clause License. See 8LICENSE.md. 9""" 10import unittest 11import unittest.mock 12 13import afscgap.flat_cursor 14 15 16class FlatCursorTests(unittest.TestCase): 17 18 def setUp(self): 19 def make_record(target_id): 20 mock = unittest.mock.MagicMock() 21 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 22 mock.to_dict = unittest.mock.MagicMock(return_value={'id': target_id}) 23 return mock 24 25 self._records = [make_record(1), make_record(2), make_record(3)] 26 self._cursor = afscgap.flat_cursor.FlatCursor(self._records) 27 28 def test_get_attrs(self): 29 self.assertIsNone(self._cursor.get_limit()) 30 self.assertFalse(self._cursor.get_filtering_incomplete()) 31 32 def test_get_next(self): 33 first = self._cursor.get_next() 34 second = self._cursor.get_next() 35 third = self._cursor.get_next() 36 self.assertEqual(first.get_id(), 1) 37 self.assertEqual(second.get_id(), 2) 38 self.assertEqual(third.get_id(), 2) 39 self.assertIsNone(self._cursor.get_next()) 40 41 def test_to_dicts(self): 42 dicts = self._cursor.to_dicts() 43 self.assertEqual(len(dicts), 3) 44 self.assertEqual(dicts[0]['id'], 1) 45 self.assertEqual(dicts[1]['id'], 2) 46 self.assertEqual(dicts[2]['id'], 3) 47 48 49class CompleteCursorTests(unittest.TestCase): 50 51 def setUp(self): 52 def make_record(target_id, complete): 53 mock = unittest.mock.MagicMock() 54 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 55 mock.is_complete = unittest.mock.MagicMock(return_value=complete) 56 mock.to_dict = unittest.mock.MagicMock(return_value={ 57 'id': target_id, 58 'complete': complete 59 }) 60 return mock 61 62 self._records = [make_record(1, True), make_record(2, False), make_record(3, True)] 63 self._inner_cursor = afscgap.flat_cursor.FlatCursor(self._records) 64 self._cursor = afscgap.flat_cursor.CompleteCursor(self._inner_cursor) 65 66 def test_get_attrs(self): 67 self.assertIsNone(self._cursor.get_limit()) 68 self.assertTrue(self._cursor.get_filtering_incomplete()) 69 70 def test_get_next(self): 71 first = self._cursor.get_next() 72 second = self._cursor.get_next() 73 self.assertEqual(first.get_id(), 1) 74 self.assertEqual(second.get_id(), 3) 75 self.assertIsNone(self._cursor.get_next()) 76 77 def test_to_dicts(self): 78 dicts = list(self._cursor.to_dicts()) 79 self.assertEqual(len(dicts), 2) 80 self.assertEqual(dicts[0]['id'], 1) 81 self.assertEqual(dicts[1]['id'], 3) 82 83 84class FlatCursorTests(unittest.TestCase): 85 86 def setUp(self): 87 def make_record(target_id): 88 mock = unittest.mock.MagicMock() 89 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 90 mock.to_dict = unittest.mock.MagicMock(return_value={'id': target_id}) 91 return mock 92 93 self._records = [make_record(1), make_record(2), make_record(3)] 94 self._inner_cursor = afscgap.flat_cursor.FlatCursor(self._records) 95 self._cursor = afscgap.flat_cursor.LimitCursor(self._inner_cursor, 2) 96 97 def test_get_attrs(self): 98 self.assertEqual(self._cursor.get_limit(), 2) 99 self.assertFalse(self._cursor.get_filtering_incomplete()) 100 101 def test_get_next(self): 102 first = self._cursor.get_next() 103 second = self._cursor.get_next() 104 self.assertEqual(first.get_id(), 1) 105 self.assertEqual(second.get_id(), 2) 106 self.assertIsNone(self._cursor.get_next()) 107 108 def test_to_dicts(self): 109 dicts = list(self._cursor.to_dicts()) 110 self.assertEqual(len(dicts), 2) 111 self.assertEqual(dicts[0]['id'], 1) 112 self.assertEqual(dicts[1]['id'], 2)
17class FlatCursorTests(unittest.TestCase): 18 19 def setUp(self): 20 def make_record(target_id): 21 mock = unittest.mock.MagicMock() 22 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 23 mock.to_dict = unittest.mock.MagicMock(return_value={'id': target_id}) 24 return mock 25 26 self._records = [make_record(1), make_record(2), make_record(3)] 27 self._cursor = afscgap.flat_cursor.FlatCursor(self._records) 28 29 def test_get_attrs(self): 30 self.assertIsNone(self._cursor.get_limit()) 31 self.assertFalse(self._cursor.get_filtering_incomplete()) 32 33 def test_get_next(self): 34 first = self._cursor.get_next() 35 second = self._cursor.get_next() 36 third = self._cursor.get_next() 37 self.assertEqual(first.get_id(), 1) 38 self.assertEqual(second.get_id(), 2) 39 self.assertEqual(third.get_id(), 2) 40 self.assertIsNone(self._cursor.get_next()) 41 42 def test_to_dicts(self): 43 dicts = self._cursor.to_dicts() 44 self.assertEqual(len(dicts), 3) 45 self.assertEqual(dicts[0]['id'], 1) 46 self.assertEqual(dicts[1]['id'], 2) 47 self.assertEqual(dicts[2]['id'], 3)
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
87 def setUp(self): 88 def make_record(target_id): 89 mock = unittest.mock.MagicMock() 90 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 91 mock.to_dict = unittest.mock.MagicMock(return_value={'id': target_id}) 92 return mock 93 94 self._records = [make_record(1), make_record(2), make_record(3)] 95 self._inner_cursor = afscgap.flat_cursor.FlatCursor(self._records) 96 self._cursor = afscgap.flat_cursor.LimitCursor(self._inner_cursor, 2)
Hook method for setting up the test fixture before exercising it.
50class CompleteCursorTests(unittest.TestCase): 51 52 def setUp(self): 53 def make_record(target_id, complete): 54 mock = unittest.mock.MagicMock() 55 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 56 mock.is_complete = unittest.mock.MagicMock(return_value=complete) 57 mock.to_dict = unittest.mock.MagicMock(return_value={ 58 'id': target_id, 59 'complete': complete 60 }) 61 return mock 62 63 self._records = [make_record(1, True), make_record(2, False), make_record(3, True)] 64 self._inner_cursor = afscgap.flat_cursor.FlatCursor(self._records) 65 self._cursor = afscgap.flat_cursor.CompleteCursor(self._inner_cursor) 66 67 def test_get_attrs(self): 68 self.assertIsNone(self._cursor.get_limit()) 69 self.assertTrue(self._cursor.get_filtering_incomplete()) 70 71 def test_get_next(self): 72 first = self._cursor.get_next() 73 second = self._cursor.get_next() 74 self.assertEqual(first.get_id(), 1) 75 self.assertEqual(second.get_id(), 3) 76 self.assertIsNone(self._cursor.get_next()) 77 78 def test_to_dicts(self): 79 dicts = list(self._cursor.to_dicts()) 80 self.assertEqual(len(dicts), 2) 81 self.assertEqual(dicts[0]['id'], 1) 82 self.assertEqual(dicts[1]['id'], 3)
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
52 def setUp(self): 53 def make_record(target_id, complete): 54 mock = unittest.mock.MagicMock() 55 mock.get_id = unittest.mock.MagicMock(return_value=target_id) 56 mock.is_complete = unittest.mock.MagicMock(return_value=complete) 57 mock.to_dict = unittest.mock.MagicMock(return_value={ 58 'id': target_id, 59 'complete': complete 60 }) 61 return mock 62 63 self._records = [make_record(1, True), make_record(2, False), make_record(3, True)] 64 self._inner_cursor = afscgap.flat_cursor.FlatCursor(self._records) 65 self._cursor = afscgap.flat_cursor.CompleteCursor(self._inner_cursor)
Hook method for setting up the test fixture before exercising it.