Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed Mar 28, 2024
1 parent 62f948a commit 36e59d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 50 deletions.
11 changes: 6 additions & 5 deletions dsaps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import click
import structlog

from dsaps import dspace
from dsaps import helpers
from dsaps import dspace, helpers
from dsaps.s3 import S3Client


Expand Down Expand Up @@ -89,6 +88,7 @@ def main(ctx, config_file, url, email, password):
dspace_client.authenticate(email, password)
ctx.obj["dspace_client"] = dspace_client
ctx.obj["config"] = source_config
logger.info("Initializing S3 client")
ctx.obj["s3_client"] = S3Client.get_client()
ctx.obj["start_time"] = perf_counter()

Expand Down Expand Up @@ -199,7 +199,9 @@ def additems(
def newcollection(ctx, community_handle, collection_name):
"""Create a new DSpace collection within a community."""
dspace_client = ctx.obj["dspace_client"]
collection_uuid = dspace_client.post_coll_to_comm(community_handle, collection_name)
collection_uuid = dspace_client.post_collection_to_community(
community_handle, collection_name
)
ctx.obj["collection_uuid"] = collection_uuid


Expand Down Expand Up @@ -243,10 +245,9 @@ def reconcile(ctx, metadata_csv, output_directory, content_directory):
corresponding file in the content directory.
"""
source_settings = ctx.obj["config"]["settings"]
s3_client = ctx.obj["s3_client"]
bitstreams = helpers.get_files_from_s3(
s3_path=content_directory,
s3_client=s3_client,
s3_client=ctx.obj["s3_client"],
bitstream_folders=source_settings.get("bitstream_folders"),
id_regex=source_settings["id_regex"],
)
Expand Down
8 changes: 4 additions & 4 deletions dsaps/dspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, url):
self.url = url.rstrip("/")
self.cookies = None
self.header = header
logger.info("Initializing client")
logger.info("Initializing DSpace client")

def authenticate(self, email, password):
"""Authenticate user to DSpace API."""
Expand Down Expand Up @@ -111,7 +111,7 @@ def post_bitstream(self, item_uuid, bitstream):
bitstream_uuid = response["uuid"]
return bitstream_uuid

def post_coll_to_comm(self, comm_handle, coll_name):
def post_collection_to_community(self, comm_handle, coll_name):
"""Post a collection to a specified community."""
hdl_endpoint = f"{self.url}/handle/{comm_handle}"
community = requests.get(
Expand Down Expand Up @@ -152,7 +152,7 @@ def _populate_class_instance(self, class_type, rec_obj):
"""Populate class instance with data from record."""
fields = [op(field) for field in attr.fields(class_type)]
kwargs = {k: v for k, v in rec_obj.items() if k in fields}
kwargs["objtype"] = rec_obj["type"]
kwargs["type"] = rec_obj["type"]
if class_type == Community:
collections = self._build_uuid_list(rec_obj, kwargs, "collections")
rec_obj["collections"] = collections
Expand Down Expand Up @@ -189,7 +189,7 @@ class Object:
name = field(default=None)
handle = field(default=None)
link = field(default=None)
objtype = field(default=None)
type = field(default=None)


@define
Expand Down
1 change: 0 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ def test_update_metadata_csv(output_dir, mocked_s3_bucket_bitstreams):
with open(f"{output_dir}/updated-source_metadata.csv") as csvfile:
reader = csv.DictReader(csvfile)
record = next(reader)
assert record is not None
assert record == {
"item_identifier": "001",
"title": "Title 1",
Expand Down
55 changes: 15 additions & 40 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from dsaps.dspace import Bitstream, Collection, Item, MetadataEntry


def test_authenticate(dspace_client):
"""Test authenticate method."""
def test_dspace_client_authenticate(dspace_client):
email = "test@test.mock"
password = "1234"
dspace_client.authenticate(email, password)
Expand All @@ -14,27 +13,23 @@ def test_authenticate(dspace_client):


def test_filtered_item_search(dspace_client):
"""Test filtered_item_search method."""
item_links = dspace_client.filtered_item_search(
key="dc.title", string="test", query_type="contains", selected_collections=""
)
assert "1234" in item_links


def test_get_uuid_from_handle(dspace_client):
"""Test get_uuid_from_handle method."""
id = dspace_client.get_uuid_from_handle("111.1111")
assert id == "a1b2"


def test_get_record(dspace_client):
"""Test get_record method."""
rec_obj = dspace_client.get_record("123", "items")
assert attr.asdict(rec_obj)["metadata"] == {"title": "Sample title"}
dspace_item = dspace_client.get_record("123", "items")
assert attr.asdict(dspace_item)["metadata"] == {"title": "Sample title"}


def test_post_bitstream(dspace_client, mocked_s3_bucket):
"""Test post_bitstream method."""
item_uuid = "e5f6"
bitstream = Bitstream(
name="aaaa_001_01.pdf", file_path="s3://mocked-bucket/one-to-one/aaaa_001_01.pdf"
Expand All @@ -43,17 +38,15 @@ def test_post_bitstream(dspace_client, mocked_s3_bucket):
assert bit_uuid == "g7h8"


def test_post_coll_to_comm(dspace_client):
"""Test post_coll_to_comm method."""
def test_post_collection_to_community(dspace_client):
comm_handle = "111.1111"
coll_name = "Test Collection"
coll_uuid = dspace_client.post_coll_to_comm(comm_handle, coll_name)
coll_uuid = dspace_client.post_collection_to_community(comm_handle, coll_name)
assert coll_uuid == "c3d4"


@mock_aws
def test_post_item_to_collection(dspace_client, mocked_s3_bucket):
"""Test post_item_to_collection method."""
item = Item()
item.bitstreams = [
Bitstream(name="aaaa_001_01.pdf", file_path="s3://mocked-bucket/aaaa_001_01.pdf")
Expand All @@ -68,20 +61,20 @@ def test_post_item_to_collection(dspace_client, mocked_s3_bucket):
assert item_handle == "222.2222"


def test__populate_class_instance(dspace_client):
"""Test _populate_class_instance method."""
def test_populate_class_instance(dspace_client):
class_type = Collection
rec_obj = {"name": "Test title", "type": "collection", "items": []}
rec_obj = dspace_client._populate_class_instance(class_type, rec_obj)
assert type(rec_obj) is class_type
assert rec_obj.name == "Test title"
dspace_collection = {"name": "Test title", "type": "collection", "items": []}
dspace_collection = dspace_client._populate_class_instance(
class_type, dspace_collection
)
assert type(dspace_collection) is class_type
assert dspace_collection.name == "Test title"


def test__build_uuid_list(dspace_client):
"""Test _build_uuid_list method."""
rec_obj = {"items": [{"uuid": "1234"}]}
def test_build_uuid_list(dspace_client):
dspace_items = {"items": [{"uuid": "1234"}]}
children = "items"
child_list = dspace_client._build_uuid_list(rec_obj, children)
child_list = dspace_client._build_uuid_list(dspace_items, children)
assert "1234" in child_list


Expand All @@ -94,24 +87,6 @@ def test_collection_create_metadata_for_items_from_csv(
assert len(collection.items) == 5


# @mock_aws
# def test_collection_post_items(
# mocked_s3_bucket,
# dspace_client,
# source_metadata_csv,
# source_config,
# ):
# collection = DSpaceCollection(uuid="c3d4")
# collection.create_metadata_for_items_from_csv(
# source_metadata_csv, source_config["mapping"]
# )
# items = collection.post_items(dspace_client)
# print(f"item: {next(items)}")
# for item in items:
# assert item.handle != "222.2222"
# assert item.uuid != "e5f6"


def test_item_metadata_from_csv_row(source_metadata_csv, source_config):
record = next(source_metadata_csv)
item = Item.metadata_from_csv_row(record, source_config["mapping"])
Expand Down

0 comments on commit 36e59d3

Please sign in to comment.