How to use create_activity method in localstack

Best Python code snippet using localstack_python

test_models.py

Source:test_models.py Github

copy

Full Screen

...15 def setUp(self):16 mailing_list = MailingListFactory()17 self.email = EmailFactory()18 self.subscriber_1 = SubscriberFactory(mailing_list=mailing_list)19 self.subscriber_1.create_activity(ActivityTypes.SENT, email=self.email) # mock email sent activity20 self.subscriber_2 = SubscriberFactory(mailing_list=mailing_list)21 self.subscriber_2.create_activity(ActivityTypes.SENT, email=self.email) # mock email sent activity22 def test_open_rate_updated(self):23 self.assertEqual(0.0, self.subscriber_1.open_rate)24 self.subscriber_1.open(self.email)25 self.subscriber_1.refresh_from_db()26 self.subscriber_1.mailing_list.refresh_from_db()27 self.assertEqual(1.0, self.subscriber_1.open_rate)28 # two subscribers, one with open_rate = 1.0 other with open_rate = 0.0, expected mailing list open_rate = 0.529 self.assertEqual(0.5, self.subscriber_1.mailing_list.open_rate)30 def test_open_email_once(self):31 self.subscriber_1.open(self.email)32 self.email.refresh_from_db()33 self.email.campaign.refresh_from_db()34 self.assertEqual(1, self.email.campaign.unique_opens_count)35 self.assertEqual(1, self.email.campaign.total_opens_count)36 self.assertEqual(1, self.email.unique_opens_count)37 self.assertEqual(1, self.email.total_opens_count)38 self.assertEqual(1, self.subscriber_1.activities.filter(activity_type=ActivityTypes.OPENED).count())39 def test_open_email_twice(self):40 self.subscriber_1.open(self.email)41 self.subscriber_1.open(self.email)42 self.email.refresh_from_db()43 self.email.campaign.refresh_from_db()44 self.assertEqual(1, self.email.campaign.unique_opens_count)45 self.assertEqual(2, self.email.campaign.total_opens_count)46 self.assertEqual(1, self.email.unique_opens_count)47 self.assertEqual(2, self.email.total_opens_count)48 self.assertEqual(2, self.subscriber_1.activities.filter(activity_type=ActivityTypes.OPENED).count())49 def test_two_subscribers_open_email_once(self):50 self.subscriber_1.open(self.email)51 self.subscriber_2.open(self.email)52 self.email.refresh_from_db()53 self.email.campaign.refresh_from_db()54 self.assertEqual(2, self.email.campaign.unique_opens_count)55 self.assertEqual(2, self.email.campaign.total_opens_count)56 self.assertEqual(2, self.email.unique_opens_count)57 self.assertEqual(2, self.email.total_opens_count)58 self.assertEqual(1, self.subscriber_1.activities.filter(activity_type=ActivityTypes.OPENED).count())59 self.assertEqual(1, self.subscriber_2.activities.filter(activity_type=ActivityTypes.OPENED).count())60@override_settings(CELERY_TASK_ALWAYS_EAGER=True)61class SubscriberClickLinkTests(TestCase):62 def setUp(self):63 mailing_list = MailingListFactory()64 self.link = LinkFactory()65 self.subscriber_1 = SubscriberFactory(mailing_list=mailing_list)66 self.subscriber_1.create_activity(ActivityTypes.SENT, email=self.link.email) # mock email sent activity67 self.subscriber_2 = SubscriberFactory(mailing_list=mailing_list)68 self.subscriber_2.create_activity(ActivityTypes.SENT, email=self.link.email) # mock email sent activity69 def test_click_rate_update(self):70 self.assertEqual(0.0, self.subscriber_1.click_rate)71 self.subscriber_1.click(self.link)72 self.subscriber_1.refresh_from_db()73 self.subscriber_1.mailing_list.refresh_from_db()74 self.assertEqual(1.0, self.subscriber_1.click_rate)75 # two subscribers, one with click_rate = 1.0 other with click_rate = 0.0 expected mailing list click_rate = 0.576 self.assertEqual(0.5, self.subscriber_1.mailing_list.click_rate)77 def test_click_link_once(self):78 self.subscriber_1.click(self.link)79 self.link.refresh_from_db()80 self.link.email.campaign.refresh_from_db()81 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)82 self.assertEqual(1, self.link.email.campaign.total_clicks_count)83 self.assertEqual(1, self.link.unique_clicks_count)84 self.assertEqual(1, self.link.total_clicks_count)85 self.assertEqual(1, self.subscriber_1.activities.filter(activity_type=ActivityTypes.CLICKED).count())86 def test_click_link_twice(self):87 self.subscriber_1.click(self.link)88 self.subscriber_1.click(self.link)89 self.link.refresh_from_db()90 self.link.email.campaign.refresh_from_db()91 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)92 self.assertEqual(2, self.link.email.campaign.total_clicks_count)93 self.assertEqual(1, self.link.unique_clicks_count)94 self.assertEqual(2, self.link.total_clicks_count)95 self.assertEqual(2, self.subscriber_1.activities.filter(activity_type=ActivityTypes.CLICKED).count())96 def test_two_subscribers_click_link_once(self):97 self.subscriber_1.click(self.link)98 self.subscriber_2.click(self.link)99 self.link.refresh_from_db()100 self.link.email.campaign.refresh_from_db()101 self.assertEqual(2, self.link.email.campaign.unique_clicks_count)102 self.assertEqual(2, self.link.email.campaign.total_clicks_count)103 self.assertEqual(2, self.link.unique_clicks_count)104 self.assertEqual(2, self.link.total_clicks_count)105 self.assertEqual(1, self.subscriber_1.activities.filter(activity_type=ActivityTypes.CLICKED).count())106 self.assertEqual(1, self.subscriber_2.activities.filter(activity_type=ActivityTypes.CLICKED).count())107 def test_click_two_links_same_email(self):108 link_2 = LinkFactory(email=self.link.email)109 self.subscriber_1.click(self.link)110 self.subscriber_1.click(link_2)111 self.link.refresh_from_db()112 link_2.refresh_from_db()113 self.link.email.campaign.refresh_from_db()114 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)115 self.assertEqual(2, self.link.email.campaign.total_clicks_count)116 self.assertEqual(1, self.link.unique_clicks_count)117 self.assertEqual(1, self.link.total_clicks_count)118 self.assertEqual(1, link_2.unique_clicks_count)119 self.assertEqual(1, link_2.total_clicks_count)120 self.assertEqual(2, self.subscriber_1.activities.filter(activity_type=ActivityTypes.CLICKED).count())121@override_settings(CELERY_TASK_ALWAYS_EAGER=True)122class SubscriberClickLinkForceOpenTests(TestCase):123 def setUp(self):124 mailing_list = MailingListFactory()125 self.link = LinkFactory()126 self.subscriber = SubscriberFactory(mailing_list=mailing_list)127 self.subscriber.create_activity(ActivityTypes.SENT, email=self.link.email) # mock email sent activity128 def test_click_without_open(self):129 """130 Test clicking on a link without opening the email first131 The `click` method should enforce the email opening132 """133 self.subscriber.click(self.link)134 # refresh models135 self.link.refresh_from_db()136 self.link.email.refresh_from_db()137 self.link.email.campaign.refresh_from_db()138 # checks for click counts139 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)140 self.assertEqual(1, self.link.email.campaign.total_clicks_count)141 self.assertEqual(1, self.link.unique_clicks_count)142 self.assertEqual(1, self.link.total_clicks_count)143 self.assertEqual(1, self.subscriber.activities.filter(activity_type=ActivityTypes.CLICKED).count())144 # checks for open counts145 self.assertEqual(1, self.link.email.campaign.unique_opens_count)146 self.assertEqual(1, self.link.email.campaign.total_opens_count)147 self.assertEqual(1, self.link.email.unique_opens_count)148 self.assertEqual(1, self.link.email.total_opens_count)149 self.assertEqual(1, self.subscriber.activities.filter(activity_type=ActivityTypes.OPENED).count())150 def test_click_twice_without_open(self):151 """152 Test clicking on a link twice without opening the email first153 Only the first `click` method should trigger the email opening154 """155 self.subscriber.click(self.link) # trigger `open` method156 self.subscriber.click(self.link) # this time it should not trigger the `open` method157 # refresh models158 self.link.refresh_from_db()159 self.link.email.refresh_from_db()160 self.link.email.campaign.refresh_from_db()161 # checks for click counts162 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)163 self.assertEqual(2, self.link.email.campaign.total_clicks_count)164 self.assertEqual(1, self.link.unique_clicks_count)165 self.assertEqual(2, self.link.total_clicks_count)166 self.assertEqual(2, self.subscriber.activities.filter(activity_type=ActivityTypes.CLICKED).count())167 # checks for open counts168 self.assertEqual(1, self.link.email.campaign.unique_opens_count)169 self.assertEqual(1, self.link.email.campaign.total_opens_count)170 self.assertEqual(1, self.link.email.unique_opens_count)171 self.assertEqual(1, self.link.email.total_opens_count)172 self.assertEqual(1, self.subscriber.activities.filter(activity_type=ActivityTypes.OPENED).count())173 def test_open_once_click_twice(self):174 """175 Test opening email and clicking on a link twice176 """177 self.subscriber.click(self.link)178 self.subscriber.open(self.link.email)179 self.subscriber.click(self.link)180 # refresh models181 self.link.refresh_from_db()182 self.link.email.refresh_from_db()183 self.link.email.campaign.refresh_from_db()184 # checks for click counts185 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)186 self.assertEqual(2, self.link.email.campaign.total_clicks_count)187 self.assertEqual(1, self.link.unique_clicks_count)188 self.assertEqual(2, self.link.total_clicks_count)189 self.assertEqual(2, self.subscriber.activities.filter(activity_type=ActivityTypes.CLICKED).count())190 # checks for open counts191 self.assertEqual(1, self.link.email.campaign.unique_opens_count)192 self.assertEqual(2, self.link.email.campaign.total_opens_count)193 self.assertEqual(1, self.link.email.unique_opens_count)194 self.assertEqual(2, self.link.email.total_opens_count)195 self.assertEqual(2, self.subscriber.activities.filter(activity_type=ActivityTypes.OPENED).count())196 def test_open_twice_click_twice(self):197 """198 Test opening email and clicking on a link twice199 """200 self.subscriber.open(self.link.email)201 self.subscriber.click(self.link)202 self.subscriber.open(self.link.email)203 self.subscriber.click(self.link)204 # refresh models205 self.link.refresh_from_db()206 self.link.email.refresh_from_db()207 self.link.email.campaign.refresh_from_db()208 # checks for click counts209 self.assertEqual(1, self.link.email.campaign.unique_clicks_count)210 self.assertEqual(2, self.link.email.campaign.total_clicks_count)211 self.assertEqual(1, self.link.unique_clicks_count)212 self.assertEqual(2, self.link.total_clicks_count)213 self.assertEqual(2, self.subscriber.activities.filter(activity_type=ActivityTypes.CLICKED).count())214 # checks for open counts215 self.assertEqual(1, self.link.email.campaign.unique_opens_count)216 self.assertEqual(2, self.link.email.campaign.total_opens_count)217 self.assertEqual(1, self.link.email.unique_opens_count)218 self.assertEqual(2, self.link.email.total_opens_count)219 self.assertEqual(2, self.subscriber.activities.filter(activity_type=ActivityTypes.OPENED).count())220class SubscriberUpdateOpenRateTests(TestCase):221 def setUp(self):222 self.subscriber = SubscriberFactory()223 self.email = EmailFactory()224 def test_open_rate_persistence(self):225 self.assertEqual(0.0, Subscriber.objects.get(pk=self.subscriber.pk).open_rate)226 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)227 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)228 self.subscriber.update_open_rate()229 self.assertEqual(1.0, Subscriber.objects.get(pk=self.subscriber.pk).open_rate)230 def test_division_by_zero(self):231 """232 Test if the the code is handling division by zero.233 There should never be an OPENED activity without a SENT activity (thus not being possible to have a234 division by zero). But just in case.235 """236 self.assertEqual(0.0, self.subscriber.update_open_rate())237 def test_update_open_rate_distinct_count(self):238 """239 Test if the update count is only considering distinct open entries240 """241 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)242 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)243 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)244 self.assertEqual(1.0, self.subscriber.update_open_rate())245 def test_open_without_sent(self):246 """247 Test open count without sent activity248 This should not happen under normal circumstances249 """250 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)251 self.assertEqual(0.0, self.subscriber.update_open_rate())252 def test_sent_without_open(self):253 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)254 self.assertEqual(0.0, self.subscriber.update_open_rate())255 def test_update_open_rate_50_percent(self):256 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())257 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)258 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)259 self.assertEqual(0.5, self.subscriber.update_open_rate())260 def test_round_percentage(self):261 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())262 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())263 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)264 self.subscriber.create_activity(ActivityTypes.OPENED, email=self.email)265 self.assertEqual(0.3333, self.subscriber.update_open_rate())266class SubscriberUpdateClickRateTests(TestCase):267 def setUp(self):268 self.subscriber = SubscriberFactory()269 self.email = EmailFactory()270 self.link = LinkFactory(email=self.email)271 def test_click_rate_persistence(self):272 self.assertEqual(0.0, Subscriber.objects.get(pk=self.subscriber.pk).click_rate)273 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)274 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)275 self.subscriber.update_click_rate()276 self.assertEqual(1.0, Subscriber.objects.get(pk=self.subscriber.pk).click_rate)277 def test_division_by_zero(self):278 """279 Test if the the code is handling division by zero.280 There should never be an OPENED activity without a SENT activity (thus not being possible to have a281 division by zero). But just in case.282 """283 self.assertEqual(0.0, self.subscriber.update_click_rate())284 def test_update_click_rate_distinct_count(self):285 """286 Test if the update count is only considering distinct open entries287 """288 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)289 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)290 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)291 self.assertEqual(1.0, self.subscriber.update_click_rate())292 def test_open_without_sent(self):293 """294 Test open count without sent activity295 This should not happen under normal circumstances296 """297 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)298 self.assertEqual(0.0, self.subscriber.update_click_rate())299 def test_sent_without_open(self):300 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)301 self.assertEqual(0.0, self.subscriber.update_click_rate())302 def test_update_click_rate_50_percent(self):303 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())304 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)305 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)306 self.assertEqual(0.5, self.subscriber.update_click_rate())307 def test_round_percentage(self):308 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())309 self.subscriber.create_activity(ActivityTypes.SENT, email=EmailFactory())310 self.subscriber.create_activity(ActivityTypes.SENT, email=self.email)311 self.subscriber.create_activity(ActivityTypes.CLICKED, email=self.email, link=self.link)312 self.assertEqual(0.3333, self.subscriber.update_click_rate())313class SubscriptionFormTemplateTests(TestCase):314 def setUp(self):315 super().setUp()316 mailing_list = MailingListFactory(name='Test List')317 self.form_template = SubscriptionFormTemplateFactory(key=TemplateKeys.SUBSCRIBE_FORM,318 mailing_list=mailing_list)319 def test_str(self):320 self.assertEqual('Subscribe form', str(self.form_template))321 def test_settings(self):322 settings = SUBSCRIPTION_FORM_TEMPLATE_SETTINGS['subscribe']323 self.assertEqual(self.form_template.settings, settings)324 def test_is_email(self):325 self.assertFalse(self.form_template.is_email)...

