File tree Expand file tree Collapse file tree 2 files changed +18
-18
lines changed Expand file tree Collapse file tree 2 files changed +18
-18
lines changed Original file line number Diff line number Diff line change 10
10
class IdentifierException (Exception ):
11
11
pass
12
12
13
-
14
13
class Identifier :
15
14
16
15
def __init__ (self , guid : str ):
@@ -31,22 +30,17 @@ def get_id_value(self) -> str:
31
30
def get_part (self , index : int ) -> str :
32
31
key_values = self .guid .split (SEPARATOR )
33
32
34
- if len (key_values ) > 0 :
35
- # Get the last one, that's the one we are interested in
36
- key_value = key_values [- 1 ].split (KEY_VALUE_SEPARATOR )
37
- if len (key_value ) == 2 :
38
- result = key_value [index ]
39
- else :
40
- raise IdentifierException (
41
- "Unexpected number of parts in key_value [{}]: [{}]" ,
42
- key_values [1 ],
43
- len (key_value ),
44
- )
45
- else :
33
+ if not self .guid .strip () or key_values == ['' ]:
34
+ raise IdentifierException (
35
+ f"Empty or improperly formatted record identifier: [{ self .guid } ]"
36
+ )
37
+
38
+ key_value = key_values [- 1 ].split (KEY_VALUE_SEPARATOR , 1 )
39
+
40
+ if len (key_value ) != 2 :
46
41
raise IdentifierException (
47
- "Unexpected number of parts in record identifier [{}]: [{}]" ,
48
- self .guid ,
49
- len (key_values ),
42
+ f"Unexpected number of parts in key_value [{ key_values [- 1 ]} ]: [{ len (key_value )} ]"
50
43
)
51
44
52
- return result
45
+ return key_value [index ]
46
+
Original file line number Diff line number Diff line change @@ -33,11 +33,17 @@ def test_get_part_zero_index(self):
33
33
assert actual == expected
34
34
35
35
def test_get_part_no_separator (self ):
36
- with pytest .raises (IndexError ):
36
+ with pytest .raises (IdentifierException ):
37
37
identifier = Identifier ("some_id_no_separator" )
38
38
part = identifier .get_part (index = 0 )
39
39
40
40
def test_get_part_raises (self ):
41
41
with pytest .raises (IdentifierException ):
42
42
identifier = Identifier ("too_many;id_separators;in_an_id" )
43
43
part = identifier .get_part (index = 1 )
44
+
45
+ def test_get_id_type_and_value (self ):
46
+ guid = "dataset=http://example.com/resource?id=123"
47
+ identifier = Identifier (guid )
48
+ assert identifier .get_id_type () == "dataset"
49
+ assert identifier .get_id_value () == "http://example.com/resource?id=123"
You can’t perform that action at this time.
0 commit comments