Skip to content

Commit

Permalink
Fixed# 543 - attribute field to support boolean and character (at mos…
Browse files Browse the repository at this point in the history
…t 2 chars)

 * Updated tests
 * Update from supporting numeric to characters (at most 2 chars)

Signed-off-by: Chin Yeung Li <[email protected]>
  • Loading branch information
chinyeungli committed Oct 19, 2023
1 parent 024da73 commit 7c88fba
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
7 changes: 4 additions & 3 deletions docs/source/specification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,12 @@ Optional Boolean flag fields
- internal_use_only: Set this flag to yes if the component is used internal only.
Defaults to no when absent.

Optional Boolean and Numberic fields
------------------------------------
Optional Boolean and Character fields
-------------------------------------

- attribute: This field can be either in boolean value: ('yes', 'y', 'true',
'x', 'no', 'n', 'false') or numeric value field. Defaults to no when absent.
'x', 'no', 'n', 'false') or a character value field with no more than 2
characters. Defaults to no when absent.

Optional Extension fields
-------------------------
Expand Down
29 changes: 13 additions & 16 deletions src/attributecode/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,10 @@ def __eq__(self, other):
and self.value == other.value)


class BooleanAndNumbericField(SingleLineField):
class BooleanAndTwoCharactersField(SingleLineField):
"""
Field with either a boolean value or a numeric value. Validated value is
False, True, None or numeric value.
Field with either a boolean value or character(s) value (at most 2
characters). Validated value is False, True, None or character value.
"""

def default_value(self):
Expand All @@ -735,10 +735,10 @@ def default_value(self):

def _validate(self, *args, **kwargs):
"""
Check that flag are valid with either boolean value or numeric value. Default flag to
False. Return a list of errors.
Check that flag are valid with either boolean value or character value.
Default flag to False. Return a list of errors.
"""
errors = super(BooleanAndNumbericField,
errors = super(BooleanAndTwoCharactersField,
self)._validate(*args, ** kwargs)
self.about_file_path = kwargs.get('about_file_path')
flag = self.get_value(self.original_value)
Expand All @@ -748,7 +748,7 @@ def _validate(self, *args, **kwargs):
about_file_path = self.about_file_path
flag_values = self.flag_values
msg = (u'Path: %(about_file_path)s - Field %(name)s: Invalid value: %(val)r is not '
u'one of: %(flag_values)s and it is not a numeric value.' % locals())
u'one of: %(flag_values)s and it is not a 1 or 2 character value.' % locals())
errors.append(Error(ERROR, msg))
self.value = None
elif flag is None:
Expand Down Expand Up @@ -783,18 +783,15 @@ def get_value(self, value):
return None

value = value.lower()
if value in self.flag_values:
if value in self.flag_values or len(value) <= 2:
if value in self.true_flags:
return u'yes'
else:
elif value in self.false_flags:
return u'no'
else:
if value.isdigit():
return value
else:
return False
elif isinstance(value, int):
return value
return value
else:
return False
else:
return False

Expand Down Expand Up @@ -912,7 +909,7 @@ def set_standard_fields(self):
('notice_url', UrlField()),

('redistribute', BooleanField()),
('attribute', BooleanAndNumbericField()),
('attribute', BooleanAndTwoCharactersField()),
('track_changes', BooleanField()),
('modified', BooleanField()),
('internal_use_only', BooleanField()),
Expand Down
28 changes: 27 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def test_About_boolean_value(self):
assert a.redistribute.value is True
assert a.track_changes.value is None

def test_About_boolean_numeric_value(self):
def test_About_boolean_numberic_value(self):
test_file = get_test_loc('test_model/parse/boolean_numeric_data.about')
a = model.About(test_file)
expected_msg = "Field track_changes is present but empty."
Expand All @@ -669,6 +669,32 @@ def test_About_boolean_numeric_value(self):
assert a.redistribute.value is True
assert a.track_changes.value is None

def test_About_boolean_character_value(self):
test_file = get_test_loc('test_model/parse/boolean_chara_data.about')
a = model.About(test_file)
# Context of the test file
"""
about_resource: .
name: data
attribute: 11
"""
assert a.attribute.value == '11'
assert len(a.errors) == 0

def test_About_boolean_more_than_2_character_value(self):
test_file = get_test_loc(
'test_model/parse/boolean_more_than_2_chara_data.about')
a = model.About(test_file)
expected_msg = "Path: None - Field attribute: Invalid value: 'abc' is not one of: ('yes', 'y', 'true', 'x', 'no', 'n', 'false') and it is not a 1 or 2 character value."
assert expected_msg in a.errors[0].message
# Context of the test file
"""
about_resource: .
name: test
attribute: abc
"""
assert a.attribute.value is None

def test_About_contains_about_file_path(self):
test_file = get_test_loc('test_model/serialize/about.ABOUT')
# TODO: I am not sure this override of the about_file_path makes sense
Expand Down
3 changes: 3 additions & 0 deletions tests/testdata/test_model/parse/boolean_chara_data.about
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
about_resource: .
name: data
attribute: 11
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
about_resource: .
name: test
attribute: abc

0 comments on commit 7c88fba

Please sign in to comment.