afscgap.test.test_inference

Tests for zero catch inference utilities.

(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 zero catch inference utilities.
  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 unittest
 11import unittest.mock
 12
 13import afscgap.inference
 14
 15
 16class InferenceTests(unittest.TestCase):
 17
 18    def setUp(self):
 19        self._api_result = afscgap.test.test_tools.make_result_json(
 20            'limited.json'
 21        )
 22        self._api_mock_requsetor = unittest.mock.MagicMock(
 23            return_value=self._api_result
 24        )
 25        self._api_cursor = afscgap.client.ApiServiceCursor(
 26            'BASE_URL',
 27            requestor=self._api_mock_requsetor
 28        )
 29
 30        self._hauls_result = afscgap.test.test_tools.make_result_text(
 31            'hauls.csv'
 32        )
 33        self._hauls_mock_requsetor = unittest.mock.MagicMock(
 34            return_value=self._hauls_result
 35        )
 36
 37        self._all_hauls_data = afscgap.inference.get_hauls_data(
 38            {},
 39            self._hauls_mock_requsetor
 40        )
 41
 42        self._decorated_cursor = afscgap.inference.build_inference_cursor(
 43            {},
 44            self._api_cursor,
 45            requestor=self._hauls_mock_requsetor
 46        )
 47
 48    def test_build_params_checker_rejects(self):
 49        checker = afscgap.inference.build_params_checker({'year': 2021})
 50        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 51        example_2021 = list(data_2021)[0]
 52        self.assertTrue(checker(example_2021))
 53
 54
 55    def test_build_params_checker_accepts(self):
 56        checker = afscgap.inference.build_params_checker({'year': 2021})
 57        data_2021 = filter(lambda x: x.get_year() != 2021, self._all_hauls_data)
 58        example_2021 = list(data_2021)[0]
 59        self.assertFalse(checker(example_2021))
 60
 61    def test_get_all_hauls(self):
 62        self.assertEquals(len(self._all_hauls_data), 3)
 63        
 64        years = set(map(lambda x: int(x.get_year()), self._all_hauls_data))
 65        self.assertEquals(len(years), 2)
 66        self.assertTrue(2021 in years)
 67        self.assertTrue(2000 in years)
 68
 69    def test_filter_hauls(self):
 70        filtered_hauls = afscgap.inference.get_hauls_data(
 71            {'year': 2021},
 72            self._hauls_mock_requsetor
 73        )
 74
 75        self.assertEquals(len(filtered_hauls), 2)
 76
 77    def test_decorator_infer_pass_through(self):
 78        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 79        example_2021 = list(data_2021)[0]
 80        decorated = afscgap.inference.ZeroCatchHaulDecorator(
 81            example_2021,
 82            'science',
 83            'fish',
 84            12,
 85            34,
 86            567
 87        )
 88        self.assertEquals(decorated.get_year(), 2021)
 89
 90    def test_decorator_infer_override(self):
 91        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 92        example_2021 = list(data_2021)[0]
 93        decorated = afscgap.inference.ZeroCatchHaulDecorator(
 94            example_2021,
 95            'science',
 96            'fish',
 97            12,
 98            34,
 99            567
100        )
101        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
102
103    def test_decorator_infer_given(self):
104        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
105        example_2021 = list(data_2021)[0]
106        decorated = afscgap.inference.ZeroCatchHaulDecorator(
107            example_2021,
108            'science',
109            'fish',
110            12,
111            34,
112            567
113        )
114        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
115
116    def test_decorator_dict(self):
117        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
118        example_2021 = list(data_2021)[0]
119        decorated = afscgap.inference.ZeroCatchHaulDecorator(
120            example_2021,
121            'science',
122            'fish',
123            12,
124            34,
125            567
126        )
127        decorated_dict = decorated.to_dict()
128        self.assertEquals(decorated_dict['year'], 2021)
129
130    def test_get_haul_key(self):
131        example_api = list(self._api_cursor)[0]
132        api_key = self._decorated_cursor._get_haul_key(example_api)
133
134        match_hauls = filter(
135            lambda x: int(x.get_haul()) == 215,
136            self._all_hauls_data
137        )
138        example_haul = list(match_hauls)[0]
139        haul_key = self._decorated_cursor._get_haul_key(example_haul)
140
141        self.assertEqual(api_key, haul_key)
142
143    def test_build_cursor_no_params(self):
144        results = list(self._decorated_cursor)
145        self.assertEqual(len(results), 6)
146
147        haul_ids = set(map(lambda x: int(x.get_haul()), results))
148        self.assertEqual(len(haul_ids), 3)
149        self.assertTrue(110 in haul_ids)
150        self.assertTrue(214 in haul_ids)
151        self.assertTrue(215 in haul_ids)
152
153    def test_build_cursor_params(self):
154        decorated_cursor = afscgap.inference.build_inference_cursor(
155            {'year': 2021},
156            self._api_cursor,
157            requestor=self._hauls_mock_requsetor
158        )
159
160        results = list(decorated_cursor)
161        self.assertEqual(len(results), 4)
162
163        haul_ids = set(map(lambda x: int(x.get_haul()), results))
164        self.assertEqual(len(haul_ids), 2)
165        self.assertTrue(214 in haul_ids)
166        self.assertTrue(215 in haul_ids)
class InferenceTests(unittest.case.TestCase):
 17class InferenceTests(unittest.TestCase):
 18
 19    def setUp(self):
 20        self._api_result = afscgap.test.test_tools.make_result_json(
 21            'limited.json'
 22        )
 23        self._api_mock_requsetor = unittest.mock.MagicMock(
 24            return_value=self._api_result
 25        )
 26        self._api_cursor = afscgap.client.ApiServiceCursor(
 27            'BASE_URL',
 28            requestor=self._api_mock_requsetor
 29        )
 30
 31        self._hauls_result = afscgap.test.test_tools.make_result_text(
 32            'hauls.csv'
 33        )
 34        self._hauls_mock_requsetor = unittest.mock.MagicMock(
 35            return_value=self._hauls_result
 36        )
 37
 38        self._all_hauls_data = afscgap.inference.get_hauls_data(
 39            {},
 40            self._hauls_mock_requsetor
 41        )
 42
 43        self._decorated_cursor = afscgap.inference.build_inference_cursor(
 44            {},
 45            self._api_cursor,
 46            requestor=self._hauls_mock_requsetor
 47        )
 48
 49    def test_build_params_checker_rejects(self):
 50        checker = afscgap.inference.build_params_checker({'year': 2021})
 51        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 52        example_2021 = list(data_2021)[0]
 53        self.assertTrue(checker(example_2021))
 54
 55
 56    def test_build_params_checker_accepts(self):
 57        checker = afscgap.inference.build_params_checker({'year': 2021})
 58        data_2021 = filter(lambda x: x.get_year() != 2021, self._all_hauls_data)
 59        example_2021 = list(data_2021)[0]
 60        self.assertFalse(checker(example_2021))
 61
 62    def test_get_all_hauls(self):
 63        self.assertEquals(len(self._all_hauls_data), 3)
 64        
 65        years = set(map(lambda x: int(x.get_year()), self._all_hauls_data))
 66        self.assertEquals(len(years), 2)
 67        self.assertTrue(2021 in years)
 68        self.assertTrue(2000 in years)
 69
 70    def test_filter_hauls(self):
 71        filtered_hauls = afscgap.inference.get_hauls_data(
 72            {'year': 2021},
 73            self._hauls_mock_requsetor
 74        )
 75
 76        self.assertEquals(len(filtered_hauls), 2)
 77
 78    def test_decorator_infer_pass_through(self):
 79        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 80        example_2021 = list(data_2021)[0]
 81        decorated = afscgap.inference.ZeroCatchHaulDecorator(
 82            example_2021,
 83            'science',
 84            'fish',
 85            12,
 86            34,
 87            567
 88        )
 89        self.assertEquals(decorated.get_year(), 2021)
 90
 91    def test_decorator_infer_override(self):
 92        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 93        example_2021 = list(data_2021)[0]
 94        decorated = afscgap.inference.ZeroCatchHaulDecorator(
 95            example_2021,
 96            'science',
 97            'fish',
 98            12,
 99            34,
100            567
101        )
102        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
103
104    def test_decorator_infer_given(self):
105        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
106        example_2021 = list(data_2021)[0]
107        decorated = afscgap.inference.ZeroCatchHaulDecorator(
108            example_2021,
109            'science',
110            'fish',
111            12,
112            34,
113            567
114        )
115        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
116
117    def test_decorator_dict(self):
118        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
119        example_2021 = list(data_2021)[0]
120        decorated = afscgap.inference.ZeroCatchHaulDecorator(
121            example_2021,
122            'science',
123            'fish',
124            12,
125            34,
126            567
127        )
128        decorated_dict = decorated.to_dict()
129        self.assertEquals(decorated_dict['year'], 2021)
130
131    def test_get_haul_key(self):
132        example_api = list(self._api_cursor)[0]
133        api_key = self._decorated_cursor._get_haul_key(example_api)
134
135        match_hauls = filter(
136            lambda x: int(x.get_haul()) == 215,
137            self._all_hauls_data
138        )
139        example_haul = list(match_hauls)[0]
140        haul_key = self._decorated_cursor._get_haul_key(example_haul)
141
142        self.assertEqual(api_key, haul_key)
143
144    def test_build_cursor_no_params(self):
145        results = list(self._decorated_cursor)
146        self.assertEqual(len(results), 6)
147
148        haul_ids = set(map(lambda x: int(x.get_haul()), results))
149        self.assertEqual(len(haul_ids), 3)
150        self.assertTrue(110 in haul_ids)
151        self.assertTrue(214 in haul_ids)
152        self.assertTrue(215 in haul_ids)
153
154    def test_build_cursor_params(self):
155        decorated_cursor = afscgap.inference.build_inference_cursor(
156            {'year': 2021},
157            self._api_cursor,
158            requestor=self._hauls_mock_requsetor
159        )
160
161        results = list(decorated_cursor)
162        self.assertEqual(len(results), 4)
163
164        haul_ids = set(map(lambda x: int(x.get_haul()), results))
165        self.assertEqual(len(haul_ids), 2)
166        self.assertTrue(214 in haul_ids)
167        self.assertTrue(215 in haul_ids)

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.
def setUp(self):
19    def setUp(self):
20        self._api_result = afscgap.test.test_tools.make_result_json(
21            'limited.json'
22        )
23        self._api_mock_requsetor = unittest.mock.MagicMock(
24            return_value=self._api_result
25        )
26        self._api_cursor = afscgap.client.ApiServiceCursor(
27            'BASE_URL',
28            requestor=self._api_mock_requsetor
29        )
30
31        self._hauls_result = afscgap.test.test_tools.make_result_text(
32            'hauls.csv'
33        )
34        self._hauls_mock_requsetor = unittest.mock.MagicMock(
35            return_value=self._hauls_result
36        )
37
38        self._all_hauls_data = afscgap.inference.get_hauls_data(
39            {},
40            self._hauls_mock_requsetor
41        )
42
43        self._decorated_cursor = afscgap.inference.build_inference_cursor(
44            {},
45            self._api_cursor,
46            requestor=self._hauls_mock_requsetor
47        )

Hook method for setting up the test fixture before exercising it.

def test_build_params_checker_rejects(self):
49    def test_build_params_checker_rejects(self):
50        checker = afscgap.inference.build_params_checker({'year': 2021})
51        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
52        example_2021 = list(data_2021)[0]
53        self.assertTrue(checker(example_2021))
def test_build_params_checker_accepts(self):
56    def test_build_params_checker_accepts(self):
57        checker = afscgap.inference.build_params_checker({'year': 2021})
58        data_2021 = filter(lambda x: x.get_year() != 2021, self._all_hauls_data)
59        example_2021 = list(data_2021)[0]
60        self.assertFalse(checker(example_2021))
def test_get_all_hauls(self):
62    def test_get_all_hauls(self):
63        self.assertEquals(len(self._all_hauls_data), 3)
64        
65        years = set(map(lambda x: int(x.get_year()), self._all_hauls_data))
66        self.assertEquals(len(years), 2)
67        self.assertTrue(2021 in years)
68        self.assertTrue(2000 in years)
def test_filter_hauls(self):
70    def test_filter_hauls(self):
71        filtered_hauls = afscgap.inference.get_hauls_data(
72            {'year': 2021},
73            self._hauls_mock_requsetor
74        )
75
76        self.assertEquals(len(filtered_hauls), 2)
def test_decorator_infer_pass_through(self):
78    def test_decorator_infer_pass_through(self):
79        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
80        example_2021 = list(data_2021)[0]
81        decorated = afscgap.inference.ZeroCatchHaulDecorator(
82            example_2021,
83            'science',
84            'fish',
85            12,
86            34,
87            567
88        )
89        self.assertEquals(decorated.get_year(), 2021)
def test_decorator_infer_override(self):
 91    def test_decorator_infer_override(self):
 92        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
 93        example_2021 = list(data_2021)[0]
 94        decorated = afscgap.inference.ZeroCatchHaulDecorator(
 95            example_2021,
 96            'science',
 97            'fish',
 98            12,
 99            34,
100            567
101        )
102        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
def test_decorator_infer_given(self):
104    def test_decorator_infer_given(self):
105        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
106        example_2021 = list(data_2021)[0]
107        decorated = afscgap.inference.ZeroCatchHaulDecorator(
108            example_2021,
109            'science',
110            'fish',
111            12,
112            34,
113            567
114        )
115        self.assertEquals(decorated.get_cpue_weight_maybe(units='kg/ha'), 0)
def test_decorator_dict(self):
117    def test_decorator_dict(self):
118        data_2021 = filter(lambda x: x.get_year() == 2021, self._all_hauls_data)
119        example_2021 = list(data_2021)[0]
120        decorated = afscgap.inference.ZeroCatchHaulDecorator(
121            example_2021,
122            'science',
123            'fish',
124            12,
125            34,
126            567
127        )
128        decorated_dict = decorated.to_dict()
129        self.assertEquals(decorated_dict['year'], 2021)
def test_get_haul_key(self):
131    def test_get_haul_key(self):
132        example_api = list(self._api_cursor)[0]
133        api_key = self._decorated_cursor._get_haul_key(example_api)
134
135        match_hauls = filter(
136            lambda x: int(x.get_haul()) == 215,
137            self._all_hauls_data
138        )
139        example_haul = list(match_hauls)[0]
140        haul_key = self._decorated_cursor._get_haul_key(example_haul)
141
142        self.assertEqual(api_key, haul_key)
def test_build_cursor_no_params(self):
144    def test_build_cursor_no_params(self):
145        results = list(self._decorated_cursor)
146        self.assertEqual(len(results), 6)
147
148        haul_ids = set(map(lambda x: int(x.get_haul()), results))
149        self.assertEqual(len(haul_ids), 3)
150        self.assertTrue(110 in haul_ids)
151        self.assertTrue(214 in haul_ids)
152        self.assertTrue(215 in haul_ids)
def test_build_cursor_params(self):
154    def test_build_cursor_params(self):
155        decorated_cursor = afscgap.inference.build_inference_cursor(
156            {'year': 2021},
157            self._api_cursor,
158            requestor=self._hauls_mock_requsetor
159        )
160
161        results = list(decorated_cursor)
162        self.assertEqual(len(results), 4)
163
164        haul_ids = set(map(lambda x: int(x.get_haul()), results))
165        self.assertEqual(len(haul_ids), 2)
166        self.assertTrue(214 in haul_ids)
167        self.assertTrue(215 in haul_ids)
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_