Skip to content

Commit

Permalink
Merge pull request #168 from mcneilco/ACAS-797
Browse files Browse the repository at this point in the history
ACAS-797: Fix error when code_origin is explicitly None
  • Loading branch information
bffrost authored Nov 26, 2024
2 parents de0d461 + f9381b0 commit 1d4205d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
14 changes: 8 additions & 6 deletions acasclient/lsthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,11 @@ def __init__(self, code, code_type=None, code_kind=None,
self.code_kind = code_kind
self.code_origin = code_origin
# Auto-recognize some code origin:
if self.code_origin.upper() == ACAS_DDICT:
self.ddict = ACASDDict(code_type, code_kind)
elif self.code_origin.upper() == ACAS_LSTHING:
self.ddict = ACASLsThingDDict(code_type, code_kind)
if self.code_origin:
if self.code_origin.upper() == ACAS_DDICT:
self.ddict = ACASDDict(code_type, code_kind)
elif self.code_origin.upper() == ACAS_LSTHING:
self.ddict = ACASLsThingDDict(code_type, code_kind)

def __hash__(self):
return hash(f'{self.code}-{self.code_type}-{self.code_kind}-'
Expand Down Expand Up @@ -2153,7 +2154,7 @@ def _get_ddicts(self):
for state_dict in state_dicts:
for values_dict in state_dict.values():
for value in values_dict.values():
if isinstance(value, CodeValue):
if isinstance(value, CodeValue) and value.code_origin:
ddict = None
if value.code_origin.upper() == ACAS_DDICT:
ddict = ACASDDict(value.code_type, value.code_kind)
Expand All @@ -2169,6 +2170,7 @@ def _get_ddicts(self):
@validation_result
def _validate_codevalues(self, ddicts):
"""Confirm all CodeValues have valid values.
Note: validation is skipped if a CodeValue's code_origin is None
:param ddicts: dict of (code_type, code_kind, code_origin): DDict. Should come from _get_ddicts()
:type ddicts: dict
Expand All @@ -2179,7 +2181,7 @@ def _validate_codevalues(self, ddicts):
for values_dict in state_dict.values():
for value in values_dict.values():
if isinstance(value, CodeValue):
if value.code:
if value.code and value.code_origin:
# Get the corresponding DDict
ddict = ddicts.get((value.code_type, value.code_kind, value.code_origin.upper()), None)
if ddict:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_lsthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from acasclient.ddict import ACASDDict, ACASLsThingDDict
from acasclient.lsthing import (BlobValue, CodeValue, FileValue, LsThingValue,
SimpleLsThing, get_lsKind_to_lsvalue, datetime_to_ts, LsThing)
SimpleLsThing, get_lsKind_to_lsvalue, datetime_to_ts, LsThing, ACAS_DDICT)
from acasclient.validation import ValidationResult, get_validation_response
from acasclient.protocol import Protocol
from tests.test_acasclient import BaseAcasClientTest
Expand Down Expand Up @@ -970,6 +970,34 @@ def test_as_dict(self):
assert blob_value_dict['comments'] == comments
assert blob_value_dict['id'] == id

class TestCodeValue(BaseAcasClientTest):

def test_instantiation_without_code_origin(self):
"""
Verify that CodeValue can be instantiated without a code_origin.
"""
code = "1234"
code_value = CodeValue(code=code, code_type=None, code_kind=None, code_origin=None)
assert code_value.code == code
assert code_value.code_origin is None
# Construct a basic SimpleLsThing object and save it
name = str(uuid.uuid4())
meta_dict = {
NAME_KEY: name,
IS_RESTRICTED_KEY: True,
STATUS_KEY: ACTIVE,
START_DATE_KEY: datetime.now()
}
newProject = Project(recorded_by=self.client.username, **meta_dict)
# Override the status with the CodeValue with null code_origin
# Confirm this bypasses CodeValue validation
newProject.metadata[PROJECT_METADATA][PROJECT_STATUS] = code_value
newProject.save(self.client)
# Fetch the project and confirm the CodeValue is as expected (code_origin should be None)
fresh_project = Project.get_by_code(newProject.code_name, self.client, Project.ls_type, Project.ls_kind)
assert fresh_project.metadata[PROJECT_METADATA][PROJECT_STATUS].code == code
assert fresh_project.metadata[PROJECT_METADATA][PROJECT_STATUS].code_origin is None

class TestValidationResponse(BaseAcasClientTest):

def test_001_response_with_errors_and_warnings(self):
Expand Down

0 comments on commit 1d4205d

Please sign in to comment.