<commit_before>import os

from nose.tools import (assert_equal,
                        assert_true)

import ckan.tests.helpers as helpers
import ckan.tests.factories as factories

import ckanapi
import boto
from moto import mock_s3


class TestS3ControllerResourceDownload(helpers.FunctionalTestBase):

    def _upload_resource(self):
        factories.Sysadmin(apikey="my-test-key")

        app = self._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')
        return resource, demo, app

    @mock_s3
    def test_resource_show_url(self):
        '''The resource_show url is expected for uploaded resource file.'''

        resource, demo, _ = self._upload_resource()

        # does resource_show have the expected resource file url?
        resource_show = demo.action.resource_show(id=resource['id'])

        expected_url = 'http://localhost:80/dataset/{0}/resource/{1}/download/data.csv' \
            .format(resource['package_id'], resource['id'])

        assert_equal(resource_show['url'], expected_url)

    @mock_s3
    def test_resource_download_s3(self):
        '''A resource uploaded to S3 can be downloaded.'''

        resource, demo, app = self._upload_resource()
        resource_show = demo.action.resource_show(id=resource['id'])
        resource_file_url = resource_show['url']

        file_response = app.get(resource_file_url)
<commit_msg>Fix expect url in test<commit_after>import os

from nose.tools import (assert_equal,
                        assert_true)

import ckan.tests.helpers as helpers
import ckan.tests.factories as factories

import ckanapi
import boto
from moto import mock_s3


class TestS3ControllerResourceDownload(helpers.FunctionalTestBase):

    def _upload_resource(self):
        factories.Sysadmin(apikey="my-test-key")

        app = self._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')
        return resource, demo, app

    @mock_s3
    def test_resource_show_url(self):
        '''The resource_show url is expected for uploaded resource file.'''

        resource, demo, _ = self._upload_resource()

        # does resource_show have the expected resource file url?
        resource_show = demo.action.resource_show(id=resource['id'])

        expected_url = 'http://test.ckan.net/dataset/{0}/resource/{1}/download/data.csv' \
            .format(resource['package_id'], resource['id'])

        assert_equal(resource_show['url'], expected_url)

    @mock_s3
    def test_resource_download_s3(self):
        '''A resource uploaded to S3 can be downloaded.'''

        resource, demo, app = self._upload_resource()
        resource_show = demo.action.resource_show(id=resource['id'])
        resource_file_url = resource_show['url']

        file_response = app.get(resource_file_url)

{'fraction_non_alphanumeric': 0.1064385150812065, 'fraction_numerical': 0.005220417633410673, 'mean_word_length': 2.680896478121665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '923644', 'n_tokens_mistral': 995, 'n_tokens_neox': 965, 'n_words': 198}