Пример #1
0
def test_perseus_process_question(persues_question_json_fixtures):
    """
    Process a persues question and check that it finds all images, and returns
    correcrt image files -- i.e not more, not less.
    """
    
    for datum in persues_question_json_fixtures:

        # setup
        perseus_question = datum['item']
        expected_image_hashes = set(datum['image_hashes'])
        _clear_ricecookerfilecache()  # clear file cache each time to avoid test interactions

        # SIT
        testq = PerseusQuestion(id='x43bbec76d5f14f88_en', raw_data=perseus_question)
        filenames = testq.process_question()

        # check 1
        assert len(filenames) == 2, 'wrong number of filenames found'

        # check 2
        image_hashes = set()
        for filename in filenames:
            filehash, ext =  os.path.splitext(filename)
            image_hashes.add(filehash)
        assert image_hashes == expected_image_hashes, 'Unexpected image file set'
Пример #2
0
 def setup_method(self, test_method):
     """
     Called before each test method executes.
     """
     _clear_ricecookerfilecache()
     config.FAILED_FILES = []
     config.THUMBNAILS = False
Пример #3
0
def test_base_question_set_image(image_texts_fixtures):
    """
    Create a test question and check that `set_image` method performs the right image string
    replacement logic.
    """

    for datum in image_texts_fixtures:
        # setup
        _clear_ricecookerfilecache()  # clear file cache each time to avoid test interactions
        text = datum['text']
        replacement_str = datum['replacement_str']

        
        # SIT ##################################################################
        testq = BaseQuestion(id='someid', question='somequestion', question_type='input', raw_data={})
        new_text, images = testq.set_image(text)

        # check 1
        assert new_text == replacement_str, 'Unexpected replacement text produced by set_image'

        # check 2
        assert len(images) == 1, 'Should find exactly one image'

        # check 3
        image_file = images[0]
        filename = image_file.get_filename()
        assert datum['hash'] in filename, 'wront content hash for file'
        # print('filename=', filename)
        if text.startswith('web+graphie:'):
            assert new_text.startswith('web+graphie:'), 'web+graphie: was lost'
            assert filename.endswith('.graphie'), 'wrong extension for web+graphie text'
        expected_storage_dir = os.path.join(STORAGE_DIRECTORY, filename[0], filename[1])
        expected_storage_path = os.path.join(expected_storage_dir, filename)
        assert os.path.exists(expected_storage_path), 'Image file not saved to ricecooker storage dir'
Пример #4
0
def test_persues_question_process_image_field(
        persues_contentimages_field_fixtures):
    """
    Create a PerseusQuestion test object and check that `process_image_field` method works
    correctly for differnet snippets of the form:
        data = {
            "content": "Some string possibly including image URLs like ![smth](URL-key)",
            "images": {
                "URL-key":  {"width": 425, "height": 425},
                "URL-key2": {"width": 425, "height": 425}
            }
        }
    """

    for fixture in persues_contentimages_field_fixtures:
        # setup
        _clear_ricecookerfilecache(
        )  # clear file cache each time to avoid test interactions
        field = fixture['field']
        expected_new_content = fixture['new_content']
        expected_image_hashes = set(fixture['image_hashes'])

        # SIT
        testq = PerseusQuestion(id='x43bbec76d5f14f88_bg', raw_data={})
        new_images, image_files = testq.process_image_field(fixture['field'])

        # check 1
        assert field[
            'content'] == expected_new_content, 'Image URL replacement failed'
        #
        # check 2
        for image_key, image_attrs in new_images.items():
            assert 'http' not in image_key, 'Images URLs not replace with local paths'
        #
        # check 3
        image_hashes = set()
        for image_file in image_files:
            assert image_file is not None, 'image_file should not be None'
            filehash, ext = os.path.splitext(image_file.get_filename())
            image_hashes.add(filehash)
        assert image_hashes == expected_image_hashes, 'Unexpected image files set'