Skip to content

Commit

Permalink
New test method 'get_alone_element()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
genglert committed Apr 25, 2023
1 parent ed9753b commit 5508072
Show file tree
Hide file tree
Showing 107 changed files with 1,000 additions and 1,816 deletions.
21 changes: 9 additions & 12 deletions creme/activities/tests/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,10 +1385,9 @@ def test_createview_related01(self):

self.assertEqual(2, Relation.objects.count())

relations = Relation.objects.filter(type=constants.REL_SUB_PART_2_ACTIVITY)
self.assertEqual(1, len(relations))

relation = relations[0]
relation = self.get_alone_element(
Relation.objects.filter(type=constants.REL_SUB_PART_2_ACTIVITY)
)
self.assertEqual(contact02.id, relation.subject_entity_id)
self.assertEqual(meeting.id, relation.object_entity_id)

Expand Down Expand Up @@ -1586,9 +1585,10 @@ def test_editview01(self):
self.assertEqual(type_id, activity.type.id)
self.assertEqual(sub_type_id, activity.sub_type.id)

relations = Relation.objects.filter(type=constants.REL_SUB_PART_2_ACTIVITY)
self.assertEqual(1, len(relations))
self.assertEqual(rel, relations[0])
part_rel = self.get_alone_element(
Relation.objects.filter(type=constants.REL_SUB_PART_2_ACTIVITY)
)
self.assertEqual(rel, part_rel)

def test_editview02(self):
"Change type."
Expand Down Expand Up @@ -2195,14 +2195,11 @@ def test_listviews(self):

def test_listview_bulk_actions(self):
user = self.login()
export_actions = [
export_action = self.get_alone_element(
action
for action in actions.actions_registry.bulk_actions(user=user, model=Activity)
if isinstance(action, BulkExportICalAction)
]
self.assertEqual(1, len(export_actions))

export_action = export_actions[0]
)
self.assertEqual('activities-export-ical', export_action.type)
self.assertEqual(reverse('activities__dl_ical'), export_action.url)
self.assertIsNone(export_action.action_data)
Expand Down
28 changes: 12 additions & 16 deletions creme/activities/tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,7 @@ def test_add_user_calendar01(self):
},
))

cals = Calendar.objects.filter(user=user)
self.assertEqual(1, len(cals))

cal = cals[0]
cal = self.get_alone_element(Calendar.objects.filter(user=user))
self.assertEqual(name, cal.name)
self.assertEqual(color, cal.color)
self.assertIs(cal.is_default, True)
Expand Down Expand Up @@ -1181,9 +1178,9 @@ def test_activities_data_multiple_users_public_default(self):
@override_settings(ACTIVITIES_DEFAULT_CALENDAR_IS_PUBLIC=False)
def test_selected_calendars_in_session(self):
user = self.login()
sessions_before = self._get_user_sessions(user)
self.assertEqual(1, len(sessions_before))
self.assertNotIn('activities__calendars', sessions_before[0])

session_before = self.get_alone_element(self._get_user_sessions(user))
self.assertNotIn('activities__calendars', session_before)

other_user = self.other_user

Expand Down Expand Up @@ -1216,19 +1213,19 @@ def test_selected_calendars_in_session(self):
Calendar.objects.filter(id__in=get_ctxt('others_selected_calendar_ids')),
)

sessions_after = self._get_user_sessions(user)
self.assertEqual(1, len(sessions_after))
session_after = self.get_alone_element(self._get_user_sessions(user))
self.assertCountEqual(
[cal1.id, cal3.id],
sessions_after[0]['activities__calendars'],
session_after['activities__calendars'],
)

@override_settings(ACTIVITIES_DEFAULT_CALENDAR_IS_PUBLIC=True)
def test_calendars_select(self):
user = self.login()
sessions1 = self._get_user_sessions(user)
self.assertEqual(1, len(sessions1))
self.assertNotIn('activities__calendars', sessions1[0])
# sessions1 = self._get_user_sessions(user)
# self.assertEqual(1, len(sessions1))
session1 = self.get_alone_element(self._get_user_sessions(user))
self.assertNotIn('activities__calendars', session1[0])