Full Screen

Full Screen

test_statistics.py

Source:test_statistics.py Github

copy

Full Screen

...27 user = self.login()28 now_value = now()29 create_activity = partial(Activity.objects.create, user=user, type_id=ACTIVITYTYPE_MEETING)30 for i in range(1, 13):31 create_activity(title='Meeting #{}'.format(i), start=now_value - relativedelta(months=i))32 # Should not be counted (too old)33 for i in range(1, 4):34 create_activity(title='Task', start=now_value - relativedelta(years=1, months=1))35 self.assertEqual(36 [ngettext('{count} meeting per month',37 '{count} meetings per month',38 139 ).format(count=number_format(1, decimal_pos=1, use_l10n=True),40 ),41 _('No phone call since one year'),42 ],43 AveragePerMonthStatistics(Activity)()44 )45 @skipIfCustomActivity46 def test_average_per_month03(self):47 "1.5 meeting per month"48 user = self.login()49 now_value = now()50 create_activity = partial(Activity.objects.create, user=user, type_id=ACTIVITYTYPE_MEETING)51 for i in range(1, 13):52 create_activity(title='Meeting #A-{}'.format(i),53 start=now_value - relativedelta(months=i),54 )55 for i in range(1, 7):56 create_activity(title='Meeting #B-{}'.format(i),57 start=now_value - relativedelta(months=i),58 )59 # Should not be counted (not meeting)60 for i in range(1, 4):61 create_activity(title='Task #{}'.format(i),62 start=now_value - relativedelta(months=i),63 type_id=ACTIVITYTYPE_TASK,64 )65 self.assertEqual(66 [ngettext('{count} meeting per month',67 '{count} meetings per month',68 1.569 ).format(count=number_format(1.5, decimal_pos=1, use_l10n=True),70 ),71 _('No phone call since one year'),72 ],73 AveragePerMonthStatistics(Activity)()74 )75 @skipIfCustomActivity76 def test_average_per_month04(self):77 "0.5 phone call per month"78 user = self.login()79 now_value = now()80 create_activity = partial(Activity.objects.create, user=user, type_id=ACTIVITYTYPE_PHONECALL)81 create_activity(title='Task', start=now_value - relativedelta(months=1),82 type_id=ACTIVITYTYPE_TASK,83 ) # Should not be counted84 for i in range(1, 7):85 create_activity(title='Phone call#{}'.format(i),86 start=now_value - relativedelta(months=i),87 )88 self.assertEqual(89 [_('No meeting since one year'),90 ngettext('{count} phone call per month',91 '{count} phone calls per month',92 0.593 ).format(count=number_format(0.5, decimal_pos=1, use_l10n=True),94 ),95 ],96 AveragePerMonthStatistics(Activity)()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful