Commit 4fa9b274 authored by Mark Hewitt's avatar Mark Hewitt
Browse files

- Split storage get to storage list (for id's) and storage get (for records) +...

- Split storage get to storage list (for id's) and storage get (for records) + added storage get --id option
parent d983e9aa
Pipeline #94017 passed with stages
in 5 minutes and 32 seconds
......@@ -28,6 +28,12 @@ For more information, specify the `-h` flag:
Change Log
==========
0.0.35
------
- Split storage get to storage list (for id's) and storage get (for records)
- storage get --id option added
0.0.34
------
......
......@@ -12,4 +12,4 @@
""" OSDU command line environment"""
__VERSION__ = "0.0.34"
__VERSION__ = "0.0.35"
......@@ -21,37 +21,44 @@ from osducli.config import CONFIG_STORAGE_URL
# click entry point
@click.command(cls=CustomClickCommand)
@click.option("-k", "--kind", required=True, help="Get records by kind")
@click.option("-k", "--kind", help="Get records by kind")
@click.option("-id", "--id", "_id", help="An id to search for")
@handle_cli_exceptions
@command_with_output("results")
def _click_command(state: State, kind: str):
@command_with_output("records || results")
def _click_command(state: State, kind: str, _id: str):
"""Get records"""
return get(state, kind)
return get(state, kind, _id)
def get(state: State, kind: str):
def get(
state: State, kind: str = None, id: str = None
): # pylint: disable=invalid-name,redefined-builtin
"""Get records
Args:
state (State): Global state
kind (str): Kind of the schema
kind (str): Kind of records
id (str): Id of records
"""
print("NOTE: storage get is still a work in progress")
print("NOTE: storage get is still a work in progress and subject to change")
connection = CliOsduClient(state.config)
# NOTE: there is a difference between records and query endpoints
# url = "records/id"
# url = "query/records?limit=10000&kind=osdu:wks:work-product-component--WellLog:1.0.0"
# TODO: What do we want - a list of id's or the actual records? Perhaps move id list to 'storage list'
if kind is not None:
print("Work in progress - needs to query individual records")
url = "query/records?kind=" + kind
json = connection.cli_get_returning_json(CONFIG_STORAGE_URL, url)
return json
request_data = {}
# if identifier is not None:
# request_data["query"] = f'id:("{identifier}")'
request_data["records"] = [
"opendes:work-product-component--WellLog:14667082fc7e4dceb17af802904069fb"
]
json = connection.cli_post_returning_json(CONFIG_STORAGE_URL, "query/records", request_data)
return json
if id is not None:
request_data = {}
# if identifier is not None:
# request_data["query"] = f'id:("{identifier}")'
request_data["records"] = [id]
json = connection.cli_post_returning_json(CONFIG_STORAGE_URL, "query/records", request_data)
return json
raise ValueError("You must specify either a kind or id")
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Schema service get command"""
import click
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_STORAGE_URL
# click entry point
@click.command(cls=CustomClickCommand)
@click.option("-k", "--kind", required=True, help="Get records by kind")
@handle_cli_exceptions
@command_with_output("results")
def _click_command(state: State, kind: str):
"""List records"""
return list_records(state, kind)
def list_records(state: State, kind: str):
"""List records
Args:
state (State): Global state
kind (str): Kind of records
"""
connection = CliOsduClient(state.config)
url = "query/records?kind=" + kind
json = connection.cli_get_returning_json(CONFIG_STORAGE_URL, url)
return json
......@@ -18,6 +18,7 @@ from mock import patch
from nose2.tools import params
from osducli.click_cli import State
from osducli.cliclient import CliOsduClient
from osducli.commands.storage.get import get
from tests.base_test_case import BaseTestCase
from tests.helpers import MockConfig
......@@ -29,17 +30,38 @@ class StatusTests(BaseTestCase):
# region CLI interface
@params("kind1", "kind2")
def test_checkrefs_default_params(self, kind: str):
"""Test cli with required parameters"""
def test_cli_kind(self, kind: str):
"""Test cli with kind parameters"""
self.assert_cli_calls_invoked_function(
["storage", "get", "--kind", kind],
"osducli.commands.storage.get.get",
mock.ANY,
kind,
None,
)
@params("id1", "id2")
def test_cli_id(self, _id: str):
"""Test cli with id parameters"""
self.assert_cli_calls_invoked_function(
["storage", "get", "--id", _id],
"osducli.commands.storage.get.get",
mock.ANY,
None,
_id,
)
# endregion CLI interface
# region Function tests
def test_kind_or_id_required(self): # pylint: disable=W0613
"""Test kind or id is required"""
with self.assertRaises(ValueError):
state = State()
state.config = MockConfig
_ = get(state)
@params(
"osdu:wks:reference-data--MaterialType:1.0.0",
"osdu:wks:reference-data--UnitOfMeasure:1.0.0",
......@@ -61,31 +83,17 @@ class StatusTests(BaseTestCase):
)
self.assertEqual(expected_output, response)
# def test_user_friendly_output(self): # pylint: disable=R0201
# """Test user friendly output"""
# data_folder = path.join(path.dirname(path.realpath(__file__)), "data")
# data_path = path.join(data_folder, "versions.json")
# with open(data_path) as file:
# data = json.load(file)
# with patch(
# "osducli.cliclient.CliOsduClient.cli_get_returning_json", return_value=data
# ) as _:
# state = State()
# state.config = MockConfig
# _ = versions(state, "some-dummy-id")
# # TO DO - Verify stdout.
# @patch.object(CliOsduClient, "cli_get_returning_json", side_effect=SystemExit(1))
# def test_exit_on_error(self, mock_cli_get_returning_json): # pylint: disable=W0613
# """Test any exit error is propogated"""
# state = State()
# state.config = MockConfig
# with self.assertRaises(SystemExit) as sysexit:
# _ = versions(state, "some-dummy-id")
# self.assertEqual(sysexit.exception.code, 1)
@patch.object(CliOsduClient, "cli_get_returning_json", side_effect=SystemExit(1))
def test_exit_on_error(self, mock_cli_get_returning_json): # pylint: disable=W0613
"""Test any exit error is propogated"""
state = State()
state.config = MockConfig
with self.assertRaises(SystemExit) as sysexit:
_ = get(state, "osdu:wks:reference-data--UnitOfMeasure:1.0.0")
self.assertEqual(sysexit.exception.code, 1)
# endregion Function tests
if __name__ == "__main__":
......
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test cases for storage get"""
import mock
from mock import patch
from nose2.tools import params
from osducli.click_cli import State
from osducli.cliclient import CliOsduClient
from osducli.commands.storage.list import list_records
from tests.base_test_case import BaseTestCase
from tests.helpers import MockConfig
class StatusTests(BaseTestCase):
"""Test cases for storage list"""
# region CLI interface
@params("kind1", "kind2")
def test_cli_kind(self, kind: str):
"""Test cli with kind parameters"""
self.assert_cli_calls_invoked_function(
["storage", "list", "--kind", kind],
"osducli.commands.storage.list.list_records",
mock.ANY,
kind,
)
# endregion CLI interface
# region Function tests
@params(
"osdu:wks:reference-data--MaterialType:1.0.0",
"osdu:wks:reference-data--UnitOfMeasure:1.0.0",
)
def test_kind(self, kind: str):
"""Test getting for a particular kind"""
api_response = self.get_local_json_file("get-response.json", __file__)
expected_output = api_response
with patch(
"osducli.cliclient.CliOsduClient.cli_get_returning_json", return_value=api_response
) as mock_cli_put_returning_json:
state = State()
state.config = MockConfig
response = list_records(state, kind)
mock_cli_put_returning_json.assert_called_once_with(
"storage_url", "query/records?kind=" + kind
)
self.assertEqual(expected_output, response)
@patch.object(CliOsduClient, "cli_get_returning_json", side_effect=SystemExit(1))
def test_exit_on_error(self, mock_cli_get_returning_json): # pylint: disable=W0613
"""Test any exit error is propogated"""
state = State()
state.config = MockConfig
with self.assertRaises(SystemExit) as sysexit:
_ = list_records(state, "osdu:wks:reference-data--UnitOfMeasure:1.0.0")
self.assertEqual(sysexit.exception.code, 1)
# endregion Function tests
if __name__ == "__main__":
import nose2
nose2.main()
......@@ -452,7 +452,7 @@ class HelpTextTests(unittest.TestCase):
self.validate_output(
"osdu storage",
commands=("add", "delete", "get", "info", "versions"),
commands=("add", "delete", "get", "info", "list", "versions"),
)
self.validate_output(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment