<commit_before>import os
import responses
from ..utils import MontageTests, make_response, FILES


try:
    from cStringIO import StringIO
except ImportError:
    # Importing BytesIO as StringIO feels wrong...
    from io import BytesIO as StringIO

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse


class FileAPITests(MontageTests):
    @responses.activate
    def test_file_list(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.GET, endpoint, body=make_response(FILES),
            content_type='application/json')

        response = self.client.files.list()
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_filter(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.GET, endpoint, body=make_response(FILES),
            content_type='application/json')

        gigabyte = 1024 * 1024 * 1024
        response = self.client.files.list(size__gte=gigabyte)
        assert len(responses.calls) == 1
        query = urlparse(responses.calls[0].request.url).query
        assert query == 'size__gte={0}'.format(gigabyte)

    @responses.activate
    def test_file(self):
        file = FILES[0]
        endpoint = 'https://testco.hexxie.com/api/v1/files/{0}/'.format(file['id'])
        responses.add(responses.GET, endpoint, body=make_response(file),
            content_type='application/json')

        response = self.client.files.get(file['id'])
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_upload(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.POST, endpoint, body=make_response([FILES[0]]),
            content_type='application/json')

        path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files/python-powered.png')
        response = self.client.files.save(('python-powered.png', open(path, 'rb')))
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_upload_multiple(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.POST, endpoint, body=make_response(FILES),
            content_type='application/json')

        files_dir = os.path.dirname(os.path.abspath(__file__))
        python = os.path.join(files_dir, 'files/python-powered.png')
        django = os.path.join(files_dir, 'files/django-project.gif')
        hello = os.path.join(files_dir, 'files/hello-world.txt')

        # Test multiple ways of passing in the file
        response = self.client.files.save(
            ('python-powered.png', open(python, 'rb')),
            ('django-project.gif', StringIO(open(django, 'rb').read())),
            ('hello-world.txt', open(hello, 'rb').read())
        )

        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_delete(self):
        file = FILES[0]
        endpoint = 'https://testco.hexxie.com/api/v1/files/{0}/'.format(file['id'])
        responses.add(responses.DELETE, endpoint, status=204)
<commit_msg>test(test_file): fix incorrect path in tests<commit_after>import os
import responses
from ..utils import MontageTests, make_response, FILES


try:
    from cStringIO import StringIO
except ImportError:
    # Importing BytesIO as StringIO feels wrong...
    from io import BytesIO as StringIO

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse


class FileAPITests(MontageTests):
    @classmethod
    def file_path(cls):
        return os.path.abspath(os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../files'
        ))

    @responses.activate
    def test_file_list(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.GET, endpoint, body=make_response(FILES),
            content_type='application/json')

        response = self.client.files.list()
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_filter(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.GET, endpoint, body=make_response(FILES),
            content_type='application/json')

        gigabyte = 1024 * 1024 * 1024
        response = self.client.files.list(size__gte=gigabyte)
        assert len(responses.calls) == 1
        query = urlparse(responses.calls[0].request.url).query
        assert query == 'size__gte={0}'.format(gigabyte)

    @responses.activate
    def test_file(self):
        file = FILES[0]
        endpoint = 'https://testco.hexxie.com/api/v1/files/{0}/'.format(file['id'])
        responses.add(responses.GET, endpoint, body=make_response(file),
            content_type='application/json')

        response = self.client.files.get(file['id'])
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_upload(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.POST, endpoint, body=make_response([FILES[0]]),
            content_type='application/json')

        python = os.path.join(FileAPITests.file_path(), 'python-powered.png')
        response = self.client.files.save(('python-powered.png', open(python, 'rb')))
        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_upload_multiple(self):
        endpoint = 'https://testco.hexxie.com/api/v1/files/'
        responses.add(responses.POST, endpoint, body=make_response(FILES),
            content_type='application/json')

        python = os.path.join(FileAPITests.file_path(), 'python-powered.png')
        django = os.path.join(FileAPITests.file_path(), 'django-project.gif')
        hello = os.path.join(FileAPITests.file_path(), 'hello-world.txt')

        # Test multiple ways of passing in the file
        response = self.client.files.save(
            ('python-powered.png', open(python, 'rb')),
            ('django-project.gif', StringIO(open(django, 'rb').read())),
            ('hello-world.txt', open(hello, 'rb').read())
        )

        assert len(responses.calls) == 1
        assert responses.calls[0].request.url == endpoint

    @responses.activate
    def test_file_delete(self):
        file = FILES[0]
        endpoint = 'https://testco.hexxie.com/api/v1/files/{0}/'.format(file['id'])
        responses.add(responses.DELETE, endpoint, status=204)

{'fraction_non_alphanumeric': 0.13612565445026178, 'fraction_numerical': 0.010762070971495055, 'mean_word_length': 2.900737379466818, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '318881', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2127, 'n_words': 366}