def test_api_runs(self): flow = self.create_flow() contacts = self._create_contacts(50, ["Bobby", "Jimmy", "Mary"]) self._create_runs(300, flow, contacts) self.login(self.user) self.clear_cache() with SegmentProfiler("Fetch first page of flow runs from API", self, assert_queries=API_INITIAL_REQUEST_QUERIES + 7, assert_tx=0, force_profile=True): self._fetch_json('%s.json' % reverse('api.v1.runs')) # query count, terminal nodes and category nodes for the flow all now cached with SegmentProfiler("Fetch second page of flow runs from API", self, assert_queries=API_REQUEST_QUERIES + 4, assert_tx=0, force_profile=True): self._fetch_json('%s.json?page=2' % reverse('api.v1.runs')) with SegmentProfiler("Create new flow runs via API endpoint", self, assert_tx=1, force_profile=True): data = {'flow': flow.pk, 'contact': [c.uuid for c in contacts]} self._post_json('%s.json' % reverse('api.v1.runs'), data)
def test_api_messages(self): contacts = self._create_contacts(300, ["Bobby", "Jimmy", "Mary"]) # create messages and labels incoming = self._create_incoming(300, "Hello", self.tel_mtn, contacts) self._create_labels(10, ["My Label"], incoming) self.login(self.user) self.clear_cache() with SegmentProfiler("Fetch first page of messages from API", self, assert_queries=API_INITIAL_REQUEST_QUERIES + 3, assert_tx=0, force_profile=True): self._fetch_json('%s.json' % reverse('api.v1.messages')) # query count now cached with SegmentProfiler("Fetch second page of messages from API", self, assert_queries=API_REQUEST_QUERIES + 2, assert_tx=0, force_profile=True): self._fetch_json('%s.json?page=2' % reverse('api.v1.messages'))
def test_contact_create(self): num_contacts = 1000 with SegmentProfiler("Creating new contacts", self, force_profile=True): self._create_contacts(num_contacts, ["Bobby"]) with SegmentProfiler("Updating existing contacts", self, force_profile=True): self._create_contacts(num_contacts, ["Jimmy"])
def test_group_counts(self): num_contacts = 10000 contacts = self._create_contacts(num_contacts, ["Bobby", "Jimmy", "Mary"]) groups = self._create_groups(10, ["Big Group"], contacts) with SegmentProfiler("Contact group counts via regular queries", self, force_profile=True): for group in groups: self.assertEqual(group.contacts.count(), num_contacts) self.assertEqual(group.contacts.count(), num_contacts) with SegmentProfiler("Contact group counts with caching", self, force_profile=True): for group in groups: self.assertEqual(group.get_member_count(), num_contacts) self.assertEqual(group.get_member_count(), num_contacts)
def test_omnibox(self): contacts = self._create_contacts(10000, ["Bobby", "Jimmy", "Mary"]) self._create_groups(100, ["Bobbys", "Jims", "Marys"], contacts) self.login(self.user) with SegmentProfiler("Omnibox with telephone search", self, force_profile=True): self._fetch_json("%s?search=078" % reverse("contacts.contact_omnibox"))
def test_flow_start(self): contacts = self._create_contacts(10000, ["Bobby", "Jimmy", "Mary"]) groups = self._create_groups(10, ["Bobbys", "Jims", "Marys"], contacts) flow = self.create_flow() with SegmentProfiler("Starting a flow", self, True, force_profile=True): flow.start(groups, []) self.assertEqual(10000, Msg.objects.all().count()) self.assertEqual(10000, FlowRun.objects.all().count()) self.assertEqual(20000, FlowStep.objects.all().count())
def test_api_groups(self): contacts = self._create_contacts(300, ["Bobby", "Jimmy", "Mary"]) self._create_groups(300, ["Bobbys", "Jims", "Marys"], contacts) self.login(self.user) self.clear_cache() with SegmentProfiler("Fetch first page of groups from API", self, assert_queries=API_INITIAL_REQUEST_QUERIES + 2, assert_tx=0, force_profile=True): self._fetch_json('%s.json' % reverse('api.v1.contactgroups'))
def test_message_incoming(self): num_contacts = 300 with SegmentProfiler("Creating incoming messages from new contacts", self, False, force_profile=True): for c in range(0, num_contacts): scheme, path, channel = self.urn_generators[c % len( self.urn_generators)](c) Msg.create_incoming(channel, (scheme, path), "Thanks #1", self.user) with SegmentProfiler( "Creating incoming messages from existing contacts", self, False, force_profile=True): for c in range(0, num_contacts): scheme, path, channel = self.urn_generators[c % len( self.urn_generators)](c) Msg.create_incoming(channel, (scheme, path), "Thanks #2", self.user) # check messages for each channel incoming_total = 2 * num_contacts self.assertEqual( incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.tel_mtn).count()) self.assertEqual( incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.tel_tigo).count()) self.assertEqual( incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.twitter).count())
def test_contact_search(self): contacts = self._create_contacts(10000, ["Bobby", "Jimmy", "Mary"]) self._create_values(contacts, self.field_nick, lambda c: c.name.lower().replace(' ', '_')) with SegmentProfiler("Contact search with simple query", self, force_profile=True): qs, is_complex = Contact.search(self.org, 'bob') self.assertEqual(3334, qs.count()) self.assertEqual(False, is_complex) with SegmentProfiler("Contact search with complex query", self, force_profile=True): qs, is_complex = Contact.search( self.org, 'name = bob or tel has 078 or twitter = tweep_123 or nick is bob' ) self.assertEqual(3377, qs.count()) self.assertEqual(True, is_complex)
def test_pages(self): # create contacts and groups contacts = self._create_contacts(10000, ["Bobby", "Jimmy", "Mary"]) self._create_groups(10, ["My Group"], contacts) # create messages and labels incoming = self._create_incoming(10000, "Hello", self.tel_mtn, contacts) self._create_labels(10, ["My Label"], incoming) # create calls self._create_calls(10000, self.tel_mtn, contacts) # populate nickname and age fields self._create_values(contacts, self.field_nick, lambda c: c.name.lower().replace(' ', '_')) self._create_values(contacts, self.field_age, lambda c: (c.id % 80) + 1) self.login(self.user) with SegmentProfiler("Contact list page", self, force_profile=True): self.client.get(reverse('contacts.contact_list')) with SegmentProfiler("Contact list page (repeat)", self, force_profile=True): self.client.get(reverse('contacts.contact_list')) with SegmentProfiler("Message inbox page", self, force_profile=True): self.client.get(reverse('msgs.msg_inbox')) with SegmentProfiler("Message inbox page (repeat)", self, force_profile=True): self.client.get(reverse('msgs.msg_inbox'))
def test_message_export(self): # create contacts contacts = self._create_contacts(100, ["Bobby", "Jimmy", "Mary"]) # create messages and labels incoming = self._create_incoming(100, "Hello", self.tel_mtn, contacts) self._create_labels(10, ["My Label"], incoming) task = ExportMessagesTask.objects.create(org=self.org, created_by=self.user, modified_by=self.user) with SegmentProfiler("Export messages", self, True, force_profile=True): task.do_export()
def test_channellog(self): contact = self.create_contact("Test", "+250788383383") msg = Msg.create_outgoing(self.org, self.admin, contact, "This is a test message") msg = dict_to_struct('MockMsg', msg.as_task_json()) with SegmentProfiler("Channel Log inserts (10,000)", self, force_profile=True): for i in range(10000): ChannelLog.log_success(msg, "Sent Message", method="GET", url="http://foo", request="GET http://foo", response="Ok", response_status="201")
def test_message_outgoing(self): num_contacts = 3000 contacts = self._create_contacts( num_contacts, ["Bobby"]) # i.e. 1000 Bobbys of each URN type broadcast = self._create_broadcast("Hello message #1", contacts) with SegmentProfiler("Sending broadcast to new contacts", self, True, force_profile=True): broadcast.send() # give all contact URNs an assigned channel as if they've been used for incoming for contact in contacts: urn = contact.get_urn() if urn.scheme == TWITTER_SCHEME: urn.channel = self.twitter elif urn.path.startswith('+25078'): urn.channel = self.tel_mtn elif urn.path.startswith('+25072'): urn.channel = self.tel_tigo urn.save() broadcast = self._create_broadcast("Hello message #2", contacts) with SegmentProfiler("Sending broadcast when urns have channels", self, True, force_profile=True): broadcast.send() broadcast = self._create_broadcast("Hello =contact #3", contacts) with SegmentProfiler("Sending broadcast with expression", self, True, force_profile=True): broadcast.send() # check messages for each channel outgoing_total = 3 * num_contacts self.assertEqual( outgoing_total / 3, Msg.objects.filter(direction=OUTGOING, channel=self.tel_mtn).count()) self.assertEqual( outgoing_total / 3, Msg.objects.filter(direction=OUTGOING, channel=self.tel_bulk).count()) self.assertEqual( outgoing_total / 3, Msg.objects.filter(direction=OUTGOING, channel=self.twitter).count()) self.assertEqual( len(contacts) / 3, ContactURN.objects.filter(channel=self.tel_mtn).count()) self.assertEqual( len(contacts) / 3, ContactURN.objects.filter(channel=self.tel_tigo).count()) self.assertEqual( len(contacts) / 3, ContactURN.objects.filter(channel=self.twitter).count())