public void AddWithExistingKey() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; context.Values.Add(value); var value2 = new MyValue { Id = 1, Value = "2" }; Assert.IsFalse(context.Values.Add(value2)); } }
public void DeleteNonTracked() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Delete(value)); Assert.AreEqual(1, context.Values.Local.Count()); Assert.IsFalse(context.Values.Add(value)); Assert.AreEqual(1, context.Values.Local.Count()); } }
public void AddRange() { using (var context = new MyContext()) { var values = new[] { new MyValue { Id = 1, Value = "1" }, new MyValue { Id = 2, Value = "2" }, new MyValue { Id = 3, Value = "3" }, new MyValue { Id = 4, Value = "4" } }; //add and check if there context.Values.AddRange(values); Assert.AreEqual(4, context.ChangeTracker.Entries().Count()); //add again, and check if no changes... context.Values.AddRange(values); Assert.AreEqual(4, context.ChangeTracker.Entries().Count()); } }
public void WhereContainsWithPageSize() { using (var context = new MyContext(ConnectionString)) { if (context.Database.Connection.State == ConnectionState.Closed) context.Database.Connection.Open(); if (context.Database.Connection.ServerVersion.CompareTo("2.0.4") < 0) { Debug.WriteLine("Cassandra bug in place: https://issues.apache.org/jira/browse/CASSANDRA-6464"); return; } var values = context.Values.Where(r => new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 }.Contains(r.Id)).WithPageSize(10).ToList(); Assert.AreEqual(21, values.Count, "Unexpected number of results"); for (int i = 1; i <= 21; i++) { Assert.IsTrue(values.Any(v => v.Id == i), "Missing value " + i); } } }
public void NoTrackingContextAddAttachDeleteDetachEntity() { using (var context = new MyContext()) { context.TrackChanges = false; var value = new MyValue { Id = 1, Value = "1" }; Assert.IsFalse(context.Values.Attach(value)); Assert.IsFalse(context.Values.Add(value)); Assert.IsFalse(context.Values.Delete(value)); Assert.IsFalse(context.Values.Detach(value)); Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.Values.Local.Count()); } }
public void AddThenAttach() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; context.Values.Add(value); Assert.IsFalse(context.Values.Attach(value)); } }
public void CompileLongCount() { Func<MyContext, int, long> compiledQuery = CompiledQuery.Compile<MyContext, int, long>( (context, id) => context.Values.LongCount(val => val.Id == id)); using (var context = new MyContext(ConnectionString)) { var count = compiledQuery(context, 2); Assert.AreEqual(1, count); } }
public void CompileSingleArgument() { Func<MyContext, int, MyValue> compiledQuery = CompiledQuery.Compile<MyContext, int, MyValue>( (context, id) => context.Values.Single(val => val.Id == id)); using (var context = new MyContext(ConnectionString)) { var singleValue = compiledQuery(context, 2); Assert.IsNotNull(singleValue); Assert.AreEqual(2, singleValue.Id); Assert.AreEqual("Hallo 2", singleValue.Value); } }
public void UpdateInExternalTransaction() { using (var connection = new CqlConnection(ConnectionString)) using (var transaction = connection.BeginTransaction()) using (var context = new MyContext(connection, false)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); context.Database.UseTransaction(transaction); var value = context.Values.Find(500); value.Value = "Hallo daar!"; context.SaveChanges(false); var command = new CqlCommand(connection, "update myvalue set value='adjusted' where id=500"); command.Transaction = transaction; command.ExecuteNonQuery(); transaction.Commit(); //accept all changes only after commit context.AcceptAllChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(500); Assert.IsNotNull(value); Assert.AreEqual("adjusted", value.Value); } }
public void UpdateTwiceInTwoTransactions() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(400); using (var transaction1 = context.Database.BeginTransaction()) { value.Value = "Hallo daar!"; context.SaveChanges(); transaction1.Commit(); } using (var transaction2 = context.Database.BeginTransaction()) { transaction2.BatchType = CqlBatchType.Unlogged; value.Value = "Nog een keer"; context.SaveChanges(); transaction2.Commit(); } } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(400); Assert.IsNotNull(value); Assert.AreEqual("Nog een keer", value.Value); } }
public void UpdateTwiceInSingleTransactionAndRollback() { using (var context = new MyContext(ConnectionString)) using (var transaction = context.Database.BeginTransaction()) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(300); value.Value = "Hallo daar!"; context.SaveChanges(); value.Value = "Oops..."; context.SaveChanges(); transaction.Rollback(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(300); Assert.IsNotNull(value); Assert.AreEqual("Hallo 300", value.Value); } }
public void FindAndFindAgain() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; var value = context.Values.Find(100); var value2 = context.Values.Find(100); //check if same value is returned Assert.AreSame(value, value2); //only one query was executed Assert.AreEqual(1, count); } }
public void FindUpdateAndReload() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); //get entity var value = context.Values.Find(101); Assert.IsNotNull(value); //get entry var entry = context.ChangeTracker.Entry(value); Assert.IsNotNull(entry); //change it value.Value = "Hallo daar!"; Assert.IsTrue(context.ChangeTracker.DetectChanges()); Assert.AreEqual(EntityState.Modified, entry.State); //reload entry.Reload(); Assert.AreEqual(EntityState.Unchanged, entry.State); Assert.IsFalse(context.ChangeTracker.HasChanges()); } }
public void FindAndUpdate() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(100); value.Value = "Hallo daar!"; context.SaveChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(100); Assert.IsNotNull(value); Assert.AreEqual("Hallo daar!", value.Value); } }
public void EmumerateMultiple() { using (var context = new MyContext(ConnectionString)) { var query = context.Values.Where(r => new[] { 1, 2, 3, 4 }.Contains(r.Id)); Assert.AreEqual(4, query.Count(), "Unexpected number of results"); var results = query.ToList(); for (int i = 1; i <= 4; i++) { Assert.IsTrue(results.Any(v => v.Id == i), "Missing value " + i); } } }
public void CompileSimpleSelect() { Func<MyContext, MyValue> compiledQuery = CompiledQuery.Compile<MyContext, MyValue>( (context) => context.Values.First(val => val.Id == 1)); using (var context = new MyContext(ConnectionString)) { var first = compiledQuery(context); Assert.IsNotNull(first); Assert.AreEqual(1, first.Id); Assert.AreEqual("Hallo 1", first.Value); } }
public void CompileSimpleSelectWithParam() { Func<MyContext, string, string> compiledQuery = CompiledQuery.Compile<MyContext, string, string>( (context, append) => context.Values.Where(val => val.Id == 1).Select(val => val.Value + append).First()); using (var context = new MyContext(ConnectionString)) { var first = compiledQuery(context, " ook"); Assert.IsNotNull(first); Assert.AreEqual("Hallo 1 ook", first); } }
public void SelectAndUpdate() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var query = context.Values.Where(r => new[] { 201, 202, 203, 204 }.Contains(r.Id)).ToList(); query[1].Value = "Zo gaan we weer verder"; context.SaveChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(202); Assert.IsNotNull(value); Assert.AreEqual("Zo gaan we weer verder", value.Value); } }
public void CompileAny() { Func<MyContext, int, bool> compiledQuery = CompiledQuery.Compile<MyContext, int, bool>( (context, id) => context.Values.Any(val => val.Id == id)); using (var context = new MyContext(ConnectionString)) { var any = compiledQuery(context, 2); Assert.AreEqual(true, any); } }
public async Task SelectAndDelete() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var query = context.Values.Where(r => new[] { 701, 702, 703, 704 }.Contains(r.Id)).ToList(); EntityEntry<MyValue> entry = context.ChangeTracker.Entry(query[1]); Assert.IsNotNull(entry); Assert.AreEqual(EntityState.Unchanged, entry.State); Assert.IsTrue(context.Values.Delete(query[1])); Assert.AreEqual(EntityState.Deleted, entry.State); await context.SaveChangesAsync(); Assert.AreEqual(EntityState.Detached, entry.State); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(702); Assert.IsNull(value, "Value was not deleted"); } }
public void CompileWhereContainsToDictionary() { Func<MyContext, IEnumerable<int>, IDictionary<int, MyValue>> compiledQuery = CompiledQuery.Compile<MyContext, IEnumerable<int>, IDictionary<int, MyValue>> ( (context, ids) => context.Values.Where(r => ids.Contains(r.Id)).ToDictionary(v => v.Id) ); using (var context = new MyContext(ConnectionString)) { var values = compiledQuery(context, new[] { 1, 2, 3, 4 }); Assert.AreEqual(4, values.Count, "Unexpected number of results"); for (int i = 1; i <= 4; i++) { Assert.IsTrue(values.Keys.Any(key => key == i), "Missing value " + i); } } }
public void AddNewEntity() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; var newValue = new MyValue { Id = 20000, Value = "Hallo 20000" }; bool added = context.Values.Add(newValue); Assert.IsTrue(added); context.SaveChanges(); //try save again (should do nothing) context.SaveChanges(); Assert.AreEqual(1, count, "Save again introduces new query!"); //try find (should do nothing) var entity = context.Values.Find(20000); Assert.AreSame(newValue, entity); Assert.AreEqual(1, count, "Find introduces new query!"); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(20000); Assert.IsNotNull(value); Assert.AreEqual("Hallo 20000", value.Value); } }
public void AttachDetach() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Attach(value)); Assert.IsFalse(context.Values.Attach(value)); Assert.IsTrue(context.Values.Detach(value)); Assert.IsFalse(context.Values.Detach(value)); Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.Values.Local.Count()); } }
public void AddAndChangeNewEntity() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; var newValue = new MyValue { Id = 30000, Value = "Hallo 30000" }; bool added = context.Values.Add(newValue); Assert.IsTrue(added); IEntityEntry entry = context.ChangeTracker.Entry(newValue); Assert.IsNotNull(entry); Assert.AreEqual(EntityState.Added, entry.State); context.SaveChanges(); Assert.AreEqual(EntityState.Unchanged, entry.State); newValue.Value = "Hallo weer!"; context.SaveChanges(); Assert.AreEqual(2, count, "Where is my query?"); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(30000); Assert.IsNotNull(value); Assert.AreEqual("Hallo weer!", value.Value); } }
public void ContextDisableTrackingClearsTrackedEntries() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Add(value)); Assert.IsTrue(context.ChangeTracker.HasChanges()); context.TrackChanges = false; Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.ChangeTracker.Entries<MyValue>().Count()); Assert.AreEqual(0, context.ChangeTracker.Entries().Count()); } }
public void QueryNoTracking() { using (var context = new MyContext(ConnectionString)) { var query = context.Values.Where(r => new[] { 701, 702, 703, 704 }.Contains(r.Id)).AsNoTracking().ToList(); Assert.IsNull(context.ChangeTracker.Entry(query[1])); } }
public void QueryContextNoTracking() { using (var context = new MyContext(ConnectionString)) { context.TrackChanges = false; //query, and check if tracked var query = context.Values.Where(r => new[] { 701, 702, 703, 704 }.Contains(r.Id)).ToList(); Assert.IsNull(context.ChangeTracker.Entry(query[1])); } }
public async Task FindContextNoTracking() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.TrackChanges = false; context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; //find and check if tracked var entity = await context.Values.FindAsync(1); Assert.IsNotNull(entity); Assert.IsNull(context.ChangeTracker.Entry(entity)); //find again and check if tracked var entity2 = await context.Values.FindAsync(1); Assert.IsNotNull(entity); Assert.IsNull(context.ChangeTracker.Entry(entity2)); //expecting two different objects Assert.AreNotSame(entity, entity2); //expecting two queries Assert.AreEqual(2, count); } }
public void ChangeKeyResultsInException() { using (var context = new MyContext()) { context.ChangeTracker.AutoDetectChangesEnabled = false; var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Add(value)); Assert.IsTrue(context.ChangeTracker.HasChanges()); value.Id = 2; //next should throw exception context.ChangeTracker.DetectChanges(); } }
public void WhereContainsWithConsistency() { using (var context = new MyContext(ConnectionString)) { var values = context.Values.Where(r => new[] { 1, 2, 3, 4 }.Contains(r.Id)).WithConsistency(CqlConsistency.One).ToList(); Assert.AreEqual(4, values.Count, "Unexpected number of results"); for (int i = 1; i <= 4; i++) { Assert.IsTrue(values.Any(v => v.Id == i), "Missing value " + i); } } }