Esempio n. 1
0
    def test_get_mappings_from_zip_file(self):
        filename = 'folder_with_xls_and_audio_folder.zip'
        uploaded_file = self.make_uploaded_zip_file(filename)
        self.assertTrue(uploaded_file)
        self.assertEqual(uploaded_file.name, filename)
        self.assertEqual(uploaded_file.content_type, 'application/zip')

        deck = services.handle_custom_file(uploaded_file, 'test course', self.user)
        custom = True

        [file_contents, zfile, file_names, path_to_excel] = services.extract_from_zip(uploaded_file)
        [file_contents, mappings] = services.get_mappings_from_zip(deck, file_contents, file_names, zfile, path_to_excel, custom=custom)

        self.assertTrue(len(file_contents) > 0)
        self.assertTrue(mappings)
        self.assertTrue('Image' in mappings)
        self.assertTrue(len(mappings['Image'].keys()) == 0)
        self.assertTrue('Audio' in mappings)
        self.assertTrue(len(mappings['Audio'].keys()) == 6)

        zip_paths = [
            "audio/aurevoir.mp3",
            "audio/bonappetit.mp3",
            "audio/bonjour.mp3",
            "audio/double.mp3",
            "audio/horsdoeuvre.mp3",
            "audio/jenecomprendspas.mp3",
        ]

        for zip_path in zip_paths:
            zip_file_name = os.path.split(zip_path)[1] 
            self.assertNotEqual(mappings['Audio'][zip_path], zip_file_name)
            self.assertRegexpMatches(mappings['Audio'][zip_path], '[0-9a-fA-F]{16}\.\w+')
Esempio n. 2
0
    def test_extract_zip_files(self):
        tests = [{
            'filename':
            'xls_and_images.zip',
            'expected_file_names': [
                'BartSimpson4.gif', 'MaggieSimpson1.gif',
                'flashcards_template.xls'
            ],
            'expected_path_to_excel':
            ''
        }, {
            'filename':
            'folder_with_xls_and_images.zip',
            'expected_file_names': [
                'folder_with_xls_and_images/',
                'folder_with_xls_and_images/.DS_Store',
                'folder_with_xls_and_images/BartSimpson4.gif',
                'folder_with_xls_and_images/MaggieSimpson1.gif',
                'folder_with_xls_and_images/flashcards_template.xls'
            ],
            'expected_path_to_excel':
            'folder_with_xls_and_images'
        }, {
            'filename':
            'folder_with_xls_and_audio_folder.zip',
            'expected_file_names': [
                'folder_with_xls_and_audio_folder/',
                'folder_with_xls_and_audio_folder/.DS_Store',
                'folder_with_xls_and_audio_folder/audio/',
                'folder_with_xls_and_audio_folder/audio/aurevoir.mp3',
                'folder_with_xls_and_audio_folder/audio/bonappetit.mp3',
                'folder_with_xls_and_audio_folder/audio/bonjour.mp3',
                'folder_with_xls_and_audio_folder/audio/double.mp3',
                'folder_with_xls_and_audio_folder/audio/horsdoeuvre.mp3',
                'folder_with_xls_and_audio_folder/audio/jenecomprendspas.mp3',
                'folder_with_xls_and_audio_folder/deck.xls'
            ],
            'expected_path_to_excel':
            'folder_with_xls_and_audio_folder'
        }]

        for test in tests:
            filename = test['filename']
            expected_path_to_excel = test['expected_path_to_excel']
            expected_file_names = test['expected_file_names']

            uploaded_file = self.make_uploaded_zip_file(filename)
            self.assertTrue(uploaded_file)
            self.assertEqual(uploaded_file.name, filename)
            self.assertEqual(uploaded_file.content_type, 'application/zip')

            result = services.extract_from_zip(uploaded_file)
            self.assertTrue(len(result[0]) > 0)
            self.assertTrue(isinstance(result[1], zipfile.ZipFile))
            self.assertEqual(result[2], expected_file_names)
            self.assertEqual(result[3], expected_path_to_excel)
