class TestTabularReport(unittest.TestCase): maxDiff = None def setUp(self): mocks.mock_get_metadata("analytics_tabular_metadata").start() mocks.mock_login().start() self.rf = Reportforce("*****@*****.**", "1234", "XXX") def test_get_report_call_with_no_params(self, post): """With no params, we expect the params parameter in the call.""" post().json.return_value = mock_report self.rf.get_report("000O1a0940aXYhz") post.assert_called_with( URL, json=mock_metadata, params={"includeDetails": "true"}, ) def test_get_report_call_with_params(self, post): """With params, we expect them to be merged into the default.""" post().json.return_value = mock_report self.rf.get_report("000O1a0940aXYhz", params={"another": "param"}) post.assert_called_with( URL, json=mock_metadata, params={ "includeDetails": "true", "another": "param" }, )
def test_empty_summary(setup, monkeypatch): monkeypatch.setitem(SUMMARY_REPORT, "factMap", EMPTY_FACTMAP) rf = Reportforce("*****@*****.**", "1234", "token") summary_df = rf.get_report("ID", id_column="label1") assert summary_df.empty
class TestExcelWithoutFilename(unittest.TestCase): def setUp(self): mocks.mock_login().start() mocks.mock_get_metadata("analytics_tabular_metadata").start() post = patch.object(Reportforce.session, "post", **post_config) post.start() self.rf = Reportforce("*****@*****.**", "1234", "XXX") def test_get_excel_without_filename(self): """When no filename is given, the one in the headers is used.""" with patch("reportforce.report.open", mock_open, create=True): self.rf.get_report("report_id", excel=True) mock_open.assert_called_with("spreadsheet.xlsx", "wb") def test_get_excel_with_filename(self): """When a filename is given, it is used.""" with patch("reportforce.report.open", mock_open, create=True): self.rf.get_report("report_id", excel="filename.xlsx") mock_open.assert_called_with("filename.xlsx", "wb") def tearDown(self): patch.stopall()
def test_get_total_call(setup, mock_get): rf = Reportforce("*****@*****.**", "pass", "token") total = rf.get_total("ID") assert total == 30 mock_get.assert_called_once_with(EXPECTED_URL, params={"includeDetails": "false"})
def test_tabular_to_dataframe(setup, mock_generate_reports): """Test tabular report DataFrame converter.""" mock_generate_reports(TABULAR_REPORT) rf = Reportforce("*****@*****.**", "1234", "token") tabular_df = rf.get_report("ID", id_column="Opportunity Name") pd.testing.assert_frame_equal(expected_tabular_df, tabular_df)
def test_empty_matrix(setup, monkeypatch): """Test if returns an empty DataFrame when the matrix is empty.""" monkeypatch.setitem(MATRIX_REPORT, "factMap", EMPTY_FACTMAP) rf = Reportforce("*****@*****.**", "1234", "XXX") matrix_df = rf.get_report("ID") assert matrix_df.empty
def test_empty_tabular(setup, monkeypatch): """Test if DataFrame is empty when there are no rows in factMap.""" monkeypatch.setitem(TABULAR_REPORT, "factMap", EMPTY_FACTMAP) rf = Reportforce("*****@*****.**", "1234", "token") tabular_df = rf.get_report("ID", id_column="Opportunity Name") assert tabular_df.empty
def setUp(self, post): mocks.mock_get_metadata("analytics_summary_metadata").start() mocks.mock_login().start() post().json.side_effect = mocks.generate_reports(report) rf = Reportforce("*****@*****.**", "1234", "XXX") self.report = rf.get_report("report_id", id_column="label1")
def setUp(self): mocks.mock_login().start() mocks.mock_get_metadata("analytics_tabular_metadata").start() post = patch.object(Reportforce.session, "post", **post_config) post.start() self.rf = Reportforce("*****@*****.**", "1234", "XXX")
def test_get_report_no_kwargs(setup, mock_post): """Without kwargs, we expect params={'includeDetaisl': 'true'} in the POST request call.""" rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_report("ID") mock_post.assert_called_with(EXPECTED_URL, json=METADATA, params={"includeDetails": "true"})
def test_get_report_without_params_in_kwargs(setup, mock_post): """Test if includeDetails enters the call even if user did not pass it.""" rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_report("ID", timeout=30) mock_post.assert_called_with(EXPECTED_URL, json=METADATA, timeout=30, params={"includeDetails": "true"})
def test_get_report_with_kwargs(setup, mock_post): """Test if kwargs provided by user overwrite defaults.""" rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_report("ID", timeout=30, params={"includeDetails": "false"}) mock_post.assert_called_with(EXPECTED_URL, json=METADATA, timeout=30, params={"includeDetails": "false"})
def test_summary_to_dataframe(setup, mock_generate_reports): """Test summary report into DataFrame converter.""" mock_generate_reports(SUMMARY_REPORT) rf = Reportforce("*****@*****.**", "1234", "token") summary_df = rf.get_report("ID", id_column="label1") summary_df.to_pickle("tests/data/summary_df.pickle") pd.testing.assert_frame_equal(expected_summary_df, summary_df)
def test_excel_headers(mock_login): rf = Reportforce("*****@*****.**", "1234", "token") assert rf._get_excel_headers() == { "User-Agent": "python-requests/2.23.0", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Connection": "keep-alive", "Authorization": "Bearer sessionId", }
def test_get_metadata(mock_login, mocker): mock_get_metadata = mocker.patch.object(Reportforce.session, "get") EXPECTED_URL = ("https://www.salesforce.com/" "services/data/v47.0/analytics" "/reports/ID/describe") rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_metadata("ID") mock_get_metadata.assert_called_with(EXPECTED_URL)
class TestMatrixReport(unittest.TestCase): maxDiff = None def setUp(self): mocks.mock_get_metadata("analytics_matrix_metadata").start() mocks.mock_login().start() self.rf = Reportforce("*****@*****.**", "1234", "XXX") @patch.object(Reportforce.session, "post") def test_dataframe(self, post): """Test if it returns the expected DataFrame.""" post().json.return_value = mock_report df = self.rf.get_report("ReportID") pd.testing.assert_frame_equal(expected_df, df) @patch.object(Reportforce.session, "post") def test_empty_matrix(self, post): """Test if returns an empty DataFrame when the matrix is empty.""" mock_factmap = { "T!T": { "aggregates": { "label": "label", "value": "value" }, "rows": [] } } with patch.dict(mock_report, mock_report, factMap=mock_factmap): post().json.return_value = mock_report df = self.rf.get_report("ReportID") self.assertTrue(df.empty) def test_get_columns_labels_of_a_matrix(self): """Test get columns labels of a matrix.""" test = get_columns_labels(mock_report) expected = { "Delivery Day": "Delivery Day", "Product": "Product", "Supervisor": "Supervisor", "Worker": "Worker", } self.assertDictEqual(test, expected) def tearDown(self): patch.stopall()
def setup(mock_login, mock_generate_reports, mock_get_metadata): """ Simulate looping 1 more time to get the full report. """ mock_generate_reports(REPORT, n=1) mock_get_metadata(METADATA) rf = Reportforce("*****@*****.**", "1234", "token") rf.get_report( "ID", id_column="Opportunity Owner", ) return rf
def test_set_date_interval(mock_login, mock_get_metadata, mock_http_request): """Test if specifying a date interval gives the correct date filter.""" mock_get_metadata(Metadata(read_json("sample_metadata.json"))) mock_http_request(REPORT, "post") rf = Reportforce("*****@*****.**", "1234", "token") rf.get_report("ID", date_interval="Current FY") assert rf.metadata.date_filter == { "column": "CLOSE_DATE", "durationValue": "THIS_FISCAL_YEAR", "endDate": "2016-12-31T00:00:00", "startDate": "2016-01-01T00:00:00", }
def test_ignore_date_filter(mock_login, mock_get_metadata, mock_http_request): """Test if specifying ignore_date_filter removes the standard date filter.""" mock_get_metadata(METADATA) mock_http_request(REPORT, "post") rf = Reportforce("*****@*****.**", "1234", "token") rf.get_report("ID", ignore_date_filter=True) assert rf.metadata.date_filter == { "column": "column", "durationValue": "CUSTOM", "endDate": None, "startDate": None, }
def setup(mock_login, mock_generate_reports, mock_get_metadata): """ Simulate looping 1 more time to get the full report. """ mock_generate_reports(REPORT, n=1) mock_get_metadata(METADATA) rf = Reportforce("*****@*****.**", "1234", "token") rf.get_report( "ID", filters=[("Opportunity Name", "!=", "00112233")], logic="1 AND 2", id_column="Opportunity Name", start="01-01-2020", end="2020-01-31", date_column="Fiscal Period", ) return rf
def test_if_report_is_empty(self, post): mock_factmap = { "factMap": { "T!T": { "aggregates": { "label": 0, "value": 0 } } } } with patch.dict(report, mock_factmap): post().json.return_value = report rf = Reportforce("*****@*****.**", "1234", "XXX") self.report = rf.get_report("report_id", id_column="label1") self.assertTrue(self.report.empty)
def setup(mock_login, mock_http_request, mock_get_metadata): """Simulate getting a report with these parameters. We expected the underlying report metadata dictionary to change accordingly. """ mock_http_request(REPORT, "post") mock_get_metadata(METADATA) rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_report( "ID", filters=[("Opportunity Name", "!=", "VALUE")], logic="1 AND 2", id_column="Opportunity Name", start="01-01-2020", end="2020-01-31", date_column="Fiscal Period", ) return rf
def setUpClass(cls, post): mocks.mock_login().start() mocks.mock_get_metadata("analytics_tabular_metadata").start() post().json.return_value = mock_report cls.rf = Reportforce("*****@*****.**", "1234", "XXX") cls.rf.get_report("00O1a0000093ga", filters=[("Opportunity Name", "!=", "VALUE")], logic="1 AND 2", id_column="Opportunity Name", start="01-01-2020", end="2020-01-31", date_column="Fiscal Period")
def test_get_latest_version(self): """Test getting latest version of Salesforce.""" get_config = { "get.return_value.json.return_value": mocks.get_json("versions") } with patch("reportforce.login.requests", **get_config): rf = Reportforce(session_id="session_id", instance_url="instance", latest_version=True) test = rf.version expected = "48.0" self.assertEqual(test, expected)
from ct_snippets.sf_bulk import SF_Bulk from reportforce import Reportforce import numpy as np import soql_queries as soql import variables load_dotenv() SF_PASS = os.environ.get("SF_PASS") SF_TOKEN = os.environ.get("SF_TOKEN") SF_USERNAME = os.environ.get("SF_USERNAME") sf = Salesforce(username=SF_USERNAME, password=SF_PASS, security_token=SF_TOKEN) rf = Reportforce(session_id=sf.session_id, instance_url=sf.sf_instance) # Sample for loading from SOQL # query = """ """ # sf_data = SF_SOQL("data", query) # sf_data.load_from_sf_soql(sf) # sf_data.write_file() # Sample for pushing data to SFDC # sf_upload = SF_Bulk(sf_data.df) # sf_upload.data_dict = { # "C_Id": "Id", # }
def test_get_report_url(mock_login): rf = Reportforce("*****@*****.**", "pass", "token") assert rf._get_report_url("ID") == ( "https://www.salesforce.com/services/data/v47.0/analytics/reports/ID")
def test_request_call_to_get_metadata(self, get): rf = Reportforce("*****@*****.**", "1234", "XXX") rf.get_metadata("report_id") get.assert_called_with(URL)
def setUp(self): mocks.mock_get_metadata("analytics_tabular_metadata").start() mocks.mock_login().start() self.rf = Reportforce("*****@*****.**", "1234", "XXX")
def test_matrix_to_dataframe(setup): """Test matrix report into DataFrame converter.""" rf = Reportforce("*****@*****.**", "1234", "XXX") matrix_df = rf.get_report("ID") pd.testing.assert_frame_equal(expected_matrix_df, matrix_df)
def test_download_excel_with_filename(mock_login, mock_request_excel, mock_open): Reportforce("*****@*****.**", "1234", "token").get_report("ID", excel="file.xlsx") mock_open.assert_called_with("file.xlsx", "wb")