class UniqueIdGeneratorTestCase(TestCase): sut = None; def setUp(self): self.sut = UniqueIdGenerator() def test_when_generating_unique_id_then_length_is_10(self): uniqueId = self.sut.createUrlSafeUniqueId() self.assertEqual(10, len(uniqueId)) def test_when_generating_unique_id_then_does_not_contain_illegal_characters(self): illegalChar = ['+', '-', '_', '\\', '/', '='] uniqueId = self.sut.createUrlSafeUniqueId() for c in illegalChar: self.assertNotIn(c, uniqueId) def test_when_generating_unique_id_then_length_is_22(self): uniqueId = self.sut.createUniqueId() self.assertEqual(22, len(uniqueId))
class PollFacade(): def __init__(self): self._uniqueIdGenerator = UniqueIdGenerator() self._choiceFacade = ChoiceFacade() self._pollRepository = PollRepository() def create(self, poll, choices): poll.uniqueId = self._getUnusedPollUniqueId() self._pollRepository.create(poll) for choice in choices: self._choiceFacade.create(choice, poll) def _getUnusedPollUniqueId(self): for i in range(100): candidatePollUniqueId = self._uniqueIdGenerator.createUrlSafeUniqueId() existingPoll = self._pollRepository.getPollByUniqueId(candidatePollUniqueId) if existingPoll is None: break return candidatePollUniqueId