summaryrefslogtreecommitdiffstats
path: root/python/mozperftest/mozperftest/tests/test_perftestnotebook.py
blob: 55c3acabe178bdbd60bb54e6e5f87c2a2b25f5fc (plain)
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
#!/usr/bin/env python
from pathlib import Path
from unittest import mock

import mozunit
import pytest

from mozperftest.metrics.notebook.constant import Constant


def test_init(ptnb, standarized_data):
    assert isinstance(ptnb.data, dict)
    assert isinstance(ptnb.const, Constant)


def test_get_notebook_section(ptnb):
    func = "scatterplot"
    with (ptnb.const.here / "notebook-sections" / func).open() as f:
        assert ptnb.get_notebook_section(func) == f.read()


def test_get_notebook_section_unknown_analysis(ptnb):
    func = "unknown"
    assert ptnb.get_notebook_section(func) == ""


@pytest.mark.parametrize("analysis", [["scatterplot"], None])
def test_post_to_iodide(ptnb, standarized_data, analysis):

    opener = mock.mock_open()

    def mocked_open(self, *args, **kwargs):
        return opener(self, *args, **kwargs)

    with mock.patch.object(Path, "open", mocked_open), mock.patch(
        "mozperftest.metrics.notebook.perftestnotebook.webbrowser.open_new_tab"
    ) as browser, mock.patch(
        "mozperftest.metrics.notebook.perftestnotebook.HTTPServer"
    ) as server:
        ptnb.post_to_iodide(analysis=analysis)

        list_of_calls = opener.mock_calls

        header_path = ptnb.const.here / "notebook-sections" / "header"
        assert mock.call(header_path) in list_of_calls
        index1 = list_of_calls.index(mock.call(header_path))
        assert list_of_calls[index1 + 2] == mock.call().read()

        template_upload_file_path = ptnb.const.here / "template_upload_file.html"
        assert mock.call(template_upload_file_path) in list_of_calls
        index2 = list_of_calls.index(mock.call(template_upload_file_path))
        assert list_of_calls[index2 + 2] == mock.call().read()

        upload_file_path = ptnb.const.here / "upload_file.html"
        assert mock.call(upload_file_path, "w") in list_of_calls
        index3 = list_of_calls.index(mock.call(upload_file_path, "w"))
        assert list_of_calls[index3 + 2] == mock.call().write("")

        assert index1 < index2 < index3

        if analysis:
            section_path = ptnb.const.here / "notebook-sections" / analysis[0]
            assert mock.call(section_path) in list_of_calls
            index4 = list_of_calls.index(mock.call(section_path))
            assert index1 < index4 < index2
        else:
            assert list_of_calls.count(mock.call().__enter__()) == 3

        browser.assert_called_with(str(upload_file_path))
        server.assert_has_calls(
            [mock.call().serve_forever(), mock.call().server_close()]
        )


if __name__ == "__main__":
    mozunit.main()