From 1dfce835ffa06224e7586e048d2593cb29289ec3 Mon Sep 17 00:00:00 2001 From: Paul Prescod Date: Wed, 14 Jul 2021 18:10:18 -0700 Subject: [PATCH 1/2] More tests for Salesforce Stuff --- ...LDatasets.test_dataset_bad_query_bulk.yaml | 132 ++++++++++++++++++ tests/conftest_extras_w_cci.py | 1 - tests/test_salesforce_gen.py | 12 ++ tests/test_with_cci.py | 23 ++- 4 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 tests/cassettes/TestSOQLDatasets.test_dataset_bad_query_bulk.yaml diff --git a/tests/cassettes/TestSOQLDatasets.test_dataset_bad_query_bulk.yaml b/tests/cassettes/TestSOQLDatasets.test_dataset_bad_query_bulk.yaml new file mode 100644 index 00000000..9d6bd867 --- /dev/null +++ b/tests/cassettes/TestSOQLDatasets.test_dataset_bad_query_bulk.yaml @@ -0,0 +1,132 @@ +interactions: +- request: + body: queryAccountCSV + headers: + Request-Headers: + - Elided + method: POST + uri: https://orgname.my.salesforce.com/services/async/50.0/job + response: + body: + string: "\n + 7500R000004aJJGQA2\n query\n Account\n + 0050R000008NsbLQAS\n 2021-06-21T05:38:02.000Z\n + 2021-06-21T05:38:02.000Z\n Open\n + Parallel\n CSV\n + 0\n 0\n + 0\n 0\n + 0\n 0\n + 0\n 50.0\n 0\n + 0\n 0\n + 0\n" + headers: + Content-Type: + - application/xml + Response-Headers: SF-Elided + status: + code: 201 + message: Created +- request: + body: 'SELECT Xyzzy FROM Account ' + headers: + Request-Headers: + - Elided + method: POST + uri: https://orgname.my.salesforce.com/services/async/50.0/job/7500R000004aJJGQA2/batch + response: + body: + string: "\n + 7510R000004aCMsQAM\n 7500R000004aJJGQA2\n Queued\n + 2021-06-21T05:38:02.000Z\n 2021-06-21T05:38:02.000Z\n + 0\n 0\n + 0\n 0\n + 0\n" + headers: + Content-Type: + - application/xml + Response-Headers: SF-Elided + status: + code: 201 + message: Created +- request: + body: null + headers: + Request-Headers: + - Elided + method: GET + uri: https://orgname.my.salesforce.com/services/async/50.0/job/7500R000004aJJGQA2 + response: + body: + string: "\n + 7500R000004aJJGQA2\n query\n Account\n + 0050R000008NsbLQAS\n 2021-06-21T05:38:02.000Z\n + 2021-06-21T05:38:02.000Z\n Open\n + Parallel\n CSV\n + 0\n 0\n + 0\n 1\n + 1\n 0\n + 0\n 50.0\n 0\n + 0\n 0\n + 0\n" + headers: + Content-Type: + - application/xml + Response-Headers: SF-Elided + status: + code: 200 + message: OK +- request: + body: null + headers: + Request-Headers: + - Elided + method: GET + uri: https://orgname.my.salesforce.com/services/async/50.0/job/7500R000004aJJGQA2/batch + response: + body: + string: "\n + \n 7510R000004aCMsQAM\n 7500R000004aJJGQA2\n + \ Failed\n InvalidBatch : Failed to process + query: INVALID_FIELD: SELECT Xyzzy FROM Account ^ ERROR at Row:1:Column:8 + No such column 'Xyzzy' on entity 'Account'. If you are attempting to use a + custom field, be sure to append the '__c' after the custom field name. Please + reference your WSDL or the describe call for the appropriate names.\n + \ 2021-06-21T05:38:02.000Z\n 2021-06-21T05:38:03.000Z\n + \ 0\n 0\n + \ 0\n 0\n + \ 0\n \n" + headers: + Content-Type: + - application/xml + Response-Headers: SF-Elided + status: + code: 200 + message: OK +- request: + body: Closed + headers: + Request-Headers: + - Elided + method: POST + uri: https://orgname.my.salesforce.com/services/async/50.0/job/7500R000004aJJGQA2 + response: + body: + string: "\n + 7500R000004aJJGQA2\n query\n Account\n + 0050R000008NsbLQAS\n 2021-06-21T05:38:02.000Z\n + 2021-06-21T05:38:02.000Z\n Closed\n + Parallel\n CSV\n + 0\n 0\n + 0\n 1\n + 1\n 0\n + 0\n 50.0\n 0\n + 0\n 0\n + 0\n" + headers: + Content-Type: + - application/xml + Response-Headers: SF-Elided + status: + code: 200 + message: OK +version: 1 diff --git a/tests/conftest_extras_w_cci.py b/tests/conftest_extras_w_cci.py index 72d051f0..637b5144 100644 --- a/tests/conftest_extras_w_cci.py +++ b/tests/conftest_extras_w_cci.py @@ -66,7 +66,6 @@ def no_services(*args, **kwargs): # TODO: Port this back to CCI def sf_before_record_response(response): # salesforce_bulk needs the Content-Type header. - print(response["headers"]) content_type = response["headers"].get("Content-Type") response["headers"] = { "Response-Headers": "SF-Elided", diff --git a/tests/test_salesforce_gen.py b/tests/test_salesforce_gen.py index 0aed5d98..aa07d848 100644 --- a/tests/test_salesforce_gen.py +++ b/tests/test_salesforce_gen.py @@ -1,6 +1,9 @@ from base64 import b64decode +import pytest from snowfakery import generate_data +from snowfakery.standard_plugins.Salesforce import SalesforceConnection +from snowfakery import data_gen_exceptions as exc class TestSalesforceGen: @@ -11,3 +14,12 @@ def test_content_version(self, generated_rows): rawdata = b64decode(b64data) assert rawdata.startswith(b"%PDF-1.3") assert b"Helvetica" in rawdata + + +class TestSalesforceConnection: + def test_bad_kwargs(self): + sfc = SalesforceConnection(None) + with pytest.raises(exc.DataGenError, match="Unknown argument"): + sfc.compose_query( + "context_name", fields=["blah"], xyzzy="foo", **{"from": "blah"} + ) diff --git a/tests/test_with_cci.py b/tests/test_with_cci.py index 49b549d5..a31893d2 100644 --- a/tests/test_with_cci.py +++ b/tests/test_with_cci.py @@ -269,8 +269,6 @@ def test_find_records_returns_multiple(self, org_config, sf, generated_rows): assert generated_rows.mock_calls[0][1][1]["AccountId"] == first_user_id -# TODO: add tests for SOQLDatasets -# ensure that all documented params/methods are covered. @skip_if_cumulusci_missing class TestSOQLDatasets: @pytest.mark.vcr() @@ -401,6 +399,27 @@ def test_dataset_bad_query(self, org_config, sf, generated_rows): with pytest.raises(DataGenError, match="Xyzzy"): generate_data(StringIO(yaml), plugin_options={"org_name": org_config.name}) + @pytest.mark.vcr() + @patch( + "simple_salesforce.Salesforce.restful", + return_value={ + "sObjects": [{"name": "Account", "count": 3000}] + }, # forces bulk mode + ) + def test_dataset_bad_query_bulk(self, restful, org_config): + yaml = """ +- plugin: snowfakery.standard_plugins.Salesforce.SOQLDataset +- object: Contact + count: 10 + fields: + __users_from_salesforce: + SOQLDataset.shuffle: + fields: Xyzzy + from: Account + """ + with pytest.raises(DataGenError, match="No such column 'Xyzzy' on entity"): + generate_data(StringIO(yaml), plugin_options={"org_name": org_config.name}) + def test_dataset_no_fields(self, org_config, sf, generated_rows): yaml = """ - plugin: snowfakery.standard_plugins.Salesforce.SOQLDataset From e20de3a1a6926802989e42f9d85148c827f50e2f Mon Sep 17 00:00:00 2001 From: Paul Prescod Date: Fri, 30 Jul 2021 02:19:19 -0700 Subject: [PATCH 2/2] More clear test --- tests/test_salesforce_gen.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_salesforce_gen.py b/tests/test_salesforce_gen.py index aa07d848..77333df0 100644 --- a/tests/test_salesforce_gen.py +++ b/tests/test_salesforce_gen.py @@ -19,7 +19,9 @@ def test_content_version(self, generated_rows): class TestSalesforceConnection: def test_bad_kwargs(self): sfc = SalesforceConnection(None) - with pytest.raises(exc.DataGenError, match="Unknown argument"): + with pytest.raises( + exc.DataGenError, match=r"Unknown argument in context_name: \('xyzzy',\)" + ): sfc.compose_query( "context_name", fields=["blah"], xyzzy="foo", **{"from": "blah"} )