test__middleware.py 4.64 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Unit/integration tests for the Beacon's middleware.
"""
Jacek Lebioda's avatar
Jacek Lebioda committed
7
import beacon.data_types
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import beacon.helpers
import beacon.middleware
from beacon import data_types, middleware

from tests.test import MetaBeaconTestCase


class MiddlewareTestCase(MetaBeaconTestCase):
    """Test suite for beacon web application"""

    def test_validate_input(self):
        """`validate_input` function should work"""

        def build_request(*args):
            """Shortcut for creating requests w/o giving full fields names"""
            return data_types.create_beacon_allele_request(
                referenceName=args[0],
                start=args[1],
                referenceBases=args[2],
                alternateBases=args[3],
                assemblyId=args[4],
                includeDatasetResponses=args[5] if len(args) == 6 else None
            )

        def case(arguments, argument_name):
            """Test for exceptions"""
            the_message = "`validate_input` should fail: incorrect `{0}`"
            try:
                the_request = build_request(*arguments)
Jacek Lebioda's avatar
Jacek Lebioda committed
37
38
                beacon.data_types.validate_input(**the_request)
            except beacon.data_types.APIArgumentError:
39
40
41
42
43
44
45
46
                return
            self.fail(the_message.format(argument_name))  # pragma: no cover

        request = build_request('1', '1', 'a', 't', 'GRCh37')
        correct_reference_names = [str(x) for x in range(1, 23)] + ['x', 'y', 'm', 'mt', 'X', 'Y']
        for name in correct_reference_names:
            request['referenceName'] = name
            message = "`validate_input` with correct arguments should work"
Jacek Lebioda's avatar
Jacek Lebioda committed
47
            assert beacon.data_types.validate_input(**request), message
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

        case(['x', '-1', 'a', 't', 'GRCh37'], 'start')
        case(['x', '1', 'e', 't', 'GRCh37'], 'reference_bases')
        case(['x', '1', 'a', 'e', 'GRCh37'], 'alternate_bases')
        case(['x', '1', 'a', 'aa', 'GRCh39'], 'assembly_id')
        case(['xx', '1', 'a', 'aa', 'GRCh37'], 'referenceName')
        case(['0', '1', 'a', 'aa', 'GRCh37'], 'referenceName')
        case(['23', '1', 'a', 'aa', 'GRCh37'], 'referenceName')
        case(['222', '1', 'a', 'aa', 'GRCh37'], 'referenceName')
        case(['22', '1', 'a', 'aa', 'GRCh37', 'Tru'], 'includeDatasetResponses')
        case(['22', '1', 'a', 'aa', 'GRCh37', 'falsee'], 'includeDatasetResponses')
        case(['22', '1', 'a', 'aa', 'GRCh37', '1'], 'includeDatasetResponses')
        case(['22', '1', 'a', 'aa', 'GRCh37', '0'], 'includeDatasetResponses')

    @staticmethod
    def test_build_query():
        """`build_query` function should return correct URL"""
        reference_name = '3'
        start = '100000'
        reference_bases = 'aat'
        alternate_bases = 'taa'

        request = data_types.create_beacon_allele_request(referenceName=reference_name,
                                                          start=start,
                                                          referenceBases=reference_bases,
                                                          alternateBases=alternate_bases,
                                                          assemblyId='GRCh37')

        url = middleware.build_query(**request)

        import urllib.parse
        result = urllib.parse.urlsplit(url)
        assert result[2], "URL should contain hierarchical path"

        assert reference_name in url, "`reference name` should be in built query"
        assert start in url, "`start` should be in built query"
        assert reference_bases in url, "`reference bases` should be in built query"
        assert alternate_bases in url, "`alternate bases` should be in built query"

    def test_build_urls(self):
        """`build_urls` function should return correct URLs"""
        reference_name = '3'
        start = '100000'
        reference_bases = 'aat'
        alternate_bases = 'taa'

        request = data_types.create_beacon_allele_request(referenceName=reference_name,
                                                          start=start,
                                                          referenceBases=reference_bases,
                                                          alternateBases=alternate_bases,
                                                          assemblyId='GRCh37')

Jacek Lebioda's avatar
Jacek Lebioda committed
100
        urls = middleware.build_urls(self.configuration.DatasetConnections, **request)
101
102
103
104
105
106
107

        import urllib.parse
        for url in urls:
            result = urllib.parse.urlsplit(url)
            assert result[0], "URL should contain http or https schema"
            assert result[1], "URL should contain network location part"
            assert result[2], "URL should contain hierarchical path"