public void Append_ToArray() { Tuple <Table <EntityWithArrayType>, List <EntityWithArrayType> > tupleArrayType = EntityWithArrayType.SetupDefaultTable(_session); Table <EntityWithArrayType> table = tupleArrayType.Item1; List <EntityWithArrayType> expectedEntities = tupleArrayType.Item2; string[] arrToAdd = new string[] { "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), }; EntityWithArrayType singleEntity = expectedEntities.First(); EntityWithArrayType expectedEntity = singleEntity.Clone(); List <string> strValsAsList = new List <string>(); strValsAsList.AddRange(expectedEntity.ArrayType); strValsAsList.AddRange(arrToAdd); expectedEntity.ArrayType = strValsAsList.ToArray(); // Append the values table.Where(t => t.Id == singleEntity.Id).Select(t => new EntityWithArrayType { ArrayType = CqlOperator.Append(arrToAdd) }).Update().Execute(); // Validate the final state of the data var entityList = table.Where(m => m.Id == singleEntity.Id).ExecuteAsync().Result.ToList(); Assert.AreEqual(1, entityList.Count); Assert.AreNotEqual(expectedEntity.ArrayType, singleEntity.ArrayType); expectedEntity.AssertEquals(entityList[0]); }
public void Single_NoSuchRecord() { string cqlToFindNothing = _selectAllDefaultCql + " where moviemaker ='" + Randomm.RandomAlphaNum(20) + "'"; var err = Assert.Throws <InvalidOperationException>(() => _mapper.Single <Movie>(cqlToFindNothing)); Assert.AreEqual("Sequence contains no elements", err.Message); }
public void Single_Async_NoSuchRecord() { string cqlToFindNothing = _selectAllDefaultCql + " where moviemaker ='" + Randomm.RandomAlphaNum(20) + "'"; try { _mapper.SingleAsync <Movie>(cqlToFindNothing).Wait(); } catch (AggregateException e) { Assert.AreEqual("Sequence contains no elements", e.InnerException.Message); } }
public void DeleteIf_NotAllKeysRestricted_ClusteringKeyOmitted() { // Validate pre-test state var table = new Table <AllDataTypesEntity>(_session, new MappingConfiguration()); var count = table.Count().Execute(); Assert.AreEqual(_entityList.Count, count); AllDataTypesEntity entityToDelete = _entityList[0]; // Test var selectQuery = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType + Randomm.RandomAlphaNum(10)); var deleteIfQuery = selectQuery.DeleteIf(m => m.IntType == entityToDelete.IntType); var ex = Assert.Throws <InvalidQueryException>(() => deleteIfQuery.Execute()); StringAssert.Contains( "DELETE statements must restrict all PRIMARY KEY columns with equality relations in order to use IF conditions, but column 'guid_type' is not restricted", ex.Message); }
public void DeleteIf_NoMatchingRecord() { // Validate pre-test state var table = new Table <AllDataTypesEntity>(_session, new MappingConfiguration()); var count = table.Count().Execute(); Assert.AreEqual(_entityList.Count, count); AllDataTypesEntity entityToDelete = _entityList[0]; // Test var selectQuery = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType + Randomm.RandomAlphaNum(10) && m.GuidType == Guid.NewGuid()); var deleteIfQuery = selectQuery.DeleteIf(m => m.IntType == entityToDelete.IntType); string deleteIfQueryToString = deleteIfQuery.ToString(); Console.WriteLine(deleteIfQueryToString); var appliedInfo = deleteIfQuery.Execute(); Assert.False(appliedInfo.Applied); }
public void LinqUpdate_UpdateBatchType(BatchType batchType) { // Setup Table <Movie> table = new Table <Movie>(_session, new MappingConfiguration()); table.CreateIfNotExists(); Movie movieToUpdate1 = _movieList[1]; Movie movieToUpdate2 = _movieList[2]; var batch = table.GetSession().CreateBatch(batchType); var expectedMovie1 = new Movie(movieToUpdate1.Title, movieToUpdate1.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate1.MovieMaker, 1212); var update1 = table.Where(m => m.Title == movieToUpdate1.Title && m.MovieMaker == movieToUpdate1.MovieMaker && m.Director == movieToUpdate1.Director) .Select(m => new Movie { Year = expectedMovie1.Year, MainActor = expectedMovie1.MainActor }) .Update(); batch.Append(update1); var expectedMovie2 = new Movie(movieToUpdate2.Title, movieToUpdate2.Director, "also_something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate2.MovieMaker, 1212); var update2 = table.Where(m => m.Title == movieToUpdate2.Title && m.MovieMaker == movieToUpdate2.MovieMaker && m.Director == movieToUpdate2.Director) .Select(m => new Movie { Year = expectedMovie2.Year, MainActor = expectedMovie2.MainActor }) .Update(); batch.Append(update2); batch.Execute(); List <Movie> actualMovieList = table.Execute().ToList(); Assert.AreEqual(_movieList.Count, actualMovieList.Count()); Assert.AreNotEqual(expectedMovie1.MainActor, expectedMovie2.MainActor); Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie1)); Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie2)); Movie.AssertListContains(actualMovieList, expectedMovie1); Movie.AssertListContains(actualMovieList, expectedMovie2); }
public void LinqUpdate_UpdateBatchType(BatchType batchType) { // Setup var table = new Table <Movie>(Session, new MappingConfiguration()); var movieToUpdate1 = _movieList[1]; var movieToUpdate2 = _movieList[2]; var batch = table.GetSession().CreateBatch(batchType); var expectedMovie1 = new Movie(movieToUpdate1.Title, movieToUpdate1.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate1.MovieMaker, 1212); var update1 = table.Where(m => m.Title == movieToUpdate1.Title && m.MovieMaker == movieToUpdate1.MovieMaker && m.Director == movieToUpdate1.Director) .Select(m => new Movie { Year = expectedMovie1.Year, MainActor = expectedMovie1.MainActor }) .Update(); batch.Append(update1); var expectedMovie2 = new Movie(movieToUpdate2.Title, movieToUpdate2.Director, "also_something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate2.MovieMaker, 1212); var update2 = table.Where(m => m.Title == movieToUpdate2.Title && m.MovieMaker == movieToUpdate2.MovieMaker && m.Director == movieToUpdate2.Director) .Select(m => new Movie { Year = expectedMovie2.Year, MainActor = expectedMovie2.MainActor }) .Update(); batch.Append(update2); batch.Execute(); VerifyBatchStatement( 1, new[] { $"UPDATE \"{Movie.TableName}\" " + "SET \"yearMade\" = ?, \"mainGuy\" = ? " + "WHERE \"unique_movie_title\" = ? AND \"movie_maker\" = ? AND \"director\" = ?", $"UPDATE \"{Movie.TableName}\" " + "SET \"yearMade\" = ?, \"mainGuy\" = ? " + "WHERE \"unique_movie_title\" = ? AND \"movie_maker\" = ? AND \"director\" = ?" }, new[] { new object[] { expectedMovie1.Year, expectedMovie1.MainActor, movieToUpdate1.Title, movieToUpdate1.MovieMaker, movieToUpdate1.Director }, new object[] { expectedMovie2.Year, expectedMovie2.MainActor, movieToUpdate2.Title, movieToUpdate2.MovieMaker, movieToUpdate2.Director } }); }
public void DeleteIf_NotAllKeysRestricted_ClusteringKeyOmitted() { // Validate pre-test state var table = new Table <AllDataTypesEntity>(_session, new MappingConfiguration()); var count = table.Count().Execute(); Assert.AreEqual(_entityList.Count, count); AllDataTypesEntity entityToDelete = _entityList[0]; // Test var selectQuery = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType + Randomm.RandomAlphaNum(10)); var deleteIfQuery = selectQuery.DeleteIf(m => m.IntType == entityToDelete.IntType); Assert.Throws <InvalidQueryException>(() => deleteIfQuery.Execute()); }
public void MassiveAsyncTest() { Cluster localCluster = _builder.Build(); ISession localSession = localCluster.Connect(); string keyspaceName = "kp_mat_" + Randomm.RandomAlphaNum(8); localSession.Execute( string.Format(@"CREATE KEYSPACE {0} WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 }};" , keyspaceName)); localSession.ChangeKeyspace(keyspaceName); string tableName = "table" + Randomm.RandomAlphaNum(8); localSession.Execute(string.Format(@"CREATE TABLE {0}( tweet_id uuid, author text, body text, isok boolean, PRIMARY KEY(tweet_id))", tableName)); int RowsNo = 100; var ar = new bool[RowsNo]; var thr = new Thread(() => { for (int i = 0; i < RowsNo; i++) { int tmpi = i; localSession.BeginExecute(string.Format(@"INSERT INTO {0} ( tweet_id, author, isok, body) VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid(), i, i % 2 == 0 ? "false" : "true") , ConsistencyLevel.One, _ => { ar[tmpi] = true; Interlocked.MemoryBarrier(); }, null); } }); thr.Start(); var done = new HashSet <int>(); while (done.Count < RowsNo) { for (int i = 0; i < RowsNo; i++) { Interlocked.MemoryBarrier(); if (!done.Contains(i) && ar[i]) { done.Add(i); } } } thr.Join(); Trace.TraceInformation("Inserted... now we are checking the count"); var ret = localSession.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100), ConsistencyLevel.Quorum); Assert.AreEqual(RowsNo, ret.GetRows().ToList().Count); localSession.Dispose(); }
public void Delete_NoSuchRecord() { var table = new Table <AllDataTypesEntity>(_session, new MappingConfiguration()); var count = table.Count().Execute(); Assert.AreEqual(_entityList.Count, count); AllDataTypesEntity entityToDelete = _entityList[0]; var selectQuery = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType + Randomm.RandomAlphaNum(16)); var deleteQuery = selectQuery.Delete(); deleteQuery.Execute(); // make sure record was not deleted count = table.Count().Execute(); Assert.AreEqual(_entityList.Count, count); List <AllDataTypesEntity> rows = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType).Execute().ToList(); Assert.AreEqual(1, rows.Count); }
public void Update_PartitionKeyOmitted() { // Setup Movie movieToUpdate = _movieList[1]; // Update to different values var expectedMovie = new Movie(movieToUpdate.Title + "_something_different", movieToUpdate.Director, "something_different_" + Randomm.RandomAlphaNum(10), null, 1212); var err = Assert.Throws <InvalidQueryException>(() => _mapper.Update(expectedMovie)); string expectedErrMsg = "Invalid null value (for partition key part|in condition for column) moviemaker"; StringAssert.IsMatch(expectedErrMsg, err.Message); }
public void Single_Async_NoSuchRecord() { string cqlToFindNothing = _selectAllDefaultCql + " where moviemaker ='" + Randomm.RandomAlphaNum(20) + "'"; Assert.Throws <AggregateException>(() => _mapper.SingleAsync <Movie>(cqlToFindNothing).Wait()); }
public void Single_NoSuchRecord() { string cqlToFindNothing = _selectAllDefaultCql + " where moviemaker ='" + Randomm.RandomAlphaNum(20) + "'"; Assert.Throws <InvalidOperationException>(() => _mapper.Single <Movie>(cqlToFindNothing)); }
public void LinqUpdate_Single(bool async) { // Setup var table = new Table <Movie>(Session, new MappingConfiguration()); var movieToUpdate = _movieList[1]; var expectedMovie = new Movie(movieToUpdate.Title, movieToUpdate.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate.MovieMaker, 1212); var updateQuery = table.Where(m => m.Title == movieToUpdate.Title && m.MovieMaker == movieToUpdate.MovieMaker && m.Director == movieToUpdate.Director) .Select(m => new Movie { Year = expectedMovie.Year, MainActor = expectedMovie.MainActor }) .Update(); if (async) { updateQuery.ExecuteAsync().GetAwaiter().GetResult(); } else { updateQuery.Execute(); } VerifyBoundStatement( $"UPDATE \"{Movie.TableName}\" " + "SET \"yearMade\" = ?, \"mainGuy\" = ? " + "WHERE \"unique_movie_title\" = ? AND \"movie_maker\" = ? AND \"director\" = ?", 1, expectedMovie.Year, expectedMovie.MainActor, movieToUpdate.Title, movieToUpdate.MovieMaker, movieToUpdate.Director); }
public void Append_ToArray_QueryUsingCql() { Tuple <Table <EntityWithArrayType>, List <EntityWithArrayType> > tupleArrayType = EntityWithArrayType.SetupDefaultTable(_session); Table <EntityWithArrayType> table = tupleArrayType.Item1; List <EntityWithArrayType> expectedEntities = tupleArrayType.Item2; string[] arrToAdd = new string[] { "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), }; EntityWithArrayType singleEntity = expectedEntities.First(); EntityWithArrayType expectedEntity = singleEntity.Clone(); List <string> strValsAsList = new List <string>(); strValsAsList.AddRange(expectedEntity.ArrayType); strValsAsList.AddRange(arrToAdd); expectedEntity.ArrayType = strValsAsList.ToArray(); // Append the values table.Where(t => t.Id == singleEntity.Id).Select(t => new EntityWithArrayType { ArrayType = CqlOperator.Append(arrToAdd) }).Update().Execute(); // Validate the final state of the data List <Row> rows = _session.Execute("SELECT * from " + table.Name + " where id='" + expectedEntity.Id + "'").GetRows().ToList(); Assert.AreEqual(1, rows.Count); Assert.AreNotEqual(expectedEntity.ArrayType, singleEntity.ArrayType); string[] actualArr = rows[0].GetValue <string[]>("arraytype"); Assert.AreEqual(expectedEntity.ArrayType, actualArr); }
public void ParallelInsertTest() { Cluster localCluster = _builder.Build(); ISession localSession = localCluster.Connect(); string keyspaceName = "kp_pi1_" + Randomm.RandomAlphaNum(10); localSession.Execute(string.Format(@"CREATE KEYSPACE {0} WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 }};", keyspaceName)); TestUtils.WaitForSchemaAgreement(localCluster); localSession.ChangeKeyspace(keyspaceName); for (int KK = 0; KK < 1; KK++) { string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { localSession.Execute(string.Format(@" CREATE TABLE {0}( tweet_id uuid, author text, body text, isok boolean, PRIMARY KEY(tweet_id))", tableName)); TestUtils.WaitForSchemaAgreement(localCluster); } catch (AlreadyExistsException) { } int RowsNo = 1000; var ar = new IAsyncResult[RowsNo]; var threads = new List <Thread>(); var monit = new object(); int readyCnt = 0; Trace.TraceInformation("Preparing..."); for (int idx = 0; idx < RowsNo; idx++) { int i = idx; threads.Add(new Thread(() => { try { lock (monit) { readyCnt++; Monitor.Wait(monit); } ar[i] = localSession.BeginExecute(string.Format(@" INSERT INTO {0} (tweet_id, author, isok, body) VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid(), i, i % 2 == 0 ? "false" : "true"), ConsistencyLevel.One, null, null); Interlocked.MemoryBarrier(); } catch { } })); } for (int idx = 0; idx < RowsNo; idx++) { threads[idx].Start(); } lock (monit) { while (true) { if (readyCnt < RowsNo) { Monitor.Exit(monit); Thread.Sleep(100); Monitor.Enter(monit); } else { Monitor.PulseAll(monit); break; } } } Trace.TraceInformation("Start!"); var done = new HashSet <int>(); while (done.Count < RowsNo) { for (int i = 0; i < RowsNo; i++) { Interlocked.MemoryBarrier(); if (!done.Contains(i) && ar[i] != null) { if (ar[i].AsyncWaitHandle.WaitOne(10)) { try { localSession.EndExecute(ar[i]); } catch (Exception ex) { Trace.TraceError("There was an exception while trying to end the async Execution: " + Environment.NewLine + ex); } done.Add(i); } } } } Trace.TraceInformation("Inserted... now we are checking the count"); var ret = localSession.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100), ConsistencyLevel.Quorum); Assert.AreEqual(RowsNo, ret.GetRows().ToList().Count); for (int idx = 0; idx < RowsNo; idx++) { threads[idx].Join(); } } }
public void LinqFirstOrDefault_Sync_NoSuchRecord() { var first = _movieTable.FirstOrDefault(m => m.Director == "non_existant_" + Randomm.RandomAlphaNum(10)).Execute(); Assert.IsNull(first); }
public void Prepend_ToArray() { Tuple <Table <EntityWithArrayType>, List <EntityWithArrayType> > tupleArrayType = EntityWithArrayType.SetupDefaultTable(_session); Table <EntityWithArrayType> table = tupleArrayType.Item1; List <EntityWithArrayType> expectedEntities = tupleArrayType.Item2; string[] arrToAdd = new string[] { "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), "random_" + Randomm.RandomAlphaNum(10), }; EntityWithArrayType singleEntity = expectedEntities.First(); // Append the values table.Where(t => t.Id == singleEntity.Id).Select(t => new EntityWithArrayType { ArrayType = CqlOperator.Prepend(arrToAdd) }).Update().Execute(); // Validate the final state of the data var entityList = table.Where(m => m.Id == singleEntity.Id).ExecuteAsync().Result.ToList(); Assert.AreEqual(1, entityList.Count); CollectionAssert.AreEqual(arrToAdd.Concat(singleEntity.ArrayType).OrderBy(v => v), entityList[0].ArrayType.OrderBy(v => v)); }
public void LinqUpdate_Single_Async() { // Setup Table <Movie> table = new Table <Movie>(_session, new MappingConfiguration()); table.CreateIfNotExists(); Movie movieToUpdate = _movieList[1]; var expectedMovie = new Movie(movieToUpdate.Title, movieToUpdate.Director, "something_different_" + Randomm.RandomAlphaNum(10), movieToUpdate.MovieMaker, 1212); table.Where(m => m.Title == movieToUpdate.Title && m.MovieMaker == movieToUpdate.MovieMaker && m.Director == movieToUpdate.Director) .Select(m => new Movie { Year = expectedMovie.Year, MainActor = expectedMovie.MainActor }) .Update() .Execute(); List <Movie> actualMovieList = table.ExecuteAsync().Result.ToList(); Assert.AreEqual(_movieList.Count, actualMovieList.Count()); Assert.IsFalse(Movie.ListContains(_movieList, expectedMovie)); Movie.AssertListContains(actualMovieList, expectedMovie); }
public void Delete_NoSuchRecord() { var table = new Table <AllDataTypesEntity>(Session, new MappingConfiguration()); var entityToDelete = _entityList[0]; var selectQuery = table.Select(m => m).Where(m => m.StringType == entityToDelete.StringType + Randomm.RandomAlphaNum(16)); var deleteQuery = selectQuery.Delete(); deleteQuery.Execute(); // make sure record was not deleted var listOfExecutionParameters = GetBoundStatementExecutionParameters( $"DELETE FROM \"{AllDataTypesEntity.TableName}\" WHERE \"string_type\" = ?").ToList(); Assert.AreEqual(1, listOfExecutionParameters.Count); var parameter = Convert.FromBase64String((string)listOfExecutionParameters.Single().Single()); var actualParameter = (string)Session.Cluster.Metadata.ControlConnection.Serializer.GetCurrentSerializer().Deserialize(parameter, 0, parameter.Length, ColumnTypeCode.Text, null); Assert.AreNotEqual(entityToDelete.StringType, actualParameter); Assert.IsTrue(actualParameter.StartsWith(entityToDelete.StringType)); Assert.IsTrue(actualParameter.Length > entityToDelete.StringType.Length); }