Skip to content

Commit 7c88fba

Browse files
committed
Fixed# 543 - attribute field to support boolean and character (at most 2 chars)
* Updated tests * Update from supporting numeric to characters (at most 2 chars) Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 024da73 commit 7c88fba

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

docs/source/specification.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ Optional Boolean flag fields
358358
- internal_use_only: Set this flag to yes if the component is used internal only.
359359
Defaults to no when absent.
360360

361-
Optional Boolean and Numberic fields
362-
------------------------------------
361+
Optional Boolean and Character fields
362+
-------------------------------------
363363

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

367368
Optional Extension fields
368369
-------------------------

src/attributecode/model.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,10 @@ def __eq__(self, other):
720720
and self.value == other.value)
721721

722722

723-
class BooleanAndNumbericField(SingleLineField):
723+
class BooleanAndTwoCharactersField(SingleLineField):
724724
"""
725-
Field with either a boolean value or a numeric value. Validated value is
726-
False, True, None or numeric value.
725+
Field with either a boolean value or character(s) value (at most 2
726+
characters). Validated value is False, True, None or character value.
727727
"""
728728

729729
def default_value(self):
@@ -735,10 +735,10 @@ def default_value(self):
735735

736736
def _validate(self, *args, **kwargs):
737737
"""
738-
Check that flag are valid with either boolean value or numeric value. Default flag to
739-
False. Return a list of errors.
738+
Check that flag are valid with either boolean value or character value.
739+
Default flag to False. Return a list of errors.
740740
"""
741-
errors = super(BooleanAndNumbericField,
741+
errors = super(BooleanAndTwoCharactersField,
742742
self)._validate(*args, ** kwargs)
743743
self.about_file_path = kwargs.get('about_file_path')
744744
flag = self.get_value(self.original_value)
@@ -748,7 +748,7 @@ def _validate(self, *args, **kwargs):
748748
about_file_path = self.about_file_path
749749
flag_values = self.flag_values
750750
msg = (u'Path: %(about_file_path)s - Field %(name)s: Invalid value: %(val)r is not '
751-
u'one of: %(flag_values)s and it is not a numeric value.' % locals())
751+
u'one of: %(flag_values)s and it is not a 1 or 2 character value.' % locals())
752752
errors.append(Error(ERROR, msg))
753753
self.value = None
754754
elif flag is None:
@@ -783,18 +783,15 @@ def get_value(self, value):
783783
return None
784784

785785
value = value.lower()
786-
if value in self.flag_values:
786+
if value in self.flag_values or len(value) <= 2:
787787
if value in self.true_flags:
788788
return u'yes'
789-
else:
789+
elif value in self.false_flags:
790790
return u'no'
791-
else:
792-
if value.isdigit():
793-
return value
794791
else:
795-
return False
796-
elif isinstance(value, int):
797-
return value
792+
return value
793+
else:
794+
return False
798795
else:
799796
return False
800797

@@ -912,7 +909,7 @@ def set_standard_fields(self):
912909
('notice_url', UrlField()),
913910

914911
('redistribute', BooleanField()),
915-
('attribute', BooleanAndNumbericField()),
912+
('attribute', BooleanAndTwoCharactersField()),
916913
('track_changes', BooleanField()),
917914
('modified', BooleanField()),
918915
('internal_use_only', BooleanField()),

tests/test_model.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def test_About_boolean_value(self):
648648
assert a.redistribute.value is True
649649
assert a.track_changes.value is None
650650

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

672+
def test_About_boolean_character_value(self):
673+
test_file = get_test_loc('test_model/parse/boolean_chara_data.about')
674+
a = model.About(test_file)
675+
# Context of the test file
676+
"""
677+
about_resource: .
678+
name: data
679+
attribute: 11
680+
"""
681+
assert a.attribute.value == '11'
682+
assert len(a.errors) == 0
683+
684+
def test_About_boolean_more_than_2_character_value(self):
685+
test_file = get_test_loc(
686+
'test_model/parse/boolean_more_than_2_chara_data.about')
687+
a = model.About(test_file)
688+
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."
689+
assert expected_msg in a.errors[0].message
690+
# Context of the test file
691+
"""
692+
about_resource: .
693+
name: test
694+
attribute: abc
695+
"""
696+
assert a.attribute.value is None
697+
672698
def test_About_contains_about_file_path(self):
673699
test_file = get_test_loc('test_model/serialize/about.ABOUT')
674700
# TODO: I am not sure this override of the about_file_path makes sense
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
about_resource: .
2+
name: data
3+
attribute: 11
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
about_resource: .
2+
name: test
3+
attribute: abc

0 commit comments

Comments
 (0)