def test_create_repo_with_recreate_index(self): """ Test that we can create a new repo and that the refresh_index command indexes appropriately. Note that this test has custom setUp and tearDown code. """ # When user installs LORE they must call refresh_index # or recreate_index to create the index. call_command("recreate_index") repo = create_repo("new repo", "new repo", self.user.id) search_url = "{api_base}repositories/{repo_slug}/search/".format( api_base=API_BASE, repo_slug=repo.slug) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) result = json.loads(resp.content.decode('utf-8')) self.assertEqual(0, result['count']) # Import. This should index the resources automatically. import_course_from_file(self.get_course_zip(), repo.id, self.user.id) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) result = json.loads(resp.content.decode('utf-8')) self.assertTrue(result['count'] > 0)
def test_invalid_file(self): """Invalid zip file""" with self.assertRaises(ValueError) as ex: import_course_from_file(self.bad_file, self.repo.id, self.user.id) self.assertTrue( 'Invalid OLX archive, unable to extract.' in ex.exception.args) self.assertFalse(os.path.exists(self.bad_file))
def test_create_repo_with_recreate_index(self): """ Test that we can create a new repo and that the refresh_index command indexes appropriately. Note that this test has custom setUp and tearDown code. """ # When user installs LORE they must call refresh_index # or recreate_index to create the index. call_command("recreate_index") repo = create_repo("new repo", "new repo", self.user.id) search_url = "{api_base}repositories/{repo_slug}/search/".format( api_base=API_BASE, repo_slug=repo.slug ) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) result = json.loads(resp.content.decode('utf-8')) self.assertEqual(0, result['count']) # Import. This should index the resources automatically. import_course_from_file(self.get_course_zip(), repo.id, self.user.id) resp = self.client.get(search_url) self.assertEqual(resp.status_code, HTTP_200_OK) result = json.loads(resp.content.decode('utf-8')) self.assertTrue(result['count'] > 0)
def test_parse_static(self): """ Parse the static assets in the sample course """ def get_counts(): """Returns counts of resources, videos, and assets.""" counts = namedtuple("counts", "resources videos assets") kwargs = {"course__course_number": "toy"} resources = LearningResource.objects.filter(**kwargs).count() assets = StaticAsset.objects.filter(**kwargs).count() kwargs["learning_resource_type__name"] = "video" videos = LearningResource.objects.filter(**kwargs).count() return counts(resources, videos, assets) # There should be nothing. counts = get_counts() self.assertEqual(counts.resources, 0) self.assertEqual(counts.videos, 0) self.assertEqual(counts.assets, 0) # Import the course. import_course_from_file( self.get_course_single_tarball(), self.repo.id, self.user.id ) # There should be something. counts = get_counts() self.assertEqual(counts.resources, self.toy_resource_count) self.assertEqual(counts.videos, 2) self.assertEqual(counts.assets, self.toy_asset_count) # There should be a single static asset. course = Course.objects.all().order_by("-id")[0] # latest course videos = LearningResource.objects.filter( learning_resource_type__name="video", course__id=course.id ) self.assertTrue(videos.count() == 2) num_assets = sum([ video.static_assets.count() for video in videos ]) # Only one video in the course has subtitles. self.assertTrue(num_assets == 1) # The course has an HTML block with two static assets; a CSS # file and an image. htmls = LearningResource.objects.filter( course__course_number='toy', learning_resource_type__name="html", static_assets__id__isnull=False, ).distinct() self.assertEqual(htmls.count(), 1) self.assertEqual( sorted([ asset.asset.name for asset in htmls[0].static_assets.all()]), sorted([ "assets/edX/toy/TT_2012_Fall/essays_x250.png", "assets/edX/toy/TT_2012_Fall/webGLDemo.css", ]) )
def test_incompatible_file(self): """incompatible zip file (missing course structure)""" with self.assertRaises(ValueError) as ex: import_course_from_file( self.incompatible, self.repo.id, self.user.id) self.assertTrue( 'Invalid OLX archive, no courses found.' in ex.exception.args)
def setUp(self): """ Set some test data """ super(TestResources, self).setUp() import_course_from_file( self.get_course_single_tarball(), self.repo.id, self.user.id)
def test_import_multiple(self): """ Simplest possible test. """ self.assertTrue(Course.objects.count() == 0) import_course_from_file( get_course_multiple_zip(), self.repo.id, self.user.id) self.assertTrue(Course.objects.count() == 2)
def test_import_single(self): """ Single course (course.xml in root of archive). """ self.assertTrue(Course.objects.count() == 0) import_course_from_file( get_course_single_tarball(), self.repo.id, self.user.id) self.assertTrue(Course.objects.count() == 1)
def test_invalid_file(self): """Invalid zip file""" try: import_course_from_file(self.bad_file, self.repo.id, self.user.id) raise ValueError("shouldn't happen") except ValueError as ex: self.assertTrue( 'Invalid OLX archive, unable to extract.' in ex.args)
def test_import_toy(self): """ Simplest possible test. """ self.assertTrue(LearningResource.objects.count() == 0) self.assertTrue(Course.objects.count() == 0) import_course_from_file(self.course_zip, self.repo.id, self.user.id) self.assertTrue(LearningResource.objects.count() == 5) self.assertTrue(Course.objects.count() == 1)
def test_incompatible_file(self): """incompatible zip file (missing course structure)""" try: import_course_from_file( self.incompatible, self.repo.id, self.user.id) raise ValueError("shouldn't happen") except ValueError as ex: self.assertTrue( 'Invalid OLX archive, no courses found.' in ex.args)
def test_import_multiple(self): """ Simplest possible test. """ original_count = Course.objects.count() import_course_from_file( self.get_course_multiple_zip(), self.repo.id, self.user.id) self.assertEqual( Course.objects.count(), original_count + 2, )
def test_import_single(self): """ Single course (course.xml in root of archive). """ original_count = Course.objects.count() tarball_file = self.get_course_single_tarball() import_course_from_file( tarball_file, self.repo.id, self.user.id) self.assertEqual( Course.objects.count(), original_count + 1, ) self.assertFalse(os.path.exists(tarball_file))
def test_import_toy(self): """ Simplest possible test. """ resource_count = get_resources(self.repo.id).count() course_count = Course.objects.count() import_course_from_file(self.course_zip, self.repo.id, self.user.id) self.assertEqual( get_resources(self.repo.id).count(), resource_count + self.toy_resource_count) self.assertEqual( Course.objects.count(), course_count + 1, )
def test_display_name_as_url_name(self): """ Test that we use display_name if url_name does not exist. """ path = os.path.join( os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), "learningresources", "tests", "testdata", "courses", "nested_problem" ) zip_path = self._make_archive(path, True) import_course_from_file(zip_path, self.repo.id, self.user.id) self.assertEqual( sorted([resource.url_name for resource in LearningResource.objects.filter( learning_resource_type__name="problem" ).all()]), sorted(["problem_1", "problem_2"]) )
def test_import(self): """ Indexing should occur on course import. """ results = search_index() original_count = results.count() import_course_from_file(self.course_zip, self.repo.id, self.user.id) refresh_index() results = search_index() self.assertEqual(results.count(), original_count + 18) self.assertEqual(results.page_count(), 2) # Ensure the correct number of results are returned in each page. self.assertEqual(len(results.get_page(1)), 10) self.assertEqual(len(results.get_page(2)), 9) self.assertEqual(len(results.get_page(3)), 0) # Make sure the .all() function (a convenience function which # returns a generator), returns all the records without explicit # pagination. count = 0 for _ in results.all(): count += 1 self.assertEqual(count, results.count())
def save(self, user_id, repo_id): """ Receives the request.FILES from the view. Args: user_id (int): primary key of the user uploading the course. repo_id (int): primary key of repository we're uploading to """ # Assumes a single file, because we only accept # one at a time. uploaded_file = self.cleaned_data["course_file"] # Save the uploaded file into a temp file. handle, filename = mkstemp(suffix=uploaded_file.ext) os.close(handle) with open(filename, 'wb') as temp: for chunk in uploaded_file: temp.write(chunk) log.debug("UploadForm cleaned_data: %s", self.cleaned_data) import_course_from_file( filename, repo_id, user_id )