Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Nov 27, 2024
1 parent f35c470 commit 7519473
Show file tree
Hide file tree
Showing 25 changed files with 247 additions and 343 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ warn_unused_ignores = true
# silences those errors, but only for the tests module.
# See discussion here:
# https://github.com/python/mypy/issues/2427
disable_error_code = "method-assign, union-attr"
disable_error_code = "method-assign, union-attr, operator"
module = "tests.*"
# Mypy seems to have issues in the test code where it thinks code is unreachable
# when it is not. So we disable this warning for the tests module.
Expand Down
8 changes: 4 additions & 4 deletions src/palace/manager/api/admin/controller/library_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def process_post(self) -> Response:
validated_settings = ProcessFormData.get_settings(LibrarySettings, form_data)

if not library:
# Everyone can modify an existing library, but only a system admin can create a new one.
self.require_system_admin()
library, is_new = self.create_library(short_name)
library, is_new = self.create_library(short_name, name)

library.name = name
library.short_name = short_name
Expand Down Expand Up @@ -170,13 +168,15 @@ def process_post(self) -> Response:
else:
return Response(str(library.uuid), 200)

def create_library(self, short_name: str) -> tuple[Library, bool]:
def create_library(self, short_name: str, name: str) -> tuple[Library, bool]:
# Everyone can modify an existing library, but only a system admin can create a new one.
self.require_system_admin()
public_key, private_key = Library.generate_keypair()
library, is_new = create(
self._db,
Library,
short_name=short_name,
name=name,
uuid=str(uuid.uuid4()),
public_key=public_key,
private_key=private_key,
Expand Down
5 changes: 3 additions & 2 deletions src/palace/manager/core/metadata_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,9 +1613,10 @@ def _key(classification):
_db,
Resource,
url=link.original.href,
create_method_kwargs={
"data_source": data_source,
},
)
if not original_resource.data_source:
original_resource.data_source = data_source
original_resource.rights_status = rights_status
original_resource.rights_explanation = (
link.original.rights_explanation
Expand Down
3 changes: 3 additions & 0 deletions src/palace/manager/scripts/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def do_run(self, _db=None, cmd_args=None, output=sys.stdout):
raise ValueError("Could not locate library '%s'" % args.short_name)
else:
# No existing library. Make one.
if not args.name:
raise ValueError("You must provide a name to create a library.")
public_key, private_key = Library.generate_keypair()
library, ignore = create(
_db,
Expand All @@ -90,6 +92,7 @@ def do_run(self, _db=None, cmd_args=None, output=sys.stdout):
short_name=args.short_name,
public_key=public_key,
private_key=private_key,
name=args.name,
)

if args.name:
Expand Down
2 changes: 1 addition & 1 deletion src/palace/manager/sqlalchemy/model/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def lookup(
from palace.manager.sqlalchemy.model.datasource import DataSource

if isinstance(data_source, str):
data_source = DataSource.lookup(_db, data_source)
data_source = DataSource.lookup(_db, data_source, autocreate=True)
credential, is_new = get_one_or_create(
_db,
Credential,
Expand Down
4 changes: 2 additions & 2 deletions src/palace/manager/sqlalchemy/model/customlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CustomList(Base):
"DataSource", back_populates="custom_lists"
)
foreign_identifier = Column(Unicode, index=True)
name: Mapped[str] = Column(Unicode, index=True, nullable=False)
name = Column(Unicode, index=True)
description = Column(Unicode)
created = Column(DateTime(timezone=True), index=True)
updated = Column(DateTime(timezone=True), index=True)
Expand All @@ -82,7 +82,7 @@ class CustomList(Base):
uselist=True,
)

auto_update_enabled = Column(Boolean, default=False)
auto_update_enabled: Mapped[bool] = Column(Boolean, default=False, nullable=False)
auto_update_query = Column(Unicode, nullable=True) # holds json data
auto_update_facets = Column(Unicode, nullable=True) # holds json data
auto_update_last_update = Column(DateTime, nullable=True)
Expand Down
8 changes: 6 additions & 2 deletions src/palace/manager/sqlalchemy/model/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,16 @@ def equivalent_to(self, data_source, identifier, strength):
input=self,
output=identifier,
on_multiple="interchangeable",
create_method_kwargs={
"strength": strength,
},
)
eq.strength = strength
if new:
logging.info(
"Identifier equivalency: %r==%r p=%.2f", self, identifier, strength
)
else:
eq.strength = strength
return eq

@classmethod
Expand Down Expand Up @@ -1152,7 +1156,7 @@ class Equivalency(Base):
# How strong is this assertion (-1..1)? A negative number is an
# assertion that the two Identifiers do *not* identify the
# same work.
strength = Column(Float, index=True)
strength: Mapped[float] = Column(Float, index=True, nullable=False)

