Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Salesforce migration #629

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3615647
✨ Migrated salesforce source
djagoda881 Feb 15, 2023
66d9063
🔥 Removed object instance
djagoda881 Feb 15, 2023
7951428
✨ Added slaesforce source to __init__
djagoda881 Feb 15, 2023
d08d07f
✅ Added unit tests to salesfource source
djagoda881 Feb 21, 2023
8b99f6a
✅ Updated test structure
djagoda881 Feb 21, 2023
773797d
✅ Migrated unit tests from viadot 1.0
djagoda881 Feb 23, 2023
7fc3688
🔐 Updated name of salesforce credentials
djagoda881 Feb 23, 2023
ee468c9
🔥 Removed if_empty parameter
djagoda881 Mar 2, 2023
4046167
✅ Updated unit tests structure
djagoda881 Mar 2, 2023
bf79a66
📝 Updated main class docstring
djagoda881 Mar 2, 2023
a3fea0e
📝 Added docstring to Slesforce class functions
djagoda881 Mar 2, 2023
f976ecd
🎨 Changed name of 'code' var to 'valid_response_code'
djagoda881 Mar 2, 2023
f5deb9a
🎨 Changed name of 'response' var to 'response_code'
djagoda881 Mar 2, 2023
ddee24f
🔥 Removed response_code var declaration
djagoda881 Mar 3, 2023
0e07e8d
🎨 Improved connetion with env
djagoda881 Mar 6, 2023
3aaad06
💡 Updated comment in download fuction
djagoda881 Mar 6, 2023
d0f0416
🔐 Updated config_key names
djagoda881 Mar 6, 2023
63913e0
Revert "🔐 Updated config_key names"
djagoda881 Mar 6, 2023
a8fa335
🔐 Updated config_key name after git revert
djagoda881 Mar 6, 2023
b408db7
✅ Improved cleaning after tests
djagoda881 Mar 6, 2023
ab1d1af
✅ 🔥 Removed test `test_upsert_empty`
djagoda881 Mar 6, 2023
de73e4c
✅ 🎨Improved structure of upsert test
djagoda881 Mar 6, 2023
61bf161
✅🎨Improved structure of test_to_df
djagoda881 Mar 6, 2023
47e177e
✅ 🎨 Improved structure of `test_upsert`
djagoda881 Mar 6, 2023
3e2ef08
✅ 🎨 Changed name of variable
djagoda881 Mar 6, 2023
97e6dbb
🎨 Improved retrieving externalID
djagoda881 Mar 6, 2023
5625cee
⏪ Removed tests code
djagoda881 Mar 6, 2023
4215dea
✅ Chenged testing structure in upsert tests
djagoda881 Mar 9, 2023
b6e311e
🎨Rename `external_id` var to `external_id_column`
djagoda881 Mar 9, 2023
c8bdf4e
✅ Added inserting two rows
djagoda881 Mar 9, 2023
5adfbe2
✅ Improved `test_to_df`
djagoda881 Mar 9, 2023
570b579
✅📝Improved docstings in Salesforce class
djagoda881 Mar 9, 2023
4415009
🐛 Corrected variable name
djagoda881 Mar 10, 2023
990775a
✅ Added bulk upsert test and refeactored tests
djagoda881 Mar 10, 2023
03d07c4
📝 Updated docstrings in Salesforce source
djagoda881 Mar 20, 2023
7885ba7
✅ Removed try except from tests
djagoda881 Mar 21, 2023
ea0fa2a
✅ Renamed test and fixed typos
djagoda881 Mar 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `ExchangeRates` source to the library.
- Added `from_df()` method to `Azure Data Lake` source
- Added `SAPRFC` source to the library.
- Added `Salesforce` source to the library.

### Changed
- Added `SQLServerToDF` task
Expand Down
184 changes: 184 additions & 0 deletions tests/unit/test_salesforce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import pandas as pd
import pytest
from viadot.sources import Salesforce

TABLE_TO_DOWNLOAD = "Account"
TABLE_TO_UPSERT = "Contact"
TEST_LAST_NAME = "viadot-test"
trymzet marked this conversation as resolved.
Show resolved Hide resolved

EXPECTED_VALUE = pd.DataFrame(
{"LastName": ["salesforce-test"], "SAPContactId__c": ["8811111"]}
)

TEST_ROW = {"LastName": "salesforce-test", "SAPContactId__c": "8811111"}

TWO_TEST_ROWS_INSERT = {
"LastName": ["viadot-insert-1", "viadot-insert-2"],
"SAPContactId__c": ["8812000", "8812100"],
}

TWO_TEST_ROWS_UPDATE = {
"LastName": ["viadot-update-1", "viadot-update-2"],
"SAPContactId__c": ["8812000", "8812100"],
}


def get_tested_records(salesforce, multiple_rows=False):
sf = salesforce.salesforce
if multiple_rows:
result = sf.query(
f"SELECT Id, LastName FROM {TABLE_TO_UPSERT} WHERE SAPContactId__c in ('8812000','8812100')"
)
else:
result = sf.query(
f"SELECT Id, LastName FROM {TABLE_TO_UPSERT} WHERE SAPContactId__c='8811111'"
)

