Skip to content

Commit

Permalink
Add explicit test fixtures to fix tests on Windows (#91)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicholas Hairs <info@nicholashairs.com>
  • Loading branch information
Acconut and nhairs committed Dec 13, 2023
1 parent 2aaab2c commit 6d42811
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/sample_files/* binary
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'aioresponses>=0.6.2',
'coverage>=4.2',
'pytest>=3.0.3',
'pytest-cov>=2.3.1,<2.6'
'pytest-cov>=2.3.1,<2.6',
'parametrize>=0.1.1'
],
'dev': [
'tox>=2.3.1',
Expand Down
3 changes: 2 additions & 1 deletion tests/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from tusclient import client

FILEPATH_TEXT = "tests/sample_files/text.txt"

class Mixin(unittest.TestCase):
@responses.activate
Expand All @@ -12,4 +13,4 @@ def setUp(self):
self.url = 'http://tusd.tusdemo.net/files/15acd89eabdf5738ffc'
responses.add(responses.HEAD, self.url,
adding_headers={"upload-offset": "0"})
self.uploader = self.client.uploader('./LICENSE', url=self.url)
self.uploader = self.client.uploader(FILEPATH_TEXT, url=self.url)
Binary file added tests/sample_files/binary.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/sample_files/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) The tus-py-client CONTRIBUTORS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion tests/storage_file
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_default": {"1": {"url": "http://tusd.tusdemo.net/files/foo_bar", "key": "size:1078--md5:f3e5fa051d458fb7f22eaf03749c8272"}}}
{"_default": {"1": {"key": "size:1082--md5:cbb679e9dbcf82224fe3bc5fdc881f06", "url": "http://tusd.tusdemo.net/files/foo_bar"}, "2": {"key": "size:49588--md5:ae275d47f1ef9aed4902b0251455e627", "url": "http://tusd.tusdemo.net/files/foo_bar"}}}
31 changes: 22 additions & 9 deletions tests/test_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@

import six

from parametrize import parametrize

from tusclient.fingerprint import fingerprint

FILEPATH_TEXT = "tests/sample_files/text.txt"
FILEPATH_BINARY = "tests/sample_files/binary.png"


class FileStorageTest(unittest.TestCase):
def setUp(self):
self.fingerprinter = fingerprint.Fingerprint()

def test_get_fingerpint(self):
with open('./LICENSE') as f:
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
def test_get_fingerpint(self, filename: str):
with open(filename, "rb") as f:
content = f.read()
buff = six.StringIO()
buff = six.BytesIO()
buff.write(content)
buff.seek(0) # reset buffer postion before reading

with open('./LICENSE') as f:
with open(filename, "rb") as f:
self.assertEqual(
self.fingerprinter.get_fingerprint(buff),
self.fingerprinter.get_fingerprint(f)
)

def test_unique_fingerprint(self):
with open('./LICENSE') as f:
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
def test_unique_fingerprint(self, filename: str):
with open(filename, "rb") as f:
content = f.read()
buff = six.StringIO()
buff.write(content + 's') # add some salt to change value
buff = six.BytesIO()
buff.write(content + b's') # add some salt to change value
buff.seek(0) # reset buffer postion before reading

with open('./LICENSE') as f:
with open(filename, "rb") as f:
self.assertNotEqual(
self.fingerprinter.get_fingerprint(buff),
self.fingerprinter.get_fingerprint(f)
Expand Down
20 changes: 14 additions & 6 deletions tests/test_request.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import base64
import hashlib

from parametrize import parametrize
import responses

from tusclient import request
from tests import mixin


FILEPATH_TEXT = "tests/sample_files/text.txt"
FILEPATH_BINARY = "tests/sample_files/binary.png"


class TusRequestTest(mixin.Mixin):
def setUp(self):
super(TusRequestTest, self).setUp()
self.request = request.TusRequest(self.uploader)

def test_perform(self):
with open('LICENSE', 'rb') as stream, responses.RequestsMock() as resps:
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
def test_perform(self, filename: str):
with open(FILEPATH_TEXT, "rb") as stream, responses.RequestsMock() as resps:
size = stream.tell()
resps.add(responses.PATCH, self.url,
adding_headers={'upload-offset': str(size)},
Expand All @@ -26,12 +35,11 @@ def test_perform_checksum(self):
self.uploader.upload_checksum = True
tus_request = request.TusRequest(self.uploader)

with open('LICENSE', 'r') as stream, responses.RequestsMock() as resps:
license_ = stream.read()
encoded_file = license_.encode('utf-8')
with open(FILEPATH_TEXT, "rb") as stream, responses.RequestsMock() as resps:
content = stream.read()
expected_checksum = "sha1 " + \
base64.standard_b64encode(hashlib.sha1(
encoded_file).digest()).decode("ascii")
content).digest()).decode("ascii")

sent_checksum = ''
def validate_headers(req):
Expand Down
39 changes: 28 additions & 11 deletions tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
from unittest import mock

import responses
from parametrize import parametrize
import pytest

from tusclient import exceptions
from tusclient.storage import filestorage
from tests import mixin


FILEPATH_TEXT = "tests/sample_files/text.txt"
FILEPATH_BINARY = "tests/sample_files/binary.png"


class UploaderTest(mixin.Mixin):

def mock_request(self, request_mock):
Expand Down Expand Up @@ -69,16 +74,20 @@ def test_create_url_relative(self):
adding_headers={"location": "/files/foo"})
self.assertEqual(self.uploader.create_url(), 'http://tusd.tusdemo.net/files/foo')

@responses.activate
def test_url(self):
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
@responses.activate
def test_url(self, filename: str):
# test for stored urls
responses.add(responses.HEAD, 'http://tusd.tusdemo.net/files/foo_bar',
adding_headers={"upload-offset": "10"})
storage_path = '{}/storage_file'.format(os.path.dirname(os.path.abspath(__file__)))
resumable_uploader = self.client.uploader(
file_path='./LICENSE', store_url=True, url_storage=filestorage.FileStorage(storage_path)
file_path=filename, store_url=True, url_storage=filestorage.FileStorage(storage_path)
)
self.assertEqual(resumable_uploader.url, 'http://tusd.tusdemo.net/files/foo_bar')
self.assertEqual(resumable_uploader.url, "http://tusd.tusdemo.net/files/foo_bar")
self.assertEqual(resumable_uploader.offset, 10)

def test_request_length(self):
Expand All @@ -88,25 +97,33 @@ def test_request_length(self):
self.uploader.chunk_size = self.uploader.get_file_size() + 3000
self.assertEqual(self.uploader.get_request_length(), self.uploader.get_file_size())

def test_get_file_stream(self):
with open('./LICENSE', 'rb') as fs:
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
def test_get_file_stream(self, filename: str):
with open(filename, "rb") as fs:
self.uploader.file_stream = fs
self.uploader.file_path = None
self.assertEqual(self.uploader.file_stream, self.uploader.get_file_stream())

with open('./AUTHORS', 'rb') as fs:
with open(filename, "rb") as fs:
self.uploader.file_stream = None
self.uploader.file_path = './AUTHORS'
self.uploader.file_path = filename
with self.uploader.get_file_stream() as stream:
self.assertEqual(fs.read(), stream.read())

def test_file_size(self):
@parametrize(
"filename",
[FILEPATH_TEXT, FILEPATH_BINARY],
)
def test_file_size(self, filename: str):
self.assertEqual(self.uploader.get_file_size(), os.path.getsize(self.uploader.file_path))

with open('./AUTHORS', 'rb') as fs:
with open(filename, "rb") as fs:
self.uploader.file_stream = fs
self.uploader.file_path = None
self.assertEqual(self.uploader.get_file_size(), os.path.getsize('./AUTHORS'))
self.assertEqual(self.uploader.get_file_size(), os.path.getsize(filename))

@mock.patch('tusclient.uploader.uploader.TusRequest')
def test_upload_chunk(self, request_mock):
Expand Down

0 comments on commit 6d42811

Please sign in to comment.