Example #1
0
def test_sync_tool_init(tmpdir):
    td = tmpdir.mkdir('sync-dest')
    aoi = read_fixture('aoi.geojson')
    st = _SyncTool(client, td.strpath, aoi, 'ortho', ('visual', 'analytic'))
    search_json = json.loads(read_fixture('search.geojson'))
    response = MagicMock(spec=Scenes)
    response.get.return_value = search_json
    client.get_scenes_list.return_value = response

    # init w/ no limit should return count from response
    count = st.init()
    # confusing but we'll download 2 products one for each scene
    assert search_json['count'] * 2 == count

    # expect limiting to 10 ids
    count = st.init(limit=10)
    # still replies with total jobs despite the limit
    assert search_json['count'] * 2 == count
    # this tracks the internal number of ids, still 10
    assert 10 == st._scene_count

    # create a stored 'latest' date and ensure it's used
    latest = '2015-01-25T18:29:09.155671+00:00'
    td.join('sync.json').write(json.dumps({'latest': latest}))
    st.init()
    args = client.get_scenes_list.call_args
    # args are ((arguments),(kw))
    assert args[1]['published.gt'] == latest

    td.remove(True)
def test_sync_tool_init(tmpdir):
    td = tmpdir.mkdir('sync-dest')
    aoi = read_fixture('aoi.geojson')
    st = _SyncTool(client, td.strpath, aoi, 'ortho', ('visual', 'analytic'))
    search_json = json.loads(read_fixture('search.geojson'))
    response = MagicMock(spec=Scenes)
    response.get.return_value = search_json
    client.get_scenes_list.return_value = response

    # init w/ no limit should return count from response
    count = st.init()
    # confusing but we'll download 2 products one for each scene
    assert search_json['count'] * 2 == count

    # expect limiting to 10 ids
    count = st.init(limit=10)
    # still replies with total jobs despite the limit
    assert search_json['count'] * 2 == count
    # this tracks the internal number of ids, still 10
    assert 10 == st._scene_count

    # create a stored 'latest' date and ensure it's used
    latest = '2015-01-25T18:29:09.155671+00:00'
    td.join('sync.json').write(json.dumps({
        'latest': latest
    }))
    st.init()
    args = client.get_scenes_list.call_args
    # args are ((arguments),(kw))
    assert args[1]['published.gt'] == latest

    td.remove(True)
Example #3
0
def test_get_mosaics_list_for_feed(runner, client):
    feeds_blob = json.loads(read_fixture('feeds.json'))
    mosaics_blob = json.loads(read_fixture('list-mosaics.json'))

    configure_response(client.get_feed_info, json.dumps(feeds_blob['data'][0]))
    configure_response(client.get_mosaics_for_series, json.dumps(mosaics_blob))

    expected = 'source mosaics:\n\tcolor_balance_mosaic\n'
    assert_success(
        runner.invoke(main, ['analytics', 'feeds', 'list-mosaics', 'feed-id']),
        expected)
def test_search_by_aoi():

    aoi = read_fixture('search-by-aoi.geojson')
    expected = read_fixture('search-by-aoi.geojson')

    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    client.get_scenes_list.return_value = response

    # input kwarg simulates stdin
    result = runner.invoke(scripts.cli, ['search'], input=aoi)

    assert_success(result, expected)
def test_search_by_aoi():

    aoi = read_fixture('search-by-aoi.geojson')
    expected = read_fixture('search-by-aoi.geojson')

    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    client.get_scenes_list.return_value = response

    # input kwarg simulates stdin
    result = run_cli(['search'], input=aoi)

    assert_success(result, expected)
def test_workspace_create_no_id():

    workspace = json.loads(read_fixture('workspace.json'))
    workspace.pop('id')
    expected = clone(workspace)
    _set_workspace(workspace)
    client.set_workspace.assert_called_once_with(expected, None)
def test_workspace_create_no_id():

    workspace = json.loads(read_fixture('workspace.json'))
    workspace.pop('id')
    expected = clone(workspace)
    _set_workspace(workspace)
    client.set_workspace.assert_called_once_with(expected, None)
Example #8
0
def test_workspace_set_create(client):
    with requests_mock.Mocker() as m:
        uri = os.path.join(client.base_url, "workspaces/")
        workspace = json.loads(read_fixture("workspace.json"))
        request = clone(workspace)
        request.pop("id")
        m.post(uri, text=json.dumps(workspace), status_code=200)
        assert client.set_workspace(request).get_raw() == json.dumps(workspace)
