Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest #90

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: lint unit
all: lint test

export PYTHONPATH:=${PWD}
version=`python -c 'import filetype; print(filetype.version)'`
Expand All @@ -9,7 +9,10 @@ lint:

test: clean lint
@echo "Running tests ..."
@python -m unittest discover
@python -m pytest -v tests

coverage:
@python -m pytest --cov-branch --cov-report html --cov=filetype tests/

documentation:
@pdoc --html --overwrite --all-submodules --html-dir docs filetype
Expand Down
4 changes: 2 additions & 2 deletions filetype/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def is_extension_supported(ext):
Otherwise False.
"""
for kind in TYPES:
if kind.extension is ext:
if kind.extension == ext:
return True
return False

Expand All @@ -39,7 +39,7 @@ def is_mime_supported(mime):
Otherwise False.
"""
for kind in TYPES:
if kind.mime is mime:
if kind.mime == mime:
return True
return False

Expand Down
4 changes: 3 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pytest
flake8
pytest
pytest-benchmark
pytest-cov
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# -*- coding: utf-8 -*-

import os

# Absolute path to fixtures directory
FIXTURES = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures')
Binary file added tests/fixtures/sample.mp3
Binary file not shown.
Binary file added tests/fixtures/sample.tar
Binary file not shown.
Binary file added tests/fixtures/sample.ttf
Binary file not shown.
Binary file added tests/fixtures/sample.zip
Binary file not shown.
49 changes: 20 additions & 29 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@
from __future__ import absolute_import

import os

import filetype

# Absolute path to fixtures directory
FIXTURES = os.path.dirname(os.path.abspath(__file__)) + '/fixtures'


def test_infer_image_from_disk(benchmark):
benchmark(filetype.guess, FIXTURES + '/sample.jpg')


def test_infer_video_from_disk(benchmark):
benchmark(filetype.guess, FIXTURES + '/sample.mp4')


def test_infer_zip_from_disk(benchmark):
benchmark(filetype.guess, FIXTURES + '/sample.zip')


def test_infer_tar_from_disk(benchmark):
benchmark(filetype.guess, FIXTURES + '/sample.tar')

import pytest

