def test_upsert_content_file(mocker):
    """
    Tests that upsert_content_file calls the correct celery task with parameters
    """
    patched_task = mocker.patch("search.tasks.upsert_content_file")
    content_file = ContentFileFactory.create()
    upsert_content_file(content_file.id)
    patched_task.delay.assert_called_once_with(content_file.id)
def test_index_run_content_files(mocker):
    """
    Tests that index_run_content_files calls the correct celery task w/parameter
    """
    patched_task = mocker.patch("search.tasks.index_run_content_files")
    content_file = ContentFileFactory.create()
    index_run_content_files(content_file.id)
    patched_task.delay.assert_called_once_with(content_file.id)
Exemple #3
0
def test_serialize_content_file_for_bulk_deletion():
    """
    Test that serialize_content_file_for_bulk_deletion yields a valid ESContentFileSerializer
    """
    content_file = ContentFileFactory.create()
    assert serialize_content_file_for_bulk_deletion(content_file) == {
        "_id": gen_content_file_id(content_file.key),
        "_op_type": "delete",
    }
Exemple #4
0
def test_serialize_content_file_for_bulk():
    """
    Test that serialize_content_file_for_bulk yields a valid ESContentFileSerializer
    """
    content_file = ContentFileFactory.create()
    assert serialize_content_file_for_bulk(content_file) == {
        "_id": gen_content_file_id(content_file.key),
        **ESContentFileSerializer(content_file).data,
    }
def test_delete_content_file(mocker):
    """Tests that deleting a content_file triggers the correct ES delete task"""
    patched_delete_task = mocker.patch("search.task_helpers.delete_document")
    content_file = ContentFileFactory.create()
    delete_content_file(content_file)
    assert patched_delete_task.delay.called is True
    assert patched_delete_task.delay.call_args[0] == (
        gen_content_file_id(content_file.key),
        COURSE_TYPE,
    )
def test_delete_course(mocker):
    """
    Tests that delete_course calls the delete tasks for the course and its content files
    """
    patched_delete_task = mocker.patch("search.task_helpers.delete_document")
    course = CourseFactory.create()
    course_es_id = gen_course_id(course.platform, course.course_id)
    content_files = [ContentFileFactory.create(run=run) for run in course.runs.all()]

    delete_course(course)
    patched_delete_task.delay.assert_any_call(course_es_id, COURSE_TYPE)
    for content_file in content_files:
        patched_delete_task.delay.assert_any_call(
            gen_content_file_id(content_file.key), COURSE_TYPE, routing=course_es_id
        )
def test_bulk_index_content_files(mocked_es, mocker, settings, errors,
                                  indexing_func_name, doc):  # pylint: disable=too-many-arguments
    """
    index functions for content files should call bulk with correct arguments
    """
    settings.ELASTICSEARCH_INDEXING_CHUNK_SIZE = 3
    course = CourseFactory.create()
    run = LearningResourceRunFactory.create(content_object=course)
    content_files = ContentFileFactory.create_batch(5, run=run)
    mock_get_aliases = mocker.patch("search.indexing_api.get_active_aliases",
                                    autospec=True,
                                    return_value=["a", "b"])
    bulk_mock = mocker.patch("search.indexing_api.bulk",
                             autospec=True,
                             return_value=(0, errors))
    mocker.patch(
        f"search.indexing_api.serialize_content_file_for_bulk",
        autospec=True,
        return_value=doc,
    )
    mocker.patch(
        f"search.indexing_api.serialize_content_file_for_bulk_deletion",
        autospec=True,
        return_value=doc,
    )

    index_func = getattr(indexing_api, indexing_func_name)
    if errors:
        with pytest.raises(ReindexException):
            index_func(run.id)
    else:
        index_func(run.id)
        for alias in mock_get_aliases.return_value:
            for chunk in chunks(
                [doc for _ in content_files],
                    chunk_size=settings.ELASTICSEARCH_INDEXING_CHUNK_SIZE,
            ):
                bulk_mock.assert_any_call(
                    mocked_es.conn,
                    chunk,
                    index=alias,
                    doc_type=GLOBAL_DOC_TYPE,
                    chunk_size=settings.ELASTICSEARCH_INDEXING_CHUNK_SIZE,
                    routing=gen_course_id(course.platform, course.course_id),
                )
Exemple #8
0
def test_load_content_file():
    """Test that load_content_file saves a ContentFile object"""
    learning_resource_run = LearningResourceRunFactory.create()

    props = model_to_dict(
        ContentFileFactory.build(run_id=learning_resource_run.id))
    props.pop("run")
    props.pop("id")

    result = load_content_file(learning_resource_run, props)

    assert ContentFile.objects.count() == 1

    assert result.run == learning_resource_run

    # assert we got a content file back
    assert isinstance(result, ContentFile)

    for key, value in props.items():
        assert getattr(result,
                       key) == value, f"Property {key} should equal {value}"
Exemple #9
0
def test_es_content_file_serializer():
    """ Verify that the ESContentFileSerializer has the correct data"""
    content_kwargs = {
        "content": "Some text",
        "content_author": "MIT",
        "content_language": "en",
        "content_title": "test title",
    }
    content_file = ContentFileFactory.create(**content_kwargs)
    serialized = ESContentFileSerializer(content_file).data
    assert_json_equal(
        serialized,
        {
            "object_type": RESOURCE_FILE_TYPE,
            "run_id": content_file.run.run_id,
            "run_title": content_file.run.title,
            "semester": content_file.run.semester,
            "year": int(content_file.run.year),
            "topics": list(content_file.run.topics.values_list("name", flat=True)),
            "key": content_file.key,
            "uid": content_file.uid,
            "resource_relations": {
                "name": "resourcefile",
                "parent": gen_course_id(
                    content_file.run.content_object.platform,
                    content_file.run.content_object.course_id,
                ),
            },
            "title": content_file.title,
            "short_description": content_file.description,
            "file_type": content_file.file_type,
            "content_type": content_file.content_type,
            "url": content_file.url,
            "section": content_file.section,
            "content": content_kwargs["content"],
            "content_title": content_kwargs["content_title"],
            "content_author": content_kwargs["content_author"],
            "content_language": content_kwargs["content_language"],
        },
    )
def test_delete_run_content_files(mocker):
    """Tests that delete_run_content_files triggers the correct ES delete task"""
    patched_task = mocker.patch("search.tasks.delete_run_content_files")
    content_file = ContentFileFactory.create()
    delete_run_content_files(content_file.id)
    patched_task.delay.assert_called_once_with(content_file.id)