Esempio n. 3
0
    def test_extract_zip_files(self):
        tests = [
            {
                'filename': 'xls_and_images.zip',
                'expected_file_names': [
                    'BartSimpson4.gif',
                    'MaggieSimpson1.gif',
                    'flashcards_template.xls'
                ],
                'expected_path_to_excel': ''
            },
            {
                'filename': 'folder_with_xls_and_images.zip',
                'expected_file_names': [
                    'folder_with_xls_and_images/',
                    'folder_with_xls_and_images/.DS_Store',
                    'folder_with_xls_and_images/BartSimpson4.gif',
                    'folder_with_xls_and_images/MaggieSimpson1.gif',
                    'folder_with_xls_and_images/flashcards_template.xls'
                ],
                'expected_path_to_excel': 'folder_with_xls_and_images'
            },
            {
                'filename': 'folder_with_xls_and_audio_folder.zip',
                'expected_file_names': [
                    'folder_with_xls_and_audio_folder/',
                    'folder_with_xls_and_audio_folder/.DS_Store',
                    'folder_with_xls_and_audio_folder/audio/',
                    'folder_with_xls_and_audio_folder/audio/aurevoir.mp3',
                    'folder_with_xls_and_audio_folder/audio/bonappetit.mp3',
                    'folder_with_xls_and_audio_folder/audio/bonjour.mp3',
                    'folder_with_xls_and_audio_folder/audio/double.mp3',
                    'folder_with_xls_and_audio_folder/audio/horsdoeuvre.mp3',
                    'folder_with_xls_and_audio_folder/audio/jenecomprendspas.mp3',
                    'folder_with_xls_and_audio_folder/deck.xls'
                ],
                'expected_path_to_excel': 'folder_with_xls_and_audio_folder'
            }
        ]

        for test in tests:
            filename = test['filename']
            expected_path_to_excel = test['expected_path_to_excel']
            expected_file_names = test['expected_file_names']

            uploaded_file = self.make_uploaded_zip_file(filename)
            self.assertTrue(uploaded_file)
            self.assertEqual(uploaded_file.name, filename)
            self.assertEqual(uploaded_file.content_type, 'application/zip')

            result = services.extract_from_zip(uploaded_file)
            self.assertTrue(len(result[0]) > 0)
            self.assertTrue(isinstance(result[1], zipfile.ZipFile))
            self.assertEqual(result[2], expected_file_names)
            self.assertEqual(result[3], expected_path_to_excel)
Esempio n. 4
0
    def test_get_mappings_from_zip_file(self):
        filename = 'folder_with_xls_and_audio_folder.zip'
        uploaded_file = self.make_uploaded_zip_file(filename)
        self.assertTrue(uploaded_file)
        self.assertEqual(uploaded_file.name, filename)
        self.assertEqual(uploaded_file.content_type, 'application/zip')

        deck = services.handle_custom_file(uploaded_file, 'test course',
                                           self.user)
        custom = True

        [file_contents, zfile, file_names,
         path_to_excel] = services.extract_from_zip(uploaded_file)
        [file_contents,
         mappings] = services.get_mappings_from_zip(deck,
                                                    file_contents,
                                                    file_names,
                                                    zfile,
                                                    path_to_excel,
                                                    custom=custom)

        self.assertTrue(len(file_contents) > 0)
        self.assertTrue(mappings)
        self.assertTrue('Image' in mappings)
        self.assertTrue(len(mappings['Image'].keys()) == 0)
        self.assertTrue('Audio' in mappings)
        self.assertTrue(len(mappings['Audio'].keys()) == 6)

        zip_paths = [
            "audio/aurevoir.mp3",
            "audio/bonappetit.mp3",
            "audio/bonjour.mp3",
            "audio/double.mp3",
            "audio/horsdoeuvre.mp3",
            "audio/jenecomprendspas.mp3",
        ]

        for zip_path in zip_paths:
            zip_file_name = os.path.split(zip_path)[1]
            self.assertNotEqual(mappings['Audio'][zip_path], zip_file_name)
            self.assertRegexpMatches(mappings['Audio'][zip_path],
                                     '[0-9a-fA-F]{16}\.\w+')