Commit 26d1669f by Ben Patterson

Merge pull request #7103 from edx/benp/refactor-events-tests

Event-testing pattern should be made available in a Mixin.
parents ee0ae6ee 3539bac1
......@@ -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"
......
......@@ -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.
......
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment