2
2
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
3
3
#
4
4
5
- from unittest import mock
6
-
7
5
import click
8
6
import pytest
9
7
from click .testing import CliRunner
@@ -17,25 +15,70 @@ def dumb(ctx):
17
15
18
16
19
17
def test_octavia (mocker ):
20
- mocker .patch .object (entrypoint , "workspace_api" )
21
- mocker .patch .object (entrypoint , "airbyte_api_client" )
22
-
18
+ mocker .patch .object (entrypoint , "click" )
19
+ mocker .patch .object (entrypoint , "get_api_client" )
20
+ mocker .patch .object (entrypoint , "get_workspace_id" , mocker .Mock (return_value = "api-defined-workspace-id" ))
21
+ mocker .patch .object (entrypoint , "check_is_initialized" , mocker .Mock (return_value = True ))
23
22
context_object = {}
24
- mock_api_instance = entrypoint .workspace_api .WorkspaceApi .return_value
25
- mock_api_instance .list_workspaces .return_value = mock .MagicMock (workspaces = [{"workspaceId" : "expected_workspace_id" }])
23
+ entrypoint .octavia .add_command (dumb )
24
+ runner = CliRunner ()
25
+ result = runner .invoke (entrypoint .octavia , ["--airbyte-url" , "test-airbyte-url" , "dumb" ], obj = context_object )
26
+ entrypoint .get_api_client .assert_called ()
27
+ entrypoint .get_workspace_id .assert_called_with (entrypoint .get_api_client .return_value , None )
28
+ expected_message = "🐙 - Octavia is targetting your Airbyte instance running at test-airbyte-url on workspace api-defined-workspace-id."
29
+ entrypoint .click .style .assert_called_with (expected_message , fg = "green" )
30
+ entrypoint .click .echo .assert_called_with (entrypoint .click .style .return_value )
31
+ assert context_object == {
32
+ "API_CLIENT" : entrypoint .get_api_client .return_value ,
33
+ "WORKSPACE_ID" : entrypoint .get_workspace_id .return_value ,
34
+ "PROJECT_IS_INITIALIZED" : entrypoint .check_is_initialized .return_value ,
35
+ }
36
+ assert result .exit_code == 0
26
37
38
+
39
+ def test_octavia_not_initialized (mocker ):
40
+ mocker .patch .object (entrypoint , "click" )
41
+ mocker .patch .object (entrypoint , "get_api_client" )
42
+ mocker .patch .object (entrypoint , "get_workspace_id" , mocker .Mock (return_value = "api-defined-workspace-id" ))
43
+ mocker .patch .object (entrypoint , "check_is_initialized" , mocker .Mock (return_value = False ))
44
+ context_object = {}
27
45
entrypoint .octavia .add_command (dumb )
28
46
runner = CliRunner ()
29
47
result = runner .invoke (entrypoint .octavia , ["--airbyte-url" , "test-airbyte-url" , "dumb" ], obj = context_object )
30
- entrypoint .airbyte_api_client .Configuration .assert_called_with (host = "test-airbyte-url/api" )
31
- entrypoint .airbyte_api_client .ApiClient .assert_called_with (entrypoint .airbyte_api_client .Configuration .return_value )
32
- entrypoint .workspace_api .WorkspaceApi .assert_called_with (entrypoint .airbyte_api_client .ApiClient .return_value )
33
- mock_api_instance .list_workspaces .assert_called_once ()
34
- assert context_object ["API_CLIENT" ] == entrypoint .airbyte_api_client .ApiClient .return_value
35
- assert context_object ["WORKSPACE_ID" ] == "expected_workspace_id"
48
+ entrypoint .click .style .assert_called_with ("🐙 - Project is not yet initialized." , fg = "red" , bold = True )
49
+ entrypoint .click .echo .assert_called_with (entrypoint .click .style .return_value )
36
50
assert result .exit_code == 0
37
51
38
52
53
+ def test_get_api_client (mocker ):
54
+ mocker .patch .object (entrypoint , "airbyte_api_client" )
55
+ mocker .patch .object (entrypoint , "check_api_health" )
56
+ api_client = entrypoint .get_api_client ("test-url" )
57
+ entrypoint .airbyte_api_client .Configuration .assert_called_with (host = "test-url/api" )
58
+ entrypoint .airbyte_api_client .ApiClient .assert_called_with (entrypoint .airbyte_api_client .Configuration .return_value )
59
+ entrypoint .check_api_health .assert_called_with (entrypoint .airbyte_api_client .ApiClient .return_value )
60
+ assert api_client == entrypoint .airbyte_api_client .ApiClient .return_value
61
+
62
+
63
+ def test_get_workspace_id_user_defined (mocker ):
64
+ mock_api_client = mocker .Mock ()
65
+ mocker .patch .object (entrypoint , "check_workspace_exists" )
66
+ mocker .patch .object (entrypoint , "workspace_api" )
67
+ assert entrypoint .get_workspace_id (mock_api_client , "user-defined-workspace-id" ) == "user-defined-workspace-id"
68
+ entrypoint .check_workspace_exists .assert_called_with (mock_api_client , "user-defined-workspace-id" )
69
+
70
+
71
+ def test_get_workspace_id_api_defined (mocker ):
72
+ mock_api_client = mocker .Mock ()
73
+ mocker .patch .object (entrypoint , "check_workspace_exists" )
74
+ mocker .patch .object (entrypoint , "workspace_api" )
75
+ mock_api_instance = entrypoint .workspace_api .WorkspaceApi .return_value
76
+ mock_api_instance .list_workspaces .return_value = mocker .Mock (workspaces = [{"workspaceId" : "api-defined-workspace-id" }])
77
+ assert entrypoint .get_workspace_id (mock_api_client , None ) == "api-defined-workspace-id"
78
+ entrypoint .workspace_api .WorkspaceApi .assert_called_with (mock_api_client )
79
+ mock_api_instance .list_workspaces .assert_called_with (_check_return_type = False )
80
+
81
+
39
82
def test_commands_in_octavia_group ():
40
83
octavia_commands = entrypoint .octavia .commands .values ()
41
84
for command in entrypoint .AVAILABLE_COMMANDS :
@@ -44,7 +87,7 @@ def test_commands_in_octavia_group():
44
87
45
88
@pytest .mark .parametrize (
46
89
"command" ,
47
- [entrypoint .init , entrypoint . apply , entrypoint .create , entrypoint .delete , entrypoint ._import ],
90
+ [entrypoint .apply , entrypoint .create , entrypoint .delete , entrypoint ._import ],
48
91
)
49
92
def test_not_implemented_commands (command ):
50
93
runner = CliRunner ()
@@ -54,4 +97,4 @@ def test_not_implemented_commands(command):
54
97
55
98
56
99
def test_available_commands ():
57
- assert entrypoint .AVAILABLE_COMMANDS == [entrypoint .list_commands ._list ]
100
+ assert entrypoint .AVAILABLE_COMMANDS == [entrypoint .list_commands ._list , entrypoint . init_commands . init ]
0 commit comments