From e561c92e43ec63e271184e796f6cd541cd8e790d Mon Sep 17 00:00:00 2001 From: xiaolei li <85657333+xleili@users.noreply.github.com> Date: Sun, 3 Jul 2022 10:07:00 +0800 Subject: [PATCH] fix(test):C# example ineffective in CI under TDegnine/example (#14472) * fix(test):C# example ineffective in CI under tdegnine/example * fix(test):C# examples add into CI cases.task * test(fix):update C# CI example target framework to NET 6.0 * fix(test):C# example in CI, path incorrect * fix(test):C# example CI ineffective * fix(test):C# example CI taosdemo realese path incorrect --- examples/C#/C#checker/C#checker.cs | 2 +- examples/C#/C#checker/C#checker.csproj | 2 +- examples/C#/TDengineTest/TDengineTest.csproj | 2 +- examples/C#/insertCn/insertCn.csproj | 2 +- examples/C#/insertCn/lib/ResultSetUtils.cs | 43 ++ examples/C#/insertCn/lib/Utils.cs | 418 +++++++++++++++++++ examples/C#/jsonTag/JsonTag.cs | 2 +- examples/C#/jsonTag/Util.cs | 4 +- examples/C#/jsonTag/jsonTag.csproj | 6 +- examples/C#/schemaless/schemaless.csproj | 6 +- examples/C#/schemaless/schemalessSample.cs | 2 +- examples/C#/stmt/StmtDemo.cs | 2 +- examples/C#/stmt/stmt.csproj | 2 +- examples/C#/taosdemo/taosdemo.csproj | 2 +- tests/develop-test/3-connectors/c#/test.sh | 36 +- tests/parallel_test/cases.task | 1 + 16 files changed, 507 insertions(+), 25 deletions(-) create mode 100644 examples/C#/insertCn/lib/ResultSetUtils.cs create mode 100644 examples/C#/insertCn/lib/Utils.cs diff --git a/examples/C#/C#checker/C#checker.cs b/examples/C#/C#checker/C#checker.cs index 7d0b6a50b6..f49fda88cd 100644 --- a/examples/C#/C#checker/C#checker.cs +++ b/examples/C#/C#checker/C#checker.cs @@ -389,7 +389,7 @@ namespace TDengineDriver static void ExitProgram() { - System.Environment.Exit(0); + System.Environment.Exit(1); } public void cleanup() diff --git a/examples/C#/C#checker/C#checker.csproj b/examples/C#/C#checker/C#checker.csproj index afeeaf3f01..68bc2b5ad5 100644 --- a/examples/C#/C#checker/C#checker.csproj +++ b/examples/C#/C#checker/C#checker.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 C_checker diff --git a/examples/C#/TDengineTest/TDengineTest.csproj b/examples/C#/TDengineTest/TDengineTest.csproj index 211c927d3d..af51b4eeec 100644 --- a/examples/C#/TDengineTest/TDengineTest.csproj +++ b/examples/C#/TDengineTest/TDengineTest.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 diff --git a/examples/C#/insertCn/insertCn.csproj b/examples/C#/insertCn/insertCn.csproj index 69ddfdfbbf..afd8df62e4 100644 --- a/examples/C#/insertCn/insertCn.csproj +++ b/examples/C#/insertCn/insertCn.csproj @@ -6,7 +6,7 @@ Exe - net5.0 + net6.0 diff --git a/examples/C#/insertCn/lib/ResultSetUtils.cs b/examples/C#/insertCn/lib/ResultSetUtils.cs new file mode 100644 index 0000000000..7d299411ee --- /dev/null +++ b/examples/C#/insertCn/lib/ResultSetUtils.cs @@ -0,0 +1,43 @@ +using System; +using TDengineDriver; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +namespace Test.UtilsTools.ResultSet +{ + public class ResultSet + { + private List resultMeta; + private List resultData; + // private bool isValidResult = false; + public ResultSet(IntPtr res) + { + + resultMeta = UtilsTools.GetResField(res); + resultData = UtilsTools.GetResData(res); + } + + public ResultSet(List metas, List datas) + { + resultMeta = metas; + resultData = datas; + } + + public List GetResultData() + { + return resultData; + } + + public List GetResultMeta() + { + return resultMeta; + } + + public int GetFieldsNum() + { + return resultMeta.Count; + } + } + + +} diff --git a/examples/C#/insertCn/lib/Utils.cs b/examples/C#/insertCn/lib/Utils.cs new file mode 100644 index 0000000000..6107ecab57 --- /dev/null +++ b/examples/C#/insertCn/lib/Utils.cs @@ -0,0 +1,418 @@ +using System; +using TDengineDriver; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +namespace Test.UtilsTools +{ + public class UtilsTools + { + + static string ip = "127.0.0.1"; + static string user = "root"; + static string password = "taosdata"; + static string db = ""; + static short port = 0; + //get a tdengine connection + public static IntPtr TDConnection() + { + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, GetConfigPath()); + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60"); + TDengine.Init(); + IntPtr conn = TDengine.Connect(ip, user, password, db, port); + return conn; + } + //get taos.cfg file based on different os + public static string GetConfigPath() + { + string configDir = "" ; + if(OperatingSystem.IsOSPlatform("Windows")) + { + configDir = "C:/TDengine/cfg"; + } + else if(OperatingSystem.IsOSPlatform("Linux")) + { + configDir = "/etc/taos"; + } + else if(OperatingSystem.IsOSPlatform("macOS")) + { + configDir = "/etc/taos"; + } + return configDir; + } + + public static IntPtr ExecuteQuery(IntPtr conn, String sql) + { + IntPtr res = TDengine.Query(conn, sql); + if (!IsValidResult(res)) + { + Console.Write(sql.ToString() + " failure, "); + ExitProgram(); + } + else + { + Console.WriteLine(sql.ToString() + " success"); + } + return res; + } + + public static IntPtr ExecuteErrorQuery(IntPtr conn, String sql) + { + IntPtr res = TDengine.Query(conn, sql); + if (!IsValidResult(res)) + { + Console.Write(sql.ToString() + " failure, "); + ExitProgram(); + } + else + { + Console.WriteLine(sql.ToString() + " success"); + + } + return res; + } + + public static void ExecuteUpdate(IntPtr conn, String sql) + { + IntPtr res = TDengine.Query(conn, sql); + if (!IsValidResult(res)) + { + Console.Write(sql.ToString() + " failure, "); + ExitProgram(); + } + else + { + Console.WriteLine(sql.ToString() + " success"); + + } + TDengine.FreeResult(res); + } + + + public static bool IsValidResult(IntPtr res) + { + if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0)) + { + if (res != IntPtr.Zero) + { + Console.Write("reason: " + TDengine.Error(res)); + return false; + } + Console.WriteLine(""); + return false; + } + return true; + } + public static void CloseConnection(IntPtr conn) + { + if (conn != IntPtr.Zero) + { + if (TDengine.Close(conn) == 0) + { + Console.WriteLine("close connection sucess"); + } + else + { + Console.WriteLine("close Connection failed"); + } + } + TDengine.Cleanup(); + } + public static List GetResField(IntPtr res) + { + List metas = TDengine.FetchFields(res); + return metas; + } + public static void AssertEqual(string expectVal, string actualVal) + { + if (expectVal == actualVal) + { + Console.WriteLine("{0}=={1} pass", expectVal, actualVal); + } + else + { + Console.WriteLine("{0}=={1} failed", expectVal, actualVal); + ExitProgram(); + } + } + public static void ExitProgram() + { + TDengine.Cleanup(); + System.Environment.Exit(1); + } + public static List GetResData(IntPtr res) + { + List dataRaw = new List(); + if (!IsValidResult(res)) + { + ExitProgram(); + } + List metas = GetResField(res); + dataRaw = QueryRes(res, metas); + return dataRaw; + } + + public static TDengineMeta ConstructTDengineMeta(string name, string type) + { + + TDengineMeta _meta = new TDengineMeta(); + _meta.name = name; + char[] separators = new char[] { '(', ')' }; + string[] subs = type.Split(separators, StringSplitOptions.RemoveEmptyEntries); + + switch (subs[0].ToUpper()) + { + case "BOOL": + _meta.type = 1; + _meta.size = 1; + break; + case "TINYINT": + _meta.type = 2; + _meta.size = 1; + break; + case "SMALLINT": + _meta.type = 3; + _meta.size = 2; + break; + case "INT": + _meta.type = 4; + _meta.size = 4; + break; + case "BIGINT": + _meta.type = 5; + _meta.size = 8; + break; + case "TINYINT UNSIGNED": + _meta.type = 11; + _meta.size = 1; + break; + case "SMALLINT UNSIGNED": + _meta.type = 12; + _meta.size = 2; + break; + case "INT UNSIGNED": + _meta.type = 13; + _meta.size = 4; + break; + case "BIGINT UNSIGNED": + _meta.type = 14; + _meta.size = 8; + break; + case "FLOAT": + _meta.type = 6; + _meta.size = 4; + break; + case "DOUBLE": + _meta.type = 7; + _meta.size = 8; + break; + case "BINARY": + _meta.type = 8; + _meta.size = short.Parse(subs[1]); + break; + case "TIMESTAMP": + _meta.type = 9; + _meta.size = 8; + break; + case "NCHAR": + _meta.type = 10; + _meta.size = short.Parse(subs[1]); + break; + case "JSON": + _meta.type = 15; + _meta.size = 4096; + break; + default: + _meta.type = byte.MaxValue; + _meta.size = 0; + break; + } + return _meta; + } + + private static List QueryRes(IntPtr res, List metas) + { + IntPtr rowdata; + long queryRows = 0; + List dataRaw = new List(); + int fieldCount = metas.Count; + while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero) + { + queryRows++; + IntPtr colLengthPtr = TDengine.FetchLengths(res); + int[] colLengthArr = new int[fieldCount]; + Marshal.Copy(colLengthPtr, colLengthArr, 0, fieldCount); + + for (int fields = 0; fields < fieldCount; ++fields) + { + TDengineMeta meta = metas[fields]; + int offset = IntPtr.Size * fields; + IntPtr data = Marshal.ReadIntPtr(rowdata, offset); + + if (data == IntPtr.Zero) + { + dataRaw.Add("NULL"); + continue; + } + + switch ((TDengineDataType)meta.type) + { + case TDengineDataType.TSDB_DATA_TYPE_BOOL: + bool v1 = Marshal.ReadByte(data) == 0 ? false : true; + dataRaw.Add(v1); + break; + case TDengineDataType.TSDB_DATA_TYPE_TINYINT: + sbyte v2 = (sbyte)Marshal.ReadByte(data); + dataRaw.Add(v2); + break; + case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: + short v3 = Marshal.ReadInt16(data); + dataRaw.Add(v3); + break; + case TDengineDataType.TSDB_DATA_TYPE_INT: + int v4 = Marshal.ReadInt32(data); + dataRaw.Add(v4); + break; + case TDengineDataType.TSDB_DATA_TYPE_BIGINT: + long v5 = Marshal.ReadInt64(data); + dataRaw.Add(v5); + break; + case TDengineDataType.TSDB_DATA_TYPE_FLOAT: + float v6 = (float)Marshal.PtrToStructure(data, typeof(float)); + dataRaw.Add(v6); + break; + case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: + double v7 = (double)Marshal.PtrToStructure(data, typeof(double)); + dataRaw.Add(v7); + break; + case TDengineDataType.TSDB_DATA_TYPE_BINARY: + // string v8 = Marshal.PtrToStringAnsi(data, colLengthArr[fields]); + string v8 = Marshal.PtrToStringUTF8(data, colLengthArr[fields]); + dataRaw.Add(v8); + break; + case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: + long v9 = Marshal.ReadInt64(data); + dataRaw.Add(v9); + break; + case TDengineDataType.TSDB_DATA_TYPE_NCHAR: + // string v10 = Marshal.PtrToStringAnsi(data, colLengthArr[fields]); + string v10 = Marshal.PtrToStringUTF8(data, colLengthArr[fields]); + dataRaw.Add(v10); + break; + case TDengineDataType.TSDB_DATA_TYPE_UTINYINT: + byte v12 = Marshal.ReadByte(data); + dataRaw.Add(v12); + break; + case TDengineDataType.TSDB_DATA_TYPE_USMALLINT: + ushort v13 = (ushort)Marshal.ReadInt16(data); + dataRaw.Add(v13); + break; + case TDengineDataType.TSDB_DATA_TYPE_UINT: + uint v14 = (uint)Marshal.ReadInt32(data); + dataRaw.Add(v14); + break; + case TDengineDataType.TSDB_DATA_TYPE_UBIGINT: + ulong v15 = (ulong)Marshal.ReadInt64(data); + dataRaw.Add(v15); + break; + default: + dataRaw.Add("unknown value"); + break; + } + } + + } + if (TDengine.ErrorNo(res) != 0) + { + Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res)); + } + TDengine.FreeResult(res); + Console.WriteLine(""); + return dataRaw; + } + + // Generate insert sql for the with the coldata and tag data + public static string ConstructInsertSql(string table,string stable,List colData,List tagData,int numOfRows) + { + int numofFileds = colData.Count / numOfRows; + StringBuilder insertSql; + + if (stable == "") + { + insertSql = new StringBuilder($"insert into {table} values("); + } + else + { + insertSql = new StringBuilder($"insert into {table} using {stable} tags("); + + for (int j = 0; j < tagData.Count; j++) + { + if (tagData[j] is String) + { + insertSql.Append('\''); + insertSql.Append(tagData[j]); + insertSql.Append('\''); + } + else + { + insertSql.Append(tagData[j]); + } + if (j + 1 != tagData.Count) + { + insertSql.Append(','); + } + } + + insertSql.Append(")values("); + } + for (int i = 0; i < colData.Count; i++) + { + + if (colData[i] is String) + { + insertSql.Append('\''); + insertSql.Append(colData[i]); + insertSql.Append('\''); + } + else + { + insertSql.Append(colData[i]); + } + + if ((i + 1) % numofFileds == 0 && (i + 1) != colData.Count) + { + insertSql.Append(")("); + } + else if ((i + 1) == colData.Count) + { + insertSql.Append(')'); + } + else + { + insertSql.Append(','); + } + } + insertSql.Append(';'); + //Console.WriteLine(insertSql.ToString()); + + return insertSql.ToString(); + } + + public static List CombineColAndTagData(List colData,List tagData, int numOfRows) + { + var list = new List(); + for (int i = 0; i < colData.Count; i++) + { + list.Add(colData[i]); + if ((i + 1) % (colData.Count / numOfRows) == 0) + { + for (int j = 0; j < tagData.Count; j++) + { + list.Add(tagData[j]); + } + } + } + return list; + } + } +} diff --git a/examples/C#/jsonTag/JsonTag.cs b/examples/C#/jsonTag/JsonTag.cs index 453e54eabd..5c94df8b5a 100644 --- a/examples/C#/jsonTag/JsonTag.cs +++ b/examples/C#/jsonTag/JsonTag.cs @@ -11,7 +11,7 @@ namespace Cases IntPtr conn = IntPtr.Zero; Console.WriteLine("===================JsonTagTest===================="); conn = conn = UtilsTools.TDConnection("127.0.0.1", "root", "taosdata", "", 0); - UtilsTools.ExecuteUpdate(conn, "create database if not exists csharp_sample keep 3650"); + UtilsTools.ExecuteUpdate(conn, "create database if not exists csharp keep 3650"); UtilsTools.ExecuteUpdate(conn, "use csharp"); JsonTagSample jsonTagSample = new JsonTagSample(); jsonTagSample.Test(conn); diff --git a/examples/C#/jsonTag/Util.cs b/examples/C#/jsonTag/Util.cs index 5138938df6..7446378fc7 100644 --- a/examples/C#/jsonTag/Util.cs +++ b/examples/C#/jsonTag/Util.cs @@ -217,10 +217,10 @@ namespace Utils } } } - public static void ExitProgram() + public static void ExitProgram(int i = 1) { TDengine.Cleanup(); - System.Environment.Exit(0); + System.Environment.Exit(i); } } } \ No newline at end of file diff --git a/examples/C#/jsonTag/jsonTag.csproj b/examples/C#/jsonTag/jsonTag.csproj index ed3af6e806..2962d33a69 100644 --- a/examples/C#/jsonTag/jsonTag.csproj +++ b/examples/C#/jsonTag/jsonTag.csproj @@ -2,11 +2,11 @@ Exe - net5.0 + net6.0 - - + + diff --git a/examples/C#/schemaless/schemaless.csproj b/examples/C#/schemaless/schemaless.csproj index d132e34589..50e84ec1f9 100644 --- a/examples/C#/schemaless/schemaless.csproj +++ b/examples/C#/schemaless/schemaless.csproj @@ -2,11 +2,11 @@ Exe - net5.0 + net6.0 - - + + diff --git a/examples/C#/schemaless/schemalessSample.cs b/examples/C#/schemaless/schemalessSample.cs index f27ac352a6..8d0b7f60d0 100644 --- a/examples/C#/schemaless/schemalessSample.cs +++ b/examples/C#/schemaless/schemalessSample.cs @@ -289,7 +289,7 @@ namespace TDengineDriver static void ExitProgram() { - System.Environment.Exit(0); + System.Environment.Exit(1); } public void cleanup() diff --git a/examples/C#/stmt/StmtDemo.cs b/examples/C#/stmt/StmtDemo.cs index fdd647fdb5..56a5aa20f3 100644 --- a/examples/C#/stmt/StmtDemo.cs +++ b/examples/C#/stmt/StmtDemo.cs @@ -543,7 +543,7 @@ namespace TDengineDriver public static void ExitProgram() { TDengine.Cleanup(); - System.Environment.Exit(0); + System.Environment.Exit(1); } } } diff --git a/examples/C#/stmt/stmt.csproj b/examples/C#/stmt/stmt.csproj index f0370cbf56..629f476422 100644 --- a/examples/C#/stmt/stmt.csproj +++ b/examples/C#/stmt/stmt.csproj @@ -6,7 +6,7 @@ Exe - net5.0 + net6.0 diff --git a/examples/C#/taosdemo/taosdemo.csproj b/examples/C#/taosdemo/taosdemo.csproj index 8d4b786ba3..f6beeda56b 100644 --- a/examples/C#/taosdemo/taosdemo.csproj +++ b/examples/C#/taosdemo/taosdemo.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 false diff --git a/tests/develop-test/3-connectors/c#/test.sh b/tests/develop-test/3-connectors/c#/test.sh index 8cfb3fe4fc..06d3188788 100755 --- a/tests/develop-test/3-connectors/c#/test.sh +++ b/tests/develop-test/3-connectors/c#/test.sh @@ -15,25 +15,45 @@ rm -rf /var/lib/taos/* rm -rf /var/log/taos/* nohup taosd -c /etc/taos/ > /dev/null 2>&1 & sleep 10 + +# define fun to check if execute correct. +check(){ +if [ $1 -eq 0 ] +then + echo "===================$2 succeed===================" +else + echo "===================$2 failed===================" + exit 1 +fi +} + cd ../../ WKC=`pwd` -cd ${WKC}/src/connector/C# -dotnet test -# run example under Driver -cd ${WKC}/src/connector/C#/examples -dotnet run - -#dotnet run --project src/test/Cases/Cases.csproj +echo "WKC:${WKC}" # run example with neuget package cd ${WKC}/tests/examples/C# + dotnet run --project C#checker/C#checker.csproj +check $? C#checker.csproj + dotnet run --project TDengineTest/TDengineTest.csproj +check $? TDengineTest.csproj + dotnet run --project schemaless/schemaless.csproj +check $? schemaless.csproj + dotnet run --project jsonTag/jsonTag.csproj +check $? jsonTag.csproj + dotnet run --project stmt/stmt.csproj +check $? stmt.csproj + +dotnet run --project insertCn/insertCn.csproj +check $? insertCn.csproj cd ${WKC}/tests/examples/C#/taosdemo dotnet build -c Release tree | true -./bin/Release/net5.0/taosdemo -c /etc/taos -y +./bin/Release/net6.0/taosdemo -c /etc/taos -y +check $? taosdemo diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index e8e7b28668..059d77f18d 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -236,6 +236,7 @@ 30,,script,./test.sh -f general/import/commit.sim 30,,script,./test.sh -f general/compute/diff2.sim 30,,develop-test,bash 3-connectors/R/test.sh +30,,develop-test,bash 3-connectors/c#/test.sh 29,,system-test,python3 ./test.py -f 0-others/create_col_tag.py 29,,script,./test.sh -f unique/arbitrator/dn3_mn1_full_createTableFail.sim 29,,script,./test.sh -f general/wal/maxtables.sim -- GitLab