def test_infer_image_from_bytes(benchmark):
benchmark(filetype.guess,
bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
from . import FIXTURES


def test_infer_video_from_bytes(benchmark):
benchmark(filetype.guess,
bytearray([0x1A, 0x45, 0xDF, 0xA3, 0x08]))
@pytest.mark.parametrize('filename',
[
'sample.jpg',
'sample.mp4',
'sample.zip',
'sample.tar',
])
def test_infer_from_disk(benchmark, filename):
benchmark(filetype.guess, os.path.join(FIXTURES, filename))


def test_infer_audio_from_bytes(benchmark):
benchmark(filetype.guess,
bytearray([0x4D, 0x54, 0x68, 0xA3, 0x64]))
@pytest.mark.parametrize('bytes_',
[
[0xFF, 0xD8, 0xFF, 0x00, 0x08],
[0x1A, 0x45, 0xDF, 0xA3, 0x08],
[0x4D, 0x54, 0x68, 0xA3, 0x64],
],
ids=['image', 'video', 'audio'])
def test_infer_from_bytes(benchmark, bytes_):
benchmark(filetype.guess, bytearray(bytes_))
148 changes: 115 additions & 33 deletions tests/test_filetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,166 @@
from __future__ import absolute_import

import os
import unittest
import sys

# Python 2.7 workaround
try:
import pathlib
except ImportError:
pathlib = None

import filetype
from filetype.types import image
from filetype.types.base import Type

import pytest

# Absolute path to fixtures directory
FIXTURES = os.path.dirname(os.path.abspath(__file__)) + '/fixtures'
from . import FIXTURES


class TestFileType(unittest.TestCase):
class TestFileType(object):

def test_guess_file_path(self):
kind = filetype.guess(FIXTURES + '/sample.jpg')
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jpeg')
self.assertEqual(kind.extension, 'jpg')
kind = filetype.guess(os.path.join(FIXTURES, 'sample.jpg'))
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'

def test_guess_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
kind = filetype.guess(buf)
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jpeg')
self.assertEqual(kind.extension, 'jpg')
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'

def test_guess_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
kind = filetype.guess(buf)
self.assertTrue(kind is None)
assert kind is None

def test_guess_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jpeg')
self.assertEqual(kind.extension, 'jpg')
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'

@pytest.mark.skipif(sys.version_info < (3,),
reason="No workarounds anymore for python 2.7")
def test_guess_bytes(self):
buf = bytes(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'

@pytest.mark.skipif(sys.version_info < (3, 6),
reason="No workarounds for unsupported versions")
def test_guess_pathlib(self):
path = pathlib.Path(os.path.join(FIXTURES, 'sample.jpg'))
kind = filetype.guess(path)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'

@pytest.mark.skipif(sys.version_info < (3, 6),
reason="No workarounds for unsupported versions")
def test_invalid_input(self):
with pytest.raises(TypeError):
filetype.guess(tuple)


class TestFileTypeExtension(object):

class TestFileTypeExtension(unittest.TestCase):
def test_guess_extension_file_path(self):
ext = filetype.guess_extension(FIXTURES + '/sample.jpg')
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
ext = filetype.guess_extension(os.path.join(FIXTURES, 'sample.jpg'))
assert ext is not None
assert ext == 'jpg'

def test_guess_extension_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
assert ext is not None
assert ext == 'jpg'

def test_guess_extension_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is None)
assert ext is None

def test_guess_extension_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
assert ext is not None
assert ext == 'jpg'


class TestFileTypeMIME(unittest.TestCase):
class TestFileTypeMIME(object):

def test_guess_mime_file_path(self):
mime = filetype.guess_mime(FIXTURES + '/sample.jpg')
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
mime = filetype.guess_mime(os.path.join(FIXTURES, 'sample.jpg'))
assert mime is not None
assert mime == 'image/jpeg'

def test_guess_mime_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
assert mime is not None
assert mime == 'image/jpeg'

def test_guess_mime_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is None)
assert mime is None

def test_guess_mime_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
assert mime is not None
assert mime == 'image/jpeg'


def test_get_type_mime():
type_ = filetype.get_type(mime='image/png')
assert isinstance(type_, image.Png)


def test_get_type_ext():
type_ = filetype.get_type(ext='png')
assert isinstance(type_, image.Png)


def test_get_type():
type_ = filetype.get_type(mime='image/png', ext='png')
assert isinstance(type_, image.Png)


def test_get_type_invalid():
type_ = filetype.get_type(mime='foo', ext='bar')
assert type_ is None


def test_invalid_custom_type():
with pytest.raises(TypeError):
filetype.add_type(tuple)


@pytest.mark.skipif(sys.version_info < (3,),
reason="No workarounds anymore for python 2.7")
def test_custom_type():
mime = 'app/bar'
ext = 'bar'

class Custom(Type):

def __init__(self):
super().__init__(mime=mime, extension=ext)

def match(self, buf):
return buf == b"bar"

filetype.add_type(Custom())
kind = filetype.guess(b"bar")

assert kind.extension == ext
assert kind.mime == mime
44 changes: 44 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import os

from filetype import helpers

from . import FIXTURES


def test_supported_extension():
assert helpers.is_extension_supported('jpg') is True


def test_unsupported_extension():
assert helpers.is_extension_supported('foo') is False


def test_supported_mime():
assert helpers.is_mime_supported('image/jpeg') is True


def test_unsupported_mime():
assert helpers.is_mime_supported('foo') is False


def test_is_archive():
assert helpers.is_archive(os.path.join(FIXTURES, 'sample.tar')) is True


def test_is_font():
assert helpers.is_font(os.path.join(FIXTURES, 'sample.ttf')) is True


def test_is_image():
assert helpers.is_image(os.path.join(FIXTURES, 'sample.png')) is True


def test_is_video():
assert helpers.is_video(os.path.join(FIXTURES, 'sample.mp4')) is True


def test_is_audio():
assert helpers.is_audio(os.path.join(FIXTURES, 'sample.mp3')) is True
Loading