def test_no_paired_friends(self): profile = ProfileFactory.create() friend = ProfileFactory.create() profile.friends.add(friend) paired_friend = ProfileFactory.create() paired_friend.paired_with = ProfileFactory.create() profile.friends.add(paired_friend) form = PairProposalForm(profile, data={'to_profile': friend.id}) self.assertEqual(form.fields['from_profile'].queryset.get(), profile) self.assertEqual(set(form.fields['to_profile'].queryset), set(Profile.objects.filter(id=friend.id))) self.assertTrue(form.is_valid())
def test_valid(self): profile = ProfileFactory.create() friend = ProfileFactory.create() profile.friends.add(friend) form = PairProposalForm(profile, data={'to_profile': friend.id}) self.assertEqual(len(PairProposal.objects.all()), 0) self.assertTrue(form.is_valid()) form.save() self.assertEqual(len(PairProposal.objects.all()), 1) proposal = PairProposal.objects.get() self.assertEqual(proposal.from_profile, profile) self.assertEqual(proposal.to_profile, friend)
def test_friends_queryset(self): ProfileFactory.create() profile = ProfileFactory.create() friends = [ProfileFactory.create() for x in range(2)] profile.friends.add(*friends) form = PairProposalForm(profile, data={'to_profile': friends[0].id}) self.assertEqual(form.fields['from_profile'].queryset.get(), profile) self.assertEqual( set(form.fields['to_profile'].queryset), set( Profile.objects.filter( id__in=[friend.id for friend in friends]))) self.assertTrue(form.is_valid())
def test_random(self): profile = ProfileFactory.create(allow_random=True) rando = ProfileFactory.create(allow_random=True) form = PairProposalForm(profile, data={'to_profile': rando.id}) self.assertTrue(form.is_valid()) form.save() self.proposal = profile.proposals_made.get() form = RejectPairProposalForm(data=self._data(), instance=self.proposal) self.assertTrue(form.is_valid()) form.save()
def propose_swap(request): logger.info("%s is propsing a swap", request.user) if request.method == "POST": form = PairProposalForm( from_profile=request.user.profile, data=request.POST) if form.is_valid(): form.save() _send_swap_proposal_email(request.user, form.instance) return json_response({'status': 'ok', 'errors': {}}) else: logger.error("swap proposal failed: %s", form.errors) return json_response({'status': 'error', 'errors': form.errors}) else: logger.error("%s didn't POST to propose_swap view", request.user) return json_response( {'status': 'error', 'errors': {'method': 'Must POST with to_profile set'}})
def test_random(self): profile = ProfileFactory.create(allow_random=True) friend = ProfileFactory.create() profile.friends.add(friend) # Add a friend and make sure it's there self.assertEqual(friend, profile.all_unpaired_friends.get()) rando = ProfileFactory.create(allow_random=True) # The rando also accepts random friends, so it should show up as an # unpaired friend self.assertEqual(set([friend, rando]), set(profile.all_unpaired_friends)) form = PairProposalForm(profile, data={'to_profile': rando.id}) self.assertEqual(len(PairProposal.objects.all()), 0) self.assertTrue(form.is_valid()) form.save() self.assertEqual(len(PairProposal.objects.all()), 1) proposal = PairProposal.objects.get() self.assertEqual(proposal.from_profile, profile) self.assertEqual(proposal.to_profile, rando)
def test_querydict(self): ProfileFactory.create() profile = ProfileFactory.create() friends = [ProfileFactory.create() for x in range(2)] profile.friends.add(*friends) form = PairProposalForm( profile, data=QueryDict('from_profile=%s&to_profile=%s' % (profile.id, friends[0].id))) self.assertEqual(form.fields['from_profile'].queryset.get(), profile) self.assertEqual(set(form.fields['to_profile'].queryset.all()), set(friends))
def test_no_friends_invalid(self): profile = ProfileFactory.create() form = PairProposalForm(profile) self.assertFalse(form.is_valid())