Skip to content

Commit

Permalink
✅ created salesforce unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego-H-S committed Sep 18, 2024
1 parent 065f5fb commit 3d72ecf
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ repos:
hooks:
- id: prettier

- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]
# - repo: https://github.com/Yelp/detect-secrets
# rev: v1.5.0
# hooks:
# - id: detect-secrets
# args: ["--baseline", ".secrets.baseline"]
166 changes: 166 additions & 0 deletions tests/unit/test_salesforce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"""'test_salesforce.py'."""

import pytest
from simple_salesforce import Salesforce

from viadot.exceptions import CredentialError
from viadot.sources import SalesForce
from viadot.sources.salesforce import SalesForceCredentials

variables = {
"credentials": {
"username": "test_user",
"password": "test_password",
"token": "test_token",
},
"records_1": [
{
"Id": "001",
"Name": "Test Record",
"attributes": {
"type": "Account",
"url": "/services/data/v50.0/sobjects/Account/001",
},
},
],
"records_2": [
{
"Id": "001",
"Name": "Test Record",
"attributes": {
"type": "Account",
"url": "/services/data/v50.0/sobjects/Account/001",
},
},
],
"data": [
{"Id": "001", "Name": "Test Record"},
{"Id": "002", "Name": "Another Record"},
],
}


@pytest.mark.basic()
def test_salesforce_init_dev_env(mocker):
"""Test SalesForce, starting in dev mode."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
sf_instance = SalesForce(credentials=variables["credentials"], env="DEV")

assert sf_instance.salesforce == mock_sf_instance


class TestSalesForceCredentials:
"""Test SalesForce Credentials Class."""

@pytest.mark.basic()
def test_salesforce_credentials(self):
"""Test SalesForce credentials."""
SalesForceCredentials(
username="test_user",
password="test_password",
token="test_token",
)


@pytest.mark.basic()
def test_salesforce_init_prod_env(mocker):
"""Test SalesForce, starting in prod mode."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
sf_instance = SalesForce(credentials=variables["credentials"], env="PROD")

assert sf_instance.salesforce == mock_sf_instance


@pytest.mark.basic()
def test_salesforce_invalid_env():
"""Test SalesForce, invalid `env` parameter."""
with pytest.raises(
ValueError, match="The only available environments are DEV, QA, and PROD."
):
SalesForce(credentials=variables["credentials"], env="INVALID")


@pytest.mark.basic()
def test_salesforce_missing_credentials():
"""Test SalesForce missing credentials."""
incomplete_creds = {"username": "user", "password": "pass"}
with pytest.raises(CredentialError):
SalesForce(credentials=incomplete_creds)


@pytest.mark.connect()
def test_salesforce_api_connection(mocker):
"""Test SalesForce `api_connection` method with a query."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
salesforce_instance = SalesForce(credentials=variables["credentials"])

mock_sf_instance.query.return_value = {"records": variables["records_1"]}

salesforce_instance.api_connection(query="SELECT Id, Name FROM Account")

assert salesforce_instance.data == [{"Id": "001", "Name": "Test Record"}]
mock_sf_instance.query.assert_called_once_with("SELECT Id, Name FROM Account")


@pytest.mark.connect()
def test_salesforce_api_connection_with_columns(mocker):
"""Test SalesForce `api_connection` method with columns."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
salesforce_instance = SalesForce(credentials=variables["credentials"])

mock_sf_instance.query.return_value = {"records": variables["records_2"]}

salesforce_instance.api_connection(table="Account", columns=["Id", "Name"])

assert salesforce_instance.data == [{"Id": "001", "Name": "Test Record"}]
mock_sf_instance.query.assert_called_once_with("SELECT Id, Name FROM Account")


@pytest.mark.functions()
def test_salesforce_to_df(mocker):
"""Test SalesForce `to_df` method."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
salesforce_instance = SalesForce(credentials=variables["credentials"])
salesforce_instance.data = variables["data"]

df = salesforce_instance.to_df()

assert not df.empty
assert df.shape == (2, 4)
assert list(df.columns) == [
"Id",
"Name",
"_viadot_source",
"_viadot_downloaded_at_utc",
]
assert df.iloc[0]["Id"] == "001"


@pytest.mark.functions()
def test_salesforce_to_df_empty_data(mocker):
"""Test SalesForce `to_df` method with empty df."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
salesforce_instance = SalesForce(credentials=variables["credentials"])
salesforce_instance.data = []

with pytest.raises(ValueError, match="The response does not contain any data."):
salesforce_instance.to_df(if_empty="fail")


@pytest.mark.functions()
def test_salesforce_to_df_warn_empty_data(mocker):
"""Test SalesForce `to_df` method with empty df, warn."""
mock_sf_instance = mocker.MagicMock(spec=Salesforce)
mocker.patch("viadot.sources.salesforce.Salesforce", return_value=mock_sf_instance)
salesforce_instance = SalesForce(credentials=variables["credentials"])
salesforce_instance.data = []

df = salesforce_instance.to_df(if_empty="warn")

assert df.empty

0 comments on commit 3d72ecf

Please sign in to comment.