示例#1
0
    def test_refresh(self):
        """ Verify the method refreshes data for a single course. """
        course_id = 'SesameStreetX/Cookies/1T2016'
        name = 'C is for Cookie'
        body = {
            'id': course_id,
            'name': name
        }

        # Mock the call to the E-Commerce API
        url = '{host}/courses/{course_id}/'.format(host=ECOMMERCE_API_URL, course_id=course_id)
        responses.add(responses.GET, url, body=json.dumps(body), content_type=JSON)  # pylint: disable=no-member

        # Refresh the course, and ensure the attributes are correct.
        course = Course.refresh(course_id, ACCESS_TOKEN)
        attrs = {
            'id': course_id,
            'body': body,
            'name': name,
        }
        self.assert_course_attrs(course, attrs)

        # Ensure the data is persisted to the data store
        course = Course.get(course_id)
        self.assert_course_attrs(course, attrs)
示例#2
0
    def test_refresh(self):
        """ Verify the method refreshes data for a single course. """
        course_id = 'SesameStreetX/Cookies/1T2016'
        name = 'C is for Cookie'
        body = {'id': course_id, 'name': name}

        # Mock the call to the E-Commerce API
        url = '{host}/courses/{course_id}/'.format(host=ECOMMERCE_API_URL,
                                                   course_id=course_id)
        responses.add(responses.GET,
                      url,
                      body=json.dumps(body),
                      content_type=JSON)  # pylint: disable=no-member

        # Refresh the course, and ensure the attributes are correct.
        course = Course.refresh(course_id, ACCESS_TOKEN)
        attrs = {
            'id': course_id,
            'body': body,
            'name': name,
        }
        self.assert_course_attrs(course, attrs)

        # Ensure the data is persisted to the data store
        course = Course.get(course_id)
        self.assert_course_attrs(course, attrs)
示例#3
0
 def test_get_with_missing_course(self):
     """
     Verify the method raises a CourseNotFoundError if the specified course does not exist in the data store.
     """
     # Note (CCB): This consistently fails on Travis with the error below. Trying index refresh as a last-ditch
     # effort to resolve.
     #
     # elasticsearch.exceptions.TransportError: TransportError(503,
     # 'NoShardAvailableActionException[[course_discovery_test][1] null]; nested:
     # IllegalIndexShardStateException[[course_discovery_test][1] CurrentState[POST_RECOVERY] operations only
     # allowed when started/relocated]; ')
     #
     self.refresh_index()
     course_id = 'fake.course'
     expected_msg_regexp = r'Course \[{}\] was not found in the data store.'.format(course_id)
     with self.assertRaisesRegex(CourseNotFoundError, expected_msg_regexp):
         Course.get(course_id)
示例#4
0
 def test_get_with_missing_course(self):
     """
     Verify the method raises a CourseNotFoundError if the specified course does not exist in the data store.
     """
     # Note (CCB): This consistently fails on Travis with the error below. Trying index refresh as a last-ditch
     # effort to resolve.
     #
     # elasticsearch.exceptions.TransportError: TransportError(503,
     # 'NoShardAvailableActionException[[course_discovery_test][1] null]; nested:
     # IllegalIndexShardStateException[[course_discovery_test][1] CurrentState[POST_RECOVERY] operations only
     # allowed when started/relocated]; ')
     #
     self.refresh_index()
     course_id = 'fake.course'
     expected_msg_regexp = r'Course \[{}\] was not found in the data store.'.format(
         course_id)
     with self.assertRaisesRegex(CourseNotFoundError, expected_msg_regexp):
         Course.get(course_id)
示例#5
0
    def test_refresh_all(self):
        """ Verify the method refreshes data for all courses. """
        course_bodies = self.mock_refresh_all()
        self.refresh_index()

        # Ensure the data is persisted to the data store
        for body in course_bodies:
            course_id = body['id']
            attrs = {
                'id': course_id,
                'body': body,
                'name': body['name'],
            }
            course = Course.get(course_id)
            self.assert_course_attrs(course, attrs)
示例#6
0
    def test_refresh_all(self):
        """ Verify the method refreshes data for all courses. """
        course_bodies = self.mock_refresh_all()
        self.refresh_index()

        # Ensure the data is persisted to the data store
        for body in course_bodies:
            course_id = body['id']
            attrs = {
                'id': course_id,
                'body': body,
                'name': body['name'],
            }
            course = Course.get(course_id)
            self.assert_course_attrs(course, attrs)
示例#7
0
    def test_save(self):
        """ Verify the method creates and/or updates new courses. """
        course_id = 'TestX/Saving/4T2015'
        body = {
            'id': course_id,
            'name': 'Save Me!'
        }

        self.assertFalse(self.es.exists(index=self.index, doc_type=Course.doc_type, id=course_id))
        Course(course_id, body).save()
        self.refresh_index()

        self.assertTrue(self.es.exists(index=self.index, doc_type=Course.doc_type, id=course_id))
        course = Course.get(course_id)
        self.assertEqual(course.id, course_id)
        self.assertEqual(course.body, body)
示例#8
0
    def test_all(self):
        """ Verify the method returns a list of all courses. """
        course_bodies = self.mock_refresh_all()

        courses = []
        for body in course_bodies:
            courses.append(Course.get(body['id']))

        expected = {
            'limit': 10,
            'offset': 0,
            'total': 2,
            'results': courses,
        }

        self.assertDictEqual(Course.all(), expected)
示例#9
0
    def test_all(self):
        """ Verify the method returns a list of all courses. """
        course_bodies = self.mock_refresh_all()

        courses = []
        for body in course_bodies:
            courses.append(Course.get(body['id']))

        expected = {
            'limit': 10,
            'offset': 0,
            'total': 2,
            'results': courses,
        }

        self.assertDictEqual(Course.all(), expected)
示例#10
0
    def test_save(self):
        """ Verify the method creates and/or updates new courses. """
        course_id = 'TestX/Saving/4T2015'
        body = {'id': course_id, 'name': 'Save Me!'}

        self.assertFalse(
            self.es.exists(index=self.index,
                           doc_type=Course.doc_type,
                           id=course_id))
        Course(course_id, body).save()
        self.refresh_index()

        self.assertTrue(
            self.es.exists(index=self.index,
                           doc_type=Course.doc_type,
                           id=course_id))
        course = Course.get(course_id)
        self.assertEqual(course.id, course_id)
        self.assertEqual(course.body, body)
示例#11
0
 def test_get(self):
     """ Verify the method returns a single course. """
     course = CourseFactory()
     retrieved = Course.get(course.id)
     self.assertEqual(course, retrieved)
示例#12
0
 def get_object(self):
     """ Return a single course. """
     return Course.get(self.kwargs[self.lookup_url_kwarg or self.lookup_field])
示例#13
0
 def test_get(self):
     """ Verify the method returns a single course. """
     course = CourseFactory()
     retrieved = Course.get(course.id)
     self.assertEqual(course, retrieved)
示例#14
0
 def get_object(self):
     """ Return a single course. """
     return Course.get(self.kwargs[self.lookup_url_kwarg
                                   or self.lookup_field])