forked from elastic/connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_oracle.py
118 lines (98 loc) · 3.45 KB
/
test_oracle.py
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
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License 2.0;
# you may not use this file except in compliance with the Elastic License 2.0.
#
"""Tests the Oracle Database source class methods"""
from contextlib import contextmanager
from unittest.mock import patch
import pytest
from sqlalchemy.engine import Engine
from connectors.sources.oracle import OracleClient, OracleDataSource, OracleQueries
from tests.sources.support import create_source
from tests.sources.test_generic_database import ConnectionSync
DSN = "oracle+oracledb://admin:Password_123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=9090))(CONNECT_DATA=(SID=xe)))"
@contextmanager
def oracle_client(**extras):
arguments = {
"host": "127.0.0.1",
"port": 9090,
"user": "admin",
"password": "Password_123",
"database": "xe",
"tables": "*",
"protocol": "TCP",
"oracle_home": "",
"wallet_config": "",
"logger_": None,
} | extras
client = OracleClient(**arguments)
try:
yield client
finally:
client.close()
@patch("connectors.sources.oracle.create_engine")
def test_engine_in_thin_mode(mock_fun):
"""Test engine method of OracleClient class in thin mode"""
# Setup
with oracle_client() as client:
# Execute
_ = client.engine
# Assert
mock_fun.assert_called_with(DSN)
@patch("connectors.sources.oracle.create_engine")
def test_engine_in_thick_mode(mock_fun):
"""Test engine method of OracleClient class in thick mode"""
oracle_home = "/home/devuser"
config_file_path = {"lib_dir": f"{oracle_home}/lib", "config_dir": ""}
# Setup
with oracle_client(oracle_home="/home/devuser") as client:
mock_fun.return_value = "Mock Response"
# Execute
_ = client.engine
# Assert
mock_fun.assert_called_with(DSN, thick_mode=config_file_path)
@pytest.mark.asyncio
async def test_ping():
async with create_source(OracleDataSource) as source:
with patch.object(
Engine, "connect", return_value=ConnectionSync(OracleQueries())
):
await source.ping()
@pytest.mark.asyncio
async def test_get_docs():
# Setup
async with create_source(
OracleDataSource,
username="admin",
password="changeme",
database="xe",
tables="*",
) as source:
with patch.object(
Engine, "connect", return_value=ConnectionSync(OracleQueries())
):
actual_response = []
expected_response = [
{
"emp_table_ids": 1,
"emp_table_names": "abcd",
"_id": "xe_emp_table_1_",
"_timestamp": "2023-02-21T08:37:15+00:00",
"Database": "xe",
"Table": "emp_table",
},
{
"emp_table_ids": 2,
"emp_table_names": "xyz",
"_id": "xe_emp_table_2_",
"_timestamp": "2023-02-21T08:37:15+00:00",
"Database": "xe",
"Table": "emp_table",
},
]
# Execute
async for doc in source.get_docs():
actual_response.append(doc[0])
# Assert
assert actual_response == expected_response