Exemple #1
0
    def test_to_preview(self, snowflake_conn_str, snowflake_table):
        with mock.patch(
                "dbnd_snowflake.snowflake_values.SnowflakeController._query"
        ) as preview_query_patch, mock.patch(
                "dbnd_snowflake.snowflake_values.SnowflakeController.get_column_types"
        ) as get_column_types_patch:
            # Arrange
            get_column_types_patch.side_effect = [
                OrderedDict(foo="varchar", bar="integer")
            ]
            preview_query_patch.side_effect = [[["1", "2"], ["2", "3"],
                                                ["3", "4"]]]
            # Act
            snowflake = SnowflakeController(snowflake_conn_str)
            preview = snowflake.to_preview(snowflake_table)

            # Assert
            preview_query_patch.assert_has_calls(
                [
                    mock.call(
                        'select TRY_HEX_DECODE_STRING(HEX_ENCODE("foo")) AS foo,TRY_HEX_DECODE_STRING(HEX_ENCODE("bar")) AS bar from SNOWFLAKE_SAMPLE_DATA.TPCDS_SF100TCL.CUSTOMER limit 20'
                    ),
                ],
                any_order=True,
            )
            assert preview == dedent("""\
                  0    1
                ---  ---
                  1    2
                  2    3
                  3    4
                ...""")
Exemple #2
0
    def test_get_column_types(self, snowflake_conn_str, snowflake_table):
        with mock.patch(
                "dbnd_snowflake.snowflake_values.SnowflakeController._query"
        ) as query_patch:
            # Arrange
            query_patch.side_effect = [
                [
                    {
                        "COLUMN_NAME": "foo",
                        "DATA_TYPE": "varchar"
                    },
                ],
            ]

            # Act
            snowflake = SnowflakeController(snowflake_conn_str)
            column_types = snowflake.get_column_types(snowflake_table)

            # Assert
            query_patch.assert_has_calls(
                [
                    mock.call(
                        "SELECT column_name, data_type\n"
                        "FROM SNOWFLAKE_SAMPLE_DATA.information_schema.columns\n"
                        "WHERE LOWER(table_name) = LOWER('CUSTOMER')\n"
                        "    and LOWER(table_schema) = LOWER('TPCDS_SF100TCL')"
                    ),
                ],
                any_order=True,
            )
            assert column_types == {"foo": "varchar"}
Exemple #3
0
    def test_get_dimensions(self, snowflake_conn_str, snowflake_table):
        with mock.patch(
                "dbnd_snowflake.snowflake_values.SnowflakeController._query"
        ) as query_patch, mock.patch(
                "dbnd_snowflake.snowflake_values.SnowflakeController.get_column_types"
        ) as get_column_types_patch:
            # Arrange
            query_patch.side_effect = [[{"rows": 100, "bytes": 543}]]
            get_column_types_patch.side_effect = [{
                "foo": "varchar",
                "bar": "integer"
            }]
            # Act
            snowflake = SnowflakeController(snowflake_conn_str)
            dimensions = snowflake.get_dimensions(snowflake_table)

            # Assert
            query_patch.assert_has_calls(
                [
                    mock.call(
                        "SHOW TABLES LIKE 'CUSTOMER' in schema SNOWFLAKE_SAMPLE_DATA.TPCDS_SF100TCL"
                    ),
                ],
                any_order=True,
            )
            assert dimensions == {
                "rows": 100,
                "cols": 2,
                "bytes": 543,
            }
Exemple #4
0
def _connect_and_query(connection_string, query, *params):
    """ connect if needed, then query. """
    if not connection_string:
        logger.error("Connection string is empty, don't know where to connect")
        return

    with SnowflakeController(connection_string) as snowflake:
        return snowflake._query(query, params)
def _connect_and_query(connection_string, query, *params):
    """ connect if needed, then query. """
    # if (connection is None) and (connection_string is None):
    if connection_string is None:
        logger.error(
            "connection and connection string are None, one of them is required to query redshift"
        )
        return

    with SnowflakeController(connection_string) as snowflake:
        return snowflake._query(query, params)