Debian Codegraph @

tests/test_publish_heroku.py

raw
1 from click.testing import CliRunner
2 from datasette import cli
3 from unittest import mock
4 import os
5 import pathlib
6 import pytest
7
8
9 @pytest.mark.serial
10 @mock.patch("shutil.which")
11 def test_publish_heroku_requires_heroku(mock_which, tmp_path_factory):
12 mock_which.return_value = False
13 runner = CliRunner()
14 os.chdir(tmp_path_factory.mktemp("runner"))
15 with open("test.db", "w") as fp:
16 fp.write("data")
17 result = runner.invoke(cli.cli, ["publish", "heroku", "test.db"])
18 assert result.exit_code == 1
19 assert "Publishing to Heroku requires heroku" in result.output
20
21
22 @pytest.mark.serial
23 @mock.patch("shutil.which")
24 @mock.patch("datasette.publish.heroku.check_output")
25 @mock.patch("datasette.publish.heroku.call")
26 def test_publish_heroku_installs_plugin(
27 mock_call, mock_check_output, mock_which, tmp_path_factory
28 ):
29 mock_which.return_value = True
30 mock_check_output.side_effect = lambda s: {"['heroku', 'plugins']": b""}[repr(s)]
31 runner = CliRunner()
32 os.chdir(tmp_path_factory.mktemp("runner"))
33 with open("t.db", "w") as fp:
34 fp.write("data")
35 result = runner.invoke(cli.cli, ["publish", "heroku", "t.db"], input="y\n")
36 assert 0 != result.exit_code
37 mock_check_output.assert_has_calls(
38 [mock.call(["heroku", "plugins"]), mock.call(["heroku", "apps:list", "--json"])]
39 )
40 mock_call.assert_has_calls(
41 [mock.call(["heroku", "plugins:install", "heroku-builds"])]
42 )
43
44
45 @mock.patch("shutil.which")
46 def test_publish_heroku_invalid_database(mock_which):
47 mock_which.return_value = True
48 runner = CliRunner()
49 result = runner.invoke(cli.cli, ["publish", "heroku", "woop.db"])
50 assert result.exit_code == 2
51 assert "Path 'woop.db' does not exist" in result.output
52
53
54 @pytest.mark.serial
55 @mock.patch("shutil.which")
56 @mock.patch("datasette.publish.heroku.check_output")
57 @mock.patch("datasette.publish.heroku.call")
58 def test_publish_heroku(mock_call, mock_check_output, mock_which, tmp_path_factory):
59 mock_which.return_value = True
60 mock_check_output.side_effect = lambda s: {
61 "['heroku', 'plugins']": b"heroku-builds",
62 "['heroku', 'apps:list', '--json']": b"[]",
63 "['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}',
64 }[repr(s)]
65 runner = CliRunner()
66 os.chdir(tmp_path_factory.mktemp("runner"))
67 with open("test.db", "w") as fp:
68 fp.write("data")
69 result = runner.invoke(cli.cli, ["publish", "heroku", "test.db", "--tar", "gtar"])
70 assert 0 == result.exit_code, result.output
71 mock_call.assert_has_calls(
72 [
73 mock.call(
74 [
75 "heroku",
76 "builds:create",
77 "-a",
78 "f",
79 "--include-vcs-ignore",
80 "--tar",
81 "gtar",
82 ]
83 ),
84 ]
85 )
86
87
88 @pytest.mark.serial
89 @mock.patch("shutil.which")
90 @mock.patch("datasette.publish.heroku.check_output")
91 @mock.patch("datasette.publish.heroku.call")
92 def test_publish_heroku_plugin_secrets(
93 mock_call, mock_check_output, mock_which, tmp_path_factory
94 ):
95 mock_which.return_value = True
96 mock_check_output.side_effect = lambda s: {
97 "['heroku', 'plugins']": b"heroku-builds",
98 "['heroku', 'apps:list', '--json']": b"[]",
99 "['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}',
100 }[repr(s)]
101 runner = CliRunner()
102 os.chdir(tmp_path_factory.mktemp("runner"))
103 with open("test.db", "w") as fp:
104 fp.write("data")
105 result = runner.invoke(
106 cli.cli,
107 [
108 "publish",
109 "heroku",
110 "test.db",
111 "--plugin-secret",
112 "datasette-auth-github",
113 "client_id",
114 "x-client-id",
115 ],
116 )
117 assert 0 == result.exit_code, result.output
118 mock_call.assert_has_calls(
119 [
120 mock.call(
121 [
122 "heroku",
123 "config:set",
124 "-a",
125 "f",
126 "DATASETTE_AUTH_GITHUB_CLIENT_ID=x-client-id",
127 ]
128 ),
129 mock.call(["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"]),
130 ]
131 )
132
133
134 @pytest.mark.serial
135 @mock.patch("shutil.which")
136 @mock.patch("datasette.publish.heroku.check_output")
137 @mock.patch("datasette.publish.heroku.call")
138 def test_publish_heroku_generate_dir(
139 mock_call, mock_check_output, mock_which, tmp_path_factory
140 ):
141 mock_which.return_value = True
142 mock_check_output.side_effect = lambda s: {
143 "['heroku', 'plugins']": b"heroku-builds",
144 }[repr(s)]
145 runner = CliRunner()
146 os.chdir(tmp_path_factory.mktemp("runner"))
147 with open("test.db", "w") as fp:
148 fp.write("data")
149 output = str(tmp_path_factory.mktemp("generate_dir") / "output")
150 result = runner.invoke(
151 cli.cli,
152 [
153 "publish",
154 "heroku",
155 "test.db",
156 "--generate-dir",
157 output,
158 ],
159 )
160 assert result.exit_code == 0
161 path = pathlib.Path(output)
162 assert path.exists()
163 file_names = {str(r.relative_to(path)) for r in path.glob("*")}
164 assert file_names == {
165 "requirements.txt",
166 "bin",
167 "runtime.txt",
168 "Procfile",
169 "test.db",
170 }
171 for name, expected in (
172 ("requirements.txt", "datasette"),
173 ("runtime.txt", "python-3.11.0"),
174 (
175 "Procfile",
176 (
177 "web: datasette serve --host 0.0.0.0 -i test.db "
178 "--cors --port $PORT --inspect-file inspect-data.json"
179 ),
180 ),
181 ):
182 with open(path / name) as fp:
183 assert fp.read().strip() == expected