Skip to content

Commit

Permalink
Fix: corrige procedimento de validação para 'issue' (#529)
Browse files Browse the repository at this point in the history
* Remove testes obsoletos

* Adiciona teste para quando não há valor de 'issue'

* Adiciona teste para quando o valor de 'issue' não corresponde a nenhum padrão esperado

* Adiciona conjunto de testes para quando o valor de 'issue' é um número

* Adiciona testes para quando o valor de 'issue' é um número especial

* Adiciona conjunto de testes para quando o valor de 'issue' é suplemento de volume

* Adiciona conjunto de testes para quando o valor de 'issue' é suplemento de número

* Adiciona função para verificação de número válido

* Adiciona função para verificação de número especial válido

* Adiciona função para verificação de suplemento válido

* Adiciona função para validação de número

* Adiciona função para validação de número especial

* Adiciona função para validação de suplemento

* Adiciona funções auxiliares

* Adiciona função de validação para 'issue'

* Modifica função para validar um valor ao invés de um número

* Corrige '_is_special_number'

* Corrige '_is_supplement'

* Remove '_validate_number' e adiciona '_validate_value'

* Corrige mensagens

* Remove função obsoleta

* Refatora validação

* Remove tag '<supplement>' de testes

* Corrige teste para '<issue>' não obrigatório

* Adapta os testes às modificações na validação

* Adiciona teste

* Corrige nomes de funções para melhorar a semântica

* Adiciona resposta para o caso de não haver identificador para 'issue'

* Adapta os testes às modificações

* Adiciona classe de exceção

* Adiciona o parâmetro 'response_type_for_absent_issue'

* Adapta os testes
  • Loading branch information
Rossi-Luciano authored Jan 22, 2024
1 parent 08bd897 commit 6edce03
Show file tree
Hide file tree
Showing 3 changed files with 573 additions and 94 deletions.
3 changes: 3 additions & 0 deletions packtools/sps/validation/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ class ValidationLicenseCodeException(Exception):

class ValidationDataAvailabilityException(Exception):
...

class ValidationIssueMissingValue(Exception):
...
159 changes: 130 additions & 29 deletions packtools/sps/validation/front_articlemeta_issue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
from ..models.front_articlemeta_issue import ArticleMetaIssue
from ..validation.exceptions import ValidationIssueMissingValue


def _issue_identifier_is_valid(value):
if value.isnumeric():
return str(int(value)) == value
else:
return not('.' in value or ' ' in value)


def _issue_special_number_is_valid(value):
"""
a special number value cannot contain a space or dot
<issue>spe1</issue>
<issue>spe</issue>
"""
anterior, posterior = value.split('spe')
return anterior == '' and (_issue_identifier_is_valid(posterior) or posterior == '')


def _issue_supplement_is_valid(value):
"""
supplement cannot have a dot
<issue>4 suppl 1</issue>
<issue>suppl 1</issue>
"""
anterior, posterior = value.split('suppl')
if anterior:
return _issue_identifier_is_valid(anterior.strip()) and _issue_identifier_is_valid(posterior.strip())
else:
return _issue_identifier_is_valid(posterior.strip())


def _validate_issue_identifier(obtained):
if obtained.isnumeric():
message = 'a numeric value that does not start with zero'
advice = 'Provide a valid numeric value'
else:
message = 'a alphanumeric value that does not contain space or dot'
advice = 'Provide a valid alphanumeric value'
if not _issue_identifier_is_valid(obtained):
return False, message, advice
else:
return _successful_validation(obtained)


def _validate_special_number(obtained):
if not _issue_special_number_is_valid(obtained):
return False, 'speX where X is a valid alphanumeric value or None', 'Provide a valid value to special number'
else:
return _successful_validation(obtained)


def _validate_supplement(obtained):
if not _issue_supplement_is_valid(obtained):
return False, 'X suppl Y where X and Y are alphanumeric value', 'Provide a valid value to supplement'
else:
return _successful_validation(obtained)


def _successful_validation(obtained):
return True, obtained, None


class IssueValidation:
Expand Down Expand Up @@ -39,38 +101,78 @@ def validate_volume(self, expected_value):
)
return resp_vol

def validate_issue(self, expected_value):
def validate_article_issue(self, response_type_for_absent_issue):
"""
Checks the correctness of a issue.
Parameters
----------
expected_value : str
Correct value for issue.
Checks whether the format of a value for issue is valid.
XML input
---------
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
<front>
<article-meta>
<volume>56</volume>
<issue>4</issue>
<supplement>2</supplement>
</article-meta>
</front>
</article>
Returns
-------
dict
A dictionary as described in the example.
Examples
--------
>>> validate_issue('4')
{
'object': 'issue',
'output_expected': '4',
'output_obteined': '4',
'match': True
}
list of dict
A list of dictionaries, such as:
[
{
'title': 'Article-meta issue element validation',
'xpath': './/front/article-meta/issue',
'validation_type': 'format',
'response': 'OK',
'expected_value': '4',
'got_value': '4',
'message': 'Got 4 expected 4',
'advice': None
}
]
"""
resp_issue = dict(
object='issue',
output_expected=expected_value,
output_obteined=self.article_issue.issue,
match=(expected_value == self.article_issue.issue)
)
return resp_issue
if not response_type_for_absent_issue:
raise ValidationIssueMissingValue("Function requires response type for absent value")

obtained = self.article_issue.issue

if obtained:
if 'spe' in obtained:
is_valid, expected, advice = _validate_special_number(obtained)
elif 'suppl' in obtained:
is_valid, expected, advice = _validate_supplement(obtained)
else:
is_valid, expected, advice = _validate_issue_identifier(obtained)

return [
{
'title': 'Article-meta issue element validation',
'xpath': './/front/article-meta/issue',
'validation_type': 'format',
'response': 'OK' if is_valid else 'ERROR',
'expected_value': expected,
'got_value': obtained,
'message': 'Got {} expected {}'.format(obtained, expected),
'advice': advice
}
]
else:
expected = 'an identifier for the publication issue'
return [
{
'title': 'Article-meta issue element validation',
'xpath': './/front/article-meta/issue',
'validation_type': 'exist',
'response': response_type_for_absent_issue,
'expected_value': expected,
'got_value': None,
'message': 'Got None expected {}'.format(expected),
'advice': 'Provide an identifier for the publication issue'
}
]

def validate_supplement(self, expected_value):
"""
Expand Down Expand Up @@ -104,8 +206,7 @@ def validate_supplement(self, expected_value):
match=(expected_value == self.article_issue.suppl)
)
return resp_suppl



def validate(self, data):
"""
Função que executa as validações da classe IssueValidation.
Expand Down
Loading

0 comments on commit 6edce03

Please sign in to comment.