Skip to content

Commit

Permalink
work in progress, two test cases, Bauble/bauble.classic#59.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrasca committed Jan 27, 2016
1 parent 35a52ae commit 10fd1d1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 45 deletions.
73 changes: 36 additions & 37 deletions bauble/plugins/garden/accession.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,6 @@ def dms_to_decimal(dir, deg, min, sec, precision=6):
return dec.quantize(nplaces)


def get_next_code():
"""
Return the next available accession code. This function should be
specific to the institution.
At the moment it assumes that you are using an accession code of
the format: YYYY.CCCC where YYYY is the four digit year and CCCC
is the four digit code left filled with zeroes
If there is an error getting the next code the None is returned.
"""
# auto generate/increment the accession code
session = db.Session()
format = '%Y%PD####'
format = format.replace('%PD', Plant.get_delimiter())
format = datetime.date.today().strftime(format)
start = format.rstrip('#')
digits = len(format) - len(start)
format = start + '%%0%dd' % digits
q = session.query(Accession.code).\
filter(Accession.code.startswith(start))
next = None
try:
if q.count() > 0:
codes = [int(row[0][len(start):]) for row in q]
next = format % (max(codes)+1)
else:
next = format % 1
except Exception, e:
logger.debug(e)
pass
finally:
session.close()
return next


def generic_taxon_add_action(model, view, presenter, top_presenter,
button, taxon_entry):
"""user hit click on taxon add button
Expand Down Expand Up @@ -589,6 +553,7 @@ class Accession(db.Base, db.Serializable, db.WithNotes):
# columns
#: the accession code
code = Column(Unicode(20), nullable=False, unique=True)
code_format = u'%Y%PD####'

@validates('code')
def validate_stripping(self, key, value):
Expand Down Expand Up @@ -653,6 +618,40 @@ def validate_stripping(self, key, value):
intended2_location = relation(
'Location', primaryjoin='Accession.intended2_location_id==Location.id')

@classmethod
def get_next_code(cls):
"""
Return the next available accession code.
the format is stored in the `bauble` table.
the format may contain a %PD, replaced by the plant delimiter.
date formatting is applied.
If there is an error getting the next code the None is returned.
"""
# auto generate/increment the accession code
session = db.Session()
format = cls.code_format.replace('%PD', Plant.get_delimiter())
format = datetime.date.today().strftime(format)
start = unicode(format.rstrip('#'))
digits = len(format) - len(start)
format = start + '%%0%dd' % digits
q = session.query(Accession.code).\
filter(Accession.code.startswith(start))
next = None
try:
if q.count() > 0:
codes = [int(row[0][len(start):]) for row in q]
next = format % (max(codes)+1)
else:
next = format % 1
except Exception, e:
logger.debug(e)
pass
finally:
session.close()
return unicode(next)

def search_view_markup_pair(self):
"""provide the two lines describing object for SearchView row.
Expand Down Expand Up @@ -1725,7 +1724,7 @@ def __init__(self, model, view):
self.current_source_box = None

if not model.code:
model.code = get_next_code()
model.code = self.get_next_code()
if self.model.species:
self._dirty = True

Expand Down
44 changes: 36 additions & 8 deletions bauble/plugins/garden/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,36 +1887,64 @@ def test_plant_note_from_dict(self):
self.assertFalse(p is None)


from bauble.plugins.garden.accession import get_next_code
from bauble.plugins.garden.location import mergevalues


class GlobalFunctionsTests(GardenTestCase):
class AccessionGetNextCode(GardenTestCase):
def test_get_next_code_first_this_year(self):
this_year = str(datetime.date.today().year)
self.assertEquals(get_next_code(), this_year + '.0001')
self.assertEquals(Accession.get_next_code(), this_year + '.0001')

def test_get_next_code_second_this_year(self):
this_year = str(datetime.date.today().year)
this_code = get_next_code()
acc = Accession(species=self.species, code=this_code)
this_code = Accession.get_next_code()
acc = Accession(species=self.species, code=unicode(this_code))
self.session.add(acc)
self.session.flush()
self.assertEquals(get_next_code(), this_year + '.0002')
self.assertEquals(Accession.get_next_code(), this_year + '.0002')

def test_get_next_code_absolute_beginning(self):
this_year = str(datetime.date.today().year)
self.session.query(Accession).delete()
self.session.flush()
self.assertEquals(get_next_code(), this_year + '.0001')
self.assertEquals(Accession.get_next_code(), this_year + '.0001')

def test_get_next_code_next_with_hole(self):
this_year = str(datetime.date.today().year)
this_code = this_year + u'.0050'
acc = Accession(species=self.species, code=this_code)
self.session.add(acc)
self.session.flush()
self.assertEquals(get_next_code(), this_year + '.0051')
self.assertEquals(Accession.get_next_code(), this_year + '.0051')

def test_get_next_code_alter_format_first(self):
this_year = str(datetime.date.today().year)
this_code = this_year + u'.0050'
orig = Accession.code_format
acc = Accession(species=self.species, code=this_code)
self.session.add(acc)
self.session.flush()
Accession.code_format = u'H.###'
self.assertEquals(Accession.get_next_code(), 'H.001')
Accession.code_format = u'SD.###'
self.assertEquals(Accession.get_next_code(), 'SD.001')
Accession.code_format = orig

def test_get_next_code_alter_format_next(self):
orig = Accession.code_format
acc = Accession(species=self.species, code=u'H.012')
self.session.add(acc)
acc = Accession(species=self.species, code=u'SD.002')
self.session.add(acc)
self.session.flush()
Accession.code_format = u'H.###'
self.assertEquals(Accession.get_next_code(), 'H.013')
Accession.code_format = u'SD.###'
self.assertEquals(Accession.get_next_code(), 'SD.003')
Accession.code_format = orig


class GlobalFunctionsTests(GardenTestCase):

def test_mergevalues_equal(self):
'if the values are equal, return it'
Expand Down

0 comments on commit 10fd1d1

Please sign in to comment.