diff --git a/Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs index ce3365cc8..e5cc8b912 100755 --- a/Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs @@ -28,18 +28,21 @@ public void BindTestITDone() [Test] public void testArrayBind() { + using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "cola INTEGER", + "colb STRING" + }); using (IDbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testArrayBind(cola integer, colb string)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - - string insertCommand = "insert into testArrayBind values (?, ?)"; + string insertCommand = $"insert into {TableName} values (?, ?)"; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -54,11 +57,8 @@ public void testArrayBind() p2.Value = new string[] { "str1", "str2", "str3" }; cmd.Parameters.Add(p2); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(3, count); - - cmd.CommandText = "drop table if exists testArrayBind"; - cmd.ExecuteNonQuery(); } conn.Close(); @@ -72,144 +72,281 @@ public void testBindNullValue() { dbConnection.ConnectionString = ConnectionString; dbConnection.Open(); - try + CreateOrReplaceTable(dbConnection, TableName, new[] { + "intData NUMBER", + "fixedNumericData NUMBER(10,1)", + "floatingNumData DOUBLE", + "stringData VARCHAR", + "binaryData BINARY", + "boolData BOOLEAN", + "dateData DATE", + "timeData TIME", + "dateTimeData DATETIME", + "dateTimeWithTimeZone TIMESTAMP_TZ" + }); + foreach (DbType type in Enum.GetValues(typeof(DbType))) + { + bool isTypeSupported = true; + string colName = null; using (IDbCommand command = dbConnection.CreateCommand()) { - command.CommandText = "create or replace table TEST_TBL (" + - "intData NUMBER," + - "fixedNumericData NUMBER(10,1), " + - "floatingNumData DOUBLE," + - "stringData VARCHAR, " + - "binaryData BINARY, " + - "boolData BOOLEAN, " + - "dateData DATE, " + - "timeData TIME, " + - "dateTimeData DATETIME, " + - "dateTimeWithTimeZone TIMESTAMP_TZ);"; - command.ExecuteNonQuery(); + var param = command.CreateParameter(); + param.ParameterName = "p0"; + param.DbType = type; + + switch (type) + { + case DbType.SByte: + case DbType.Int16: + case DbType.Int32: + case DbType.Int64: + case DbType.Byte: + case DbType.UInt16: + case DbType.UInt32: + case DbType.UInt64: + colName = "intData"; + break; + case DbType.Decimal: + case DbType.VarNumeric: + colName = "fixedNumericData"; + break; + + case DbType.Boolean: + colName = "boolData"; + break; + + case DbType.Double: + case DbType.Single: + colName = "floatingNumData"; + break; + + case DbType.Guid: + case DbType.String: + case DbType.StringFixedLength: + colName = "stringData"; + break; + + case DbType.Date: + colName = "dateData"; + break; + + case DbType.Time: + colName = "timeData"; + break; + + case DbType.DateTime: + case DbType.DateTime2: + colName = "dateTimeData"; + break; + + case DbType.DateTimeOffset: + colName = "dateTimeWithTimeZone"; + break; + + case DbType.Binary: + colName = "binaryData"; + break; + default: + // Not supported + colName = "stringData"; + isTypeSupported = false; + break; + } + + if (isTypeSupported) + { + command.CommandText = $"insert into {TableName}({colName}) values(:p0)"; + param.Value = DBNull.Value; + command.Parameters.Add(param); + int rowsInserted = command.ExecuteNonQuery(); + Assert.AreEqual(1, rowsInserted); + } + else + { + try + { + command.CommandText = $"insert into {TableName}(stringData) values(:p0)"; + param.Value = DBNull.Value; + command.Parameters.Add(param); + int rowsInserted = command.ExecuteNonQuery(); + } + catch (SnowflakeDbException e) + { + Assert.AreEqual(270053, e.ErrorCode); + } + } } - - foreach (DbType type in Enum.GetValues(typeof(DbType))) + + if (isTypeSupported) { - bool isTypeSupported = true; - string colName = null; using (IDbCommand command = dbConnection.CreateCommand()) { - var param = command.CreateParameter(); - param.ParameterName = "p0"; - param.DbType = type; - - switch (type) + command.CommandText = $"select {colName} from {TableName};"; + using (IDataReader reader = command.ExecuteReader()) { - case DbType.SByte: - case DbType.Int16: - case DbType.Int32: - case DbType.Int64: - case DbType.Byte: - case DbType.UInt16: - case DbType.UInt32: - case DbType.UInt64: - colName = "intData"; - break; - case DbType.Decimal: - case DbType.VarNumeric: - colName = "fixedNumericData"; - break; - - case DbType.Boolean: - colName = "boolData"; - break; - - case DbType.Double: - case DbType.Single: - colName = "floatingNumData"; - break; - - case DbType.Guid: - case DbType.String: - case DbType.StringFixedLength: - colName = "stringData"; - break; - - case DbType.Date: - colName = "dateData"; - break; - - case DbType.Time: - colName = "timeData"; - break; + reader.Read(); + Assert.IsTrue(reader.IsDBNull(0)); + reader.Close(); + } + } + } - case DbType.DateTime: - case DbType.DateTime2: - colName = "dateTimeData"; - break; + // Clean up between each case + using (IDbCommand command = dbConnection.CreateCommand()) + { + command.CommandText = $"DELETE FROM {TableName}"; + command.ExecuteNonQuery(); + } + } + } + } - case DbType.DateTimeOffset: - colName = "dateTimeWithTimeZone"; - break; + [Test] + public void testBindValue() + { + using (SnowflakeDbConnection dbConnection = new SnowflakeDbConnection()) + { + dbConnection.ConnectionString = ConnectionString; + dbConnection.Open(); + + CreateOrReplaceTable(dbConnection, TableName, new[] + { + "intData NUMBER", + "fixedNumericData NUMBER(10,1)", + "floatingNumData DOUBLE", + "stringData VARCHAR", + "binaryData BINARY", + "boolData BOOLEAN", + "dateData DATE", + "timeData TIME", + "dateTimeData DATETIME", + "dateTimeWithTimeZone TIMESTAMP_TZ" + }); + + foreach (DbType type in Enum.GetValues(typeof(DbType))) + { + bool isTypeSupported = true; + string colName = null; + using (IDbCommand command = dbConnection.CreateCommand()) + { + var param = command.CreateParameter(); + param.ParameterName = "p0"; + param.DbType = type; - case DbType.Binary: - colName = "binaryData"; - break; - default: - // Not supported - colName = "stringData"; - isTypeSupported = false; - break; - } + switch (type) + { + case DbType.SByte: + case DbType.Byte: + colName = "intData"; + param.Value = 1; + break; + case DbType.Int16: + case DbType.Int32: + case DbType.Int64: + case DbType.UInt16: + case DbType.UInt32: + case DbType.UInt64: + colName = "intData"; + param.Value = 10; + break; + case DbType.Decimal: + case DbType.VarNumeric: + colName = "fixedNumericData"; + param.Value = 10.1; + break; + + case DbType.Boolean: + colName = "boolData"; + param.Value = true; + break; + + case DbType.Double: + case DbType.Single: + colName = "floatingNumData"; + param.Value = 2.5; + break; + + case DbType.Guid: + case DbType.String: + case DbType.StringFixedLength: + colName = "stringData"; + param.Value = "thisIsAString"; + break; + + case DbType.Date: + colName = "dateData"; + param.Value = DateTime.Now; + break; + + case DbType.Time: + colName = "timeData"; + param.Value = DateTime.Now; + break; + + case DbType.DateTime: + case DbType.DateTime2: + colName = "dateTimeData"; + param.Value = DateTime.Now; + break; + + case DbType.DateTimeOffset: + colName = "dateTimeWithTimeZone"; + param.Value = DateTimeOffset.Now; + break; + + case DbType.Binary: + colName = "binaryData"; + param.Value = Encoding.UTF8.GetBytes("BinaryData"); + break; + default: + // Not supported + colName = "stringData"; + isTypeSupported = false; + break; + } - if (isTypeSupported) + if (isTypeSupported) + { + command.CommandText = $"insert into {TableName}({colName}) values(:p0)"; + command.Parameters.Add(param); + int rowsInserted = command.ExecuteNonQuery(); + Assert.AreEqual(1, rowsInserted); + } + else + { + try { - command.CommandText = $"insert into TEST_TBL({colName}) values(:p0)"; + command.CommandText = $"insert into {TableName}(stringData) values(:p0)"; param.Value = DBNull.Value; command.Parameters.Add(param); int rowsInserted = command.ExecuteNonQuery(); - Assert.AreEqual(1, rowsInserted); } - else + catch (SnowflakeDbException e) { - try - { - command.CommandText = $"insert into TEST_TBL(stringData) values(:p0)"; - param.Value = DBNull.Value; - command.Parameters.Add(param); - int rowsInserted = command.ExecuteNonQuery(); - } - catch (SnowflakeDbException e) - { - Assert.AreEqual(270053, e.ErrorCode); - } + Assert.AreEqual(270053, e.ErrorCode); } } + } - if (isTypeSupported) + if (isTypeSupported) + { + using (IDbCommand command = dbConnection.CreateCommand()) { - using (IDbCommand command = dbConnection.CreateCommand()) + command.CommandText = $"select {colName} from {TableName};"; + using (IDataReader reader = command.ExecuteReader()) { - command.CommandText = $"select {colName} from TEST_TBL;"; - using (IDataReader reader = command.ExecuteReader()) - { - reader.Read(); - Assert.IsTrue(reader.IsDBNull(0)); - reader.Close(); - } + reader.Read(); + Assert.IsTrue(!reader.IsDBNull(0)); + reader.Close(); } } - - // Clean up between each case - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = "DELETE FROM TEST_TBL"; - command.ExecuteNonQuery(); - } } - } - finally - { + + // Clean up between each case using (IDbCommand command = dbConnection.CreateCommand()) { - command.CommandText = "drop table if exists TEST_TBL"; + command.CommandText = $"DELETE FROM {TableName}"; command.ExecuteNonQuery(); } } @@ -217,131 +354,104 @@ public void testBindNullValue() } [Test] - public void testBindValue() + public void testBindValueWithSFDataType() { using (SnowflakeDbConnection dbConnection = new SnowflakeDbConnection()) { dbConnection.ConnectionString = ConnectionString; dbConnection.Open(); - try + foreach (SFDataType type in Enum.GetValues(typeof(SFDataType))) { - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = "create or replace table TEST_TBL (" + - "intData NUMBER," + - "fixedNumericData NUMBER(10,1), " + - "floatingNumData DOUBLE," + - "stringData VARCHAR, " + - "binaryData BINARY, " + - "boolData BOOLEAN, " + - "dateData DATE, " + - "timeData TIME, " + - "dateTimeData DATETIME, " + - "dateTimeWithTimeZone TIMESTAMP_TZ);"; - command.ExecuteNonQuery(); - } - - foreach (DbType type in Enum.GetValues(typeof(DbType))) + if (!type.Equals(SFDataType.None)) { bool isTypeSupported = true; - string colName = null; + string[] columns; + if (!type.Equals(SFDataType.FIXED)) + { + columns = new[] + { + $"data {type.ToString()}", + "unsupportedType VARCHAR" + }; + } + else + { + columns = new[] + { + "data NUMBER", + "unsupportedType VARCHAR" + }; + } + + CreateOrReplaceTable(dbConnection, TableName, columns); + using (IDbCommand command = dbConnection.CreateCommand()) { - var param = command.CreateParameter(); + SnowflakeDbParameter param = (SnowflakeDbParameter)command.CreateParameter(); param.ParameterName = "p0"; - param.DbType = type; - + param.SFDataType = type; switch (type) { - case DbType.SByte: - case DbType.Byte: - colName = "intData"; - param.Value = 1; + case SFDataType.BINARY: + param.Value = Encoding.UTF8.GetBytes("BinaryData"); break; - case DbType.Int16: - case DbType.Int32: - case DbType.Int64: - case DbType.UInt16: - case DbType.UInt32: - case DbType.UInt64: - colName = "intData"; + case SFDataType.FIXED: param.Value = 10; break; - case DbType.Decimal: - case DbType.VarNumeric: - colName = "fixedNumericData"; - param.Value = 10.1; - break; - - case DbType.Boolean: - colName = "boolData"; + case SFDataType.BOOLEAN: param.Value = true; break; - - case DbType.Double: - case DbType.Single: - colName = "floatingNumData"; - param.Value = 2.5; + case SFDataType.DATE: + param.Value = DateTime.Now; break; - - case DbType.Guid: - case DbType.String: - case DbType.StringFixedLength: - colName = "stringData"; + case SFDataType.TEXT: param.Value = "thisIsAString"; break; - - case DbType.Date: - colName = "dateData"; - param.Value = DateTime.Now; - break; - - case DbType.Time: - colName = "timeData"; - param.Value = DateTime.Now; + case SFDataType.TIMESTAMP_LTZ: + param.Value = DateTimeOffset.Now; break; - - case DbType.DateTime: - case DbType.DateTime2: - colName = "dateTimeData"; + case SFDataType.TIMESTAMP_NTZ: param.Value = DateTime.Now; break; - - case DbType.DateTimeOffset: - colName = "dateTimeWithTimeZone"; + case SFDataType.TIMESTAMP_TZ: param.Value = DateTimeOffset.Now; break; - - case DbType.Binary: - colName = "binaryData"; - param.Value = Encoding.UTF8.GetBytes("BinaryData"); + case SFDataType.TIME: + param.Value = DateTime.Now; + break; + case SFDataType.REAL: + param.Value = 25.3; break; default: - // Not supported - colName = "stringData"; isTypeSupported = false; + param.Value = "InvalidSFDataType"; break; } if (isTypeSupported) { - command.CommandText = $"insert into TEST_TBL({colName}) values(:p0)"; + // Set to an unsupported DB type to check that SFDataType has precedence + param.DbType = DbType.Object; + command.CommandText = $"insert into {TableName}(data) values(:p0)"; command.Parameters.Add(param); int rowsInserted = command.ExecuteNonQuery(); Assert.AreEqual(1, rowsInserted); } - else + // DB rejects query if param type is VARIANT, OBJECT or ARRAY + else if (!type.Equals(SFDataType.VARIANT) && + !type.Equals(SFDataType.OBJECT) && + !type.Equals(SFDataType.ARRAY)) { try { - command.CommandText = $"insert into TEST_TBL(stringData) values(:p0)"; + command.CommandText = $"insert into {TableName}(unsupportedType) values(:p0)"; param.Value = DBNull.Value; command.Parameters.Add(param); int rowsInserted = command.ExecuteNonQuery(); } catch (SnowflakeDbException e) { - Assert.AreEqual(270053, e.ErrorCode); + Assert.AreEqual(270054, e.ErrorCode); } } } @@ -350,7 +460,7 @@ public void testBindValue() { using (IDbCommand command = dbConnection.CreateCommand()) { - command.CommandText = $"select {colName} from TEST_TBL;"; + command.CommandText = $"select data from {TableName};"; using (IDataReader reader = command.ExecuteReader()) { reader.Read(); @@ -359,147 +469,6 @@ public void testBindValue() } } } - - // Clean up between each case - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = "DELETE FROM TEST_TBL"; - command.ExecuteNonQuery(); - } - } - } - finally - { - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = "drop table if exists TEST_TBL"; - command.ExecuteNonQuery(); - } - } - } - } - - [Test] - public void testBindValueWithSFDataType() - { - using (SnowflakeDbConnection dbConnection = new SnowflakeDbConnection()) - { - dbConnection.ConnectionString = ConnectionString; - dbConnection.Open(); - try - { - foreach (SFDataType type in Enum.GetValues(typeof(SFDataType))) - { - if (!type.Equals(SFDataType.None)) - { - bool isTypeSupported = true; - using (IDbCommand command = dbConnection.CreateCommand()) - { - if (!type.Equals(SFDataType.FIXED)) - { - command.CommandText = $"create or replace table TEST_TBL (data {type.ToString()}, unsupportedType VARCHAR)"; - } - else - { - command.CommandText = $"create or replace table TEST_TBL (data NUMBER, unsupportedType VARCHAR)"; - } - command.ExecuteNonQuery(); - } - - - using (IDbCommand command = dbConnection.CreateCommand()) - { - SnowflakeDbParameter param = (SnowflakeDbParameter)command.CreateParameter(); - param.ParameterName = "p0"; - param.SFDataType = type; - switch (type) - { - case SFDataType.BINARY: - param.Value = Encoding.UTF8.GetBytes("BinaryData"); - break; - case SFDataType.FIXED: - param.Value = 10; - break; - case SFDataType.BOOLEAN: - param.Value = true; - break; - case SFDataType.DATE: - param.Value = DateTime.Now; - break; - case SFDataType.TEXT: - param.Value = "thisIsAString"; - break; - case SFDataType.TIMESTAMP_LTZ: - param.Value = DateTimeOffset.Now; - break; - case SFDataType.TIMESTAMP_NTZ: - param.Value = DateTime.Now; - break; - case SFDataType.TIMESTAMP_TZ: - param.Value = DateTimeOffset.Now; - break; - case SFDataType.TIME: - param.Value = DateTime.Now; - break; - case SFDataType.REAL: - param.Value = 25.3; - break; - default: - isTypeSupported = false; - param.Value = "InvalidSFDataType"; - break; - } - - if (isTypeSupported) - { - // Set to an unsupported DB type to check that SFDataType has precedence - param.DbType = DbType.Object; - command.CommandText = "insert into TEST_TBL(data) values(:p0)"; - command.Parameters.Add(param); - int rowsInserted = command.ExecuteNonQuery(); - Assert.AreEqual(1, rowsInserted); - } - // DB rejects query if param type is VARIANT, OBJECT or ARRAY - else if (!type.Equals(SFDataType.VARIANT) && - !type.Equals(SFDataType.OBJECT) && - !type.Equals(SFDataType.ARRAY)) - { - try - { - command.CommandText = "insert into TEST_TBL(unsupportedType) values(:p0)"; - param.Value = DBNull.Value; - command.Parameters.Add(param); - int rowsInserted = command.ExecuteNonQuery(); - } - catch (SnowflakeDbException e) - { - Assert.AreEqual(270054, e.ErrorCode); - } - } - } - - if (isTypeSupported) - { - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = $"select data from TEST_TBL;"; - using (IDataReader reader = command.ExecuteReader()) - { - reader.Read(); - Assert.IsTrue(!reader.IsDBNull(0)); - reader.Close(); - } - } - } - } - } - } - finally - { - using (IDbCommand command = dbConnection.CreateCommand()) - { - command.CommandText = "drop table if exists TEST_TBL"; - command.ExecuteNonQuery(); } } } @@ -568,14 +537,20 @@ public void testPutArrayBind() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "cola INTEGER", + "colb STRING", + "colc DATE", + "cold TIME", + "cole TIMESTAMP_NTZ", + "colf TIMESTAMP_TZ" + }); using (IDbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testPutArrayBind(cola integer, colb string, colc date, cold time, cole TIMESTAMP_NTZ, colf TIMESTAMP_TZ)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - - string insertCommand = "insert into testPutArrayBind values (?, ?, ?, ?, ?, ?)"; + string insertCommand = $"insert into {TableName} values (?, ?, ?, ?, ?, ?)"; cmd.CommandText = insertCommand; int total = 250000; @@ -672,10 +647,10 @@ public void testPutArrayBind() p6.Value = arrTz.ToArray(); cmd.Parameters.Add(p6); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(total * 3, count); - cmd.CommandText = "SELECT * FROM testPutArrayBind"; + cmd.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -698,14 +673,17 @@ public void testPutArrayBindWorkDespiteOtTypeNameHandlingAuto() using (IDbConnection conn = new SnowflakeDbConnection(ConnectionString)) { conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "cola REAL", + "colb TEXT", + "colc NUMBER(38,0)" + }); using (IDbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testPutArrayBind(cola REAL, colb TEXT, colc NUMBER(38,0))"; - var rowsCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowsCount); - - var insertCommand = "insert into testPutArrayBind values (?, ?, ?)"; + var insertCommand = $"insert into {TableName} values (?, ?, ?)"; cmd.CommandText = insertCommand; var total = 250; @@ -745,10 +723,10 @@ public void testPutArrayBindWorkDespiteOtTypeNameHandlingAuto() p3.Value = arrint.ToArray(); cmd.Parameters.Add(p3); - rowsCount = cmd.ExecuteNonQuery(); + var rowsCount = cmd.ExecuteNonQuery(); Assert.AreEqual(total * 3, rowsCount); - cmd.CommandText = "SELECT * FROM testPutArrayBind"; + cmd.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); } @@ -764,14 +742,15 @@ public void testPutArrayBind1() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "cola INTEGER" + }); using (IDbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testPutArrayBind1(cola integer)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - - string insertCommand = "insert into testPutArrayBind1 values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; int total = 70000; @@ -787,16 +766,12 @@ public void testPutArrayBind1() p1.Value = arrint.ToArray(); cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(70000, count); - cmd.CommandText = "SELECT * FROM testPutArrayBind1"; + cmd.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); - - cmd.CommandText = "drop table if exists testPutArrayBind"; - cmd.ExecuteNonQuery(); - } conn.Close(); } diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index 5dbcac200..0652be39d 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -268,10 +268,10 @@ public void TestConnectString() cmd.CommandText = $"use schema \"{schemaName}\""; cmd.ExecuteNonQuery(); //cmd.CommandText = "create table \"dlTest\".\"dlSchema\".test1 (col1 string, col2 int)"; - cmd.CommandText = "create table test1 (col1 string, col2 int)"; + cmd.CommandText = $"create table {TableName} (col1 string, col2 int)"; cmd.ExecuteNonQuery(); //cmd.CommandText = "insert into \"dlTest\".\"dlSchema\".test1 Values ('test 1', 1);"; - cmd.CommandText = "insert into test1 Values ('test 1', 1);"; + cmd.CommandText = $"insert into {TableName} Values ('test 1', 1);"; cmd.ExecuteNonQuery(); } @@ -296,7 +296,7 @@ public void TestConnectString() conn1.Open(); using (IDbCommand cmd = conn1.CreateCommand()) { - cmd.CommandText = "SELECT count(*) FROM test1"; + cmd.CommandText = $"SELECT count(*) FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); Assert.AreEqual(1, reader.GetInt32(0)); @@ -710,19 +710,14 @@ public void TestConnectWithDifferentRole() [Test] public void TestConnectionDispose() { - using (IDbConnection conn = new SnowflakeDbConnection()) + using (IDbConnection conn = new SnowflakeDbConnection(ConnectionString)) { - // Setup - conn.ConnectionString = ConnectionString; conn.Open(); - IDbCommand command = conn.CreateCommand(); - command.CommandText = "create or replace table testConnDispose(c int)"; - command.ExecuteNonQuery(); - - IDbTransaction t1 = conn.BeginTransaction(); - IDbCommand t1c1 = conn.CreateCommand(); + CreateOrReplaceTable(conn, TableName, new []{"c INT"}); + var t1 = conn.BeginTransaction(); + var t1c1 = conn.CreateCommand(); t1c1.Transaction = t1; - t1c1.CommandText = "insert into testConnDispose values (1)"; + t1c1.CommandText = $"insert into {TableName} values (1)"; t1c1.ExecuteNonQuery(); } @@ -733,13 +728,9 @@ public void TestConnectionDispose() conn.ConnectionString = ConnectionString; conn.Open(); IDbCommand command = conn.CreateCommand(); - command.CommandText = "SELECT * FROM testConnDispose"; + command.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader = command.ExecuteReader(); Assert.IsFalse(reader.Read()); - - // Cleanup - command.CommandText = "DROP TABLE IF EXISTS testConnDispose"; - command.ExecuteNonQuery(); } } diff --git a/Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs index fb7d77a65..c4deeb25b 100755 --- a/Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs @@ -137,28 +137,22 @@ public void TestLongRunningQuery() [Ignore("This test case takes too much time so run it manually")] public void TestRowsAffectedOverflowInt() { - using (IDbConnection conn = new SnowflakeDbConnection()) + using (IDbConnection conn = new SnowflakeDbConnection(ConnectionString)) { - conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"c1 NUMBER"}); using (IDbCommand command = conn.CreateCommand()) { - command.CommandText = "create or replace table test_rows_affected_overflow(c1 number)"; - command.ExecuteNonQuery(); - - command.CommandText = "insert into test_rows_affected_overflow select seq4() from table(generator(rowcount=>2147484000))"; + command.CommandText = $"INSERT INTO {TableName} SELECT SEQ4() FROM TABLE(GENERATOR(ROWCOUNT=>2147484000))"; int affected = command.ExecuteNonQuery(); Assert.AreEqual(-1, affected); - - command.CommandText = "drop table if exists test_rows_affected_overflow"; - command.ExecuteNonQuery(); } conn.Close(); } } - } [TestFixture] @@ -459,11 +453,12 @@ public void TestTransaction() IDbCommand command = conn.CreateCommand(); command.Transaction = tran; - command.CommandText = "create or replace table testtransaction(cola string)"; + command.CommandText = $"create or replace table {TableName}(cola string)"; command.ExecuteNonQuery(); command.Transaction.Commit(); + AddTableToRemoveList(TableName); - command.CommandText = "show tables like 'testtransaction'"; + command.CommandText = $"show tables like '{TableName}'"; IDataReader reader = command.ExecuteReader(); Assert.IsTrue(reader.Read()); Assert.IsFalse(reader.Read()); @@ -471,17 +466,17 @@ public void TestTransaction() // start another transaction to test rollback tran = conn.BeginTransaction(IsolationLevel.ReadCommitted); command.Transaction = tran; - command.CommandText = "insert into testtransaction values('test')"; + command.CommandText = $"insert into {TableName} values('test')"; command.ExecuteNonQuery(); - command.CommandText = "select * from testtransaction"; + command.CommandText = $"select * from {TableName}"; reader = command.ExecuteReader(); Assert.IsTrue(reader.Read()); Assert.AreEqual("test", reader.GetString(0)); command.Transaction.Rollback(); // no value will be in table since it has been rollbacked - command.CommandText = "select * from testtransaction"; + command.CommandText = $"select * from {TableName}"; reader = command.ExecuteReader(); Assert.IsFalse(reader.Read()); @@ -494,12 +489,12 @@ public void TestRowsAffected() { String[] testCommands = { - "create or replace table test_rows_affected(cola int, colb string)", - "insert into test_rows_affected values(1, 'a'),(2, 'b')", - "merge into test_rows_affected using (select 1 as cola, 'c' as colb) m on " + - "test_rows_affected.cola = m.cola when matched then update set test_rows_affected.colb='update' " + + $"create or replace table {TableName}(cola int, colb string)", + $"insert into {TableName} values(1, 'a'),(2, 'b')", + $"merge into {TableName} using (select 1 as cola, 'c' as colb) m on " + + $"{TableName}.cola = m.cola when matched then update set {TableName}.colb='update' " + "when not matched then insert (cola, colb) values (3, 'd')", - "drop table if exists test_rows_affected" + $"drop table if exists {TableName}" }; int[] expectedResult = @@ -573,10 +568,9 @@ public void TestRowsAffectedUnload() using (IDbCommand command = conn.CreateCommand()) { - command.CommandText = "create or replace table test_rows_affected_unload(c1 number)"; - command.ExecuteNonQuery(); + CreateOrReplaceTable(conn, TableName, new []{"c1 NUMBER"}); - command.CommandText = "insert into test_rows_affected_unload values(1), (2), (3), (4), (5), (6)"; + command.CommandText = $"insert into {TableName} values(1), (2), (3), (4), (5), (6)"; command.ExecuteNonQuery(); command.CommandText = "drop stage if exists my_unload_stage"; @@ -585,16 +579,13 @@ public void TestRowsAffectedUnload() command.CommandText = "create stage if not exists my_unload_stage"; command.ExecuteNonQuery(); - command.CommandText = "copy into @my_unload_stage/unload/ from test_rows_affected_unload;"; + command.CommandText = $"copy into @my_unload_stage/unload/ from {TableName};"; int affected = command.ExecuteNonQuery(); Assert.AreEqual(6, affected); command.CommandText = "drop stage if exists my_unload_stage"; command.ExecuteNonQuery(); - - command.CommandText = "drop table if exists test_rows_affected_unload"; - command.ExecuteNonQuery(); } conn.Close(); } @@ -604,10 +595,10 @@ public void TestRowsAffectedUnload() //[Ignore("Ignore flaky unstable test case for now. Will revisit later and sdk issue created (210)")] public void testPutArrayBindAsync() { - ArrayBindTest(ConnectionString, "testPutArrayBind", 15000); + ArrayBindTest(ConnectionString, TableName, 15000); } - static void ArrayBindTest(string connstr, string tableName, int size) + private void ArrayBindTest(string connstr, string tableName, int size) { CancellationTokenSource externalCancel = new CancellationTokenSource(TimeSpan.FromSeconds(100)); @@ -615,13 +606,19 @@ static void ArrayBindTest(string connstr, string tableName, int size) { conn.ConnectionString = connstr; conn.Open(); + + CreateOrReplaceTable(conn, tableName, new [] + { + "cola INTEGER", + "colb STRING", + "colc DATE", + "cold TIME", + "cole TIMESTAMP_NTZ", + "colf TIMESTAMP_TZ" + }); using (DbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table "+ tableName + " (cola integer, colb string, colc date, cold time, cole TIMESTAMP_NTZ, colf TIMESTAMP_TZ)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - string insertCommand = "insert into " + tableName + " values (?, ?, ?, ?, ?, ?)"; cmd.CommandText = insertCommand; @@ -727,12 +724,7 @@ static void ArrayBindTest(string connstr, string tableName, int size) cmd.CommandText = "SELECT * FROM " + tableName; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); - - //cmd.CommandText = "drop table if exists " + tableName; - //cmd.ExecuteNonQuery(); - } - System.Threading.Thread.Sleep(2000); // wait for 2 seconds, in case other test still running. conn.Close(); } } @@ -740,9 +732,11 @@ static void ArrayBindTest(string connstr, string tableName, int size) [Test] public void TestPutArrayBindAsyncMultiThreading() { - var connStr = ConnectionString + ""; - Thread t1 = new Thread(() => ThreadProcess1(ConnectionString)); - Thread t2 = new Thread(() => ThreadProcess2(ConnectionString)); + var t1TableName = TableName + 1; + var t2TableName = TableName + 2; + + Thread t1 = new Thread(() => ThreadProcess1(ConnectionString, t1TableName)); + Thread t2 = new Thread(() => ThreadProcess2(ConnectionString, t2TableName)); //Thread t3 = new Thread(() => ThreadProcess3(ConnectionString)); //Thread t4 = new Thread(() => ThreadProcess4(ConnectionString)); @@ -750,26 +744,28 @@ public void TestPutArrayBindAsyncMultiThreading() t2.Start(); //t3.Start(); //t4.Start(); + t1.Join(); + t2.Join(); } - static void ThreadProcess1(string connstr) + private void ThreadProcess1(string connstr, string tableName) { - ArrayBindTest(connstr, "testPutArrayBind1", 15000); + ArrayBindTest(connstr, tableName, 15000); } - static void ThreadProcess2(string connstr) + private void ThreadProcess2(string connstr, string tableName) { - ArrayBindTest(connstr, "testPutArrayBind2", 15000); + ArrayBindTest(connstr, tableName, 15000); } - static void ThreadProcess3(string connstr) + private void ThreadProcess3(string connstr, string tableName) { - ArrayBindTest(connstr, "testPutArrayBind3", 20000); + ArrayBindTest(connstr, tableName, 20000); } - static void ThreadProcess4(string connstr) + private void ThreadProcess4(string connstr, string tableName) { - ArrayBindTest(connstr, "testPutArrayBind4", 25000); + ArrayBindTest(connstr, tableName, 25000); } [Test] @@ -780,13 +776,12 @@ public void testExecuteScalarAsyncSelect() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola INTEGER"}); using (DbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testScalarAsync(cola integer)"; - int count = cmd.ExecuteNonQuery(); - - string insertCommand = "insert into testScalarAsync values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; int total = 1000; @@ -800,16 +795,13 @@ public void testExecuteScalarAsyncSelect() p1.DbType = DbType.Int16; p1.Value = arrint.ToArray(); cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + cmd.ExecuteNonQuery(); - cmd.CommandText = "SELECT COUNT(*) FROM testScalarAsync"; + cmd.CommandText = $"SELECT COUNT(*) FROM {TableName}"; Task task = cmd.ExecuteScalarAsync(externalCancel.Token); task.Wait(); Assert.AreEqual(total, task.Result); - - cmd.CommandText = "drop table if exists testScalarAsync"; - cmd.ExecuteNonQuery(); } conn.Close(); } diff --git a/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs index ad2cf7b17..50de55fcc 100755 --- a/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs @@ -35,12 +35,11 @@ public void testRecordsAffected() conn.ConnectionString = ConnectionString; conn.Open(); + CreateOrReplaceTable(conn, TableName, new []{"cola NUMBER"}); + IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testRecordsAffected(cola number)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - string insertCommand = "insert into testRecordsAffected values (1),(1),(1)"; + string insertCommand = $"insert into {TableName} values (1),(1),(1)"; cmd.CommandText = insertCommand; IDataReader reader = cmd.ExecuteReader(); Assert.AreEqual(3, reader.RecordsAffected); @@ -49,8 +48,8 @@ public void testRecordsAffected() reader.Close(); Assert.AreEqual(3, reader.RecordsAffected); - cmd.CommandText = "drop table if exists testRecordsAffected"; - count = cmd.ExecuteNonQuery(); + cmd.CommandText = $"drop table if exists {TableName}"; + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(0, count); // Reader's RecordsAffected should be available even if the connection is closed @@ -66,17 +65,16 @@ public void testGetNumber() { conn.ConnectionString = ConnectionString; conn.Open(); - + + CreateOrReplaceTable(conn, TableName, new []{"cola NUMBER"}); + IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetNumber(cola number)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); int numInt = 10000; long numLong = 1000000L; short numShort = 10; - string insertCommand = "insert into testgetnumber values (?),(?),(?)"; + string insertCommand = $"insert into {TableName} values (?),(?),(?)"; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -97,10 +95,10 @@ public void testGetNumber() p3.DbType = DbType.Int16; cmd.Parameters.Add(p3); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(3, count); - cmd.CommandText = "select * from testgetnumber"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -114,33 +112,28 @@ public void testGetNumber() Assert.IsFalse(reader.Read()); reader.Close(); - - cmd.CommandText = "drop table if exists testgetnumber"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + conn.Close(); } } [Test] - public void testGetFloat() + public void testGetDouble() { using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola DOUBLE"}); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetDouble(cola double)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); float numFloat = (float)1.23; double numDouble = (double)1.2345678; - string insertCommand = "insert into testgetdouble values (?),(?)" ; + string insertCommand = $"insert into {TableName} values (?),(?)" ; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -155,10 +148,10 @@ public void testGetFloat() p2.DbType = DbType.Double; cmd.Parameters.Add(p2); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(2, count); - cmd.CommandText = "select * from testgetdouble"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -172,10 +165,6 @@ public void testGetFloat() Assert.IsFalse(reader.Read()); reader.Close(); - cmd.CommandText = "drop table if exists testgetdouble"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -234,16 +223,18 @@ public void testGetTimeSpan(string inputTimeStr) // Insert data int fractionalPartIndex = inputTimeStr.IndexOf('.'); var precision = fractionalPartIndex > 0 ? inputTimeStr.Length - (inputTimeStr.IndexOf('.') + 1) : 0; + CreateOrReplaceTable(conn, TableName, new [] + { + $"cola TIME{ (precision > 0 ? string.Empty : $"({precision})")}" + }); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = $"create or replace table testGetTimeSpan(cola TIME{ (precision > 0 ? string.Empty : $"({precision})")});"; - int count = cmd.ExecuteNonQuery(); - string insertCommand = $"insert into testGetTimeSpan values ('{inputTimeStr}')"; + string insertCommand = $"insert into {TableName} values ('{inputTimeStr}')"; cmd.CommandText = insertCommand; - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "SELECT cola FROM testGetTimeSpan"; + cmd.CommandText = $"SELECT cola FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -261,10 +252,7 @@ public void testGetTimeSpan(string inputTimeStr) Assert.AreEqual(dateTimeTime.Minute, timeSpanTime.Minutes); Assert.AreEqual(dateTimeTime.Second, timeSpanTime.Seconds); Assert.AreEqual(dateTimeTime.Millisecond, timeSpanTime.Milliseconds); - - cmd.CommandText = "drop table if exists testGetTimeSpan"; - count = cmd.ExecuteNonQuery(); - + conn.Close(); } } @@ -277,32 +265,45 @@ public void testGetTimeSpanError() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "C1 NUMBER", + "C2 FLOAT", + "C3 VARCHAR(255)", + "C4 BINARY(255)", + "C5 BOOLEAN", + "C6 DATE", + "C7 TIMESTAMP_NTZ(9)", + "C8 TIMESTAMP_LTZ(9)", + "C9 TIMESTAMP_TZ(9)", + "C10 VARIANT", + "C11 OBJECT", + "C12 ARRAY", + "C13 VARCHAR(1)", + "C14 TIME" + }); // Insert data IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetTimeSpanAllDatTypes(C1 NUMBER(38,0), C2 FLOAT, C3 VARCHAR(255), C4 BINARY(255), C5 BOOLEAN," + - " C6 DATE, C7 TIMESTAMP_NTZ(9), C8 TIMESTAMP_LTZ(9), C9 TIMESTAMP_TZ(9), " + - "C10 VARIANT, C11 OBJECT, C12 ARRAY, C13 VARCHAR(1), C14 TIME); "; - //Console.WriteLine(cmd.CommandText); - int count = cmd.ExecuteNonQuery(); - string insertCommand = "insert into testGetTimeSpanAllDatTypes(C1, C10, C11, C12) select 1, "+ + string insertCommand = $"insert into {TableName}(C1, C10, C11, C12) select 1, "+ "PARSE_JSON('{ \"key1\": \"value1\", \"key2\": \"value2\" }')" + ", PARSE_JSON(' { \"outer_key1\": { \"inner_key1A\": \"1a\", \"inner_key1B\": NULL }, '||' \"outer_key2\": { \"inner_key2\": 2 } '||' } ')," + " ARRAY_CONSTRUCT(1, 2, 3, NULL)"; cmd.CommandText = insertCommand; //Console.WriteLine(insertCommand); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - insertCommand = "update testGetTimeSpanAllDatTypes set C2 = 2.5, C3 = 'C3Val', C4 = TO_BINARY('C4'), C5 = true, C6 = '2021-01-01', " + + insertCommand = $"update {TableName} set C2 = 2.5, C3 = 'C3Val', C4 = TO_BINARY('C4'), C5 = true, C6 = '2021-01-01', " + "C7 = '2017-01-01 12:00:00', C8 = '2017-01-01 12:00:00 +04:00', C9 = '2014-01-02 16:00:00 +10:00', C14 = '12:00:00' where C1 = 1"; cmd.CommandText = insertCommand; //Console.WriteLine(insertCommand); count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "SELECT * FROM testGetTimeSpanAllDatTypes"; + cmd.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -340,9 +341,6 @@ public void testGetTimeSpanError() reader.Close(); - cmd.CommandText = "drop table if exists testGetTimeSpanAllDatTypes"; - count = cmd.ExecuteNonQuery(); - conn.Close(); } } @@ -365,12 +363,13 @@ private void testGetDateAndOrTime(string inputTimeStr, int? precision, SFDataTyp conn.ConnectionString = ConnectionString; conn.Open(); - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = $"create or replace table testGetDateAndOrTime(cola {dataType}{ (precision == null ? string.Empty : $"({precision})" )});"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); + CreateOrReplaceTable(conn, TableName, new [] + { + $"cola {dataType}{ (precision == null ? string.Empty : $"({precision})" )}" + }); - string insertCommand = "insert into testGetDateAndOrTime values (?)"; + IDbCommand cmd = conn.CreateCommand(); + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -393,10 +392,10 @@ private void testGetDateAndOrTime(string inputTimeStr, int? precision, SFDataTyp cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testGetDateAndOrTime"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -430,10 +429,6 @@ private void testGetDateAndOrTime(string inputTimeStr, int? precision, SFDataTyp reader.Close(); - cmd.CommandText = "drop table if exists testGetDateAndOrTime"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -466,15 +461,14 @@ public void testGetTimestampTZ() { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetTimestampTZ(cola timestamp_tz)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + + CreateOrReplaceTable(conn, TableName, new []{"cola TIMESTAMP_TZ"}); + DateTimeOffset now = DateTimeOffset.Now; + + IDbCommand cmd = conn.CreateCommand(); - string insertCommand = "insert into testgettimestamptz values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -483,10 +477,10 @@ public void testGetTimestampTZ() p1.DbType = DbType.DateTimeOffset; cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testgettimestamptz"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -495,10 +489,6 @@ public void testGetTimestampTZ() Assert.AreEqual(0, DateTimeOffset.Compare(now, dtOffset)); - cmd.CommandText = "drop table if exists testgettimestamptz"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } @@ -511,15 +501,14 @@ public void testGetTimestampLTZ() { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetTimestampLTZ(cola timestamp_ltz)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + + CreateOrReplaceTable(conn, TableName, new []{"cola TIMESTAMP_LTZ"}); + DateTimeOffset now = DateTimeOffset.Now; + + IDbCommand cmd = conn.CreateCommand(); - string insertCommand = "insert into testgettimestampltz values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; var p1 = (SnowflakeDbParameter)cmd.CreateParameter(); @@ -529,10 +518,10 @@ public void testGetTimestampLTZ() p1.SFDataType = Core.SFDataType.TIMESTAMP_LTZ; cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testgettimestampltz"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -541,10 +530,6 @@ public void testGetTimestampLTZ() Assert.AreEqual(0, DateTimeOffset.Compare(now, dtOffset)); - cmd.CommandText = "drop table if exists testgettimestamptz"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -556,13 +541,12 @@ public void testGetBoolean() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola BOOLEAN"}); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetBoolean(cola boolean)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - string insertCommand = "insert into testgetboolean values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -571,20 +555,16 @@ public void testGetBoolean() p1.Value = true; cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testgetboolean"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.GetBoolean(0)); reader.Close(); - cmd.CommandText = "drop table if exists testgetboolean"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -596,16 +576,19 @@ public void testGetBinary() { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetBinary(col1 binary, col2 VARCHAR(50), col3 double)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + + CreateOrReplaceTable(conn, TableName, new [] + { + "col1 BINARY", + "col2 VARCHAR(50)", + "col3 DOUBLE" + }); + byte[] testBytes = Encoding.UTF8.GetBytes("TEST_GET_BINARAY"); string testChars = "TEST_GET_CHARS"; double testDouble = 1.2345678; - string insertCommand = $"insert into testgetbinary values (?, '{testChars}',{testDouble.ToString()})"; + string insertCommand = $"insert into {TableName} values (?, '{testChars}',{testDouble.ToString()})"; + IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = insertCommand; var p1 = cmd.CreateParameter(); @@ -614,10 +597,10 @@ public void testGetBinary() p1.Value = testBytes; cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testgetbinary"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -725,10 +708,6 @@ public void testGetBinary() reader.Close(); - cmd.CommandText = "drop table if exists testgetbinary"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -740,17 +719,19 @@ public void testGetChars() { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetChars(col1 VARCHAR(50), col2 binary, col3 double)"; - - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + + CreateOrReplaceTable(conn, TableName, new [] + { + "col1 VARCHAR(50)", + "col2 BINARY", + "col3 DOUBLE" + }); + string testChars = "TEST_GET_CHARS"; byte[] testBytes = Encoding.UTF8.GetBytes("TEST_GET_BINARY"); double testDouble = 1.2345678; - cmd.CommandText = $"insert into testGetChars values ('{testChars}', ?, {testDouble.ToString()})"; + IDbCommand cmd = conn.CreateCommand(); + cmd.CommandText = $"insert into {TableName} values ('{testChars}', ?, {testDouble.ToString()})"; var p1 = cmd.CreateParameter(); p1.ParameterName = "1"; @@ -759,10 +740,10 @@ public void testGetChars() cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testGetChars"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -872,10 +853,6 @@ public void testGetChars() reader.Close(); - cmd.CommandText = "drop table if exists testGetChars"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -887,17 +864,19 @@ public void testGetStream() { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetChars(col1 VARCHAR(50), col2 binary, col3 double)"; - - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - + + CreateOrReplaceTable(conn, TableName, new [] + { + "col1 VARCHAR(50)", + "col2 BINARY", + "col3 DOUBLE" + }); + string testChars = "TEST_GET_CHARS"; byte[] testBytes = Encoding.UTF8.GetBytes("TEST_GET_BINARY"); double testDouble = 1.2345678; - cmd.CommandText = $"insert into testGetChars values ('{testChars}', ?, {testDouble.ToString()})"; + IDbCommand cmd = conn.CreateCommand(); + cmd.CommandText = $"insert into {TableName} values ('{testChars}', ?, {testDouble.ToString()})"; var p1 = cmd.CreateParameter(); p1.ParameterName = "1"; @@ -906,10 +885,10 @@ public void testGetStream() cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testGetChars"; + cmd.CommandText = $"select * from {TableName}"; DbDataReader reader = (DbDataReader) cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -948,10 +927,6 @@ public void testGetStream() reader.Close(); - cmd.CommandText = "drop table if exists testGetChars"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } } @@ -1065,16 +1040,20 @@ public void testReadOutNullVal() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "a INTEGER", + "b STRING" + }); using (IDbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testnull(a integer, b string)"; - cmd.ExecuteNonQuery(); - cmd.CommandText = "insert into testnull values(null, null)"; + cmd.CommandText = $"insert into {TableName} values(null, null)"; cmd.ExecuteNonQuery(); - cmd.CommandText = "select * from testnull"; + cmd.CommandText = $"select * from {TableName}"; using (IDataReader reader = cmd.ExecuteReader()) { reader.Read(); @@ -1085,9 +1064,6 @@ public void testReadOutNullVal() reader.Close(); } - - cmd.CommandText = "drop table if exists testnull"; - cmd.ExecuteNonQuery(); } conn.Close(); @@ -1101,13 +1077,11 @@ public void testGetGuid() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola STRING"}); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testGetGuid(cola string)"; - int count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - - string insertCommand = "insert into testgetguid values (?)"; + string insertCommand = $"insert into {TableName} values (?)"; cmd.CommandText = insertCommand; Guid val = Guid.NewGuid(); @@ -1118,10 +1092,10 @@ public void testGetGuid() p1.Value = val; cmd.Parameters.Add(p1); - count = cmd.ExecuteNonQuery(); + var count = cmd.ExecuteNonQuery(); Assert.AreEqual(1, count); - cmd.CommandText = "select * from testgetguid"; + cmd.CommandText = $"select * from {TableName}"; IDataReader reader = cmd.ExecuteReader(); Assert.IsTrue(reader.Read()); @@ -1137,10 +1111,6 @@ public void testGetGuid() reader.Close(); - cmd.CommandText = "drop table if exists testgetguid"; - count = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, count); - conn.Close(); } @@ -1149,35 +1119,32 @@ public void testGetGuid() [Test] public void TestCopyCmdUpdateCount() { + var stageName = TestName; using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola STRING"}); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace stage emptyStage"; - cmd.ExecuteNonQuery(); - - cmd.CommandText = "create or replace table testCopy (cola string)"; + cmd.CommandText = $"create or replace stage {stageName}"; cmd.ExecuteNonQuery(); - cmd.CommandText = "copy into testCopy from @emptyStage"; + cmd.CommandText = $"copy into {TableName} from @{stageName}"; int updateCount = cmd.ExecuteNonQuery(); Assert.AreEqual(0, updateCount); // test rows_loaded exists - cmd.CommandText = "copy into @%testcopy from (select 'test_string')"; + cmd.CommandText = $"copy into @%{TableName} from (select 'test_string')"; cmd.ExecuteNonQuery(); - cmd.CommandText = "copy into testcopy"; + cmd.CommandText = $"copy into {TableName}"; updateCount = cmd.ExecuteNonQuery(); Assert.AreEqual(1, updateCount); // clean up - cmd.CommandText = "drop stage emptyStage"; - cmd.ExecuteNonQuery(); - - cmd.CommandText = "drop table testCopy"; + cmd.CommandText = $"drop stage {stageName}"; cmd.ExecuteNonQuery(); conn.Close(); @@ -1187,19 +1154,19 @@ public void TestCopyCmdUpdateCount() [Test] public void TestCopyCmdResultSet() { + var stageName = TestName; using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"cola STRING"}); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace stage emptyStage"; - cmd.ExecuteNonQuery(); - - cmd.CommandText = "create or replace table testCopy (cola string)"; + cmd.CommandText = $"create or replace stage {stageName}"; cmd.ExecuteNonQuery(); - cmd.CommandText = "copy into testCopy from @emptyStage"; + cmd.CommandText = $"copy into {TableName} from @{stageName}"; using (var rdr = cmd.ExecuteReader()) { // Can read the first row @@ -1207,14 +1174,14 @@ public void TestCopyCmdResultSet() } // test rows_loaded exists - cmd.CommandText = "copy into @%testcopy from (select 'test_string')"; + cmd.CommandText = $"copy into @%{TableName} from (select 'test_string')"; using (var rdr = cmd.ExecuteReader()) { // Can read the first row Assert.AreEqual(true, rdr.Read()); } - cmd.CommandText = "copy into testcopy"; + cmd.CommandText = $"copy into {TableName}"; using (var rdr = cmd.ExecuteReader()) { // Can read the first row @@ -1222,10 +1189,7 @@ public void TestCopyCmdResultSet() } // clean up - cmd.CommandText = "drop stage emptyStage"; - cmd.ExecuteNonQuery(); - - cmd.CommandText = "drop table testCopy"; + cmd.CommandText = $"drop stage {stageName}"; cmd.ExecuteNonQuery(); conn.Close(); @@ -1239,13 +1203,18 @@ public void TestRetrieveSemiStructuredData() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "cola VARIANT", + "colb ARRAY", + "colc OBJECT" + }, + "as select '[\"1\", \"2\"]', '[\"1\", \"2\"]', '{\"key\": \"value\"}'"); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table testsemi(cola variant, colb array, colc object) " + - "as select '[\"1\", \"2\"]', '[\"1\", \"2\"]', '{\"key\": \"value\"}'"; - cmd.ExecuteNonQuery(); - cmd.CommandText = "select * from testsemi"; + cmd.CommandText = $"select * from {TableName}"; using (IDataReader reader = cmd.ExecuteReader()) { Assert.AreEqual(true, reader.Read()); @@ -1265,13 +1234,20 @@ public void TestResultSetMetadata() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new [] + { + "c1 NUMBER(20, 4)", + "c2 STRING(100)", + "c3 DOUBLE", + "c4 TIMESTAMP_NTZ", + "c5 VARIANT not null", + "c6 BOOLEAN" + }); IDbCommand cmd = conn.CreateCommand(); - cmd.CommandText = "create or replace table meta(c1 number(20, 4), c2 string(100), " + - "c3 double, c4 timestamp_ntz, c5 variant not null, c6 boolean) "; - cmd.ExecuteNonQuery(); - cmd.CommandText = "select * from meta"; + cmd.CommandText = $"select * from {TableName}"; using (IDataReader reader = cmd.ExecuteReader()) { var dataTable = reader.GetSchemaTable(); diff --git a/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs index 0116d55a7..a92637518 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs @@ -26,37 +26,27 @@ public void DbTransactionITDone() // Test that when a transaction is disposed, rollback would be sent out public void TestTransactionDispose() { - var conn = new SnowflakeDbConnection(); - try + using (var conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); - - IDbCommand command = conn.CreateCommand(); - command.CommandText = "create or replace table testTransactionDispose(c int)"; - command.ExecuteNonQuery(); + + CreateOrReplaceTable(conn, TableName, new []{"c INT"}); using (IDbTransaction t1 = conn.BeginTransaction()) { IDbCommand t1c1 = conn.CreateCommand(); t1c1.Transaction = t1; - t1c1.CommandText = "insert into testTransactionDispose values (1)"; + t1c1.CommandText = $"insert into {TableName} values (1)"; t1c1.ExecuteNonQuery(); } // Transaction t1 would be disposed and rollback at this point, tuple inserted is not visible IDbCommand c2 = conn.CreateCommand(); - c2.CommandText = "SELECT * FROM testTransactionDispose"; + c2.CommandText = $"SELECT * FROM {TableName}"; IDataReader reader2 = c2.ExecuteReader(); Assert.IsFalse(reader2.Read()); } - finally - { - IDbCommand command = conn.CreateCommand(); - command.CommandText = "DROP TABLE IF EXISTS testTransactionDispose"; - command.ExecuteNonQuery(); - conn.Close(); - } } [Test] @@ -66,17 +56,18 @@ public void TestTransactionRollback() var conn = new SnowflakeDbConnection(); conn.ConnectionString = ConnectionString; conn.Open(); - - DbCommand command = conn.CreateCommand(); - command.CommandText = "create or replace table testTransaction(x timestamp_ntz, a integer)"; - command.ExecuteNonQuery(); - Console.WriteLine("Created table"); - + + CreateOrReplaceTable(conn, TableName, new [] + { + "x TIMESTAMP_NTZ", + "a INTEGER" + }); + using (DbTransaction transaction = conn.BeginTransaction()) { IDbCommand t1c1 = conn.CreateCommand(); t1c1.Transaction = transaction; - t1c1.CommandText = "insert into testTransaction values (current_timestamp(), 1), (current_timestamp(), 2), (current_timestamp(), 3)"; + t1c1.CommandText = $"insert into {TableName} values (current_timestamp(), 1), (current_timestamp(), 2), (current_timestamp(), 3)"; t1c1.ExecuteNonQuery(); t1c1.Transaction.Commit(); @@ -87,13 +78,13 @@ public void TestTransactionRollback() IDbCommand t1c3 = conn.CreateCommand(); t1c3.Transaction = transaction; - t1c3.CommandText = "insert into testTransaction values (current_timestamp(), 4)"; + t1c3.CommandText = $"insert into {TableName} values (current_timestamp(), 4)"; t1c3.ExecuteNonQuery(); t1c3.Transaction.Rollback(); } IDbCommand command1 = conn.CreateCommand(); - command1.CommandText = "Select * from testTransaction"; + command1.CommandText = $"Select * from {TableName}"; IDataReader reader = command1.ExecuteReader(); int row = 0; @@ -116,17 +107,18 @@ public void TestTransactionRollbackOn2Transactions() var conn = new SnowflakeDbConnection(); conn.ConnectionString = ConnectionString; conn.Open(); - - DbCommand command = conn.CreateCommand(); - command.CommandText = "create or replace table testTransaction(x timestamp_ntz, a integer)"; - command.ExecuteNonQuery(); - Console.WriteLine("Created table"); - + + CreateOrReplaceTable(conn, TableName, new [] + { + "x TIMESTAMP_NTZ", + "a INTEGER" + }); + using (DbTransaction transaction = conn.BeginTransaction()) { IDbCommand t1c1 = conn.CreateCommand(); t1c1.Transaction = transaction; - t1c1.CommandText = "insert into testTransaction values (current_timestamp(), 1), (current_timestamp(), 2), (current_timestamp(), 3)"; + t1c1.CommandText = $"insert into {TableName} values (current_timestamp(), 1), (current_timestamp(), 2), (current_timestamp(), 3)"; t1c1.ExecuteNonQuery(); t1c1.Transaction.Commit(); } @@ -135,13 +127,13 @@ public void TestTransactionRollbackOn2Transactions() { IDbCommand t2c2 = conn.CreateCommand(); t2c2.Transaction = transaction2; - t2c2.CommandText = "insert into testTransaction values (current_timestamp(), 4)"; + t2c2.CommandText = $"insert into {TableName} values (current_timestamp(), 4)"; t2c2.ExecuteNonQuery(); t2c2.Transaction.Rollback(); } IDbCommand command1 = conn.CreateCommand(); - command1.CommandText = "Select * from testTransaction"; + command1.CommandText = $"Select * from {TableName}"; IDataReader reader = command1.ExecuteReader(); int row = 0; diff --git a/Snowflake.Data.Tests/IntegrationTests/SFMultiStatementsIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFMultiStatementsIT.cs index 91cc2df75..8a5b4ee12 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFMultiStatementsIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFMultiStatementsIT.cs @@ -128,11 +128,11 @@ public void testMixedQueryTypeWithBinding() using (DbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace table testMSArrayBind(cola integer, colb string);" + - "insert into testMSArrayBind values (?, ?);" + - "insert into testMSArrayBind values (?, ?), (?, ?);" + - "select * from testMSArrayBind;" + - "drop table if exists testMSArrayBind"; + cmd.CommandText = $"create or replace table {TableName}(cola integer, colb string);" + + $"insert into {TableName} values (?, ?);" + + $"insert into {TableName} values (?, ?), (?, ?);" + + $"select * from {TableName};" + + $"drop table if exists {TableName}"; // Set statement count var stmtCountParam = cmd.CreateParameter(); @@ -232,11 +232,11 @@ public void testWithExecuteNonQuery() using (DbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "create or replace temporary table testMSArrayBind(cola integer, colb string);" + - "insert into testMSArrayBind values (?, ?);" + - "insert into testMSArrayBind values (?, ?), (?, ?);" + - "select * from testMSArrayBind;" + - "drop table if exists testMSArrayBind"; + cmd.CommandText = $"create or replace temporary table {TableName}(cola integer, colb string);" + + $"insert into {TableName} values (?, ?);" + + $"insert into {TableName} values (?, ?), (?, ?);" + + $"select * from {TableName};" + + $"drop table if exists {TableName}"; // Set statement count var stmtCountParam = cmd.CreateParameter(); @@ -301,13 +301,13 @@ public void testWithAllQueryTypes() using (DbCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select 1;" + - "create or replace temporary table t1(c1 varchar);" + - "explain using text select * from t1;" + + $"create or replace temporary table {TableName}(c1 varchar);" + + $"explain using text select * from {TableName};" + "show parameters;" + - "insert into t1 values ('str1');" + - "desc table t1;" + - "list @%t1;" + - "remove @%t1;" + + $"insert into {TableName} values ('str1');" + + $"desc table {TableName};" + + $"list @%{TableName};" + + $"remove @%{TableName};" + "create or replace temporary procedure P1() returns varchar language javascript as $$ return ''; $$;" + "call p1();" + "use role public"; diff --git a/Snowflake.Data.Tests/IntegrationTests/SFReusableChunkTest.cs b/Snowflake.Data.Tests/IntegrationTests/SFReusableChunkTest.cs index 8c08d8657..bc216c9a4 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFReusableChunkTest.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFReusableChunkTest.cs @@ -26,22 +26,19 @@ public void testDelCharPr431() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"col STRING"}); IDbCommand cmd = conn.CreateCommand(); int rowCount = 0; - string createCommand = "create or replace table del_test (col string)"; - cmd.CommandText = createCommand; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - int largeTableRowCount = 100000; - string insertCommand = $"insert into del_test(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; + string insertCommand = $"insert into {TableName}(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; cmd.CommandText = insertCommand; IDataReader insertReader = cmd.ExecuteReader(); Assert.AreEqual(largeTableRowCount, insertReader.RecordsAffected); - string selectCommand = "select * from del_test"; + string selectCommand = $"select * from {TableName}"; cmd.CommandText = selectCommand; rowCount = 0; @@ -60,10 +57,6 @@ public void testDelCharPr431() } Assert.AreEqual(largeTableRowCount, rowCount); - cmd.CommandText = "drop table if exists del_test"; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - // Reader's RecordsAffected should be available even if the connection is closed conn.Close(); } @@ -79,39 +72,36 @@ public void testParseJson() { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"src VARIANT"}); IDbCommand cmd = conn.CreateCommand(); int rowCount = 0; - string createCommand = "create or replace table car_sales(src variant)"; - cmd.CommandText = createCommand; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - - string insertCommand = @" + string insertCommand = $@" -- borrowed from https://docs.snowflake.com/en/user-guide/querying-semistructured.html#sample-data-used-in-examples -insert into car_sales ( -select parse_json('{ +insert into {TableName} ( +select parse_json('{{ ""date"" : ""2017 - 04 - 28"", ""dealership"" : ""Valley View Auto Sales"", - ""salesperson"" : { + ""salesperson"" : {{ ""id"": ""55"", ""name"": ""Frank Beasley"" - }, + }}, ""customer"" : [ - { ""name"": ""Joyce Ridgely"", ""phone"": ""16504378889"", ""address"": ""San Francisco, CA""} + {{ ""name"": ""Joyce Ridgely"", ""phone"": ""16504378889"", ""address"": ""San Francisco, CA""}} ], ""vehicle"" : [ - { ""make"": ""Honda"", ""model"": ""Civic"", ""year"": ""2017"", ""price"": ""20275"", ""extras"":[""ext warranty"", ""paint protection""]} + {{ ""make"": ""Honda"", ""model"": ""Civic"", ""year"": ""2017"", ""price"": ""20275"", ""extras"":[""ext warranty"", ""paint protection""]}} ] -}') from table(generator(rowcount => 500)) +}}') from table(generator(rowcount => 500)) ) "; cmd.CommandText = insertCommand; IDataReader insertReader = cmd.ExecuteReader(); Assert.AreEqual(500, insertReader.RecordsAffected); - string selectCommand = "select * from car_sales"; + string selectCommand = $"select * from {TableName}"; cmd.CommandText = selectCommand; cmd.CommandType = System.Data.CommandType.Text; @@ -126,10 +116,6 @@ select parse_json('{ } Assert.AreEqual(500, rowCount); - cmd.CommandText = "drop table if exists car_sales"; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - // Reader's RecordsAffected should be available even if the connection is closed conn.Close(); } @@ -141,27 +127,24 @@ public void testChunkRetry() { IChunkParserFactory previous = ChunkParserFactory.Instance; ChunkParserFactory.Instance = new TestChunkParserFactory(6); // lower than default retry of 7 - + using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"col STRING"}); IDbCommand cmd = conn.CreateCommand(); int rowCount = 0; - string createCommand = "create or replace table del_test (col string)"; - cmd.CommandText = createCommand; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - int largeTableRowCount = 100000; - string insertCommand = $"insert into del_test(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; + string insertCommand = $"insert into {TableName}(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; cmd.CommandText = insertCommand; IDataReader insertReader = cmd.ExecuteReader(); Assert.AreEqual(largeTableRowCount, insertReader.RecordsAffected); - string selectCommand = "select * from del_test"; + string selectCommand = $"select * from {TableName}"; cmd.CommandText = selectCommand; rowCount = 0; @@ -180,10 +163,6 @@ public void testChunkRetry() } Assert.AreEqual(largeTableRowCount, rowCount); - cmd.CommandText = "drop table if exists del_test"; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - // Reader's RecordsAffected should be available even if the connection is closed conn.Close(); } @@ -196,26 +175,24 @@ public void testExceptionThrownWhenChunkDownloadRetryCountExceeded() { IChunkParserFactory previous = ChunkParserFactory.Instance; ChunkParserFactory.Instance = new TestChunkParserFactory(8); // larger than default max retry of 7 + using (IDbConnection conn = new SnowflakeDbConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); + + CreateOrReplaceTable(conn, TableName, new []{"col STRING"}); IDbCommand cmd = conn.CreateCommand(); int rowCount = 0; - string createCommand = "create or replace table del_test (col string)"; - cmd.CommandText = createCommand; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - int largeTableRowCount = 100000; - string insertCommand = $"insert into del_test(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; + string insertCommand = $"insert into {TableName}(select hex_decode_string(hex_encode('snow') || '7F' || hex_encode('FLAKE')) from table(generator(rowcount => {largeTableRowCount})))"; cmd.CommandText = insertCommand; IDataReader insertReader = cmd.ExecuteReader(); Assert.AreEqual(largeTableRowCount, insertReader.RecordsAffected); - string selectCommand = "select * from del_test"; + string selectCommand = $"select * from {TableName}"; cmd.CommandText = selectCommand; rowCount = 0; @@ -237,10 +214,6 @@ public void testExceptionThrownWhenChunkDownloadRetryCountExceeded() }); Assert.AreNotEqual(largeTableRowCount, rowCount); - cmd.CommandText = "drop table if exists del_test"; - rowCount = cmd.ExecuteNonQuery(); - Assert.AreEqual(0, rowCount); - conn.Close(); } ChunkParserFactory.Instance = previous; diff --git a/Snowflake.Data.Tests/SFBaseTest.cs b/Snowflake.Data.Tests/SFBaseTest.cs index 443f385d1..81c40c7c7 100755 --- a/Snowflake.Data.Tests/SFBaseTest.cs +++ b/Snowflake.Data.Tests/SFBaseTest.cs @@ -53,14 +53,20 @@ public class SFBaseTestAsync private const string ConnectionStringWithoutAuthFmt = "scheme={0};host={1};port={2};" + "account={3};role={4};db={5};schema={6};warehouse={7}"; private const string ConnectionStringSnowflakeAuthFmt = ";user={0};password={1};"; + protected readonly string TestName = TestContext.CurrentContext.Test.MethodName; + + protected string TableName => TestName + TestContext.CurrentContext.WorkerId?.Replace("#", "_"); private Stopwatch _stopwatch; + + private List _tablesToRemove; [SetUp] public void BeforeTest() { _stopwatch = new Stopwatch(); _stopwatch.Start(); + _tablesToRemove = new List(); } [TearDown] @@ -70,6 +76,41 @@ public void AfterTest() var testName = $"{TestContext.CurrentContext.Test.FullName}"; TestEnvironment.RecordTestPerformance(testName, _stopwatch.Elapsed); + RemoveTables(); + } + + private void RemoveTables() + { + if (_tablesToRemove.Count == 0) + return; + + using (var conn = new SnowflakeDbConnection(ConnectionString)) + { + conn.Open(); + + var cmd = conn.CreateCommand(); + + foreach (var table in _tablesToRemove) + { + cmd.CommandText = $"DROP TABLE IF EXISTS {table}"; + cmd.ExecuteNonQuery(); + } + } + } + + protected void CreateOrReplaceTable(IDbConnection conn, string tableName, IEnumerable columns, string additionalQueryStr = null) + { + var columnsStr = string.Join(", ", columns); + var cmd = conn.CreateCommand(); + cmd.CommandText = $"CREATE OR REPLACE TABLE {tableName}({columnsStr}) {additionalQueryStr}"; + cmd.ExecuteNonQuery(); + + _tablesToRemove.Add(tableName); + } + + protected void AddTableToRemoveList(string tableName) + { + _tablesToRemove.Add(tableName); } public SFBaseTestAsync()