Package: pytest-snowflake-bdd
pytest-snowflake_bddSetup test data and run tests on snowflake in BDD style!FeaturesProvidespytest-bddstep definitions for testing snow-sql scripts against a snowflake account.InstallationYou can install “pytest-snowflake_bdd” viapip.$ pip install pytest-snowflake-bddUsageThis plugin relies onpytest-bddto run bdd tests.You can pass your snowflake account details using the cli arguments to pytest command.customoptions:--snowflake-user=SNOWFLAKE_USERsnowflakeuserfortestenvironment--snowflake-password=SNOWFLAKE_PASSWORDsnowflakepasswordfortestenvironment--snowflake-account=SNOWFLAKE_ACCOUNTsnowflakepasswordfortestenvironment--snowflake-role=SNOWFLAKE_ROLEoptionalsnowflakerolefortestenvironment--snowflake-warehouse=SNOWFLAKE_WAREHOUSEoptionalsnowflakewarehousefortestenvironmentBelow example illustrates the usage of step definitions provided by the plugin.Feature:ExampleFeature for snowflake testingScenario:example_scenarioGivena snowflake connectionWhena temporary table called "SNOWFLAKE_LIQUIBASE.PUBLIC.DEPARTMENT" has|dept_id: INTEGER|dept_name: STRING||1|"Computer Science"||2|"Software Engineering"|Whena temporary table called "SNOWFLAKE_LIQUIBASE.PUBLIC.PEOPLE" has|people_id: INTEGER|name: STRING|dept_id: INTEGER||10|"tilak"|1|Thena sql script "./sql/example.sql" runs and the result is|people_id: INTEGER|name: STRING|dept_id: INTEGER|dept_name: STRING||10|"tilak"|1|"Computer Science"|dept_id: INTEGER.dept_idis the column name andINTEGERis the snowflake data type.The stepa temporary table called "<fully_qualified_table_name>" hasReplaces the existing table with atemporarytable. And adds data to the temporary table. This shadows the existing
table in snowflake for the entire session. Any changes done to the temporary table does not reflect on the actual
database. If the table does not exists creates a new temporary table.The stepThen a sql script "<sql_script_path>" runs and the result isThis runs the sql script and compares the output with given dataframe.Available Step definitionsCreating a new snowflake sessionGivena snowflake connectionSetting up a temporary snowflake table for testReplaces the existing table with atemporarytable. And adds data to the temporary table. This shadows the existing
table in snowflake for the entire session. Any changes done to the temporary table does not reflect on the actual
database. If the table does not exists creates a new temporary table.Whena temporary table called "SNOWFLAKE_LIQUIBASE.PUBLIC.DEPARTMENT" has|dept_id: INTEGER|dept_name: STRING||1|"Computer Science"||2|"Software Engineering"|Setting up a snowflake table for testCreates a normal table. Will fail if table already exists.Whena table called "SNOWFLAKE_LIQUIBASE.PUBLIC.DEPARTMENT" has|dept_id: INTEGER|dept_name: STRING||1|"Computer Science"||2|"Software Engineering"|Running a sql script and validating resultsThena sql script "./sql/example.sql" runs and the result is|people_id: INTEGER|name: STRING|dept_id: INTEGER|dept_name: STRING||10|"tilak"|1|"Computer Science"|Representing null in table dataUse{null}| people_id: INTEGER|name: STRING|dept_id: INTEGER|dept_name: STRING||10|"tilak"|1|{null}|Stubbing current time related functionsSupports stubbing the following functions with the fixture value.current_timestamp, localtimestamp, getdate, systimestamp, sysdate, current_time, localtimeThese functions will be replaced in the sql query by statements likeCAST('2022-01-0504:12:17' as TIMESTAMP)orCAST ('04:12:17' as TIME)Feature:ExampleFeature for snowflake testingScenario:example_scenarioGivena snowflake connectionAndcurrent timestamp "2022-01-05 04:12:17"Andcurrent time "04:12:17"Whena temporary table called "SNOWFLAKE_LIQUIBASE.PUBLIC.DEPARTMENT" has|dept_id: INTEGER|dept_name: STRING||1|"Computer Science"||2|"Software Engineering"|Whena temporary table called "SNOWFLAKE_LIQUIBASE.PUBLIC.PEOPLE" has|people_id: INTEGER|name: STRING|dept_id: INTEGER||10|"tilak"|1|Thena sql script "./sql/example.sql" runs and the result is|people_id: INTEGER|name: STRING|dept_id: INTEGER|dept_name: STRING||10|"tilak"|1|"Computer Science"|Representing different data types in table| a: CHAR|b: CHARACTER|c: STRING|d: TEXT|e: BINARY|f: VARBINARY|| sample|sample|sample|sample|sample|sample|| a: FLOAT|b: DOUBLE|c: INT|d: INTEGER|e: BIGINT|f: SMALLINT|g: TINYINT|h: BYTEINT||1.0|1.0|1|1|1|1|1|1|| a: DATE|b: DATETIME|c: TIME|d: TIMESTAMP||2021-05-05|2021-05-05 01:35:00|01:35:00|2021-05-05 01:35:00|Understanding data-type mismatch errorsFor assertion of tables we are using pandas. Differences are shown
in-terms of pandas dataframe.Below snowflake to pandas type table can help in understanding the
errors:Snowflake datatypePandas datatypeBIGINTint64BINARYbytesBOOLEANboolCHARstrCHARACTERstrDATEobjectDATETIMEobjectDECobjectDECIMALobjectDOUBLEfloat64FIXEDobjectFLOATfloat64INTint64INTEGERint64NUMBERobjectREALfloat64BYTEINTint64SMALLINTint64STRINGstrTEXTstrTIMEobjectTIMESTAMPobjectTINYINTint64VARBINARYbytesVARCHARstrContributingContributions are very welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.LicenseDistributed under the terms of theMITlicense, “pytest-snowflake_bdd” is free and open source softwareIssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.