Skip to content

Commit

Permalink
Merge pull request #5471 from nyaruka/test_cleanup
Browse files Browse the repository at this point in the history
Only import real flows in tests where it's required
  • Loading branch information
rowanseymour authored Aug 23, 2024
2 parents 11eef1f + ce2bb0e commit a0102fa
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 67 deletions.
4 changes: 2 additions & 2 deletions temba/api/v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3184,7 +3184,7 @@ def test_flow_starts(self, mock_async_start):
self.assertPostNotPermitted(endpoint_url, [None, self.agent, self.user])
self.assertDeleteNotAllowed(endpoint_url)

flow = self.get_flow("favorites_v13")
flow = self.create_flow("Test")

# try to create an empty flow start
self.assertPost(endpoint_url, self.editor, {}, errors={"flow": "This field is required."})
Expand Down Expand Up @@ -3402,7 +3402,7 @@ def test_flow_starts(self, mock_async_start):
{
"id": start3.id,
"uuid": str(start3.uuid),
"flow": {"uuid": flow.uuid, "name": "Favorites"},
"flow": {"uuid": flow.uuid, "name": "Test"},
"contacts": [{"uuid": self.joe.uuid, "name": "Joe Blow"}],
"groups": [{"uuid": hans_group.uuid, "name": "hans"}],
"restart_participants": False,
Expand Down
2 changes: 1 addition & 1 deletion temba/channels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_release(self, mr_mocks):
channel2 = Channel.create(self.org, self.user, "", "T", "Test Channel", "0785553333")

# add channel trigger
flow = self.get_flow("color")
flow = self.create_flow("Test")
Trigger.create(self.org, self.admin, Trigger.TYPE_CATCH_ALL, flow, channel=channel1)

# create some activity on this channel
Expand Down
5 changes: 2 additions & 3 deletions temba/contacts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,8 @@ def test_delete(self):
@mock_mailroom
def test_start(self, mr_mocks):
sample_flows = list(self.org.flows.order_by("name"))
background_flow = self.get_flow("background")
self.get_flow("media_survey")
archived_flow = self.get_flow("color")
background_flow = self.create_flow("Background")
archived_flow = self.create_flow("Archived")
archived_flow.archive(self.admin)

contact = self.create_contact("Joe", phone="+593979000111")
Expand Down
94 changes: 43 additions & 51 deletions temba/flows/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_clean_name(self):

@patch("temba.mailroom.queue_interrupt")
def test_archive(self, mock_queue_interrupt):
flow = self.get_flow("color")
flow = self.create_flow("Test")
flow.archive(self.admin)

mock_queue_interrupt.assert_called_once_with(self.org, flow=flow)
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_flow_archive_with_campaign(self):
self.assertTrue(flow.is_archived)

def test_editor(self):
flow = self.get_flow("color")
flow = self.create_flow("Test")

self.login(self.admin)

Expand Down Expand Up @@ -1245,7 +1245,7 @@ def test_group_send(self):
self.get_flow("group_send_flow")

def test_flow_delete_of_inactive_flow(self):
flow = self.get_flow("favorites")
flow = self.create_flow("Test")
flow.release(self.admin)

self.login(self.admin)
Expand Down Expand Up @@ -1630,7 +1630,7 @@ def test_views(self):
create_url = reverse("flows.flow_create")

self.create_contact("Eric", phone="+250788382382")
flow = self.get_flow("color")
flow = self.create_flow("Test")

# create a flow for another org
other_flow = Flow.create(self.org2, self.admin2, "Flow2")
Expand Down Expand Up @@ -1821,7 +1821,7 @@ def test_views(self):
self.assertEqual(language_flow.base_language, "eng")

def test_update_messaging_flow(self):
flow = self.get_flow("color_v13")
flow = self.create_flow("Test")
update_url = reverse("flows.flow_update", args=[flow.id])

def assert_triggers(expected: list):
Expand All @@ -1833,9 +1833,9 @@ def assert_triggers(expected: list):
update_url,
[self.editor, self.admin],
form_fields={
"name": "Colors",
"name": "Test",
"keyword_triggers": [],
"expires_after_minutes": 720,
"expires_after_minutes": 10080,
"ignore_triggers": False,
},
)
Expand Down Expand Up @@ -1931,7 +1931,7 @@ def assert_triggers(expected: list):
)

def test_update_voice_flow(self):
flow = self.get_flow("ivr")
flow = self.create_flow("IVR Test", flow_type=Flow.TYPE_VOICE)
update_url = reverse("flows.flow_update", args=[flow.id])

self.assertRequestDisallowed(update_url, [None, self.user, self.agent, self.admin2])
Expand Down Expand Up @@ -1979,7 +1979,7 @@ def test_update_voice_flow(self):
self.assertEqual(30, flow.metadata["ivr_retry"])

