From 3539bac16026e412ad42e0ce460f904dbe22ec3b Mon Sep 17 00:00:00 2001 From: Ben Patterson Date: Wed, 25 Feb 2015 09:06:35 -0500 Subject: [PATCH] Event-testing pattern should be supported by a mixin. --- .../discussion/test_cohort_management.py | 8 +-- common/test/acceptance/tests/helpers.py | 49 ++++++++++++------- common/test/acceptance/tests/lms/test_lms.py | 25 +++------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/common/test/acceptance/tests/discussion/test_cohort_management.py b/common/test/acceptance/tests/discussion/test_cohort_management.py index 2a99c78c20..686e1d8c17 100644 --- a/common/test/acceptance/tests/discussion/test_cohort_management.py +++ b/common/test/acceptance/tests/discussion/test_cohort_management.py @@ -5,13 +5,11 @@ End-to-end tests related to the cohort management on the LMS Instructor Dashboar from datetime import datetime -from pymongo import MongoClient - from pytz import UTC, utc from bok_choy.promise import EmptyPromise from nose.plugins.attrib import attr from .helpers import CohortTestMixin -from ..helpers import UniqueCourseTest, create_user_partition_json +from ..helpers import UniqueCourseTest, EventsTestMixin, create_user_partition_json from xmodule.partitions.partitions import Group from ...fixtures.course import CourseFixture from ...pages.lms.auto_auth import AutoAuthPage @@ -23,7 +21,7 @@ import uuid @attr('shard_3') -class CohortConfigurationTest(UniqueCourseTest, CohortTestMixin): +class CohortConfigurationTest(EventsTestMixin, UniqueCourseTest, CohortTestMixin): """ Tests for cohort management on the LMS Instructor Dashboard """ @@ -34,8 +32,6 @@ class CohortConfigurationTest(UniqueCourseTest, CohortTestMixin): """ super(CohortConfigurationTest, self).setUp() - self.event_collection = MongoClient()["test"]["events"] - # create course with cohorts self.manual_cohort_name = "ManualCohort1" self.auto_cohort_name = "AutoCohort1" diff --git a/common/test/acceptance/tests/helpers.py b/common/test/acceptance/tests/helpers.py index 45ae0ec215..ffb726a475 100644 --- a/common/test/acceptance/tests/helpers.py +++ b/common/test/acceptance/tests/helpers.py @@ -6,10 +6,12 @@ import unittest import functools import requests import os +from datetime import datetime from path import path from bok_choy.javascript import js_defined from bok_choy.web_app_test import WebAppTest from opaque_keys.edx.locator import CourseLocator +from pymongo import MongoClient from xmodule.partitions.partitions import UserPartition from xmodule.partitions.tests.test_partitions import MockUserPartitionScheme from selenium.webdriver.support.select import Select @@ -236,24 +238,6 @@ def element_has_text(page, css_selector, text): return text_present -def assert_event_emitted_num_times(event_collection, event_name, event_time, event_user_id, num_times_emitted): - """ - Tests the number of times a particular event was emitted. - :param event_collection: MongoClient instance to query. - :param event_name: Expected event name (e.g., "edx.course.enrollment.activated") - :param event_time: Latest expected time, after which the event would fire (e.g., the beginning of the test case) - """ - assert( - event_collection.find( - { - "name": event_name, - "time": {"$gt": event_time}, - "event.user_id": int(event_user_id), - } - ).count() == num_times_emitted - ) - - def get_modal_alert(browser): """ Returns instance of modal alert box shown in browser after waiting @@ -263,6 +247,35 @@ def get_modal_alert(browser): return browser.switch_to.alert +class EventsTestMixin(object): + """ + Helpers and setup for running tests that evaluate events emitted + """ + def setUp(self): + super(EventsTestMixin, self).setUp() + self.event_collection = MongoClient()["test"]["events"] + self.event_collection.drop() + self.start_time = datetime.now() + + def assert_event_emitted_num_times(self, event_name, event_time, event_user_id, num_times_emitted): + """ + Tests the number of times a particular event was emitted. + :param event_name: Expected event name (e.g., "edx.course.enrollment.activated") + :param event_time: Latest expected time, after which the event would fire (e.g., the beginning of the test case) + :param event_user_id: user_id expected in the event + :param num_times_emitted: number of times the event is expected to appear since the event_time + """ + self.assertEqual( + self.event_collection.find( + { + "name": event_name, + "time": {"$gt": event_time}, + "event.user_id": int(event_user_id), + } + ).count(), num_times_emitted + ) + + class UniqueCourseTest(WebAppTest): """ Test that provides a unique course ID. diff --git a/common/test/acceptance/tests/lms/test_lms.py b/common/test/acceptance/tests/lms/test_lms.py index eae9948bf3..567efc79ed 100644 --- a/common/test/acceptance/tests/lms/test_lms.py +++ b/common/test/acceptance/tests/lms/test_lms.py @@ -3,21 +3,19 @@ End-to-end tests for the LMS. """ -from datetime import datetime from textwrap import dedent from unittest import skip from nose.plugins.attrib import attr -from pymongo import MongoClient from bok_choy.promise import EmptyPromise from bok_choy.web_app_test import WebAppTest from ..helpers import ( UniqueCourseTest, + EventsTestMixin, load_data_str, generate_course_key, select_option_by_value, - element_has_text, - assert_event_emitted_num_times + element_has_text ) from ...pages.lms.auto_auth import AutoAuthPage from ...pages.lms.create_mode import ModeCreationPage @@ -201,7 +199,7 @@ class RegisterFromCombinedPageTest(UniqueCourseTest): @attr('shard_1') -class PayAndVerifyTest(UniqueCourseTest): +class PayAndVerifyTest(EventsTestMixin, UniqueCourseTest): """Test that we can proceed through the payment and verification flow.""" def setUp(self): """Initialize the test. @@ -217,8 +215,6 @@ class PayAndVerifyTest(UniqueCourseTest): self.upgrade_page = PaymentAndVerificationFlow(self.browser, self.course_id, entry_point='upgrade') self.fake_payment_page = FakePaymentPage(self.browser, self.course_id) self.dashboard_page = DashboardPage(self.browser) - self.event_collection = MongoClient()["test"]["events"] - self.start_time = datetime.now() # Create a course CourseFixture( @@ -252,8 +248,7 @@ class PayAndVerifyTest(UniqueCourseTest): self.fake_payment_page.submit_payment() # Expect enrollment activated event - assert_event_emitted_num_times( - self.event_collection, + self.assert_event_emitted_num_times( "edx.course.enrollment.activated", self.start_time, student_id, @@ -261,8 +256,7 @@ class PayAndVerifyTest(UniqueCourseTest): ) # Expect that one mode_changed enrollment event fired as part of the upgrade - assert_event_emitted_num_times( - self.event_collection, + self.assert_event_emitted_num_times( "edx.course.enrollment.mode_changed", self.start_time, student_id, @@ -307,8 +301,7 @@ class PayAndVerifyTest(UniqueCourseTest): self.fake_payment_page.submit_payment() # Expect enrollment activated event - assert_event_emitted_num_times( - self.event_collection, + self.assert_event_emitted_num_times( "edx.course.enrollment.activated", self.start_time, student_id, @@ -346,8 +339,7 @@ class PayAndVerifyTest(UniqueCourseTest): self.fake_payment_page.submit_payment() # Expect that one mode_changed enrollment event fired as part of the upgrade - assert_event_emitted_num_times( - self.event_collection, + self.assert_event_emitted_num_times( "edx.course.enrollment.mode_changed", self.start_time, student_id, @@ -355,8 +347,7 @@ class PayAndVerifyTest(UniqueCourseTest): ) # Expect no enrollment activated event - assert_event_emitted_num_times( - self.event_collection, + self.assert_event_emitted_num_times( "edx.course.enrollment.activated", self.start_time, student_id,