The context used for testing
Inheritance: CqlContext
Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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());

            }
        }
Ejemplo n.º 3
0
        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());

            }
        }
Ejemplo n.º 4
0
        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);
                }
            }
        }
Ejemplo n.º 5
0
        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());
            }
        }
Ejemplo n.º 6
0
        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));
            }
        }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        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);
            }
        }
Ejemplo n.º 9
0
        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);
            }
        }
Ejemplo n.º 10
0
        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);
            }
        }
Ejemplo n.º 11
0
        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);
            }
        }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
        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());
            }
        }
Ejemplo n.º 14
0
        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);
            }
        }
Ejemplo n.º 15
0
        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);
                }
            }
        }
Ejemplo n.º 16
0
        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);
            }
        }
Ejemplo n.º 17
0
        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);
            }
        }
Ejemplo n.º 18
0
        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);
            }
        }
Ejemplo n.º 19
0
        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);
            }
        }
Ejemplo n.º 20
0
        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");
            }
        }
Ejemplo n.º 21
0
        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);
                }
            }
        }
Ejemplo n.º 22
0
        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);
            }
        }
Ejemplo n.º 23
0
        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());
            }
        }
Ejemplo n.º 24
0
        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);
            }
        }
Ejemplo n.º 25
0
        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());
            }
        }
Ejemplo n.º 26
0
        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]));
            }
        }
Ejemplo n.º 27
0
        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]));
            }
        }
Ejemplo n.º 28
0
        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);
            }
        }
Ejemplo n.º 29
0
        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();
            }
        }
Ejemplo n.º 30
0
        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);
                }
            }
        }