Skip to content

Commit

Permalink
Updates after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mmwinther committed Dec 18, 2024
1 parent aaaa5ed commit 0be19bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/dapla_metadata/variable_definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Client for working with Variable Definitions at Statistics Norway."""

from .generated.vardef_client.exceptions import * # noqa: F403
from .generated.vardef_client.models import * # noqa: F403
from .vardef import Vardef
from .variable_definition import CompletePatchOutput
from .variable_definition import VariableDefinition
66 changes: 36 additions & 30 deletions src/dapla_metadata/variable_definitions/variable_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CompletePatchOutput(CompleteResponse):
def from_model(
model: CompleteResponse,
) -> "CompletePatchOutput":
"""Create a CompletePatchOutput instance from a CompleteResponse."""
"""Create a CompletePatchOutput instance from a CompletePatchOutput."""
return CompletePatchOutput.model_construct(**model.model_dump())

def __str__(self) -> str:
Expand All @@ -56,11 +56,11 @@ class VariableDefinition(CompletePatchOutput):
def from_model(
model: CompleteResponse,
) -> "VariableDefinition":
"""Create a VariableDefinition instance from a CompleteResponse or CompletePatchOutput."""
"""Create a VariableDefinition instance from a CompletePatchOutput or CompletePatchOutput."""
return VariableDefinition.model_construct(**model.model_dump())

@vardef_exception_handler
def list_validity_periods(self) -> list[CompleteResponse]:
def list_validity_periods(self) -> list[CompletePatchOutput]:
"""List all Validity Periods for this Variable Definition."""
return [
CompletePatchOutput.from_model(validity_period)
Expand All @@ -72,7 +72,7 @@ def list_validity_periods(self) -> list[CompleteResponse]:
]

@vardef_exception_handler
def list_patches(self) -> list[CompleteResponse]:
def list_patches(self) -> list[CompletePatchOutput]:
"""List all Patches for this Variable Definition."""
return [
CompletePatchOutput.from_model(patch)
Expand All @@ -85,7 +85,7 @@ def list_patches(self) -> list[CompleteResponse]:
def update_draft(
self,
update_draft: UpdateDraft,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Update this Variable definition.
Variable definition must have status 'DRAFT'.
Expand All @@ -94,14 +94,16 @@ def update_draft(
update_draft: The input with updated values.
Returns:
CompleteResponse: Updated Variable definition with all details.
CompletePatchOutput: Updated Variable definition with all details.
"""
return DraftVariableDefinitionsApi(
VardefClient.get_client(),
).update_variable_definition_by_id(
variable_definition_id=self.id,
active_group=config.get_active_group(),
update_draft=update_draft,
return CompletePatchOutput.from_model(
DraftVariableDefinitionsApi(
VardefClient.get_client(),
).update_variable_definition_by_id(
variable_definition_id=self.id,
active_group=config.get_active_group(),
update_draft=update_draft,
),
)

@vardef_exception_handler
Expand All @@ -125,7 +127,7 @@ def delete_draft(
return f"Variable {self.id} safely deleted"

@vardef_exception_handler
def get_patch(self, patch_id: int) -> CompleteResponse:
def get_patch(self, patch_id: int) -> CompletePatchOutput:
"""Get a single Patch by ID.
Args:
Expand All @@ -146,7 +148,7 @@ def create_patch(
self,
patch: Patch,
valid_from: date | None = None,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Create a new patch for this Variable definition.
Args:
Expand All @@ -155,23 +157,25 @@ def create_patch(
If value is None the patch is created in the last validity period.
Returns:
CompleteResponse: Variable definition with all details.
CompletePatchOutput: Variable definition with all details.
"""
return PatchesApi(
VardefClient.get_client(),
).create_patch(
variable_definition_id=self.id,
active_group=config.get_active_group(),
patch=patch,
valid_from=valid_from,
return CompletePatchOutput.from_model(
PatchesApi(
VardefClient.get_client(),
).create_patch(
variable_definition_id=self.id,
active_group=config.get_active_group(),
patch=patch,
valid_from=valid_from,
),
)

@vardef_exception_handler
def create_validity_period(
self,
validity_period: ValidityPeriod,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Create a new validity period for this Variable definition.
In order to create a new validity period input must contain updated
Expand All @@ -181,12 +185,14 @@ def create_validity_period(
validity_period: The input for new validity period
Returns:
CompleteResponse: Variable definition with all details.
CompletePatchOutput: Variable definition with all details.
"""
return ValidityPeriodsApi(
VardefClient.get_client(),
).create_validity_period(
variable_definition_id=self.id,
active_group=config.get_active_group(),
validity_period=validity_period,
return CompletePatchOutput.from_model(
ValidityPeriodsApi(
VardefClient.get_client(),
).create_validity_period(
variable_definition_id=self.id,
active_group=config.get_active_group(),
validity_period=validity_period,
),
)
9 changes: 3 additions & 6 deletions tests/variable_definitions/test_vardef.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from dapla_metadata.variable_definitions.generated.vardef_client.models.complete_response import (
CompletePatchOutput,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.complete_response import (
CompleteResponse,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.draft import (
Draft,
)
Expand Down Expand Up @@ -137,7 +134,7 @@ def test_update_draft(
my_draft = Vardef.get_variable_definition(
variable_definition_id=VARDEF_EXAMPLE_DEFINITION_ID,
)
assert isinstance(my_draft.update_draft(update_draft), CompleteResponse)
assert isinstance(my_draft.update_draft(update_draft), CompletePatchOutput)


def test_delete_draft(
Expand Down Expand Up @@ -170,7 +167,7 @@ def test_create_patch(
)
assert isinstance(
created_patch,
CompleteResponse,
CompletePatchOutput,
)
assert created_patch.patch_id == PATCH_ID

Expand All @@ -186,5 +183,5 @@ def test_create_validity_period(
my_variable = variable_definition
assert isinstance(
my_variable.create_validity_period(validity_period),
CompleteResponse,
CompletePatchOutput,
)

0 comments on commit 0be19bc

Please sign in to comment.