Skip to content

Commit

Permalink
Merge pull request #39 from jamilatta/tk6
Browse files Browse the repository at this point in the history
Regra de formação de URL para Suplemento.
  • Loading branch information
jfunez authored Jun 23, 2017
2 parents cbf3c31 + ee3a012 commit 0bea84d
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 63 deletions.
15 changes: 12 additions & 3 deletions legendarium/legendarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import re


class Legendarium(object):

def __init__(self, acron_title='', year_pub='', volume='', number='',
fpage='', lpage='', article_id=''):
fpage='', lpage='', article_id='', suppl_number=''):

self.acron_title = acron_title
self.year_pub = str(year_pub)
self.volume = volume
self.number = number
self.suppl_number = suppl_number
self.fpage = fpage
self.lpage = lpage
self.article_id = article_id
Expand Down Expand Up @@ -67,6 +69,12 @@ def _clean_acron_title(self):
"""
return self.acron_title.strip()

def _clean_suppl_number(self):
"""
Clean the suppl_number stripped the beginning and the end of the string.
"""
return self.suppl_number.strip()

def get_journal(self):
"""
Method to build the journal, it can have ``year_pub`` or not.
Expand All @@ -86,9 +94,10 @@ def get_issue(self):
"""
volume = self._clean_volume()
number = self._clean_number()
suppl = self._clean_suppl_number()

if number:
number = u'({0})'.format(number)
if number or suppl:
number = u'({0}{1})'.format(number, '{0}suppl.{1}'.format(' ' if number else '', suppl) if suppl else '')

if volume or number:
return u'{0}{1}{2}'.format(';', volume, number)
Expand Down
21 changes: 17 additions & 4 deletions legendarium/urlegendarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import re


class URLegendarium(object):

def __init__(self, acron='', year_pub='', volume='', number='',
fpage='', lpage='', article_id=''):
fpage='', lpage='', article_id='', suppl_number=''):

self.acron = acron
self.year_pub = str(year_pub)
self.volume = volume
self.number = number
self.suppl_number = suppl_number
self.fpage = fpage
self.lpage = lpage
self.article_id = article_id
Expand Down Expand Up @@ -67,6 +69,12 @@ def _clean_acron(self):
"""
return self.acron.strip()

def _clean_suppl_number(self):
"""
Clean the supplement number stripped the beginning and the end of the string.
"""
return self.suppl_number.strip()

def get_journal_seg(self):
"""
Method to build the journal URL.
Expand All @@ -85,6 +93,10 @@ def get_issue_seg(self):
year = self._clean_year_pub()
volume = self._clean_volume()
number = self._clean_number()
suppl = self._clean_suppl_number()

if suppl:
suppl = u'suppl{0}'.format(suppl)

if number:
number = u'n{0}'.format(number)
Expand All @@ -95,10 +107,11 @@ def get_issue_seg(self):
if year:
year = u'{0}'.format(year)

if year or volume or number:
return u'{0}.{1}{2}'.format(year, volume, number)
if year or volume or number or suppl:
# We give preference to number
return u'{0}.{1}{2}{3}'.format(year, volume, number, suppl)
else:
raise ValueError(u'Year or Volume or Year must exists to form URL Issue Segment')
raise ValueError(u'Year or Volume or Year must exists to form URL Issue Segment')

def get_article_seg(self):
"""
Expand Down
121 changes: 65 additions & 56 deletions tests/test_legendarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,206 +11,214 @@ def setUp(self):
'year_pub': u'2011',
'volume': u'67',
'number': u'9',
'suppl_number': u'3',
'fpage': u'154',
'lpage': u'200',
'article_id': u'e00120416'
}


def test_build_legend_with_all_param(self):
leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):e00120416', leg.stamp)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9 suppl.3):e00120416', leg.stamp)

def test_build_legend_without_acron_title(self):

del(self.dict_leg['acron_title']) # Remove the journal
del(self.dict_leg['acron_title']) # Remove the journal

leg = Legendarium(**self.dict_leg)

self.assertRaises(ValueError, lambda: leg.stamp)


def test_build_legend_without_year_pub(self):

del(self.dict_leg['year_pub']) # Remove the publisher year
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['year_pub']) # Remove the publisher year

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj;67(9):e00120416', leg.stamp)


def test_build_legend_without_volume(self):

del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['volume']) # Remove the volume

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;(9):e00120416', leg.stamp)

def test_build_legend_without_number_and_suppl_number(self):

def test_build_legend_without_number(self):

del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['number']) # Remove the number

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67:e00120416', leg.stamp)


def test_build_legend_without_volume_and_number(self):

del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011:e00120416', leg.stamp)

def test_build_legend_without_volume_and_number_and_year_pub_and_suppl_number(self):

def test_build_legend_without_volume_and_number_and_year_pub(self):

del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['year_pub']) # Remove the number
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['year_pub']) # Remove the number

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj:e00120416', leg.stamp)

def test_build_legend_without_volume_and_number_and_year_pub_(self):

del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['year_pub']) # Remove the number

leg = Legendarium(**self.dict_leg)

def test_build_legend_without_volume_and_number_and_year_pub_and_pages(self):
self.assertEqual(u'Rev.Mal-Estar Subj;(suppl.3):e00120416', leg.stamp)

