private void ParameterizedStatement(Type type, bool testAsync = false) { var tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); var cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(type); var expectedValues = new List <object[]>(1); var val = Randomm.RandomVal(type); var bindValues = new object[] { Guid.NewGuid(), val }; expectedValues.Add(bindValues); CreateTable(tableName, cassandraDataTypeName); var statement = new SimpleStatement(string.Format("INSERT INTO {0} (id, val) VALUES (?, ?)", tableName), bindValues); if (testAsync) { Session.ExecuteAsync(statement).Wait(500); } else { Session.Execute(statement); } // Verify results RowSet rs = Session.Execute("SELECT * FROM " + tableName); VerifyData(rs, expectedValues); }
public void insertingSingleValue(Type tp) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(tp); string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, value {1} );", tableName, cassandraDataTypeName))); } catch (AlreadyExistsException) { } var toInsert = new List <object[]>(1); object val = Randomm.RandomVal(tp); if (tp == typeof(string)) { val = "'" + val.ToString().Replace("'", "''") + "'"; } var row1 = new object[2] { Guid.NewGuid(), val }; toInsert.Add(row1); bool isFloatingPoint = false; if (row1[1].GetType() == typeof(string) || row1[1].GetType() == typeof(byte[])) { QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id,value) VALUES ({1}, {2});", tableName, toInsert[0][0], row1[1].GetType() == typeof(byte[]) ? "0x" + CqlQueryTools.ToHex((byte[])toInsert[0][1]) : "'" + toInsert[0][1] + "'"), null); } // rndm.GetType().GetMethod("Next" + tp.Name).Invoke(rndm, new object[] { }) else { if (tp == typeof(Single) || tp == typeof(Double)) { isFloatingPoint = true; } QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id,value) VALUES ({1}, {2});", tableName, toInsert[0][0], !isFloatingPoint ? toInsert[0][1] : toInsert[0][1].GetType() .GetMethod("ToString", new[] { typeof(string) }) .Invoke(toInsert[0][1], new object[] { "r" })), null); } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel(), toInsert); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void ExceedingCassandraType(Type toExceed, Type toExceedWith, bool sameOutput = true) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(toExceed); string tableName = TestUtils.GetUniqueTableName(); var query = String.Format("CREATE TABLE {0}(tweet_id uuid PRIMARY KEY, label text, number {1});", tableName, cassandraDataTypeName); QueryTools.ExecuteSyncNonQuery(Session, query); object Minimum = toExceedWith.GetField("MinValue").GetValue(this); object Maximum = toExceedWith.GetField("MaxValue").GetValue(this); var row1 = new object[3] { Guid.NewGuid(), "Minimum", Minimum }; var row2 = new object[3] { Guid.NewGuid(), "Maximum", Maximum }; var toInsert_and_Check = new List <object[]>(2) { row1, row2 }; if (toExceedWith == typeof(Double) || toExceedWith == typeof(Single)) { Minimum = Minimum.GetType().GetMethod("ToString", new[] { typeof(string) }).Invoke(Minimum, new object[1] { "r" }); Maximum = Maximum.GetType().GetMethod("ToString", new[] { typeof(string) }).Invoke(Maximum, new object[1] { "r" }); if (!sameOutput) //for ExceedingCassandra_FLOAT() test case { toInsert_and_Check[0][2] = Single.NegativeInfinity; toInsert_and_Check[1][2] = Single.PositiveInfinity; } } try { QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id, label, number) VALUES ({1}, '{2}', {3});", tableName, toInsert_and_Check[0][0], toInsert_and_Check[0][1], Minimum), null); QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id, label, number) VALUES ({1}, '{2}', {3});", tableName, toInsert_and_Check[1][0], toInsert_and_Check[1][1], Maximum), null); } catch (InvalidQueryException) { if (!sameOutput && toExceed == typeof(Int32)) //for ExceedingCassandra_INT() test case { return; } } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), ConsistencyLevel.One, toInsert_and_Check); }
public void InsertingSingleValue(Type tp) { var cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(tp); var tableName = TestUtils.GetUniqueTableName(); try { var query = $@"CREATE TABLE {tableName}(tweet_id uuid PRIMARY KEY, value {cassandraDataTypeName});"; QueryTools.ExecuteSyncNonQuery(Session, query); } catch (AlreadyExistsException) { } var toInsert = new List <object[]>(1); var val = Randomm.RandomVal(tp); if (tp == typeof(string)) { val = "'" + val.ToString().Replace("'", "''") + "'"; } var row1 = new object[2] { Guid.NewGuid(), val }; toInsert.Add(row1); var isFloatingPoint = false; if (row1[1].GetType() == typeof(string) || row1[1].GetType() == typeof(byte[])) { QueryTools.ExecuteSyncNonQuery(Session, $"INSERT INTO {tableName}(tweet_id,value) VALUES ({toInsert[0][0]}, {(row1[1].GetType() == typeof(byte[]) ? "0x" + CqlQueryTools.ToHex((byte[]) toInsert[0][1]) : "'" + toInsert[0][1] + "'")});", null); } else { if (tp == typeof(Single) || tp == typeof(Double)) { isFloatingPoint = true; } QueryTools.ExecuteSyncNonQuery(Session, $"INSERT INTO {tableName}(tweet_id,value) VALUES ({toInsert[0][0]}, {(!isFloatingPoint ? toInsert[0][1] : toInsert[0][1].GetType().GetMethod("ToString", new[] {typeof(string)}).Invoke(toInsert[0][1], new object[] {"r"}))});", null); } QueryTools.ExecuteSyncQuery(Session, $"SELECT * FROM {tableName};", Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel(), toInsert); }
public void InsertingSingleValuePrepared(Type tp, object value = null) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(tp); string tableName = "table" + Guid.NewGuid().ToString("N"); Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, value {1} );", tableName, cassandraDataTypeName)) ); var toInsert = new List <object[]>(1); object val = Randomm.RandomVal(tp); if (tp == typeof(string)) { val = "'" + val.ToString().Replace("'", "''") + "'"; } var row1 = new object[2] { Guid.NewGuid(), val }; toInsert.Add(row1); PreparedStatement prep = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id, value) VALUES ({1}, ?);", tableName, toInsert[0][0])); if (value == null) { QueryTools.ExecutePreparedQuery(Session, prep, new object[] { toInsert[0][1] }); } else { QueryTools.ExecutePreparedQuery(Session, prep, new object[] { value }); } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), ConsistencyLevel.One, toInsert); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void checkingOrderOfCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null, string pendingMode = "") { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string openBracket = CassandraCollectionType == "list" ? "[" : "{"; string closeBracket = CassandraCollectionType == "list" ? "]" : "}"; string mapSyntax = ""; string randomKeyValue = string.Empty; if (TypeOfKeyForMap != null) { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; if (TypeOfKeyForMap == typeof(DateTimeOffset)) { randomKeyValue = Randomm.RandomVal(typeof(DateTimeOffset)) .GetType() .GetMethod("ToString", new[] { typeof(string) }) .Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] { "yyyy-MM-dd H:mm:sszz00" }) + "' : '"; } else { randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed) + "' : '"; } } string tableName = "table" + Guid.NewGuid().ToString("N"); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName))); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); var longQ = new StringBuilder(); longQ.AppendLine("BEGIN BATCH "); int CollectionElementsNo = 100; var orderedAsInputed = new List <Int32>(CollectionElementsNo); string inputSide = "some_collection + {1}"; if (CassandraCollectionType == "list" && pendingMode == "prepending") { inputSide = "{1} + some_collection"; } for (int i = 0; i < CollectionElementsNo; i++) { int data = i * (i % 2); longQ.AppendFormat(@"UPDATE {0} SET some_collection = " + inputSide + " WHERE tweet_id = {2};" , tableName, openBracket + randomKeyValue + data + closeBracket, tweet_id); orderedAsInputed.Add(data); } longQ.AppendLine("APPLY BATCH;"); QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting..."); if (CassandraCollectionType == "set") { orderedAsInputed.Sort(); orderedAsInputed.RemoveRange(0, orderedAsInputed.LastIndexOf(0)); } else if (CassandraCollectionType == "list" && pendingMode == "prepending") { orderedAsInputed.Reverse(); } var rs = Session.Execute(string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); { int ind = 0; foreach (Row row in rs.GetRows()) { foreach (object value in row[1] as IEnumerable) { Assert.True(orderedAsInputed[ind] == (int)value); ind++; } } } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void insertingSingleCollectionPrepared(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string mapSyntax = ""; object valueCollection = null; int Cnt = 10; if (CassandraCollectionType == "list" || CassandraCollectionType == "set") { Type openType = CassandraCollectionType == "list" ? typeof(List <>) : typeof(HashSet <>); Type listType = openType.MakeGenericType(TypeOfDataToBeInputed); valueCollection = Activator.CreateInstance(listType); MethodInfo addM = listType.GetMethod("Add"); for (int i = 0; i < Cnt; i++) { object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); addM.Invoke(valueCollection, new[] { randomValue }); } } else if (CassandraCollectionType == "map") { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; Type openType = typeof(SortedDictionary <,>); Type dicType = openType.MakeGenericType(TypeOfKeyForMap, TypeOfDataToBeInputed); valueCollection = Activator.CreateInstance(dicType); MethodInfo addM = dicType.GetMethod("Add"); for (int i = 0; i < Cnt; i++) { RETRY: try { object randomKey = Randomm.RandomVal(TypeOfKeyForMap); object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); addM.Invoke(valueCollection, new[] { randomKey, randomValue }); } catch { goto RETRY; } } } string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName)) ); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); PreparedStatement prepInsert = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES (?, ?);", tableName)); Session.Execute(prepInsert.Bind(tweet_id, valueCollection).SetConsistencyLevel(ConsistencyLevel.Quorum)); QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void insertingSingleCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string openBracket = CassandraCollectionType == "list" ? "[" : "{"; string closeBracket = CassandraCollectionType == "list" ? "]" : "}"; string mapSyntax = ""; object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); if (TypeOfDataToBeInputed == typeof(string)) { randomValue = "'" + randomValue.ToString().Replace("'", "''") + "'"; } string randomKeyValue = string.Empty; if (TypeOfKeyForMap != null) { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; if (TypeOfKeyForMap == typeof(DateTimeOffset)) { randomKeyValue = "'" + (Randomm.RandomVal(typeof(DateTimeOffset)) .GetType() .GetMethod("ToString", new[] { typeof(string) }) .Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] { "yyyy-MM-dd H:mm:sszz00" }) + "'"); } else if (TypeOfKeyForMap == typeof(string)) { randomKeyValue = "'" + Randomm.RandomVal(TypeOfDataToBeInputed).ToString().Replace("'", "''") + "'"; } else { randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed).ToString(); } } string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName)) ); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES ({1}, {2});", tableName, tweet_id, openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + randomValue + closeBracket)); var longQ = new StringBuilder(); longQ.AppendLine("BEGIN BATCH "); int CollectionElementsNo = 100; object rval = Randomm.RandomVal(TypeOfDataToBeInputed); for (int i = 0; i < CollectionElementsNo; i++) { object val = rval; if (TypeOfDataToBeInputed == typeof(string)) { val = "'" + val.ToString().Replace("'", "''") + "'"; } longQ.AppendFormat(@"UPDATE {0} SET some_collection = some_collection + {1} WHERE tweet_id = {2};" , tableName, openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + val + closeBracket, tweet_id); } longQ.AppendLine("APPLY BATCH;"); QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting..."); QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
//////////////////////////////////// /// Test Helpers //////////////////////////////////// public void ExceedingCassandraType(Type toExceed, Type toExceedWith, bool sameOutput = true) { var cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(toExceed); var tableName = TestUtils.GetUniqueTableName(); var query = $"CREATE TABLE {tableName}(tweet_id uuid PRIMARY KEY, label text, number {cassandraDataTypeName});"; QueryTools.ExecuteSyncNonQuery(Session, query); var minimum = toExceedWith.GetTypeInfo().GetField("MinValue").GetValue(this); var maximum = toExceedWith.GetTypeInfo().GetField("MaxValue").GetValue(this); var row1 = new object[3] { Guid.NewGuid(), "Minimum", minimum }; var row2 = new object[3] { Guid.NewGuid(), "Maximum", maximum }; var toInsertAndCheck = new List <object[]>(2) { row1, row2 }; if (toExceedWith == typeof(double) || toExceedWith == typeof(Single)) { minimum = minimum.GetType().GetTypeInfo().GetMethod("ToString", new[] { typeof(string) }).Invoke(minimum, new object[1] { "r" }); maximum = maximum.GetType().GetTypeInfo().GetMethod("ToString", new[] { typeof(string) }).Invoke(maximum, new object[1] { "r" }); toInsertAndCheck[0][2] = minimum; toInsertAndCheck[1][2] = maximum; if (!sameOutput) //for ExceedingCassandra_FLOAT() test case { toInsertAndCheck[0][2] = Single.NegativeInfinity; toInsertAndCheck[1][2] = Single.PositiveInfinity; } } try { QueryTools.ExecuteSyncNonQuery(Session, $"INSERT INTO {tableName}(tweet_id, label, number) VALUES ({toInsertAndCheck[0][0]}, '{toInsertAndCheck[0][1]}', {toInsertAndCheck[0][2]});", null); QueryTools.ExecuteSyncNonQuery(Session, $"INSERT INTO {tableName}(tweet_id, label, number) VALUES ({toInsertAndCheck[1][0]}, '{toInsertAndCheck[1][1]}', {toInsertAndCheck[1][2]});", null); } catch (InvalidQueryException) { if (!sameOutput && toExceed == typeof(Int32)) //for ExceedingCassandra_INT() test case { return; } throw; } VerifyStatement( QueryType.Query, $"INSERT INTO {tableName}(tweet_id, label, number) VALUES ({toInsertAndCheck[0][0]}, '{toInsertAndCheck[0][1]}', {toInsertAndCheck[0][2]});", 1); VerifyStatement( QueryType.Query, $"INSERT INTO {tableName}(tweet_id, label, number) VALUES ({toInsertAndCheck[1][0]}, '{toInsertAndCheck[1][1]}', {toInsertAndCheck[1][2]});", 1); TestCluster.PrimeFluent( b => b.WhenQuery($"SELECT * FROM {tableName};", when => when.WithConsistency(ConsistencyLevel.One)) .ThenRowsSuccess( new[] { "tweet_id", "label", "number" }, r => r.WithRow(toInsertAndCheck[0][0], toInsertAndCheck[0][1], toInsertAndCheck[0][2]) .WithRow(toInsertAndCheck[1][0], toInsertAndCheck[1][1], toInsertAndCheck[1][2]))); QueryTools.ExecuteSyncQuery(Session, $"SELECT * FROM {tableName};", ConsistencyLevel.One, toInsertAndCheck); }