return result["records"]


@pytest.fixture(scope="session")
def salesforce():

s = Salesforce(config_key="salesforce_dev")

yield s

sf = s.salesforce
result = sf.query(
f"SELECT Id FROM {TABLE_TO_UPSERT} WHERE SAPContactId__c in ('8812000','8812100','8811111') "
)

# Deletes test rows from Salesforce if any remain
nr_rows = len(result["records"])
for nr in range(nr_rows):
sf.Contact.delete(result["records"][nr]["Id"])


def test_upsert_row_id(salesforce):

assert not get_tested_records(salesforce)

sf = salesforce.salesforce
sf.Contact.create(TEST_ROW)

data = {
"Id": [get_tested_records(salesforce)[0]["Id"]],
"LastName": [TEST_LAST_NAME],
}
df = pd.DataFrame(data=data)

try:
salesforce.upsert(df=df, table=TABLE_TO_UPSERT)
except Exception as exception:
raise exception

updated_row = get_tested_records(salesforce)

assert updated_row[0]["LastName"] == TEST_LAST_NAME

sf.Contact.delete(updated_row[0]["Id"])


def test_upsert(salesforce):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this and test_upsert_row_id()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In test_upsert it performs upsert operations based on the external_id. In this test, performs an upsert operation based on the Id of the row (the Id is allocated when the new row is created )


assert not get_tested_records(salesforce, multiple_rows=True)

df = pd.DataFrame(data=TWO_TEST_ROWS_INSERT)

try:
salesforce.upsert(df=df, table="Contact", external_id_column="SAPContactId__c")
except Exception as exception:
raise exception

inserted_rows = get_tested_records(salesforce, multiple_rows=True)
assert inserted_rows[0]["LastName"] == "viadot-insert-1"
assert inserted_rows[1]["LastName"] == "viadot-insert-2"

df = pd.DataFrame(data=TWO_TEST_ROWS_UPDATE)

try:
salesforce.upsert(df=df, table="Contact", external_id_column="SAPContactId__c")
except Exception as exception:
raise exception

updated_rows = get_tested_records(salesforce, multiple_rows=True)
assert updated_rows[0]["LastName"] == "viadot-update-1"
assert updated_rows[1]["LastName"] == "viadot-update-2"

sf = salesforce.salesforce
sf.Contact.delete(inserted_rows[0]["Id"])
sf.Contact.delete(inserted_rows[1]["Id"])


def test_bulk_upsert(salesforce):

assert not get_tested_records(salesforce, multiple_rows=True)

df_insert = pd.DataFrame(data=TWO_TEST_ROWS_INSERT)

try:
salesforce.bulk_upsert(
df=df_insert, table="Contact", external_id_column="SAPContactId__c"
)
except Exception as exception:
raise exception

inserted_rows = get_tested_records(salesforce, multiple_rows=True)

assert inserted_rows[0]["LastName"] == "viadot-insert-1"
assert inserted_rows[1]["LastName"] == "viadot-insert-2"

df_update = pd.DataFrame(data=TWO_TEST_ROWS_UPDATE)

try:
salesforce.bulk_upsert(
df=df_update, table="Contact", external_id_column="SAPContactId__c"
)
except Exception as exception:
raise exception

updated_rows = get_tested_records(salesforce, multiple_rows=True)
assert updated_rows[0]["LastName"] == "viadot-update-1"
assert updated_rows[1]["LastName"] == "viadot-update-2"

sf = salesforce.salesforce
sf.Contact.delete(updated_rows[0]["Id"])
sf.Contact.delete(updated_rows[1]["Id"])


def test_upsert_external_id_column_wrong(salesforce):
data = {
"LastName": [TEST_LAST_NAME],
"SAPContactId__c": ["8811111"],
}
df = pd.DataFrame(data=data)
with pytest.raises(ValueError):
salesforce.upsert(df=df, table=TABLE_TO_UPSERT, external_id_column="SAPId")


def test_download_no_query(salesforce):
records = salesforce.download(table=TABLE_TO_DOWNLOAD)
assert len(records) > 0


def test_download_with_query(salesforce):
query = f"SELECT Id, Name FROM {TABLE_TO_DOWNLOAD}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add some limit to the query to not download the whole table?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

records = salesforce.download(query=query)
assert len(records) > 0


def test_to_df(salesforce):

assert not get_tested_records(salesforce)

sf = salesforce.salesforce
sf.Contact.create(TEST_ROW)

df = salesforce.to_df(
query=f"SELECT LastName, SAPContactId__c FROM {TABLE_TO_UPSERT} WHERE SAPContactId__c='8811111'"
)

assert df.equals(EXPECTED_VALUE)

sf.Contact.delete(get_tested_records(salesforce)[0]["Id"])
1 change: 1 addition & 0 deletions viadot/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from .s3 import S3
from .sharepoint import Sharepoint
from .redshift_spectrum import RedshiftSpectrum
from .salesforce import Salesforce
Loading