other_user = self.other_user

Expand All @@ -1243,11 +1240,10 @@ def test_calendars_select(self):
self.assertGET405(url)

self.assertPOST200(url, data={'add': [str(cal1.id), str(cal2.id)]})
sessions2 = self._get_user_sessions(user)
self.assertEqual(1, len(sessions2))
session2 = self.get_alone_element(self._get_user_sessions(user))
self.assertCountEqual(
[cal1.id, cal2.id],
sessions2[0]['activities__calendars'],
session2['activities__calendars'],
)

# Ignore other not-public Calendars
Expand Down
15 changes: 3 additions & 12 deletions creme/activities/tests/test_mass_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ def test_import01(self):
)
self.assertEqual(constants.NARROW, act7.floating_type)

jr_errors = [r for r in results if r.messages]
self.assertEqual(1, len(jr_errors))

jr_error = jr_errors[0]
jr_error = self.get_alone_element(r for r in results if r.messages)
self.assertListEqual(
[_('End time is before start time')],
jr_error.messages,
Expand Down Expand Up @@ -356,10 +353,7 @@ def test_import02(self):
results = self._get_job_results(job)
self.assertEqual(len(lines), len(results))

jr_errors = [r for r in results if r.messages]
self.assertEqual(1, len(jr_errors))

jr_error = jr_errors[0]
jr_error = self.get_alone_element(r for r in results if r.messages)
self.assertEqual(
[_('The participant «{}» cannot be found').format(unfoundable)],
jr_error.messages,
Expand Down Expand Up @@ -468,10 +462,7 @@ def test_import03(self):
results = self._get_job_results(job)
self.assertEqual(len(lines), len(results))

jr_errors = [r for r in results if r.messages]
self.assertEqual(1, len(jr_errors))

jr_error = jr_errors[0]
jr_error = self.get_alone_element(r for r in results if r.messages)
err_fmt = _('The participant «{}» cannot be found').format
self.assertListEqual(
[
Expand Down
19 changes: 6 additions & 13 deletions creme/assistants/tests/test_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,8 @@ def test_create_with_absolute_date(self):

self.assertEqual(title, str(alert))

jobs = queue.refreshed_jobs
self.assertEqual(1, len(jobs))
self.assertEqual(self.get_reminder_job(), jobs[0][0])
job, _data = self.get_alone_element(queue.refreshed_jobs)
self.assertEqual(self.get_reminder_job(), job)

def test_create_with_relative_datetime(self):
"DatetimeField + dynamic user."
Expand Down Expand Up @@ -926,20 +925,15 @@ def test_reminder1(self):
self.assertLess(job.type.next_wakeup(job, now_value), now())

self.execute_reminder_job(job)
reminders = DateReminder.objects.exclude(id__in=reminder_ids)
self.assertEqual(1, len(reminders))

reminder = reminders[0]
reminder = self.get_alone_element(DateReminder.objects.exclude(id__in=reminder_ids))
self.assertEqual(alert1, reminder.object_of_reminder)
self.assertEqual(1, reminder.ident)
self.assertDatetimesAlmostEqual(now_value, reminder.date_of_remind, seconds=60)
self.assertTrue(self.refresh(alert1).reminded)
self.assertFalse(self.refresh(alert2).reminded)

messages = mail.outbox
self.assertEqual(1, len(messages))

message = messages[0]
message = self.get_alone_element(mail.outbox)
self.assertEqual([user.email], message.to)

software = 'My CRM'
Expand Down Expand Up @@ -994,9 +988,8 @@ def test_reminder3(self):
self.execute_reminder_job(self.get_reminder_job())
self.assertEqual(1, DateReminder.objects.exclude(id__in=reminder_ids).count())

messages = mail.outbox
self.assertEqual(1, len(messages))
self.assertEqual([self.other_user.email], messages[0].to)
message = self.get_alone_element(mail.outbox)
self.assertEqual([self.other_user.email], message.to)

@override_settings(DEFAULT_TIME_ALERT_REMIND=30)
def test_next_wakeup1(self):
Expand Down
20 changes: 5 additions & 15 deletions creme/assistants/tests/test_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,20 +528,14 @@ def test_reminder01(self):
self.execute_reminder_job(job)
self.assertIsNone(job.user)

reminders = DateReminder.objects.all()
self.assertEqual(1, len(reminders))

reminder = reminders[0]
reminder = self.get_alone_element(DateReminder.objects.all())
self.assertEqual(todo1, reminder.object_of_reminder)
self.assertEqual(1, reminder.ident)
self.assertDatetimesAlmostEqual(now_value, reminder.date_of_remind, seconds=60)
self.assertTrue(self.refresh(todo1).reminded)
self.assertFalse(self.refresh(todo4).reminded)

messages = mail.outbox
self.assertEqual(1, len(messages))

message = messages[0]
message = self.get_alone_element(mail.outbox)
self.assertEqual([user.email], message.to)

software = 'My CRM'
Expand Down Expand Up @@ -621,10 +615,7 @@ def send_messages(this, messages):
self.assertTrue(send_messages_called)
self.assertEqual(1, DateReminder.objects.exclude(id__in=reminder_ids).count())

jresults = JobResult.objects.filter(job=job)
self.assertEqual(1, len(jresults))

jresult = jresults[0]
jresult = self.get_alone_element(JobResult.objects.filter(job=job))
self.assertListEqual(
[
_(
Expand Down Expand Up @@ -688,9 +679,8 @@ def test_reminder05(self):
self.execute_reminder_job(self.get_reminder_job())
self.assertEqual(1, DateReminder.objects.count())

messages = mail.outbox
self.assertEqual(1, len(messages))
self.assertEqual([self.other_user.email], messages[0].to)
message = self.get_alone_element(mail.outbox)
self.assertEqual([self.other_user.email], message.to)

def test_next_wakeup01(self):
"Next wake is one day later + minimum hour."
Expand Down
29 changes: 8 additions & 21 deletions creme/assistants/tests/test_user_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ def test_create01(self):
)
self._create_usermessage(title, body, priority, [user01], entity)

messages = UserMessage.objects.all()
self.assertEqual(1, len(messages))

message = messages[0]
message = self.get_alone_element(UserMessage.objects.all())
self.assertEqual(title, message.title)
self.assertEqual(body, message.body)
self.assertEqual(priority, message.priority)
Expand All @@ -112,9 +109,8 @@ def test_create01(self):

self.assertEqual(title, str(message))

jobs = queue.refreshed_jobs
self.assertEqual(1, len(jobs))
self.assertEqual(self._get_usermessages_job(), jobs[0][0])
job, _data = self.get_alone_element(queue.refreshed_jobs)
self.assertEqual(self._get_usermessages_job(), job)

@override_settings(SOFTWARE_LABEL='My CRM')
def test_create02(self):
Expand Down Expand Up @@ -183,10 +179,7 @@ def test_create03(self):

self._create_usermessage('TITLE', 'BODY', priority, [user01], None)

messages = UserMessage.objects.all()
self.assertEqual(1, len(messages))

message = messages[0]
message = self.get_alone_element(UserMessage.objects.all())
self.assertIsNone(message.entity_id)
self.assertIsNone(message.entity_content_type_id)
# self.assertIsNone(message.creme_entity)
Expand Down Expand Up @@ -372,9 +365,7 @@ def test_delete_priority01(self):
def test_delete_priority02(self):
priority = UserMessagePriority.objects.create(title='Important')
self._create_usermessage('TITLE', 'BODY', priority, [self.user], None)

messages = UserMessage.objects.all()
self.assertEqual(1, len(messages))
self.get_alone_element(UserMessage.objects.all())

response = self.assertPOST200(reverse(
'creme_config__delete_instance',
Expand Down Expand Up @@ -410,14 +401,10 @@ def send_messages(this, messages):

self.assertTrue(self.send_messages_called)

messages = UserMessage.objects.all()
self.assertEqual(1, len(messages))
self.assertTrue(messages[0].email_sent)

jresults = JobResult.objects.filter(job=job)
self.assertEqual(1, len(jresults))
message = self.get_alone_element(UserMessage.objects.all())
self.assertTrue(message.email_sent)

jresult = jresults[0]
jresult = self.get_alone_element(JobResult.objects.filter(job=job))
self.assertListEqual(
[
_('An error occurred while sending emails'),
Expand Down
6 changes: 3 additions & 3 deletions creme/billing/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,10 @@ def assertConvertButtons(self, response, expected):
else:
self.assertEqual('span', button_node.tag)
self.assertIn('forbidden', button_node.attrib.get('class').split())
labels = [*filter(None, (txt.strip() for txt in button_node.itertext()))]
self.assertEqual(1, len(labels))
found.append({
'label': labels[0],
'label': self.get_alone_element(
filter(None, (txt.strip() for txt in button_node.itertext()))
),
# 'json_data': json_data,
'disabled': True,
})
Expand Down
30 changes: 9 additions & 21 deletions creme/billing/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ def test_with_address(self):
self.assertFalse(Invoice.objects.count())
self._convert(200, quote, 'invoice')

invoices = Invoice.objects.all()
self.assertEqual(1, len(invoices))

invoice = invoices[0]
invoice = self.get_alone_element(Invoice.objects.all())
self.assertEqual(
_('{src} (converted into {dest._meta.verbose_name})').format(
src=quote.name, dest=Invoice,
Expand Down Expand Up @@ -206,9 +203,8 @@ def test_disabled_rtype(self):
rtype.enabled = True
rtype.save()

invoices = Invoice.objects.all()
self.assertEqual(1, len(invoices))
self.assertRelationCount(0, invoices[0], REL_SUB_INVOICE_FROM_QUOTE, quote)
invoice = self.get_alone_element(Invoice.objects.all())
self.assertRelationCount(0, invoice, REL_SUB_INVOICE_FROM_QUOTE, quote)

@skipIfCustomQuote
@skipIfCustomSalesOrder
Expand Down Expand Up @@ -239,10 +235,7 @@ def test_not_super_user(self):
self._convert(200, quote, 'sales_order')
self.assertEqual(0, Invoice.objects.count())

orders = SalesOrder.objects.all()
self.assertEqual(1, len(orders))

order = orders[0]
order = self.get_alone_element(SalesOrder.objects.all())
self.assertEqual('ORD1', order.number)
self.assertRelationCount(0, order, REL_SUB_INVOICE_FROM_QUOTE, quote)

Expand Down Expand Up @@ -381,10 +374,7 @@ def test_with_lines(self):

self._convert(200, quote, 'invoice')

invoices = Invoice.objects.all()
self.assertEqual(1, len(invoices))

invoice = invoices[0]
invoice = self.get_alone_element(Invoice.objects.all())
self.assertEqual(2, invoice.get_lines(ServiceLine).count())
self.assertEqual(2, invoice.get_lines(ProductLine).count())

Expand Down Expand Up @@ -433,9 +423,8 @@ def test_status01(self):

self._convert(200, quote, 'sales_order')

orders = SalesOrder.objects.all()
self.assertEqual(1, len(orders))
self.assertEqual(1, orders[0].status_id)
order = self.get_alone_element(SalesOrder.objects.all())
self.assertEqual(1, order.status_id)

@skipIfCustomQuote
@skipIfCustomInvoice
Expand All @@ -455,9 +444,8 @@ def test_status02(self):

self._convert(200, quote, 'invoice')

invoices = Invoice.objects.all()
self.assertEqual(1, len(invoices))
self.assertEqual(1, invoices[0].status_id)
invoice = self.get_alone_element(Invoice.objects.all())
self.assertEqual(1, invoice.status_id)

@skipIfCustomQuote
def test_not_copiable_relations(self):
Expand Down
Loading

0 comments on commit 5508072

Please sign in to comment.