# Should this equivalency actually be used in calculations? This
# is not manipulated directly, but it gives us the ability to use
Expand Down
2 changes: 1 addition & 1 deletion src/palace/manager/sqlalchemy/model/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class Measurement(Base):

# A Measurement always comes from some DataSource.
data_source_id: Mapped[int] = Column(
Integer, ForeignKey("datasources.id"), index=True, nullable=False
Integer, ForeignKey("datasources.id"), index=True
)
data_source: Mapped[DataSource] = relationship(
"DataSource", back_populates="measurements"
Expand Down
69 changes: 60 additions & 9 deletions tests/fixtures/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from palace.manager.sqlalchemy.model.customlist import CustomList
from palace.manager.sqlalchemy.model.datasource import DataSource
from palace.manager.sqlalchemy.model.edition import Edition
from palace.manager.sqlalchemy.model.identifier import Identifier
from palace.manager.sqlalchemy.model.identifier import Equivalency, Identifier
from palace.manager.sqlalchemy.model.integration import (
IntegrationConfiguration,
IntegrationLibraryConfiguration,
Expand All @@ -84,8 +84,9 @@
LicenseStatus,
RightsStatus,
)
from palace.manager.sqlalchemy.model.measurement import Measurement
from palace.manager.sqlalchemy.model.patron import Patron
from palace.manager.sqlalchemy.model.resource import Hyperlink, Representation
from palace.manager.sqlalchemy.model.resource import Hyperlink, Representation, Resource
from palace.manager.sqlalchemy.model.work import Work
from palace.manager.sqlalchemy.session import SessionManager
from palace.manager.sqlalchemy.util import create, get_one_or_create
Expand Down Expand Up @@ -725,7 +726,7 @@ def edition(
unlimited_access=False,
):
id = identifier_id or self.fresh_str()
source = DataSource.lookup(self.session, data_source_name)
source = DataSource.lookup(self.session, data_source_name, autocreate=True)
wr = Edition.for_foreign_id(self.session, source, identifier_type, id)[0]
if not title:
title = self.fresh_str()
Expand Down Expand Up @@ -845,10 +846,54 @@ def representation(self, url=None, media_type=None, content=None, mirrored=False
repr.content = content
repr.fetched_at = utc_now()
if mirrored:
repr.mirror_url = "http://foo.com/" + self.fresh_str()
repr.mirror_url = "http://foo.com/" + self.fresh_str()
repr.mirrored_at = utc_now()
return repr, is_new

def resource(
self, url: str | None = None, data_source: str | DataSource | None = None
) -> Resource:
if data_source is None:
data_source = DataSource.GUTENBERG
if isinstance(data_source, str):
data_source = DataSource.lookup(self.session, data_source)
return create(self.session, Resource, url=url, data_source=data_source)[0]

def equivalency(
self,
input: Identifier,
output: Identifier,
strength: float = 1,
data_source: str | DataSource | None = None,
) -> Equivalency:
if data_source is None:
data_source = DataSource.GUTENBERG
if isinstance(data_source, str):
data_source = DataSource.lookup(self.session, data_source)
return create(
self.session,
Equivalency,
input=input,
output=output,
strength=strength,
data_source=data_source,
)[0]

def measurement(
self,
value: float | None = None,
quantity_measured: str = "answer",
is_most_recent: bool = True,
) -> Measurement:
return create(
self.session,
Measurement,
quantity_measured=quantity_measured,
value=value,
is_most_recent=is_most_recent,
)[0]

def lane(
self,
display_name=None,
Expand Down Expand Up @@ -1129,12 +1174,15 @@ def classification(

def customlist(
self,
foreign_identifier=None,
name=None,
data_source_name=DataSource.NYT,
num_entries=1,
entries_exist_as_works=True,
):
foreign_identifier: str | None = None,
name: str | None = None,
data_source_name: str = DataSource.NYT,
num_entries: int = 1,
entries_exist_as_works: bool = True,
library: Library | None = None,
auto_update_enabled: bool = False,
auto_update_query: str | None = None,
) -> tuple[CustomList, list[Edition]]:
data_source = DataSource.lookup(self.session, data_source_name)
foreign_identifier = foreign_identifier or self.fresh_str()
now = utc_now()
Expand All @@ -1146,9 +1194,12 @@ def customlist(
updated=now,
name=name or self.fresh_str(),
description=self.fresh_str(),
auto_update_enabled=auto_update_enabled,
auto_update_query=auto_update_query,
),
data_source=data_source,
foreign_identifier=foreign_identifier,
library=library,
)

editions = []
Expand Down
Loading

0 comments on commit 7519473

Please sign in to comment.