You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/08/09 01:38:35 UTC

[superset] branch upload_gsheets updated (565f7e6890 -> 3ec803d14f)

This is an automated email from the ASF dual-hosted git repository.

beto pushed a change to branch upload_gsheets
in repository https://gitbox.apache.org/repos/asf/superset.git


 discard 565f7e6890 Add tests
     new 3ec803d14f Add tests

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (565f7e6890)
            \
             N -- N -- N   refs/heads/upload_gsheets (3ec803d14f)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 superset/db_engine_specs/gsheets.py              |  2 +-
 tests/unit_tests/db_engine_specs/test_gsheets.py | 64 +++++++++++++++++++++++-
 2 files changed, 64 insertions(+), 2 deletions(-)


[superset] 01/01: Add tests

Posted by be...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch upload_gsheets
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 3ec803d14f795d2277c21e04a56de4f480c2519b
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Tue Aug 8 17:09:51 2023 -0700

    Add tests
---
 superset/db_engine_specs/gsheets.py              |  2 +-
 tests/unit_tests/db_engine_specs/test_gsheets.py | 92 ++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/superset/db_engine_specs/gsheets.py b/superset/db_engine_specs/gsheets.py
index 729a90787a..226dceec6e 100644
--- a/superset/db_engine_specs/gsheets.py
+++ b/superset/db_engine_specs/gsheets.py
@@ -412,7 +412,7 @@ class GSheetsEngineSpec(ShillelaghEngineSpec):
         }
         url = (
             "https://sheets.googleapis.com/v4/spreadsheets/"
-            f"{spreadsheet_id}/values/{range_}:append"
+            f"{spreadsheet_id}/values/'{range_}':append"
         )
         cls._do_post(
             session,
diff --git a/tests/unit_tests/db_engine_specs/test_gsheets.py b/tests/unit_tests/db_engine_specs/test_gsheets.py
index 042e486642..40c515ede8 100644
--- a/tests/unit_tests/db_engine_specs/test_gsheets.py
+++ b/tests/unit_tests/db_engine_specs/test_gsheets.py
@@ -19,9 +19,13 @@
 
 import json
 
+import pandas as pd
+import pytest
 from pytest_mock import MockFixture
 
 from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
+from superset.exceptions import SupersetException
+from superset.sql_parse import Table
 
 
 class ProgrammingError(Exception):
@@ -307,3 +311,91 @@ def test_unmask_encrypted_extra_when_new_is_none() -> None:
     new = None
 
     assert GSheetsEngineSpec.unmask_encrypted_extra(old, new) is None
+
+
+def test_upload_new(mocker: MockFixture) -> None:
+    """
+    Test file upload when the table does not exist.
+    """
+    from superset.db_engine_specs.gsheets import GSheetsEngineSpec
+
+    mocker.patch("superset.db_engine_specs.gsheets.db")
+    get_adapter_for_table_name = mocker.patch(
+        "superset.db_engine_specs.gsheets.get_adapter_for_table_name"
+    )
+    session = get_adapter_for_table_name()._get_session()
+    session.post().json.return_value = {
+        "spreadsheetId": 1,
+        "spreadsheetUrl": "https://docs.example.org",
+        "sheets": [{"properties": {"title": "sample_data"}}],
+    }
+
+    database = mocker.MagicMock()
+    database.get_extra.return_value = {}
+
+    df = pd.DataFrame([1, "foo", 3.0])
+    table = Table("sample_data")
+
+    GSheetsEngineSpec.df_to_sql(database, table, df, {})
+    assert database.extra == json.dumps(
+        {"engine_params": {"catalog": {"sample_data": "https://docs.example.org"}}}
+    )
+
+
+def test_upload_existing(mocker: MockFixture) -> None:
+    """
+    Test file upload when the table does exist.
+    """
+    from superset.db_engine_specs.gsheets import GSheetsEngineSpec
+
+    mocker.patch("superset.db_engine_specs.gsheets.db")
+    get_adapter_for_table_name = mocker.patch(
+        "superset.db_engine_specs.gsheets.get_adapter_for_table_name"
+    )
+    adapter = get_adapter_for_table_name()
+    adapter._spreadsheet_id = 1
+    adapter._sheet_name = "sheet0"
+    session = adapter._get_session()
+    session.post().json.return_value = {
+        "spreadsheetId": 1,
+        "spreadsheetUrl": "https://docs.example.org",
+        "sheets": [{"properties": {"title": "sample_data"}}],
+    }
+
+    database = mocker.MagicMock()
+    database.get_extra.return_value = {
+        "engine_params": {"catalog": {"sample_data": "https://docs.example.org"}}
+    }
+
+    df = pd.DataFrame([1, "foo", 3.0])
+    table = Table("sample_data")
+
+    with pytest.raises(SupersetException) as excinfo:
+        GSheetsEngineSpec.df_to_sql(database, table, df, {"if_exists": "append"})
+    assert str(excinfo.value) == "Append operation not currently supported"
+
+    with pytest.raises(SupersetException) as excinfo:
+        GSheetsEngineSpec.df_to_sql(database, table, df, {"if_exists": "fail"})
+    assert str(excinfo.value) == "Table already exists"
+
+    GSheetsEngineSpec.df_to_sql(database, table, df, {"if_exists": "replace"})
+    session.post.assert_has_calls(
+        [
+            mocker.call(),
+            mocker.call(
+                "https://sheets.googleapis.com/v4/spreadsheets/1/values/'sheet0':clear",
+                json={},
+            ),
+            mocker.call().json(),
+            mocker.call(
+                "https://sheets.googleapis.com/v4/spreadsheets/1/values/'sheet0':append",
+                json={
+                    "range": "sheet0",
+                    "majorDimension": "ROWS",
+                    "values": [[1], ["foo"], [3.0]],
+                },
+                params={"valueInputOption": "USER_ENTERED"},
+            ),
+            mocker.call().json(),
+        ]
+    )