tests.py 4.35 KB
Newer Older
1
"""
2
Copyright [2009-2017] EMBL-European Bioinformatics Institute
3 4 5 6 7 8 9 10 11 12 13 14 15 16
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json
import requests

17
from django.test import TestCase
18
from django.urls import reverse
19 20


21
class ExportSearchResultsTestCase(TestCase):
22 23
    """
    Base class for export search results testing.
24
    Using the test website because I don't want to run redis locally for now.
25
    """
carlosribas's avatar
Bug fix  
carlosribas committed
26
    base_url = 'https://test.rnacentral.org'
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    def setUp(self):
        self.queries = {
            'small': 'hotair',
        }

    def _submit_query(self, query, _format=''):
        """
        Submit a query and return the response object.
        """
        url = ''.join([self.base_url,
                       reverse('export-submit-job'),
                       '?q=%s' % query,
                       '&format=%s' % _format if _format else ''])
        return requests.get(url)


class SubmitExportTests(ExportSearchResultsTestCase):
    """
    Tests for the job submission endpoint.
    """

    def test_submit_empty_query(self):
        """
        No query is provided in the url.
        """
        r = self._submit_query(query='')
        self.assertEqual(r.status_code, 400)

    def test_submit_invalid_format(self):
        """
        Invalid export format.
        """
        r = self._submit_query(query=self.queries['small'], _format='bar')
        self.assertEqual(r.status_code, 404)

    def test_submit_export_job(self):
        """
        Submit a small query in multiple formats.
        """
        formats = ['fasta', 'json', '']
        query = self.queries['small']
        for _format in formats:
            r = self._submit_query(query=query, _format=_format)
            data = json.loads(r.text)
            self.assertEqual(r.status_code, 200)
            self.assertIn('job_id', data)


class GetStatusTests(ExportSearchResultsTestCase):
    """
    Tests for the job status endpoint.
    """

    def test_get_status_no_job_id(self):
        """
        No job id is provided in the url.
        """
        url = self.base_url + reverse('export-job-status')
        r = requests.get(url)
        self.assertEqual(r.status_code, 400)

    def test_get_status_invalid_job_id(self):
        """
        Invalid job id or job id not found.
        """
        job_id = 'foobar'
        url = self.base_url + reverse('export-job-status') + '?job=%s' % job_id
        r = requests.get(url)
        self.assertEqual(r.status_code, 404)

    def test_get_status_valid_job(self):
        """
        Submit a small query and check its status.
        """
        r = self._submit_query(query=self.queries['small'])
        job_id = json.loads(r.text)['job_id']
        url = self.base_url + reverse('export-job-status') + '?job=%s' % job_id
        r = requests.get(url)
        self.assertEqual(r.status_code, 200)


class DownloadResultsTests(ExportSearchResultsTestCase):
    """
    Tests for the download results endpoint.
    """

    def test_download_no_job_id(self):
        """
        No job id is provided in the url.
        """
        url = self.base_url + reverse('export-download-result')
        r = requests.get(url)
        self.assertEqual(r.status_code, 400)

    def test_download_invalid_job_id(self):
        """
        Invalid job id or job id not found.
        """
        job_id = 'foobar'
        url = ''.join([self.base_url,
                       reverse('export-download-result'),
                       '?job=%s' % job_id])
        r = requests.get(url)
        self.assertEqual(r.status_code, 404)
carlosribas's avatar
carlosribas committed
132 133 134 135 136 137 138 139 140 141


class ResultsPageTests(ExportSearchResultsTestCase):
    def test_results_status_code(self):
        response = self.client.get(reverse('export-job-results'))
        self.assertEquals(response.status_code, 200)

    def test_results_template(self):
        response = self.client.get(reverse('export-job-results'))
        self.assertTemplateUsed(response, 'portal/search/export-job-results.html')