def test_segmentation(): themis_meta = PdsMetadata( 'themis_vis', lines=100, samples=50, center_latitude=0, center_longitude=0, pixel_width=1.0, pixel_aspect_ratio=1.0, north_azimuth=0.0, ) ctx_meta = PdsMetadata( 'ctx', lines=100, samples=50, center_latitude=0, center_longitude=0, image_width=50.0, image_height=100.0, north_azimuth=0.0, usage_note='N', ) test_cases = [ (1, 1, True), (-10, -10, False), (50, 25, True), (150, 75, False), ] resolutions = [ (50.0, 4), (100.0, 2), ] for meta in [themis_meta, ctx_meta]: loc = get_localizer(meta) for resolution, exp_segs in resolutions: tsf = TriSegmentedFootprint(meta, resolution, {}) assert len(tsf.segments) == exp_segs for row, col, exp in test_cases: lat, lon = loc.pixel_to_latlon(row, col) n_inclusions = sum([ segment.includes_point(PointQuery(lat, lon, 0)) for segment in tsf.segments ]) if exp: # Could be in up to 2 segments if it fall along a boundary assert n_inclusions in (1, 2) else: assert n_inclusions == 0
def test_store_metadata(mock_exists, mock_db_manager): mock_exists.return_value = False with mock.patch('sqlite3.connect', mock_db_manager): metadata = store_metadata( 'output.db', 'instrument_name', TEST_TABLE, TEST_CONFIG ) assert len(metadata) == 3 assert metadata[0] == PdsMetadata('instrument_name', col1=0, col2=6.0) assert metadata[1] == PdsMetadata('instrument_name', col1=1, col2=8.0) assert metadata[2] == PdsMetadata('instrument_name', col1=2, col2=10.0) with mock_db_manager('output.db') as conn: cursor = conn.cursor() cursor.execute('SELECT * FROM metadata') results = cursor.fetchall() assert len(results) == 3
def test_metadata_dump_fallback(): meta = PdsMetadata( instrument='test_instrument', test_strange_type=set([1, 2, 3]), ) # Cannot serialize `set` data type with pytest.raises(TypeError): json_dumps([meta])
def test_hirise_edr_localizer(lines, samples, pixel_width, ccd_name, channel_number, binning, lat, lon, row, col): # This test is specifically for PSP_001334_2645 meta = PdsMetadata( 'hirise_edr', lines=lines, samples=samples, pixel_width=pixel_width, ccd_name=ccd_name, channel_number=channel_number, binning=binning, center_latitude=84.4102, center_longitude=343.495, north_azimuth=118.967, ) latlon_rowcol = [((lat, lon), (row, col))] test_localizer(meta, latlon_rowcol)
def test_metadata_consistency(): meta = PdsMetadata( instrument='test_instrument', other_field_str='test_other', other_field_int=5, other_field_float=1.234, other_field_date=datetime.datetime(1985, 10, 26, 1, 20), ) metas = [meta] meta_json_str = json_dumps(metas) metas_reloaded = json_loads(meta_json_str) assert len(metas_reloaded) == 1 meta_reloaded = metas_reloaded[0] assert meta == meta_reloaded # Test not equal to an object of a different type assert not meta == None
from pdsc.metadata import PdsMetadata from pdsc.segment import TriSegmentedFootprint from pdsc.ingest import ( get_idx_file_pair, ingest_idx, store_segment_tree, store_segments, store_metadata ) TEST_DATA = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data' ) TEST_META = PdsMetadata( 'themis_ir', observation_id='themis_id', center_latitude=-54.121, center_longitude=202.748, lines=272, samples=320, north_azimuth=100.239, pixel_aspect_ratio=0.845, pixel_height=102.0, pixel_width=120.0, ) TEST_CONFIG = { 'scale_factors': { 'col2': 2.0, }, 'index': [ 'col1', ], 'columns': [ ('col1', 'col1', 'integer'), ('col2', 'col2', 'real'), ], }
def test_metadata_repr(): meta = PdsMetadata(instrument='test_instrument', other_field='test_other') expected = ("PdsMetadata(instrument='test_instrument', " "other_field='test_other')") assert repr(meta) == expected
def test_http_client(mock_get, mock_post, mock_env): mock_env.reset_mock() mock_env.return_value = '1234' client = PdsHttpClient(host='localhost') mock_env.assert_called_once_with(PORT_VAR, None) assert client.base_url == 'http://localhost:1234/' mock_env.reset_mock() mock_env.return_value = '12.34' with pytest.raises(ValueError): client = PdsHttpClient(host='localhost') mock_env.assert_called_once_with(PORT_VAR, None) mock_env.reset_mock() mock_env.return_value = None client = PdsHttpClient(host='localhost') mock_env.assert_called_once_with(PORT_VAR, None) assert client.base_url == 'http://localhost/' mock_env.reset_mock() mock_env.return_value = 'localhost' client = PdsHttpClient(port=1234) mock_env.assert_called_once_with(SERVER_VAR, None) assert client.base_url == 'http://localhost:1234/' mock_env.reset_mock() mock_env.return_value = None with pytest.raises(ValueError): client = PdsHttpClient(port=1234) mock_env.assert_called_once_with(SERVER_VAR, None) # Port must be integer, not string with pytest.raises(TypeError): client = PdsHttpClient(host='localhost', port='1234') class MockResponse(object): def __init__(self, expected): self.expected = expected self.text = json_dumps(self.expected) def json(self): return self.expected def raise_for_status(self): pass def assert_expected(self, retval): assert retval == self.expected mock_response = MockResponse(['test_a', 'test_b']) mock_get.return_value = mock_response mock_get.reset_mock() client = PdsHttpClient(host='localhost', port=1234) overlapping = client.find_overlapping_observations('instrument1', 'obsid', 'instrument2') mock_get.assert_called_once_with( 'http://localhost:1234/queryByOverlap', { 'instrument': 'instrument1', 'observation_id': 'obsid', 'other_instrument': 'instrument2', }) mock_response.assert_expected(overlapping) mock_get.reset_mock() obs = client.find_observations_of_latlon('instrument', 0, 1, 2) mock_get.assert_called_once_with('http://localhost:1234/queryByLatLon', { 'instrument': 'instrument', 'lat': 0, 'lon': 1, 'radius': 2, }) mock_response.assert_expected(obs) mock_response = MockResponse( [PdsMetadata( instrument='test_instrument', field1='value1', )]) mock_post.return_value = mock_response mock_post.reset_mock() obs = client.query_by_observation_id('instrument', 'obsid') mock_post.assert_called_once_with( 'http://localhost:1234/queryByObservationId', { 'instrument': 'instrument', 'observation_ids': '"obsid"', }) mock_response.assert_expected(obs) mock_post.reset_mock() obs = client.query_by_observation_id('instrument', ['obsid1']) mock_post.assert_called_once_with( 'http://localhost:1234/queryByObservationId', { 'instrument': 'instrument', 'observation_ids': '["obsid1"]', }) mock_response.assert_expected(obs) mock_post.reset_mock() obs = client.query('instrument') mock_post.assert_called_once_with('http://localhost:1234/query', { 'instrument': 'instrument', }) mock_response.assert_expected(obs) mock_post.reset_mock() obs = client.query('instrument', conditions=[('corner1_latitude', '>', -0.5)]) mock_post.assert_called_once_with( 'http://localhost:1234/query', { 'instrument': 'instrument', 'conditions': '[["corner1_latitude", ">", -0.5]]', }) mock_response.assert_expected(obs)
def test_missing_localizer(): meta = PdsMetadata('bad_instrument') pytest.raises(IndexError, get_localizer, meta)
# 4. mappt FROM=ESP_050016_1870_RED.cub TYPE=IMAGE LINE=23798 SAMPLE=1 # 5. mappt FROM=ESP_050016_1870_RED.cub TYPE=IMAGE LINE=11899 SAMPLE=11012 # # Results: # 1. line=1, sample=1 ==> lat=6.9937526632708, lon=69.985892127602 # 2. line=1, sample=22023 ==> lat=6.9937526632708, lon=70.079132239075 # 3. line=23798, sampe=22023 ==> lat=6.8933806899744, lon=70.079132239075 # 4. line=23798, sample=1 ==> lat=6.8933806899744, lon=69.985892127602 # 5. line=11899, sample=11012 ==> lat=6.9435687855433, lon=70.032512183339 HIRISE_ESP_050016_1870_META = PdsMetadata( 'hirise_rdr', map_projection_type='EQUIRECTANGULAR', projection_center_latitude=5.0, projection_center_longitude=180.0, map_scale=0.25, line_projection_offset=1658135.5, sample_projection_offset=25983782.0, lines=23798, samples=22023, corner1_latitude=6.9035, corner1_longitude=70.0791, corner2_latitude=6.8934, corner2_longitude=69.9971, corner3_latitude=6.9837, corner3_longitude=69.9859, corner4_latitude=6.9937, corner4_longitude=70.068, ) HIRISE_ESP_050016_1870_TEST_CASE = [ ((6.9937526632708, 69.985892127602), (1, 1)), ((6.9937526632708, 70.079132239075), (1, 22023)), ((6.8933806899744, 70.079132239075), (23798, 22023)), ((6.8933806899744, 69.985892127602), (23798, 1)), ((6.9435687855433, 70.032512183339), (11899, 11012)), ] # Test case 2: # Test lat/lon to pixel conversion for equirectangular projection.