def test_workspace_set_create(client):
    with requests_mock.Mocker() as m:
        uri = os.path.join(client.base_url, 'workspaces/')
        workspace = json.loads(read_fixture('workspace.json'))
        request = clone(workspace)
        request.pop('id')
        m.post(uri, text=json.dumps(workspace), status_code=200)
        assert client.set_workspace(request).get_raw() == json.dumps(workspace)
def test_build_conditions():
    workspace = json.loads(read_fixture('workspace.json'))
    c = utils.build_conditions(workspace)
    assert c['image_statistics.image_quality.gte'] == 'standard'
    assert c['image_statistics.snr.gt'] == 10
    assert c['sat.off_nadir.lte'] == 25
    assert c['sat.alt.gte'] == 200
    assert c['sat.alt.lte'] == 650
    assert c['age.lte'] == 63072000
Example #11
0
def test_get_collection_resource_types(runner, client):
    feature_blob = read_fixture('af_features.json')
    configure_response(client.list_collection_features, feature_blob)

    expected_output = "Found resource types: ['source-image-info']\n"
    assert_success(
        runner.invoke(
            main,
            ['analytics', 'collections', 'resource-types', 'collection-id']),
        expected_output)
def test_sync_tool_sync(tmpdir):
    td = tmpdir.mkdir('sync-dest')
    aoi = read_fixture('aoi.geojson')
    st = _SyncTool(client, td.strpath, aoi, 'ortho', ('visual',))
    search_json = json.loads(read_fixture('search.geojson'))

    class Page:
        def get(self):
            return search_json

    class FakeScenes:
        def items_iter(self, limit):
            return (f for f in Page().get()['features'][:limit])

        def iter(self):
            return iter([Page()])

    class FakeBody:
        def __init__(self, val, name):
            self.val = val
            self.name = name

        def __len__(self):
            return len(self.val)

    class FakeGeoTiff:
        def __init__(self, val):
            self.body = FakeBody(val, val)
            self.name = val

        def await(self):
            pass

        def get_body(self):
            return self.body

        def write(self, f, cb):
            with open(f, 'w') as fp:
                fp.write(self.body.val)

        def __len__(self):
            return 371
def test_list_mosaics():

    with Mocker() as m:

        text = read_fixture('list-mosaics.json')
        uri = os.path.join(client.base_url, 'mosaics/')
        m.get(uri, text=text, status_code=200)

        r = client.list_mosaics()

        assert r.response.status_code == 200
        assert r.get() == json.loads(text)
def test_search():

    expected = read_fixture('search.geojson')

    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    client.get_scenes_list.return_value = response

    result = runner.invoke(scripts.cli, ['search'])

    assert_success(result, expected)
Example #15
0
def test_list_mosaics():

    with Mocker() as m:

        text = read_fixture('list-mosaics.json')
        uri = os.path.join(client.base_url, 'mosaics/')
        m.get(uri, text=text, status_code=200)

        r = client.list_mosaics()

        assert r.response.status_code == 200
        assert r.get() == json.loads(text)
def test_search():

    expected = read_fixture('search.geojson')

    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    client.get_scenes_list.return_value = response

    result = run_cli(['search'])

    assert_success(result, expected)
def test_probably_wkt():
    # not wkt
    assert not utils.probably_wkt('')
    assert not utils.probably_wkt('{ geojson }')
    # it is wkt but we don't support it
    assert not utils.probably_wkt('POLYHEDRALSURFACE ()')
    assert not utils.probably_wkt('TRIANGLE((0 0 0,0 1 0,1 1 0,0 0 0))')

    # all valid wkt
    wkt = read_fixture('valid-wkt.txt').split('\n')
    assert len(wkt) > 0
    for valid in wkt:
        assert utils.probably_wkt(valid)
def test_probably_wkt():
    # not wkt
    assert not utils.probably_wkt('')
    assert not utils.probably_wkt('{ geojson }')
    # it is wkt but we don't support it
    assert not utils.probably_wkt('POLYHEDRALSURFACE ()')
    assert not utils.probably_wkt('TRIANGLE((0 0 0,0 1 0,1 1 0,0 0 0))')

    # all valid wkt
    wkt = read_fixture('valid-wkt.txt').split('\n')
    assert len(wkt) > 0
    for valid in wkt:
        assert utils.probably_wkt(valid)
Example #19
0
def test_get_mosaic():

    mosaic_name = 'color_balance_mosaic'

    with Mocker() as m:

        text = read_fixture('get-mosaic.json')
        uri = os.path.join(client.base_url, 'mosaics/%s' % mosaic_name)
        m.get(uri, text=text, status_code=200)

        r = client.get_mosaic(mosaic_name)

        assert r.response.status_code == 200
        assert r.get() == json.loads(text)
