def test_generate_checksum(self): identifier = base32.generate(length=3, checksum=True) # checksum is always 2 digits affixed at the end assert len(identifier) == 3 assert re.match(r'\d\d$', identifier[-2:]) # hyphenation is done after checksum identifier = base32.generate(length=3, split_every=1, checksum=True) assert len(identifier) == 3 + 2 assert re.match(r'\d-\d$', identifier[-3:]) with pytest.raises(ValueError): assert base32.generate(length=2, checksum=True)
def test_generate_hyphenate(self): identifier = base32.generate(length=3, split_every=1) assert len(identifier) == 3 + 2 assert identifier.count('-') == 2 with pytest.raises(ValueError): assert base32.generate(split_every=-1) identifier = base32.generate(length=3, split_every=0) assert identifier.count('-') == 0 identifier = base32.generate(length=3, split_every=3) assert identifier.count('-') == 0 identifier = base32.generate(length=3, split_every=4) assert identifier.count('-') == 0
def generate_id(cls, options=None): """Generate record id.""" passed_options = options or {} # WHY: A new dict needs to be created to prevent side-effects options = copy.deepcopy( current_app.config.get('PIDSTORE_RECORDID_OPTIONS', {})) options.update(passed_options) length = options.get('length', 10) split_every = options.get('split_every', 0) checksum = options.get('checksum', True) return base32.generate(length=length, split_every=split_every, checksum=checksum)
def test_generate_length(self): assert len(base32.generate(length=2)) == 2 with pytest.raises(ValueError): assert base32.generate(length=-1)