Skip to content

Commit aea43e3

Browse files
committed
fix tests
1 parent 0c938ca commit aea43e3

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

tests/test_credentials.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_from_env(self):
1818
"client_id": "test_client_id",
1919
"client_secret": "test_secret",
2020
"login_url": "https://test.login.url",
21+
"dataspace": "test_dataspace", # Added dataspace
2122
}
2223

2324
with patch.dict(
@@ -30,6 +31,30 @@ def test_from_env(self):
3031
assert creds.client_id == test_creds["client_id"]
3132
assert creds.client_secret == test_creds["client_secret"]
3233
assert creds.login_url == test_creds["login_url"]
34+
assert creds.dataspace == test_creds["dataspace"] # Added dataspace assertion
35+
36+
def test_from_env_without_dataspace(self):
37+
"""Test loading credentials from environment variables without dataspace."""
38+
test_creds = {
39+
"username": "test_user",
40+
"password": "test_pass",
41+
"client_id": "test_client_id",
42+
"client_secret": "test_secret",
43+
"login_url": "https://test.login.url",
44+
}
45+
46+
# Create env dict without dataspace
47+
env_dict = {v: test_creds[k] for k, v in ENV_CREDENTIALS.items() if k != "dataspace"}
48+
49+
with patch.dict(os.environ, env_dict, clear=True):
50+
creds = Credentials.from_env()
51+
52+
assert creds.username == test_creds["username"]
53+
assert creds.password == test_creds["password"]
54+
assert creds.client_id == test_creds["client_id"]
55+
assert creds.client_secret == test_creds["client_secret"]
56+
assert creds.login_url == test_creds["login_url"]
57+
assert creds.dataspace is None # Should default to None
3358

3459
def test_from_env_missing_vars(self):
3560
"""Test that missing environment variables raise appropriate error."""
@@ -47,13 +72,15 @@ def test_from_ini(self):
4772
client_id = ini_client_id
4873
client_secret = ini_secret
4974
login_url = https://ini.login.url
75+
dataspace = ini_dataspace
5076
5177
[other_profile]
5278
username = other_user
5379
password = other_pass
5480
client_id = other_client_id
5581
client_secret = other_secret
5682
login_url = https://other.login.url
83+
dataspace = other_dataspace
5784
"""
5885

5986
with (
@@ -73,6 +100,7 @@ def test_from_ini(self):
73100
assert creds.client_id == "ini_client_id"
74101
assert creds.client_secret == "ini_secret"
75102
assert creds.login_url == "https://ini.login.url"
103+
assert creds.dataspace == "ini_dataspace"
76104

77105
# Test other profile
78106
creds = Credentials.from_ini(
@@ -83,6 +111,7 @@ def test_from_ini(self):
83111
assert creds.client_id == "other_client_id"
84112
assert creds.client_secret == "other_secret"
85113
assert creds.login_url == "https://other.login.url"
114+
assert creds.dataspace == "other_dataspace"
86115

87116
def test_from_available_env(self):
88117
"""Test that from_available uses environment variables when available."""
@@ -92,6 +121,7 @@ def test_from_available_env(self):
92121
"client_id": "test_client_id",
93122
"client_secret": "test_secret",
94123
"login_url": "https://test.login.url",
124+
"dataspace": "test_dataspace", # Added dataspace
95125
}
96126

97127
with (
@@ -107,6 +137,7 @@ def test_from_available_env(self):
107137
assert creds.client_id == test_creds["client_id"]
108138
assert creds.client_secret == test_creds["client_secret"]
109139
assert creds.login_url == test_creds["login_url"]
140+
assert creds.dataspace == test_creds["dataspace"] # Added dataspace assertion
110141

111142
def test_from_available_ini(self):
112143
"""Test that from_available uses INI file when env vars not available."""
@@ -117,6 +148,7 @@ def test_from_available_ini(self):
117148
client_id = ini_client_id
118149
client_secret = ini_secret
119150
login_url = https://ini.login.url
151+
dataspace = ini_dataspace
120152
"""
121153

122154
with (
@@ -137,6 +169,7 @@ def test_from_available_ini(self):
137169
assert creds.client_id == "ini_client_id"
138170
assert creds.client_secret == "ini_secret"
139171
assert creds.login_url == "https://ini.login.url"
172+
assert creds.dataspace == "ini_dataspace"
140173

141174
def test_from_available_no_creds(self):
142175
"""Test that from_available raises error when no credentials are found."""

tests/test_credentials_profile_integration.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_query_api_reader_with_custom_profile(self):
3232
mock_credentials.password = "custom_password"
3333
mock_credentials.client_id = "custom_client_id"
3434
mock_credentials.client_secret = "custom_secret"
35+
mock_credentials.dataspace = "custom_dataspace" # Added dataspace
3536
mock_from_available.return_value = mock_credentials
3637

3738
# Mock the SalesforceCDPConnection
@@ -49,13 +50,56 @@ def test_query_api_reader_with_custom_profile(self):
4950
# Verify the correct profile was used
5051
mock_from_available.assert_called_with(profile="custom_profile")
5152

52-
# Verify the connection was created with the custom credentials
53+
# Verify the connection was created with the custom credentials including dataspace
5354
mock_conn_class.assert_called_once_with(
5455
"https://custom.salesforce.com",
5556
"custom@example.com",
5657
"custom_password",
5758
"custom_client_id",
5859
"custom_secret",
60+
dataspace="custom_dataspace", # Added dataspace parameter
61+
)
62+
63+
def test_query_api_reader_without_dataspace(self):
64+
"""Test QueryAPIDataCloudReader works when dataspace is None."""
65+
mock_spark = MagicMock()
66+
67+
with patch(
68+
"datacustomcode.credentials.Credentials.from_available"
69+
) as mock_from_available:
70+
# Mock credentials without dataspace
71+
mock_credentials = MagicMock()
72+
mock_credentials.login_url = "https://custom.salesforce.com"
73+
mock_credentials.username = "custom@example.com"
74+
mock_credentials.password = "custom_password"
75+
mock_credentials.client_id = "custom_client_id"
76+
mock_credentials.client_secret = "custom_secret"
77+
mock_credentials.dataspace = None # No dataspace
78+
mock_from_available.return_value = mock_credentials
79+
80+
# Mock the SalesforceCDPConnection
81+
with patch(
82+
"datacustomcode.io.reader.query_api.SalesforceCDPConnection"
83+
) as mock_conn_class:
84+
mock_conn = MagicMock()
85+
mock_conn_class.return_value = mock_conn
86+
87+
# Test with custom profile
88+
QueryAPIDataCloudReader(
89+
mock_spark, credentials_profile="custom_profile"
90+
)
91+
92+
# Verify the correct profile was used
93+
mock_from_available.assert_called_with(profile="custom_profile")
94+
95+
# Verify the connection was created WITHOUT dataspace parameter when None
96+
mock_conn_class.assert_called_once_with(
97+
"https://custom.salesforce.com",
98+
"custom@example.com",
99+
"custom_password",
100+
"custom_client_id",
101+
"custom_secret",
102+
# No dataspace parameter when None
59103
)
60104

61105
def test_print_writer_with_custom_profile(self):

0 commit comments

Comments
 (0)