def mock_request_excel(mock_get_metadata, requests_mock): mock_get_metadata(read_json("tabular_metadata.json")) requests_mock.post( "https://www.salesforce.com/services/data/v47.0/analytics/reports/ID", content=b"1,2,3", headers={ "Content-Disposition": 'attachment; filename="spreadsheet.xlsx"' }, )
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", }
import pytest from reportforce import Reportforce from fixtures_utils import read_json REPORT = read_json("tabular.json") METADATA = read_json("tabular_metadata.json") @pytest.fixture 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_report_filters(setup): assert setup.metadata.report_filters == [ { "column": "column", "filterType": "filterType",
import pytest import pandas as pd from reportforce import Reportforce from fixtures_utils import read_json SUMMARY_REPORT = read_json("summary.json") SUMMARY_METADATA = read_json("summary_metadata.json") @pytest.fixture def setup(mock_login, mock_http_request, mock_get_metadata): mock_get_metadata(SUMMARY_METADATA) mock_http_request(SUMMARY_REPORT, "post") expected_summary_df = pd.read_pickle("tests/data/summary_df.pickle") 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)
import pandas as pd from reportforce.helpers.summary import Summary from fixtures_utils import read_json SUMMARY_REPORT = read_json("summary.json") EXPECTED_SUMMARY_DF = pd.read_pickle("tests/data/summary_df.pickle") def test_summary_to_dataframe(): """Test summary report into DataFrame converter.""" summary_df = Summary(SUMMARY_REPORT).to_dataframe() pd.testing.assert_frame_equal(EXPECTED_SUMMARY_DF, summary_df) EMPTY_FACTMAP = {"T!T": {"rows": []}} def test_empty_summary(monkeypatch): monkeypatch.setitem(SUMMARY_REPORT, "factMap", EMPTY_FACTMAP) assert Summary(SUMMARY_REPORT).to_dataframe().empty
from fixtures_utils import read_json from reportforce.helpers.metadata import Metadata metadata = Metadata(read_json("sample_metadata.json")) class TestMetadataGetters: def test_get_column_api_name(self): assert metadata.get_column_api_name( "Opportunity Name") == "OPPORTUNITY_NAME" def test_get_column_label(self): assert metadata.get_column_label( "OPPORTUNITY_NAME") == "Opportunity Name" def test_get_column_dtype(self): assert metadata.get_column_dtype("OPPORTUNITY_NAME") == "string" def test_get_operator(self): assert metadata.get_operator("==") == "equals" def test_get_aggregate_api_name(self): assert metadata.get_column_api_name("Record Count") == "RowCount" def test_get_aggregate_label(self): assert metadata.get_column_label("RowCount") == "Record Count" def test_get_aggregate_dtype(self): assert metadata.get_column_dtype("RowCount") == "int" def test_get_non_included_column_api_name(self):
import pytest import pandas as pd from reportforce import Reportforce from fixtures_utils import read_json TABULAR_REPORT = read_json("tabular.json") TABULAR_METADATA = read_json("tabular_metadata.json") @pytest.fixture def setup(mock_login, mock_http_request, mock_get_metadata): mock_get_metadata(TABULAR_METADATA) mock_http_request(TABULAR_REPORT, "post") expected_tabular_df = pd.read_pickle("tests/data/tabular_df.pickle") 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) EMPTY_FACTMAP = {"T!T": {"rows": []}}
assert sf.version == "47.0" assert sf.session_id == "sessionId" assert sf.instance_url == "www.salesforce.com" assert sf.headers == {"Authorization": "Bearer sessionId"} def test_use_session_id(mock_login): """Test authentication when user provides session ID and instance URL directly.""" sf = Salesforce(session_id="userSessionId", instance_url="www.enterprise.com") assert sf.version == "47.0" assert sf.session_id == "userSessionId" assert sf.instance_url == "www.enterprise.com" assert sf.headers == {"Authorization": "Bearer userSessionId"} def test_change_version(mock_login): """Test authentication when user changes default version.""" sf = Salesforce(USERNAME, PASSWORD, TOKEN, version="48.0") assert sf.version == "48.0" VERSIONS = read_json("versions.json") def test_get_latest_version(mock_login, requests_mock): """Test getting latest version of Salesforce.""" requests_mock.get("https://www.salesforce.com/services/data/", json=VERSIONS) sf = Salesforce(USERNAME, PASSWORD, TOKEN, version="36.0", latest_version=True) assert sf.version == "48.0"
import pandas as pd from reportforce.helpers.matrix import Matrix from fixtures_utils import read_json MATRIX_REPORT = read_json("matrix.json") EXPECTED_MATRIX_DF = pd.read_pickle("tests/data/expected_matrix.pickle") def test_matrix_to_dataframe(): """Test matrix report into DataFrame converter.""" matrix_df = Matrix(MATRIX_REPORT).to_dataframe() pd.testing.assert_frame_equal(EXPECTED_MATRIX_DF, matrix_df) EMPTY_FACTMAP = {"T!T": {"rows": []}} def test_empty_matrix(monkeypatch): """Test if returns an empty DataFrame when the matrix is empty.""" monkeypatch.setitem(MATRIX_REPORT, "factMap", EMPTY_FACTMAP) assert Matrix(MATRIX_REPORT).to_dataframe().empty
import pandas as pd from reportforce.helpers.tabular import Tabular from fixtures_utils import read_json TABULAR_REPORT = read_json("tabular.json") EXPECTED_TABULAR_DF = pd.read_pickle("tests/data/tabular_df.pickle") def test_tabular_to_dataframe(): """Test tabular report DataFrame converter.""" tabular_df = Tabular(TABULAR_REPORT).to_dataframe() pd.testing.assert_frame_equal(EXPECTED_TABULAR_DF, tabular_df) EMPTY_FACTMAP = {"T!T": {"rows": []}} def test_empty_tabular(monkeypatch): """Test if DataFrame is empty when there are no rows in factMap.""" monkeypatch.setitem(TABULAR_REPORT, "factMap", EMPTY_FACTMAP) assert Tabular(TABULAR_REPORT).to_dataframe().empty
import pytest import pandas as pd from reportforce import Reportforce from reportforce.helpers.parsers import get_columns_labels from fixtures_utils import read_json MATRIX_REPORT = read_json("matrix.json") MATRIX_METADATA = read_json("matrix_metadata.json") @pytest.fixture def setup(mock_login, mock_http_request, mock_get_metadata): mock_http_request(MATRIX_REPORT, "post") mock_get_metadata(MATRIX_METADATA) expected_matrix_df = pd.read_pickle("tests/data/expected_matrix.pickle") 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) EMPTY_FACTMAP = {"T!T": {"rows": []}}
def test_get_metadata(mock_login, requests_mock): requests_mock.get(METADATA_URL, json=read_json("tabular_metadata.json")) metadata = Reportforce("*****@*****.**", "pass", "token").get_metadata("ID") assert isinstance(metadata, Metadata)
from reportforce.helpers.report import Report from reportforce.helpers.matrix import Matrix from fixtures_utils import read_json tabular = Report(read_json("tabular.json")) class TestReportFunctions: def test_report_columns_dtypes(self): assert tabular.get_columns_dtypes() == [ "string", "currency", "currency", "string", "percent", "string", "int", "datetime", "string", "string", ] def test_report_columns_labels(self): assert tabular.get_columns_labels() == [ "Opportunity Name", "Amount", "Lead Source", "Next Step", "Probability (%)", "Fiscal Period",
import pytest from reportforce import Reportforce from fixtures_utils import read_json, MockJsonResponse EXPECTED_URL = "https://www.salesforce.com/services/data/v47.0/analytics/reports/ID" METADATA = {"reportMetadata": {"reportFormat": "TABULAR"}} REPORT = read_json("tabular.json") FACTMAP = {"T!T": {"aggregates": [{"label": "$30", "value": 30}]}} @pytest.fixture def mock_get(mocker, monkeypatch): monkeypatch.setitem(REPORT, "factMap", FACTMAP) response = MockJsonResponse(REPORT) return mocker.patch.object(Reportforce.session, "get", return_value=response) @pytest.fixture def setup(mock_login, mock_get_metadata): mock_get_metadata(METADATA) def test_get_total_call(setup, mock_get): rf = Reportforce("*****@*****.**", "pass", "token") total = rf.get_total("ID") assert total == 30