Skip to content

Commit

Permalink
Feat: pagination validation new (#612)
Browse files Browse the repository at this point in the history
* Refatora validação de paginação

* Adapta os testes
  • Loading branch information
Rossi-Luciano authored May 14, 2024
1 parent b7ebe18 commit ca6c340
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 40 deletions.
89 changes: 49 additions & 40 deletions packtools/sps/validation/front_articlemeta_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def validate(self, data):
Parameters
----------
date : dict
data : dict
data={
'expected_value_volume': '56',
'response_type_for_absent_issue': 'WARNING',
Expand Down Expand Up @@ -310,21 +310,6 @@ def validate(self, data):
yield from self.validate_supplement(data['expected_value_supplement'])


def _response(sub_item, expected, advice, obtained=None):
return format_response(
title='Pagination validation',
parent=None,
parent_id=None,
item='article-meta',
sub_item=sub_item,
validation_type='exist',
is_valid=False,
expected=expected,
obtained=obtained,
advice=advice
)


class Pagination:
def __init__(self, xml_tree):
self.xml_tree = xml_tree
Expand Down Expand Up @@ -366,29 +351,53 @@ def validation_pagination_attributes_exist(self):
},...
]
"""
if self.issue.elocation_id is None:
if self.issue.fpage is None:
response = _response(
sub_item='fpage',
expected='a value for fpage',
advice='provide a value for fpage'
)
response['data'] = self.issue.data
yield response
if self.issue.lpage is None:
response = _response(
sub_item='lpage',
expected='a value for lpage',
advice='provide a value for lpage'
)
response['data'] = self.issue.data
yield response
elif self.issue.fpage is not None or self.issue.lpage is not None:
response = _response(
sub_item='elocation-id',
expected='no values for fpage and lpage OR no value for elocation-id',
e_location = self.issue.elocation_id is not None
fpage = self.issue.fpage is not None
lpage = self.issue.lpage is not None
volume = self.issue.volume is not None
number = self.issue.number is not None

if e_location and fpage and lpage:
yield format_response(
title='Pagination validation',
parent='article',
parent_id=None,
item='article-meta',
sub_item='e-location, fpage, lpage',
validation_type='exist',
is_valid=False,
expected='e-location OR fpage + lpage',
obtained=f'elocation-id: {self.issue.elocation_id}, fpage: {self.issue.fpage}, lpage: {self.issue.lpage}',
advice='remove values for fpage and lpage OR remove value for elocation-id'
advice='it is necessary to provide e-location OR fpage + lpage',
data=self.issue.data
)

elif not e_location and not fpage and not lpage and volume and number:
yield format_response(
title='Pagination validation',
parent='article',
parent_id=None,
item='article-meta',
sub_item='e-location, fpage, lpage',
validation_type='exist',
is_valid=False,
expected='e-location OR fpage + lpage',
obtained=f'elocation-id: {self.issue.elocation_id}, fpage: {self.issue.fpage}, lpage: {self.issue.lpage}',
advice='it is necessary to provide e-location OR fpage + lpage',
data=self.issue.data
)

else:
yield format_response(
title='Pagination validation',
parent='article',
parent_id=None,
item='article-meta',
sub_item='e-location, fpage, lpage',
validation_type='exist',
is_valid=True,
expected='e-location OR fpage + lpage',
obtained=f'elocation-id: {self.issue.elocation_id}, fpage: {self.issue.fpage}, lpage: {self.issue.lpage}',
advice=None,
data=self.issue.data
)
response['data'] = self.issue.data
yield response
149 changes: 149 additions & 0 deletions tests/sps/validation/test_front_articlemeta_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,152 @@ def test_validate_article_issue(self):
for i, item in enumerate(expected):
with self.subTest(i):
self.assertDictEqual(obtained[i], item)


class PaginationTest(TestCase):
def test_validation_pages_success(self):
self.maxDiff = None
xml = (
'''
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
<front>
<article-meta>
<fpage>220</fpage>
<lpage>240</lpage>
</article-meta>
</front>
</article>
'''
)
xmltree = etree.fromstring(xml)

expected = [
{
'title': 'Pagination validation',
'parent': 'article',
'parent_id': None,
'item': 'article-meta',
'sub_item': 'e-location, fpage, lpage',
'validation_type': 'exist',
'response': 'OK',
'expected_value': 'e-location OR fpage + lpage',
'got_value': 'elocation-id: None, fpage: 220, lpage: 240',
'message': 'Got elocation-id: None, fpage: 220, lpage: 240, expected e-location OR fpage + lpage',
'advice': None,
'data': {'fpage': '220', 'lpage': '240'},
}
]
obtained = list(Pagination(xmltree).validation_pagination_attributes_exist())
for i, item in enumerate(expected):
with self.subTest(i):
self.assertDictEqual(item, obtained[i])

def test_validation_e_location_success(self):
self.maxDiff = None
xml = (
'''
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
<front>
<article-meta>
<elocation-id>e51467</elocation-id>
</article-meta>
</front>
</article>
'''
)
xmltree = etree.fromstring(xml)
expected = [
{
'title': 'Pagination validation',
'parent': 'article',
'parent_id': None,
'item': 'article-meta',
'sub_item': 'e-location, fpage, lpage',
'validation_type': 'exist',
'response': 'OK',
'expected_value': 'e-location OR fpage + lpage',
'got_value': 'elocation-id: e51467, fpage: None, lpage: None',
'message': 'Got elocation-id: e51467, fpage: None, lpage: None, expected e-location OR fpage + lpage',
'advice': None,
'data': {'elocation_id': 'e51467'},
}
]
obtained = list(Pagination(xmltree).validation_pagination_attributes_exist())
for i, item in enumerate(expected):
with self.subTest(i):
self.assertDictEqual(item, obtained[i])

def test_validation_pages_and_e_location_exists_fail(self):
self.maxDiff = None
xml = (
'''
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
<front>
<article-meta>
<elocation-id>e51467</elocation-id>
<fpage>220</fpage>
<lpage>240</lpage>
</article-meta>
</front>
</article>
'''
)

xmltree = etree.fromstring(xml)
expected = [
{
'title': 'Pagination validation',
'parent': 'article',
'parent_id': None,
'item': 'article-meta',
'sub_item': 'e-location, fpage, lpage',
'validation_type': 'exist',
'response': 'ERROR',
'expected_value': 'e-location OR fpage + lpage',
'got_value': 'elocation-id: e51467, fpage: 220, lpage: 240',
'message': 'Got elocation-id: e51467, fpage: 220, lpage: 240, expected e-location OR fpage + lpage',
'advice': 'it is necessary to provide e-location OR fpage + lpage',
'data': {'elocation_id': 'e51467', 'fpage': '220', 'lpage': '240'},
}
]
obtained = list(Pagination(xmltree).validation_pagination_attributes_exist())
for i, item in enumerate(expected):
with self.subTest(i):
self.assertDictEqual(item, obtained[i])

def test_validation_pages_and_e_location_not_exists_fail(self):
self.maxDiff = None
xml = (
'''
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
<front>
<article-meta>
<volume>4</volume>
<issue>1</issue>
</article-meta>
</front>
</article>
'''
)

xmltree = etree.fromstring(xml)
expected = [
{
'title': 'Pagination validation',
'parent': 'article',
'parent_id': None,
'item': 'article-meta',
'sub_item': 'e-location, fpage, lpage',
'validation_type': 'exist',
'response': 'ERROR',
'expected_value': 'e-location OR fpage + lpage',
'got_value': 'elocation-id: None, fpage: None, lpage: None',
'message': 'Got elocation-id: None, fpage: None, lpage: None, expected e-location OR fpage + lpage',
'advice': 'it is necessary to provide e-location OR fpage + lpage',
'data': {'number': '1', 'volume': '4'},
}
]
obtained = list(Pagination(xmltree).validation_pagination_attributes_exist())
for i, item in enumerate(expected):
with self.subTest(i):
self.assertDictEqual(item, obtained[i])

0 comments on commit ca6c340

Please sign in to comment.