afscgap.test.test_flat_http
Tests for HTTP utility functions for interacting with Avro flat files.
(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 HTTP utility functions for interacting with Avro flat files. 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_http 14import afscgap.flat_model 15 16 17class FlatHttpTests(unittest.TestCase): 18 19 def setUp(self): 20 self._meta_params = afscgap.flat_model.ExecuteMetaParams( 21 'base_url:', 22 None, 23 None, 24 False, 25 False, 26 False, 27 lambda x: print(x) 28 ) 29 30 self._index_filter = unittest.mock.MagicMock() 31 self._index_filter.get_index_names = unittest.mock.MagicMock(return_value=['test_index']) 32 self._index_filter.get_matches = lambda x: x % 2 == 1 33 34 def test_build_haul_from_avro(self): 35 input_dict = {'year': 2024, 'survey': 'Gulf of Alaska', 'haul': 123} 36 haul_key = afscgap.flat_http.build_haul_from_avro(input_dict) 37 self.assertEqual(haul_key.get_year(), 2024) 38 self.assertEqual(haul_key.get_survey(), 'Gulf of Alaska') 39 self.assertEqual(haul_key.get_haul(), 123) 40 41 def test_build_requestor(self): 42 requestor = afscgap.flat_http.build_requestor() 43 self.assertIsNotNone(requestor) 44 45 def test_get_index_url_all(self): 46 urls = list(afscgap.flat_http.get_index_urls(self._meta_params)) 47 self.assertEqual(len(urls), 1) 48 url = urls[0] 49 self.assertTrue('base_url:' in url) 50 self.assertTrue('.avro' in url) 51 52 def test_get_index_url_subset(self): 53 urls = list(afscgap.flat_http.get_index_urls(self._meta_params, self._index_filter)) 54 self.assertEqual(len(urls), 1) 55 url = urls[0] 56 self.assertTrue('base_url:' in url) 57 self.assertTrue('test_index' in url) 58 self.assertTrue('.avro' in url) 59 60 def test_determine_matching_hauls_from_index(self): 61 options = [ 62 {'value': 1, 'keys': ['a', 'b']}, 63 {'value': 2, 'keys': ['c', 'd']}, 64 {'value': 3, 'keys': ['e', 'f']} 65 ] 66 keys = afscgap.flat_http.determine_matching_hauls_from_index(options, self._index_filter) 67 keys_realized = list(keys) 68 self.assertEqual(len(keys_realized), 4) 69 self.assertEqual(keys_realized[0], 'a') 70 self.assertEqual(keys_realized[1], 'b') 71 self.assertEqual(keys_realized[2], 'e') 72 self.assertEqual(keys_realized[3], 'f') 73
18class FlatHttpTests(unittest.TestCase): 19 20 def setUp(self): 21 self._meta_params = afscgap.flat_model.ExecuteMetaParams( 22 'base_url:', 23 None, 24 None, 25 False, 26 False, 27 False, 28 lambda x: print(x) 29 ) 30 31 self._index_filter = unittest.mock.MagicMock() 32 self._index_filter.get_index_names = unittest.mock.MagicMock(return_value=['test_index']) 33 self._index_filter.get_matches = lambda x: x % 2 == 1 34 35 def test_build_haul_from_avro(self): 36 input_dict = {'year': 2024, 'survey': 'Gulf of Alaska', 'haul': 123} 37 haul_key = afscgap.flat_http.build_haul_from_avro(input_dict) 38 self.assertEqual(haul_key.get_year(), 2024) 39 self.assertEqual(haul_key.get_survey(), 'Gulf of Alaska') 40 self.assertEqual(haul_key.get_haul(), 123) 41 42 def test_build_requestor(self): 43 requestor = afscgap.flat_http.build_requestor() 44 self.assertIsNotNone(requestor) 45 46 def test_get_index_url_all(self): 47 urls = list(afscgap.flat_http.get_index_urls(self._meta_params)) 48 self.assertEqual(len(urls), 1) 49 url = urls[0] 50 self.assertTrue('base_url:' in url) 51 self.assertTrue('.avro' in url) 52 53 def test_get_index_url_subset(self): 54 urls = list(afscgap.flat_http.get_index_urls(self._meta_params, self._index_filter)) 55 self.assertEqual(len(urls), 1) 56 url = urls[0] 57 self.assertTrue('base_url:' in url) 58 self.assertTrue('test_index' in url) 59 self.assertTrue('.avro' in url) 60 61 def test_determine_matching_hauls_from_index(self): 62 options = [ 63 {'value': 1, 'keys': ['a', 'b']}, 64 {'value': 2, 'keys': ['c', 'd']}, 65 {'value': 3, 'keys': ['e', 'f']} 66 ] 67 keys = afscgap.flat_http.determine_matching_hauls_from_index(options, self._index_filter) 68 keys_realized = list(keys) 69 self.assertEqual(len(keys_realized), 4) 70 self.assertEqual(keys_realized[0], 'a') 71 self.assertEqual(keys_realized[1], 'b') 72 self.assertEqual(keys_realized[2], 'e') 73 self.assertEqual(keys_realized[3], 'f')
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.
20 def setUp(self): 21 self._meta_params = afscgap.flat_model.ExecuteMetaParams( 22 'base_url:', 23 None, 24 None, 25 False, 26 False, 27 False, 28 lambda x: print(x) 29 ) 30 31 self._index_filter = unittest.mock.MagicMock() 32 self._index_filter.get_index_names = unittest.mock.MagicMock(return_value=['test_index']) 33 self._index_filter.get_matches = lambda x: x % 2 == 1
Hook method for setting up the test fixture before exercising it.
35 def test_build_haul_from_avro(self): 36 input_dict = {'year': 2024, 'survey': 'Gulf of Alaska', 'haul': 123} 37 haul_key = afscgap.flat_http.build_haul_from_avro(input_dict) 38 self.assertEqual(haul_key.get_year(), 2024) 39 self.assertEqual(haul_key.get_survey(), 'Gulf of Alaska') 40 self.assertEqual(haul_key.get_haul(), 123)
61 def test_determine_matching_hauls_from_index(self): 62 options = [ 63 {'value': 1, 'keys': ['a', 'b']}, 64 {'value': 2, 'keys': ['c', 'd']}, 65 {'value': 3, 'keys': ['e', 'f']} 66 ] 67 keys = afscgap.flat_http.determine_matching_hauls_from_index(options, self._index_filter) 68 keys_realized = list(keys) 69 self.assertEqual(len(keys_realized), 4) 70 self.assertEqual(keys_realized[0], 'a') 71 self.assertEqual(keys_realized[1], 'b') 72 self.assertEqual(keys_realized[2], 'e') 73 self.assertEqual(keys_realized[3], 'f')