test_statistics_controllers.py 5.23 KB
Newer Older
1
2
3
"""
Tests for the search statistics API
"""
David Mendez's avatar
David Mendez committed
4
5
import datetime
import json
6
import unittest
David Mendez's avatar
David Mendez committed
7

8
from app import create_app
9
from app.authorisation import token_generator
10
11
from app.db import DB
from app.models import delayed_job_models
12
13


David Mendez's avatar
David Mendez committed
14
# pylint: disable=too-many-locals,no-member
15
class TestStatus(unittest.TestCase):
David Mendez's avatar
David Mendez committed
16
17
18
    """
    jljl
    """
19
20
21
22
23
24
25
26
27
    def setUp(self):
        self.flask_app = create_app()
        self.client = self.flask_app.test_client()

    def tearDown(self):
        with self.flask_app.app_context():
            delayed_job_models.delete_all_jobs()

    def test_cannot_save_statistics_with_invalid_token(self):
David Mendez's avatar
David Mendez committed
28
        """
29

David Mendez's avatar
David Mendez committed
30
31
        :return:
        """
32
33
34
35
36
37
        with self.flask_app.app_context():
            statistics = {
                'total_items': 100,
                'file_size': 100
            }

38
            token = token_generator.generate_job_token('another_id')
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
            headers = {
                'X-JOB-KEY': token
            }

            client = self.client
            response = client.post('record/search/some_job_id', data=statistics, headers=headers)
            self.assertEqual(response.status_code, 401,
                             msg='I should not be authorised to save statistics for a job with an invalid token')

    def test_job_id_must_exist(self):
        """
        Tests that a job id must exist when saving statistics for it
        :return:
        """

        with self.flask_app.app_context():
            statistics = {
                'total_items': 100,
                'file_size': 100
            }

60
            token = token_generator.generate_job_token('some_job_id')
61
62
63
64
65
66
67
68
            headers = {
                'X-JOB-KEY': token
            }

            client = self.client
            response = client.post('record/search/some_job_id', data=statistics, headers=headers)

            self.assertEqual(response.status_code, 404,
69
70
71
                             msg='I should not be able to save statistics for a job that does not exist')

    def test_job_must_be_finished_to_save_statistics(self):
David Mendez's avatar
David Mendez committed
72
73
74
75
        """
        hkhkhkhk
        :return:
        """
76
77
78
        with self.flask_app.app_context():
            job_type = delayed_job_models.JobTypes.SIMILARITY
            params = {
79
                'search_type': str(delayed_job_models.JobTypes.SIMILARITY),
80
81
82
83
84
85
86
87
88
89
90
91
                'structure': '[H]C1(CCCN1C(=N)N)CC1=NC(=NO1)C1C=CC(=CC=1)NC1=NC(=CS1)C1C=CC(Br)=CC=1',
                'threshold': '70'
            }

            with self.flask_app.app_context():
                job_must_be = delayed_job_models.get_or_create(job_type, params)

                statistics = {
                    'total_items': 100,
                    'file_size': 100
                }

92
                token = token_generator.generate_job_token(job_must_be.id)
93
94
95
96
97
98
99
                headers = {
                    'X-JOB-KEY': token
                }

                client = self.client
                response = client.post(f'record/search/{job_must_be.id}', data=statistics, headers=headers)
                self.assertEqual(response.status_code, 412, msg='The job must be finished before saving statistics')
100

101
    def test_saves_statistics_for_search_job(self):
David Mendez's avatar
David Mendez committed
102
103
104
105
        """
        hkhjk
        :return:
        """
106
107
108
        with self.flask_app.app_context():
            job_type = delayed_job_models.JobTypes.SIMILARITY
            params = {
109
                'search_type': str(delayed_job_models.JobTypes.SIMILARITY),
110
111
112
113
114
115
116
117
118
119
120
                'structure': '[H]C1(CCCN1C(=N)N)CC1=NC(=NO1)C1C=CC(=CC=1)NC1=NC(=CS1)C1C=CC(Br)=CC=1',
                'threshold': '70'
            }

            with self.flask_app.app_context():
                time_taken_must_be = 10
                job_must_be = delayed_job_models.get_or_create(job_type, params)
                job_must_be.started_at = datetime.datetime.utcnow()
                job_must_be.finished_at = job_must_be.started_at + datetime.timedelta(seconds=time_taken_must_be)

                job_must_be.status = delayed_job_models.JobStatuses.FINISHED
David Mendez's avatar
David Mendez committed
121
                DB.session.commit()
122
123
124
125
126
127

                statistics = {
                    'total_items': 100,
                    'file_size': 100
                }

128
                token = token_generator.generate_job_token(job_must_be.id)
129
130
131
132
133
134
135
136
137
138
139
140
                headers = {
                    'X-JOB-KEY': token
                }

                client = self.client
                response = client.post(f'record/search/{job_must_be.id}', data=statistics, headers=headers)

                data_got = json.loads(response.data.decode('UTF-8'))
                time_taken_got = data_got.get('time_taken')
                self.assertEqual(time_taken_got, time_taken_must_be, msg='The time taken was not calculated correctly')

                search_type_must_be = str(job_must_be.type)
141
                search_type_got = data_got.get('search_type')
142
143
144
145
146
147
                self.assertEqual(search_type_must_be, search_type_got,
                                 msg='The search type is wrong!')

                request_date_must_be = job_must_be.created_at.timestamp()
                request_date_got = data_got.get('request_date')
                self.assertAlmostEqual(request_date_must_be, request_date_got, places=1,
David Mendez's avatar
David Mendez committed
148
                                       msg='The request date was not calculated correctly')