def test_update_surveyor_flow(self):
flow = self.get_flow("media_survey")
flow = self.create_flow("Survey", flow_type=Flow.TYPE_SURVEY)
update_url = reverse("flows.flow_update", args=[flow.id])

# we should only see name and contact creation option on form
Expand All @@ -1994,7 +1994,7 @@ def test_update_surveyor_flow(self):
self.assertEqual("login", flow.metadata.get("contact_creation"))

def test_update_background_flow(self):
flow = self.get_flow("background")
flow = self.create_flow("Background", flow_type=Flow.TYPE_BACKGROUND)
update_url = reverse("flows.flow_update", args=[flow.id])

# we should only see name on form
Expand All @@ -2008,14 +2008,14 @@ def test_update_background_flow(self):
self.assertEqual("New Name", flow.name)

def test_list_views(self):
flow1 = self.get_flow("color_v13")
flow2 = self.get_flow("no_ruleset_flow")
flow1 = self.create_flow("Flow 1")
flow2 = self.create_flow("Flow 2")

# archive second flow
flow2.is_archived = True
flow2.save(update_fields=("is_archived",))

flow3 = Flow.create(self.org, self.admin, "Flow 3")
flow3 = self.create_flow("Flow 3")

self.login(self.admin)

Expand Down Expand Up @@ -2293,7 +2293,7 @@ def test_save_revisions(self):
self.assertResponseError(response, "description", "Your flow has been upgraded to the latest version")

def test_inactive_flow(self):
flow = self.get_flow("color_v13")
flow = self.create_flow("Deleted")
flow.release(self.admin)

self.login(self.admin)
Expand Down Expand Up @@ -2898,6 +2898,16 @@ def test_results(self):
self.assertEqual(24, len(data["hod"]))
self.assertEqual(7, len(data["dow"]))

# check that views return 404 for inactive flows
flow = self.create_flow("Deleted")
flow.release(self.admin)

response = self.client.get(reverse("flows.flow_activity_chart", args=[flow.id]))
self.assertEqual(404, response.status_code)

response = self.client.get(reverse("flows.flow_category_counts", args=[flow.uuid]))
self.assertEqual(404, response.status_code)

def test_activity(self):
flow = self.get_flow("favorites_v13")
flow_nodes = flow.get_definition()["nodes"]
Expand Down Expand Up @@ -2939,24 +2949,6 @@ def test_activity(self):
response.json(),
)

def test_activity_chart_of_inactive_flow(self):
flow = self.get_flow("favorites")
flow.release(self.admin)

self.login(self.admin)
response = self.client.get(reverse("flows.flow_activity_chart", args=[flow.id]))

self.assertEqual(404, response.status_code)

def test_category_counts_of_inactive_flow(self):
flow = self.get_flow("favorites")
flow.release(self.admin)

self.login(self.admin)
response = self.client.get(reverse("flows.flow_category_counts", args=[flow.uuid]))

self.assertEqual(404, response.status_code)

def test_write_protection(self):
flow = self.get_flow("favorites_v13")
flow_json = flow.get_definition()
Expand Down Expand Up @@ -3738,7 +3730,7 @@ def create_session(org, created_on: datetime):

def test_trim(self):
contact = self.create_contact("Ben Haggerty", phone="+250788123123")
flow = self.get_flow("color")
flow = self.create_flow("Test")

# create some runs that have sessions
session1 = FlowSession.objects.create(
Expand Down Expand Up @@ -4953,7 +4945,7 @@ def test_from_archives(self):

def test_no_responses(self):
today = timezone.now().astimezone(self.org.timezone).date()
flow = self.get_flow("color_v13")
flow = self.create_flow("Test")

self.assertEqual(flow.get_run_stats()["total"], 0)

Expand All @@ -4963,7 +4955,7 @@ def test_no_responses(self):

# every sheet has only the head row
self.assertEqual(1, len(list(workbook.worksheets[0].rows)))
self.assertEqual(11, len(list(workbook.worksheets[0].columns)))
self.assertEqual(8, len(list(workbook.worksheets[0].columns)))


class FlowLabelTest(TembaTest):
Expand Down Expand Up @@ -5308,28 +5300,28 @@ class FlowRevisionTest(TembaTest):
def test_trim_revisions(self):
start = timezone.now()

color = self.get_flow("color")
clinic = self.get_flow("the_clinic")
flow1 = self.create_flow("Flow 1")
flow2 = self.create_flow("Flow 2")

revision = 100
FlowRevision.objects.all().update(revision=revision)

# create a single old clinic revision
FlowRevision.objects.create(
flow=clinic,
flow=flow2,
definition=dict(),
revision=99,
created_on=timezone.now() - timedelta(days=7),
created_by=self.admin,
)

# make a bunch of revisions for color on the same day
# make a bunch of revisions for flow 1 on the same day
created = timezone.now().replace(hour=6) - timedelta(days=1)
for i in range(25):
revision -= 1
created = created - timedelta(minutes=1)
FlowRevision.objects.create(
flow=color, definition=dict(), revision=revision, created_by=self.admin, created_on=created
flow=flow1, definition=dict(), revision=revision, created_by=self.admin, created_on=created
)

# then for 5 days prior, make a few more
Expand All @@ -5339,32 +5331,32 @@ def test_trim_revisions(self):
revision -= 1
created = created - timedelta(minutes=1)
FlowRevision.objects.create(
flow=color, definition=dict(), revision=revision, created_by=self.admin, created_on=created
flow=flow1, definition=dict(), revision=revision, created_by=self.admin, created_on=created
)

# trim our flow revisions, should be left with original (today), 25 from yesterday, 1 per day for 5 days = 31
self.assertEqual(76, FlowRevision.objects.filter(flow=color).count())
self.assertEqual(76, FlowRevision.objects.filter(flow=flow1).count())
self.assertEqual(45, FlowRevision.trim(start))
self.assertEqual(31, FlowRevision.objects.filter(flow=color).count())
self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count())
self.assertEqual(
7,
FlowRevision.objects.filter(flow=color)
FlowRevision.objects.filter(flow=flow1)
.annotate(created_date=TruncDate("created_on"))
.distinct("created_date")
.count(),
)