del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['year_pub']) # Remove the publisher year
del(self.dict_leg['fpage']) # Remove the first page
del(self.dict_leg['lpage']) # Remove the last page
del(self.dict_leg['article_id']) # Remove the article identifier
def test_build_legend_without_volume_and_number_and_year_pub_and_suppl_number_and_pages(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['volume']) # Remove the volume
del(self.dict_leg['number']) # Remove the number
del(self.dict_leg['year_pub']) # Remove the publisher year
del(self.dict_leg['fpage']) # Remove the first page
del(self.dict_leg['lpage']) # Remove the last page
del(self.dict_leg['article_id']) # Remove the article identifier

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj', leg.stamp)


def test_build_legend_with_wrong_value_in_year(self):

self.dict_leg['year_pub'] = "20" # Wrong value in year
self.dict_leg['year_pub'] = "20" # Wrong value in year

leg = Legendarium(**self.dict_leg)

self.assertRaises(ValueError, lambda: leg.stamp)


def test_build_legend_with_wrong_value_in_year_more_digits(self):

self.dict_leg['year_pub'] = "2009088" # Wrong value in year
self.dict_leg['year_pub'] = "2009088" # Wrong value in year

leg = Legendarium(**self.dict_leg)

self.assertRaises(ValueError, lambda: leg.stamp)


def test_build_legend_with_wrong_value_volume(self):

self.dict_leg['volume'] = "903A" # Wrong value in volume
del(self.dict_leg['suppl_number']) # Remove the suppl_number
self.dict_leg['volume'] = "903A" # Wrong value in volume

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;903(9):e00120416', leg.stamp)


def test_build_legend_with_wrong_value_fpage(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['article_id'])

self.dict_leg['fpage'] = "oja9sn10" # Wrong value in fpage, return wrong
self.dict_leg['fpage'] = "oja9sn10" # Wrong value in fpage, return wrong

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):oja9sn10-200', leg.stamp)


def test_build_legend_with_wrong_value_lpage(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['article_id'])

self.dict_leg['lpage'] = "oja9sn10" # Wrong value in lpage, return wrong
self.dict_leg['lpage'] = "oja9sn10" # Wrong value in lpage, return wrong

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):154-oja9sn10', leg.stamp)


def test_build_legend_with_just_fpage(self):

del(self.dict_leg['lpage']) # Remove fpage
del(self.dict_leg['article_id']) # Remove elocation that is preference
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['lpage']) # Remove fpage
del(self.dict_leg['article_id']) # Remove elocation that is preference

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):154', leg.stamp)


def test_build_legend_with_just_lpage(self):

del(self.dict_leg['fpage']) # Remove fpage
del(self.dict_leg['article_id']) # Remove elocation that is preference
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['fpage']) # Remove fpage
del(self.dict_leg['article_id']) # Remove elocation that is preference

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):200', leg.stamp)


def test_build_legend_without_fpage_lpage_elocation(self):

del(self.dict_leg['fpage']) # Remove fpage
del(self.dict_leg['lpage']) # Remove lpage
del(self.dict_leg['article_id']) # Remove article_id
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['fpage']) # Remove fpage
del(self.dict_leg['lpage']) # Remove lpage
del(self.dict_leg['article_id']) # Remove article_id

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9)', leg.stamp)


def test_build_legend_without_volume_number(self):

del(self.dict_leg['volume']) # Remove volume
del(self.dict_leg['number']) # Remove number
del(self.dict_leg['volume']) # Remove volume
del(self.dict_leg['number']) # Remove number

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011:e00120416', leg.stamp)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;(suppl.3):e00120416', leg.stamp)

def test_build_legend_with_elocation(self):

del(self.dict_leg['fpage']) # Remove the fpage
del(self.dict_leg['lpage']) # Remove the lpage
del(self.dict_leg['suppl_number']) # Remove the suppl_number
del(self.dict_leg['fpage']) # Remove the fpage
del(self.dict_leg['lpage']) # Remove the lpage

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):e00120416', leg.stamp)


def test_build_acron_title_with_diacritics(self):

self.dict_leg['acron_title'] = u'Acta Ortopédica Brasileira'

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Acta Ortopédica Brasileira 2011;67(9):e00120416', leg.stamp)

self.assertEqual(u'Acta Ortopédica Brasileira 2011;67(9 suppl.3):e00120416', leg.stamp)

def test_build_acron_and_check_if_article_id_is_the_preference(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number

leg = Legendarium(**self.dict_leg)

self.assertEqual(u'Rev.Mal-Estar Subj 2011;67(9):e00120416', leg.stamp)

def test_build_acron_when_number_is_None(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number

self.dict_leg['number'] = None

leg = Legendarium(**self.dict_leg)
Expand All @@ -219,6 +227,7 @@ def test_build_acron_when_number_is_None(self):

def test_build_acron_when_volume_is_None(self):

del(self.dict_leg['suppl_number']) # Remove the suppl_number
self.dict_leg['volume'] = None

leg = Legendarium(**self.dict_leg)
Expand Down
Loading

0 comments on commit 0bea84d

Please sign in to comment.