afscgap.test.test_entry
Tests for library entry point vai the query function.
(c) 2023 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 library entry point vai the query function. 3 4(c) 2023 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 csv 11import unittest 12import unittest.mock 13 14import afscgap.test.test_tools 15 16# pylint: disable=C0115, C0116 17 18 19class EntryPointNoInferenceTests(unittest.TestCase): 20 21 def setUp(self): 22 self._result_1 = afscgap.test.test_tools.make_result_json( 23 'result_1.json' 24 ) 25 self._result_2 = afscgap.test.test_tools.make_result_json( 26 'result_2.json' 27 ) 28 self._mock_requestor = unittest.mock.MagicMock( 29 side_effect=[self._result_1, self._result_2] 30 ) 31 32 def test_query_primitive(self): 33 query = afscgap.Query(requestor=self._mock_requestor) 34 query.filter_year(eq=2021) 35 query.filter_srvy(eq='BSS') 36 results = list(query.execute()) 37 self.assertEquals(len(results), 20) 38 39 def test_query_dict(self): 40 query = afscgap.Query(requestor=self._mock_requestor) 41 query.filter_year(eq=2021) 42 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 43 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 44 results = list(query.execute()) 45 self.assertEquals(len(results), 20) 46 47 def test_query_keywords(self): 48 query = afscgap.Query(requestor=self._mock_requestor) 49 query.filter_year(eq=2021) 50 query.filter_latitude(min_val=56.99, max_val=57.04) 51 query.filter_longitude(min_val=-143.96, max_val=-144.01) 52 results = list(query.execute()) 53 self.assertEquals(len(results), 20) 54 55 def test_query_dict_filter_incomplete(self): 56 query = afscgap.Query(requestor=self._mock_requestor) 57 query.filter_year(eq=2021) 58 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 59 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 60 query.set_filter_incomplete(True) 61 results = list(query.execute()) 62 self.assertEquals(len(results), 19) 63 64 def test_query_dict_invalid_filter_incomplete(self): 65 query = afscgap.Query(requestor=self._mock_requestor) 66 query.filter_year(eq=2021) 67 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 68 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 69 query.set_filter_incomplete(True) 70 result = query.execute() 71 list(result) 72 self.assertEquals(result.get_invalid().qsize(), 2) 73 74 def test_query_dict_invalid_keep_incomplete(self): 75 query = afscgap.Query(requestor=self._mock_requestor) 76 query.filter_year(eq=2021) 77 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 78 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 79 query.set_filter_incomplete(False) 80 result = query.execute() 81 list(result) 82 self.assertEquals(result.get_invalid().qsize(), 1) 83 84 def test_create_param_eq(self): 85 query = afscgap.Query(requestor=self._mock_requestor) 86 self.assertEqual(query._create_param(2021), 2021) 87 88 def test_create_param_lt(self): 89 query = afscgap.Query(requestor=self._mock_requestor) 90 self.assertEqual(query._create_param(min_val=2021), [2021, None]) 91 92 def test_create_param_gt(self): 93 query = afscgap.Query(requestor=self._mock_requestor) 94 self.assertEqual(query._create_param(max_val=2021), [None, 2021]) 95 96 def test_create_param_between(self): 97 query = afscgap.Query(requestor=self._mock_requestor) 98 self.assertEqual( 99 query._create_param(min_val=2020, max_val=2021), 100 [2020, 2021] 101 ) 102 103 104class EntryPointInferenceTests(unittest.TestCase): 105 106 def setUp(self): 107 self._api_result = afscgap.test.test_tools.make_result_json( 108 'limited.json' 109 ) 110 self._hauls_result = afscgap.test.test_tools.make_result_text( 111 'hauls.csv' 112 ) 113 self._mock_requestor = unittest.mock.MagicMock( 114 side_effect=[self._hauls_result, self._api_result] 115 ) 116 117 def test_query_keep_presence_only(self): 118 mock_requestor = unittest.mock.MagicMock( 119 side_effect=[self._api_result] 120 ) 121 122 query = afscgap.Query(requestor=mock_requestor) 123 query.filter_year(eq=2021) 124 query.set_presence_only(True) 125 result = query.execute() 126 127 results = list(result) 128 self.assertEquals(len(results), 2) 129 130 def test_query_primitive(self): 131 warn_function = unittest.mock.MagicMock() 132 133 query = afscgap.Query(requestor=self._mock_requestor) 134 query.filter_year(eq=2021) 135 query.set_presence_only(False) 136 query.set_warn_function(warn_function) 137 result = query.execute() 138 139 results = list(result) 140 self.assertEquals(len(results), 4) 141 142 def test_query_primitive_warning(self): 143 warn_function = unittest.mock.MagicMock() 144 145 query = afscgap.Query(requestor=self._mock_requestor) 146 query.filter_year(eq=2021) 147 query.set_presence_only(False) 148 query.set_warn_function(warn_function) 149 result = query.execute() 150 151 warn_function.assert_called() 152 153 def test_query_primitive_suppress(self): 154 warn_function = unittest.mock.MagicMock() 155 156 query = afscgap.Query(requestor=self._mock_requestor) 157 query.filter_year(eq=2021) 158 query.set_presence_only(False) 159 query.set_warn_function(warn_function) 160 query.set_suppress_large_warning(True) 161 result = query.execute() 162 163 warn_function.assert_not_called() 164 165 def test_prefetch(self): 166 hauls_path = afscgap.test.test_tools.get_test_file_path('hauls.csv') 167 with open(hauls_path) as f: 168 rows = csv.DictReader(f) 169 hauls = [afscgap.inference.parse_haul(row) for row in rows] 170 171 warn_function = unittest.mock.MagicMock() 172 173 query = afscgap.Query(requestor=self._mock_requestor) 174 query.filter_year(eq=2021) 175 query.set_presence_only(False) 176 query.set_warn_function(warn_function) 177 query.set_suppress_large_warning(True) 178 query.set_hauls_prefetch(hauls) 179 result = query.execute() 180 181 self._mock_requestor.assert_not_called()
20class EntryPointNoInferenceTests(unittest.TestCase): 21 22 def setUp(self): 23 self._result_1 = afscgap.test.test_tools.make_result_json( 24 'result_1.json' 25 ) 26 self._result_2 = afscgap.test.test_tools.make_result_json( 27 'result_2.json' 28 ) 29 self._mock_requestor = unittest.mock.MagicMock( 30 side_effect=[self._result_1, self._result_2] 31 ) 32 33 def test_query_primitive(self): 34 query = afscgap.Query(requestor=self._mock_requestor) 35 query.filter_year(eq=2021) 36 query.filter_srvy(eq='BSS') 37 results = list(query.execute()) 38 self.assertEquals(len(results), 20) 39 40 def test_query_dict(self): 41 query = afscgap.Query(requestor=self._mock_requestor) 42 query.filter_year(eq=2021) 43 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 44 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 45 results = list(query.execute()) 46 self.assertEquals(len(results), 20) 47 48 def test_query_keywords(self): 49 query = afscgap.Query(requestor=self._mock_requestor) 50 query.filter_year(eq=2021) 51 query.filter_latitude(min_val=56.99, max_val=57.04) 52 query.filter_longitude(min_val=-143.96, max_val=-144.01) 53 results = list(query.execute()) 54 self.assertEquals(len(results), 20) 55 56 def test_query_dict_filter_incomplete(self): 57 query = afscgap.Query(requestor=self._mock_requestor) 58 query.filter_year(eq=2021) 59 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 60 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 61 query.set_filter_incomplete(True) 62 results = list(query.execute()) 63 self.assertEquals(len(results), 19) 64 65 def test_query_dict_invalid_filter_incomplete(self): 66 query = afscgap.Query(requestor=self._mock_requestor) 67 query.filter_year(eq=2021) 68 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 69 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 70 query.set_filter_incomplete(True) 71 result = query.execute() 72 list(result) 73 self.assertEquals(result.get_invalid().qsize(), 2) 74 75 def test_query_dict_invalid_keep_incomplete(self): 76 query = afscgap.Query(requestor=self._mock_requestor) 77 query.filter_year(eq=2021) 78 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 79 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 80 query.set_filter_incomplete(False) 81 result = query.execute() 82 list(result) 83 self.assertEquals(result.get_invalid().qsize(), 1) 84 85 def test_create_param_eq(self): 86 query = afscgap.Query(requestor=self._mock_requestor) 87 self.assertEqual(query._create_param(2021), 2021) 88 89 def test_create_param_lt(self): 90 query = afscgap.Query(requestor=self._mock_requestor) 91 self.assertEqual(query._create_param(min_val=2021), [2021, None]) 92 93 def test_create_param_gt(self): 94 query = afscgap.Query(requestor=self._mock_requestor) 95 self.assertEqual(query._create_param(max_val=2021), [None, 2021]) 96 97 def test_create_param_between(self): 98 query = afscgap.Query(requestor=self._mock_requestor) 99 self.assertEqual( 100 query._create_param(min_val=2020, max_val=2021), 101 [2020, 2021] 102 )
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.
22 def setUp(self): 23 self._result_1 = afscgap.test.test_tools.make_result_json( 24 'result_1.json' 25 ) 26 self._result_2 = afscgap.test.test_tools.make_result_json( 27 'result_2.json' 28 ) 29 self._mock_requestor = unittest.mock.MagicMock( 30 side_effect=[self._result_1, self._result_2] 31 )
Hook method for setting up the test fixture before exercising it.
40 def test_query_dict(self): 41 query = afscgap.Query(requestor=self._mock_requestor) 42 query.filter_year(eq=2021) 43 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 44 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 45 results = list(query.execute()) 46 self.assertEquals(len(results), 20)
48 def test_query_keywords(self): 49 query = afscgap.Query(requestor=self._mock_requestor) 50 query.filter_year(eq=2021) 51 query.filter_latitude(min_val=56.99, max_val=57.04) 52 query.filter_longitude(min_val=-143.96, max_val=-144.01) 53 results = list(query.execute()) 54 self.assertEquals(len(results), 20)
56 def test_query_dict_filter_incomplete(self): 57 query = afscgap.Query(requestor=self._mock_requestor) 58 query.filter_year(eq=2021) 59 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 60 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 61 query.set_filter_incomplete(True) 62 results = list(query.execute()) 63 self.assertEquals(len(results), 19)
65 def test_query_dict_invalid_filter_incomplete(self): 66 query = afscgap.Query(requestor=self._mock_requestor) 67 query.filter_year(eq=2021) 68 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 69 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 70 query.set_filter_incomplete(True) 71 result = query.execute() 72 list(result) 73 self.assertEquals(result.get_invalid().qsize(), 2)
75 def test_query_dict_invalid_keep_incomplete(self): 76 query = afscgap.Query(requestor=self._mock_requestor) 77 query.filter_year(eq=2021) 78 query.filter_latitude(eq={'$gte': 56.99, '$lte': 57.04}) 79 query.filter_longitude(eq={'$gte': -143.96, '$lte': -144.01}) 80 query.set_filter_incomplete(False) 81 result = query.execute() 82 list(result) 83 self.assertEquals(result.get_invalid().qsize(), 1)
Inherited Members
- unittest.case.TestCase
- TestCase
- failureException
- longMessage
- maxDiff
- addTypeEqualityFunc
- addCleanup
- enterContext
- addClassCleanup
- enterClassContext
- tearDown
- setUpClass
- tearDownClass
- countTestCases
- defaultTestResult
- shortDescription
- id
- subTest
- run
- doCleanups
- doClassCleanups
- debug
- skipTest
- fail
- assertFalse
- assertTrue
- assertRaises
- assertWarns
- assertLogs
- assertNoLogs
- assertEqual
- assertNotEqual
- assertAlmostEqual
- assertNotAlmostEqual
- assertSequenceEqual
- assertListEqual
- assertTupleEqual
- assertSetEqual
- assertIn
- assertNotIn
- assertIs
- assertIsNot
- assertDictEqual
- assertDictContainsSubset
- assertCountEqual
- assertMultiLineEqual
- assertLess
- assertLessEqual
- assertGreater
- assertGreaterEqual
- assertIsNone
- assertIsNotNone
- assertIsInstance
- assertNotIsInstance
- assertRaisesRegex
- assertWarnsRegex
- assertRegex
- assertNotRegex
- failUnlessRaises
- failIf
- assertRaisesRegexp
- assertRegexpMatches
- assertNotRegexpMatches
- failUnlessEqual
- assertEquals
- failIfEqual
- assertNotEquals
- failUnlessAlmostEqual
- assertAlmostEquals
- failIfAlmostEqual
- assertNotAlmostEquals
- failUnless
- assert_
105class EntryPointInferenceTests(unittest.TestCase): 106 107 def setUp(self): 108 self._api_result = afscgap.test.test_tools.make_result_json( 109 'limited.json' 110 ) 111 self._hauls_result = afscgap.test.test_tools.make_result_text( 112 'hauls.csv' 113 ) 114 self._mock_requestor = unittest.mock.MagicMock( 115 side_effect=[self._hauls_result, self._api_result] 116 ) 117 118 def test_query_keep_presence_only(self): 119 mock_requestor = unittest.mock.MagicMock( 120 side_effect=[self._api_result] 121 ) 122 123 query = afscgap.Query(requestor=mock_requestor) 124 query.filter_year(eq=2021) 125 query.set_presence_only(True) 126 result = query.execute() 127 128 results = list(result) 129 self.assertEquals(len(results), 2) 130 131 def test_query_primitive(self): 132 warn_function = unittest.mock.MagicMock() 133 134 query = afscgap.Query(requestor=self._mock_requestor) 135 query.filter_year(eq=2021) 136 query.set_presence_only(False) 137 query.set_warn_function(warn_function) 138 result = query.execute() 139 140 results = list(result) 141 self.assertEquals(len(results), 4) 142 143 def test_query_primitive_warning(self): 144 warn_function = unittest.mock.MagicMock() 145 146 query = afscgap.Query(requestor=self._mock_requestor) 147 query.filter_year(eq=2021) 148 query.set_presence_only(False) 149 query.set_warn_function(warn_function) 150 result = query.execute() 151 152 warn_function.assert_called() 153 154 def test_query_primitive_suppress(self): 155 warn_function = unittest.mock.MagicMock() 156 157 query = afscgap.Query(requestor=self._mock_requestor) 158 query.filter_year(eq=2021) 159 query.set_presence_only(False) 160 query.set_warn_function(warn_function) 161 query.set_suppress_large_warning(True) 162 result = query.execute() 163 164 warn_function.assert_not_called() 165 166 def test_prefetch(self): 167 hauls_path = afscgap.test.test_tools.get_test_file_path('hauls.csv') 168 with open(hauls_path) as f: 169 rows = csv.DictReader(f) 170 hauls = [afscgap.inference.parse_haul(row) for row in rows] 171 172 warn_function = unittest.mock.MagicMock() 173 174 query = afscgap.Query(requestor=self._mock_requestor) 175 query.filter_year(eq=2021) 176 query.set_presence_only(False) 177 query.set_warn_function(warn_function) 178 query.set_suppress_large_warning(True) 179 query.set_hauls_prefetch(hauls) 180 result = query.execute() 181 182 self._mock_requestor.assert_not_called()
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.
107 def setUp(self): 108 self._api_result = afscgap.test.test_tools.make_result_json( 109 'limited.json' 110 ) 111 self._hauls_result = afscgap.test.test_tools.make_result_text( 112 'hauls.csv' 113 ) 114 self._mock_requestor = unittest.mock.MagicMock( 115 side_effect=[self._hauls_result, self._api_result] 116 )
Hook method for setting up the test fixture before exercising it.
118 def test_query_keep_presence_only(self): 119 mock_requestor = unittest.mock.MagicMock( 120 side_effect=[self._api_result] 121 ) 122 123 query = afscgap.Query(requestor=mock_requestor) 124 query.filter_year(eq=2021) 125 query.set_presence_only(True) 126 result = query.execute() 127 128 results = list(result) 129 self.assertEquals(len(results), 2)
131 def test_query_primitive(self): 132 warn_function = unittest.mock.MagicMock() 133 134 query = afscgap.Query(requestor=self._mock_requestor) 135 query.filter_year(eq=2021) 136 query.set_presence_only(False) 137 query.set_warn_function(warn_function) 138 result = query.execute() 139 140 results = list(result) 141 self.assertEquals(len(results), 4)
143 def test_query_primitive_warning(self): 144 warn_function = unittest.mock.MagicMock() 145 146 query = afscgap.Query(requestor=self._mock_requestor) 147 query.filter_year(eq=2021) 148 query.set_presence_only(False) 149 query.set_warn_function(warn_function) 150 result = query.execute() 151 152 warn_function.assert_called()
154 def test_query_primitive_suppress(self): 155 warn_function = unittest.mock.MagicMock() 156 157 query = afscgap.Query(requestor=self._mock_requestor) 158 query.filter_year(eq=2021) 159 query.set_presence_only(False) 160 query.set_warn_function(warn_function) 161 query.set_suppress_large_warning(True) 162 result = query.execute() 163 164 warn_function.assert_not_called()
166 def test_prefetch(self): 167 hauls_path = afscgap.test.test_tools.get_test_file_path('hauls.csv') 168 with open(hauls_path) as f: 169 rows = csv.DictReader(f) 170 hauls = [afscgap.inference.parse_haul(row) for row in rows] 171 172 warn_function = unittest.mock.MagicMock() 173 174 query = afscgap.Query(requestor=self._mock_requestor) 175 query.filter_year(eq=2021) 176 query.set_presence_only(False) 177 query.set_warn_function(warn_function) 178 query.set_suppress_large_warning(True) 179 query.set_hauls_prefetch(hauls) 180 result = query.execute() 181 182 self._mock_requestor.assert_not_called()
Inherited Members
- unittest.case.TestCase
- TestCase
- failureException
- longMessage
- maxDiff
- addTypeEqualityFunc
- addCleanup
- enterContext
- addClassCleanup
- enterClassContext
- tearDown
- setUpClass
- tearDownClass
- countTestCases
- defaultTestResult
- shortDescription
- id
- subTest
- run
- doCleanups
- doClassCleanups
- debug
- skipTest
- fail
- assertFalse
- assertTrue
- assertRaises
- assertWarns
- assertLogs
- assertNoLogs
- assertEqual
- assertNotEqual
- assertAlmostEqual
- assertNotAlmostEqual
- assertSequenceEqual
- assertListEqual
- assertTupleEqual
- assertSetEqual
- assertIn
- assertNotIn
- assertIs
- assertIsNot
- assertDictEqual
- assertDictContainsSubset
- assertCountEqual
- assertMultiLineEqual
- assertLess
- assertLessEqual
- assertGreater
- assertGreaterEqual
- assertIsNone
- assertIsNotNone
- assertIsInstance
- assertNotIsInstance
- assertRaisesRegex
- assertWarnsRegex
- assertRegex
- assertNotRegex
- failUnlessRaises
- failIf
- assertRaisesRegexp
- assertRegexpMatches
- assertNotRegexpMatches
- failUnlessEqual
- assertEquals
- failIfEqual
- assertNotEquals
- failUnlessAlmostEqual
- assertAlmostEquals
- failIfAlmostEqual
- assertNotAlmostEquals
- failUnless
- assert_