|
1
|
|
|
# coding: utf8 |
|
2
|
|
|
import os |
|
3
|
|
|
|
|
4
|
|
|
from django import test |
|
5
|
|
|
from feedback.utils import get_file_extension |
|
6
|
|
|
|
|
7
|
|
|
BASE_DIR = os.path.dirname(__file__) |
|
8
|
|
|
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata') |
|
9
|
|
|
GZ_FILE = os.path.join(TEST_DATA_DIR, 'test_tar_gz') |
|
10
|
|
|
TAR_FILE = os.path.join(TEST_DATA_DIR, 'test_tar') |
|
11
|
|
|
TEXT_FILE = os.path.join(TEST_DATA_DIR, 'test_none') |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class UtilsTest(test.TestCase): |
|
15
|
|
|
|
|
16
|
|
|
def test_get_file_extension_from_gz_file(self): |
|
17
|
|
|
with open(GZ_FILE, 'rb') as file: |
|
18
|
|
|
file_header = file.read(1024) |
|
19
|
|
|
file_description = get_file_extension(file_header) |
|
20
|
|
|
self.assertEqual(file_description['file_extension'], "tar.gz") |
|
21
|
|
|
self.assertEqual(file_description['mime_type'], "application/gzip") |
|
22
|
|
|
|
|
23
|
|
|
def test_get_file_extension_from_tar_file(self): |
|
24
|
|
|
with open(TAR_FILE, 'rb') as file: |
|
25
|
|
|
file_header = file.read(1024) |
|
26
|
|
|
file_description = get_file_extension(file_header) |
|
27
|
|
|
self.assertEqual(file_description['file_extension'], "tar") |
|
28
|
|
|
self.assertEqual(file_description['mime_type'], "application/x-tar") |
|
29
|
|
|
|
|
30
|
|
|
def test_get_file_extension_from_none_extension_file(self): |
|
31
|
|
|
with open(TEXT_FILE, 'rb') as file: |
|
32
|
|
|
file_header = file.read(1024) |
|
33
|
|
|
file_description = get_file_extension(file_header) |
|
34
|
|
|
self.assertEqual(file_description['file_extension'], None) |
|
35
|
|
|
self.assertEqual(file_description['mime_type'], "text/plain") |
|
36
|
|
|
|