def create_poll(request_body): user = User.objects.filter(username = request_body['username'])[0] new_poll = EventPolls(creator = user, is_finalized= False, title= request_body['poll']['title'], description= request_body['poll']['description']) new_poll.save() EmailService.send_email_to_these_usernames('ایجاد نظر سنجی', 'یک نظر سنجی جدید که شما در آن عضو هستید ایجاد شده است', request_body['poll']['participants']) Polls.create_options(new_poll, request_body['poll']['options'], request_body['poll']['participants']) return {"ok":True,"id":new_poll.id}
def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:23Z") self.new_option.save()
class TestFinalizeAndReopen(TestCase): def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:23Z") self.new_option.save() def test_finalize_poll(self): Polls.finalize(self.new_poll.id, self.new_option.id, self.new_user.username) self.assertEqual( EventPolls.objects.filter( id=self.new_poll.id).values("is_finalized")[0]['is_finalized'], True) def test_reopen_poll(self): Polls.finalize(self.new_poll.id, self.new_option.id, self.new_user.username) fields = {} fields['pollId'] = self.new_poll.id fields['notificationMessage'] = "new notification message" Polls.reopen_poll(fields) print( EventPolls.objects.filter( id=self.new_poll.id).values("is_finalized")[0]['is_finalized']) self.assertEqual( EventPolls.objects.filter( id=self.new_poll.id).values("is_finalized")[0]['is_finalized'], False) def tearDown(self): self.new_option.delete() self.new_poll.delete() self.new_user.delete()
class TestVote(TestCase): request_body = {} def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:25Z") self.new_option.save() self.new_participants_vote = ParticipantsVotes( user=self.new_user, event_poll=self.new_poll, option=self.new_option) self.new_participants_vote.save() def test_vote(self): vote_info = { 'username': '******', 'pollId': self.new_poll.id, 'votes': [{ "optionId": self.new_option.id, "voteType": 2 }] } Vote.vote(vote_info) self.assertEqual( ParticipantsVotes.objects.filter( user=self.new_user, option=self.new_option).values_list('vote_type', flat=True)[0], 2) def tearDown(self): self.new_participants_vote.delete() self.new_option.delete() self.new_poll.delete() self.new_user.delete()
class TestComment(TestCase): fields = {} new_user = User(username='******') new_poll = EventPolls(creator=new_user, title='test', description='test') new_option = Options(event_poll=new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:25Z") def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:25Z") self.new_option.save() comment = {} comment['commentedOnId'] = self.new_option.id comment['isReply'] = False comment['repliedToId'] = None comment['content'] = 'hi' self.fields['username'] = '******' self.fields['comment'] = comment def test_add_comment(self): id = Comment.add_comment(self.fields) comments = Comment.get_comments(self.new_option) comment = [c for c in comments if c['id'] == id] self.assertEqual(comment[0]['text'], 'hi') def tearDown(self): self.new_option.delete() self.new_poll.delete() self.new_user.delete()
def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:25Z") self.new_option.save() comment = {} comment['commentedOnId'] = self.new_option.id comment['isReply'] = False comment['repliedToId'] = None comment['content'] = 'hi' self.fields['username'] = '******' self.fields['comment'] = comment
class TestGetPolls(TestCase): def setUp(self): self.new_user = User(username='******') self.new_user.save() self.new_poll = EventPolls(creator=self.new_user, title='test', description='test') self.new_poll.save() self.new_option = Options(event_poll=self.new_poll, label='test', startDate="2015-02-05T11:24:23Z", endDate="2015-02-05T11:24:23Z") self.new_option.save() self.new_participants_vote = ParticipantsVotes( user=self.new_user, event_poll=self.new_poll, option=self.new_option) self.new_participants_vote.save() def test_get_poll(self): poll = Polls.get_poll(self.new_poll.id) self.assertEqual(poll['title'], 'test') self.assertEqual(poll['options'][0]['label'], 'test') def test_get_created_polls(self): polls = Polls.get_created_polls('test') self.assertEqual(polls[0]['title'], 'test') def test_get_involved_polls(self): polls = Polls.get_involved_polls('test') self.assertEqual(polls[0]['event_poll__title'], 'test') def tearDown(self): self.new_option.delete() self.new_poll.delete() self.new_user.delete() self.new_participants_vote.delete()