File: test_workers.py

package info (click to toggle)
mu-editor 1.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,492 kB
  • sloc: python: 33,326; makefile: 154; xml: 32; sh: 7
file content (157 lines) | stat: -rw-r--r-- 5,475 bytes parent folder | download
1
2
3
4
5
6
7
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
"""
Tests for UI workers to be run in separate threads.
"""
import os
from unittest import mock
from mu.interface import workers


def test_pythonanywhereworker_init():
    """
    Ensure the worker is initialised with the expected generated paths to be
    used when uploading files to PythonAnywhere.
    """
    instance = "www"
    username = "test_user"
    token = "a_test_token"
    files = {
        "app.py": "app.py",
    }
    app_name = "app"
    progress = mock.MagicMock()  # Instance of a modal progress indicator.
    paw = workers.PythonAnywhereWorker(
        instance, username, token, files, app_name, progress
    )
    assert paw.instance == instance
    assert paw.username == username
    assert paw.token == token
    assert paw.files == files
    assert paw.app_name == app_name
    assert paw.progress == progress
    assert (
        paw.url
        == "https://{instance}.pythonanywhere.com/api/v0/user/{username}/".format(
            instance=instance, username=username
        )
    )
    assert paw.files_path == "files/path/home/{username}/{app_name}/".format(
        username=username, app_name=app_name
    )
    assert (
        paw.wsgi_path
        == "/files/path/var/www/{username}_pythonanywhere_com_wsgi.py".format(
            username=username
        )
    )
    assert paw.static_path == "/home/{username}/{app_name}/static/".format(
        username=username, app_name=app_name
    )
    assert paw.wsgi_config == workers.WSGI.format(
        username=username, app_name=app_name
    )


def test_pythonanywhereworker_run_with_failure():
    """
    If an exception is raised in the "run" method, the error signal is emitted.
    """
    instance = "www"
    username = "test_user"
    token = "a_test_token"
    files = {
        "app.py": "app.py",
    }
    app_name = "app"
    progress = mock.MagicMock()  # Instance of a modal progress indicator.
    paw = workers.PythonAnywhereWorker(
        instance, username, token, files, app_name, progress
    )
    paw.error = mock.MagicMock()
    paw.finished = mock.MagicMock()
    ex = Exception("Boom!")
    with mock.patch("mu.interface.workers.requests.get", side_effect=ex):
        paw.run()
    paw.error.emit.assert_called_once_with(repr(ex))
    assert paw.finished.emit.call_count == 0


def test_pythonanywhereworker_run_to_deploy_to_main_instance():
    """
    Ensure the expected calls are made via, the requests module, to the
    PythonAnywhere API in order to deploy the application.
    """
    instance = "www"
    username = "test_user"
    token = "a_test_token"
    files = {
        # attempt to upload this test file :-)
        "app.py": os.path.abspath(__file__),
    }
    app_name = "app"
    progress = mock.MagicMock()  # Instance of a modal progress indicator.
    paw = workers.PythonAnywhereWorker(
        instance, username, token, files, app_name, progress
    )
    paw.error = mock.MagicMock()
    paw.finished = mock.MagicMock()
    mock_response = mock.MagicMock()
    mock_response.json.return_value = []  # assuming no existing app.
    mock_requests = mock.MagicMock()
    mock_requests.get.return_value = mock_response
    mock_requests.post.return_value = mock_response
    mock_requests.put.return_value = mock_response
    with mock.patch("mu.interface.workers.requests", mock_requests):
        paw.run()
    progress.setMaximum.assert_called_once_with(len(files) + 5)
    assert progress.setValue.call_count == len(files) + 5
    paw.finished.emit.assert_called_once_with("test_user.pythonanywhere.com")
    assert paw.error.emit.call_count == 0
    # Expected number of calls to API take place.
    assert mock_requests.get.call_count == 1
    assert mock_requests.put.call_count == 1
    assert mock_requests.post.call_count == 5


def test_pythonanywhereworker_run_to_deploy_to_eu_instance():
    """
    Ensure the expected calls are made via, the requests module, to the
    PythonAnywhere API in order to deploy the application to the EU zone.
    """
    instance = "eu"
    username = "test_user"
    token = "a_test_token"
    files = {
        # attempt to upload this test file :-)
        "app.py": os.path.abspath(__file__),
    }
    app_name = "app"
    progress = mock.MagicMock()  # Instance of a modal progress indicator.
    paw = workers.PythonAnywhereWorker(
        instance, username, token, files, app_name, progress
    )
    paw.error = mock.MagicMock()
    paw.finished = mock.MagicMock()
    mock_response = mock.MagicMock()
    mock_response.json.return_value = [
        # An existing domain, so no need to create app via API.
        {
            "domain_name": "test_user.eu.pythonanywhere.com",
        },
    ]
    mock_requests = mock.MagicMock()
    mock_requests.get.return_value = mock_response
    mock_requests.post.return_value = mock_response
    mock_requests.put.return_value = mock_response
    with mock.patch("mu.interface.workers.requests", mock_requests):
        paw.run()
    progress.setMaximum.assert_called_once_with(len(files) + 5)
    assert progress.setValue.call_count == len(files) + 5
    paw.finished.emit.assert_called_once_with(
        "test_user.eu.pythonanywhere.com"
    )
    assert paw.error.emit.call_count == 0
    # Expected number of calls to API take place.
    assert mock_requests.get.call_count == 1
    assert mock_requests.put.call_count == 1
    assert mock_requests.post.call_count == 3  # No need to create fresh app.