def test_metadata():

    # Read in fixture
    expected = read_fixture('20150615_190229_0905.geojson')

    # Construct a response from the fixture
    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    # Construct the return response for the client method
    client.get_scene_metadata.return_value = response

    result = run_cli(['metadata', '20150615_190229_0905'])

    assert_success(result, expected)
def test_metadata():

    # Read in fixture
    expected = read_fixture('20150615_190229_0905.geojson')

    # Construct a response from the fixture
    response = MagicMock(spec=models.JSON)
    response.get_raw.return_value = expected

    # Construct the return response for the client method
    client.get_scene_metadata.return_value = response

    result = runner.invoke(scripts.cli, ['metadata', '20150615_190229_0905'])

    assert_success(result, expected)
def test_get_mosaic():

    mosaic_name = 'color_balance_mosaic'

    with Mocker() as m:

        text = read_fixture('get-mosaic.json')
        uri = os.path.join(client.base_url,
                           'mosaics/%s' % mosaic_name)
        m.get(uri, text=text, status_code=200)

        r = client.get_mosaic(mosaic_name)

        assert r.response.status_code == 200
        assert r.get() == json.loads(text)
def test_workspace_update_stdin():
    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace)
    client.set_workspace.assert_called_once_with(expected, workspace['id'])
def test_workspace_update_from_existing_with_id():

    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace, '--id', '12345')
    client.set_workspace.assert_called_once_with(expected, '12345')
def test_workspace_create_from_existing():

    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace, '--create')
    client.set_workspace.assert_called_once_with(expected, None)
Example #26
0
def test_sync_tool_sync(tmpdir):
    td = tmpdir.mkdir('sync-dest')
    aoi = read_fixture('aoi.geojson')
    st = _SyncTool(client, td.strpath, aoi, 'ortho', ('visual', ))
    search_json = json.loads(read_fixture('search.geojson'))

    class Page:
        def get(self):
            return search_json

    class FakeScenes:
        def items_iter(self, limit):
            return (f for f in Page().get()['features'][:limit])

        def iter(self):
            return iter([Page()])

    class FakeBody:
        def __init__(self, val, name):
            self.val = val
            self.name = name

        def __len__(self):
            return len(self.val)

    class FakeGeoTiff:
        def __init__(self, val):
            self.body = FakeBody(val, val)
            self.name = val

        def await (self):
            pass

        def get_body(self):
            return self.body

        def write(self, f, cb):
            with open(f, 'w') as fp:
                fp.write(self.body.val)

        def __len__(self):
            return 371

    st._scenes = FakeScenes()

    # record invocations to a callback (could be mock?)
    called_back = []

    def callback(name, remaining):
        called_back.append((name, remaining))

    # base - no items remaining, does nothing
    st._scene_count = 0
    summary = st.sync(callback)
    assert summary.latest is None
    assert summary.transferred is 0

    # 5 items to get
    items = 5
    client.fetch_scene_geotiffs.reset_mock()
    st._scene_count = items
    responses = [FakeGeoTiff(str(i)) for i in range(st._scene_count)]

    # because process is normally async, as a sideeffect of this mock getting
    # called, we have to dispatch the callback
    def run_callbacks(ids, scene_type, product, callback):
        resp = responses.pop(0)
        callback(resp)
        return DEFAULT

    client.fetch_scene_geotiffs.side_effect = run_callbacks
    st.sync(callback)
    first_items = search_json['features'][:items]
    # should be 5 metadata files
    files = [td.join('%s_metadata.json' % f['id']) for f in first_items]
    assert len(files) == items
    assert all([f.exists() for f in files])
    # and 5 'scenes'
    files = [td.join('%s' % f) for f in range(items)]
    assert len(files) == items
    assert all([f.exists() for f in files])

    # and tiff requests were made
    args = client.fetch_scene_geotiffs.call_args_list
    # implementation detail - because we're making requests separately,
    # fetch_scene_geotiffs will be called once for each id (instead of in bulk)
    assert items == len(args)
    assert [a[0] for a in args] == \
        [([f['id']], 'ortho', 'visual') for f in first_items]
    # callbacks should be made - arguments are 'tiff name', remaining
    assert called_back == [(str(i), 4 - i) for i in range(5)]

    td.remove(True)
def test_workspace_create_from_existing():

    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace, '--create')
    client.set_workspace.assert_called_once_with(expected, None)
def test_workspace_update_from_existing_with_id():

    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace, '--id', '12345')
    client.set_workspace.assert_called_once_with(expected, '12345')
def test_workspace_update_stdin():
    workspace = json.loads(read_fixture('workspace.json'))
    expected = clone(workspace)
    _set_workspace(workspace)
    client.set_workspace.assert_called_once_with(expected, workspace['id'])