Example #1
0
class ChoiceFacadeTestCase(TestCase, Utility):
	def setUp(self):
		self.sut = ChoiceFacade()
		self.sut._choiceRepository = create_autospec(ChoiceRepository, spec_set=True, instance=True)
		self.sut._choiceRepository.getChoiceByUniqueId.side_effect = [None]
		self.sut._uniqueIdGenerator = create_autospec(UniqueIdGenerator, spec_set=True, instance=True)

	def test_when_creating_a_choice_then_poll_is_associated_with_the_choice(self):
		expectedChoice = Choice()
		expectedPoll = Poll()

		self.sut.create(expectedChoice, expectedPoll)

		self.assertEqual(expectedChoice.poll, expectedPoll)
		self.sut._choiceRepository.create.assert_called_once_with(expectedChoice)

	def test_when_creating_a_choice_then_a_unique_id_is_created(self):
		repeatId = self.getUniqueId()
		expectedUniqueId = self.getUniqueId()
		expectedChoice = Choice()

		self.sut._uniqueIdGenerator.createUniqueId.side_effect = [repeatId, expectedUniqueId]
		self.sut._choiceRepository.getChoiceByUniqueId.side_effect = [Choice(), None]
		self.sut.create(expectedChoice, Poll())
		
		self.assertEqual(expectedChoice.uniqueId, expectedUniqueId)
		self.sut._choiceRepository.create.assert_called_once_with(expectedChoice)
Example #2
0
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