# trim our clinic flow manually, should remain unchanged
self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count())
self.assertEqual(0, FlowRevision.trim_for_flow(clinic.id))
self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count())
self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count())
self.assertEqual(0, FlowRevision.trim_for_flow(flow2.id))
self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count())

# call our task
trim_flow_revisions()
self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count())
self.assertEqual(31, FlowRevision.objects.filter(flow=color).count())
self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count())
self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count())

# call again (testing reading redis key)
trim_flow_revisions()
self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count())
self.assertEqual(31, FlowRevision.objects.filter(flow=color).count())
self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count())
self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count())
4 changes: 2 additions & 2 deletions temba/globals/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_model(self):
self.assertEqual("Secret Value", global3.name)
self.assertEqual("", global3.value)

flow1 = self.get_flow("color")
flow2 = self.get_flow("favorites")
flow1 = self.create_flow("Flow 1")
flow2 = self.create_flow("Flow 2")

flow1.global_dependencies.add(global1, global2)
flow2.global_dependencies.add(global1)
Expand Down
6 changes: 3 additions & 3 deletions temba/mailroom/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class MailroomQueueTest(TembaTest):
def test_queue_flow_start(self):
flow = self.get_flow("favorites")
flow = self.create_flow("Test")
jim = self.create_contact("Jim", phone="+12065551212")
bobs = self.create_group("Bobs", [self.create_contact("Bob", phone="+12065551313")])

Expand Down Expand Up @@ -101,7 +101,7 @@ def test_queue_interrupt_by_contacts(self):
)

def test_queue_interrupt_by_flow(self):
flow = self.get_flow("favorites")
flow = self.create_flow("Test")
flow.archive(self.admin)

self.assert_org_queued(self.org, "batch")
Expand Down Expand Up @@ -472,7 +472,7 @@ def test_from_flow_run(self):
)

def test_from_event_fire(self):
flow = self.get_flow("color_v13")
flow = self.create_flow("Test")
group = self.create_group("Reporters", contacts=[])
registered = self.create_field("registered", "Registered", value_type="D")
campaign = Campaign.create(self.org, self.admin, "Welcomes", group)
Expand Down
6 changes: 2 additions & 4 deletions temba/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,15 @@ def create_flow(self, name: str, *, flow_type=Flow.TYPE_MESSAGE, nodes=None, is_
"name": name,
"type": Flow.GOFLOW_TYPES[flow_type],
"revision": 1,
"spec_version": "13.1.0",
"spec_version": Flow.CURRENT_SPEC_VERSION,
"expire_after_minutes": Flow.EXPIRES_DEFAULTS[flow_type],
"language": "eng",
"nodes": nodes,
}

flow.version_number = definition["spec_version"]
flow.save()

json_flow = Flow.migrate_definition(definition, flow)
flow.save_revision(self.admin, json_flow)
flow.save_revision(self.admin, definition)

return flow

Expand Down
2 changes: 1 addition & 1 deletion temba/tickets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def test_list(self):
self.assertEqual(("tickets", "mine", "open", str(ticket.uuid)), response.context["temba_referer"])

# contacts in a flow get interrupt menu option instead
flow = self.get_flow("color")
flow = self.create_flow("Test")
self.contact.current_flow = flow
self.contact.save()
deep_link = f"{list_url}all/open/{str(ticket.uuid)}/"
Expand Down

0 comments on commit a0102fa

Please sign in to comment.