Exemplo n.º 1
0
        public void TestSavingCallbackCalled()
        {
            var context = new MyEntityContext(_connectionString);
            _changedItems.Clear();
            context.SavingChanges += LogChangedItems;

            var alice = new Person {Name = "Alice"};
            context.Persons.Add(alice);
            var bob = context.Persons.Create();
            bob.Name = "Bob";
            context.SaveChanges();

            Assert.AreEqual(2, _changedItems.Count);
            Assert.IsTrue(_changedItems.Cast<Person>().Any(p=>p.Id.Equals(alice.Id)));
            Assert.IsTrue(_changedItems.Cast<Person>().Any(p=>p.Id.Equals(bob.Id)));
            _changedItems.Clear();

            bob.Friends.Add(alice);
            context.SaveChanges();
            Assert.AreEqual(1, _changedItems.Count);
            Assert.IsTrue(_changedItems.Cast<Person>().Any(p => p.Id.Equals(bob.Id)));
            _changedItems.Clear();

            var skill = new Skill {Name = "Programming"};
            context.Skills.Add(skill);
            context.SaveChanges();
            _changedItems.Clear();

            skill.SkilledPeople.Add(bob);
            context.SaveChanges();
            Assert.AreEqual(1, _changedItems.Count);
            Assert.IsTrue(_changedItems.Cast<Person>().Any(p => p.Id.Equals(bob.Id)));
            _changedItems.Clear();

        }
Exemplo n.º 2
0
        public void TestSaveWorksWhenNoCallback()
        {
            var context = new MyEntityContext(_connectionString);
            var carol = new Person { Name = "Carol" };
            context.Persons.Add(carol);
            context.SaveChanges();

            var found = context.Persons.FirstOrDefault(p => p.Name.Equals("Carol"));
            Assert.IsNotNull(found);
        }
Exemplo n.º 3
0
 public void TestPopulateEntityProperty()
 {
     using (var context = GetContext())
     {
         var fred = new Person{Name = "Fred"};
         var newEntity = new NoId {Name = "TestPopulateEntityProperty", Owner = fred};
         context.NoIds.Add(newEntity);
         context.Persons.Add(fred);
         context.SaveChanges();
     }
     using (var context = GetContext())
     {
         var existingEntity = context.NoIds.FirstOrDefault(x => x.Name.Equals("TestPopulateEntityProperty"));
         Assert.That(existingEntity, Is.Not.Null);
         Assert.That(existingEntity.Owner, Is.Not.Null);
         Assert.That(existingEntity.Owner.Name, Is.EqualTo("Fred"));
     }
 }
Exemplo n.º 4
0
        public void TestCustomTriplesQuery()
        {
            string storeName = Guid.NewGuid().ToString();
            var people = new Person[10];
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var person = new Person { Age = 40 - i, Name = "Person #" + i };
                        context.Persons.Add(person);
                        people[i] = person;
                    }
                    context.SaveChanges();
                }
            }


            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var query = @"
select  ?s ?p ?o
where {
   ?s ?p ?o.
        ?s a <http://www.example.org/schema/Person>
}
";
                    IList<Person> results;
                    results = context.ExecuteQuery<Person>(query).ToList();
                    Assert.AreEqual(10, results.Count);

                    foreach (Person person in results)
                    {
                        Assert.AreNotEqual(0, person.Age);
                    }
                }
            }
        }
 public void TestThrowingExceptionAbortsChanges()
 {
     using (var context = new MyEntityContext(_connectionString))
     {
         context.SavingChanges += ThrowOnChange;
         var dave = new Person {Name = "Dave"};
         context.Persons.Add(dave);
         try
         {
             context.SaveChanges();
         }
         catch (ApplicationException)
         {
             // Expected
         }
     }
     using (var context = new MyEntityContext(_connectionString))
     {
         var found = context.Persons.FirstOrDefault(p => p.Name.Equals("Dave"));
         Assert.IsNull(found);
     }
 }
Exemplo n.º 6
0
        public void TestSkipAndTake()
        {
            string storeName = Guid.NewGuid().ToString();
            var dataObjectStore = _dataObjectContext.CreateStore(storeName);
            var context = new MyEntityContext(dataObjectStore);
            var people= new Person[10];
            for(int i = 0; i < 10; i++)
            {
                var person = new Person {Age = 40 - i, Name = "Person #" + i};
                context.Persons.Add(person);
                people[i] = person;
            }
            context.SaveChanges();

            dataObjectStore = _dataObjectContext.OpenStore(storeName);
            context = new MyEntityContext(dataObjectStore);
            
            // Take, skip and skip and take with no other query expression
            var top3 = context.Persons.Take(3).ToList();
            Assert.AreEqual(3, top3.Count);
            foreach(var p in top3)
            {
                Assert.IsTrue(people.Any(x=>p.Id.Equals(x.Id)));
            }
            var after3 = context.Persons.Skip(3).ToList();
            Assert.AreEqual(7, after3.Count);
            var nextPage = context.Persons.Skip(3).Take(3).ToList();
            Assert.AreEqual(3, nextPage.Count);

            // Combined with a sort expression
            var top3ByAge = context.Persons.OrderByDescending(p => p.Age).Take(3).ToList();
            Assert.AreEqual(3, top3ByAge.Count);
            foreach(var p in top3ByAge) Assert.IsTrue(p.Age >= 38);

            var allButThreeOldest = context.Persons.OrderByDescending(p => p.Age).Skip(3).ToList();
            Assert.AreEqual(7, allButThreeOldest.Count);
            foreach(var p in allButThreeOldest) Assert.IsFalse(p.Age >= 38);

            var nextThreeOldest = context.Persons.OrderByDescending(p => p.Age).Skip(3).Take(3).ToList();
            Assert.AreEqual(3, nextThreeOldest.Count);
            foreach(var p in nextThreeOldest) Assert.IsTrue(p.Age < 38 && p.Age > 34);
        }
Exemplo n.º 7
0
        public void TestAddGeneratesIdentity()
        {
            string storeName = Guid.NewGuid().ToString();
            var dataObjectStore = _dataObjectContext.CreateStore(storeName);
            var context = new MyEntityContext(dataObjectStore);
            var skill = new Skill {Name = "Bar"};
            var person = new Person {Name = "Kal"};
            //var person2 = new Person2 {Name = "Jen"};
            var company = new Company {Name = "NetworkedPlanet"};
            context.Persons.Add(person);
            //context.Person2s.Add(person2);
            context.Skills.Add(skill);
            context.Companies.Add(company);
            context.SaveChanges();

            dataObjectStore = _dataObjectContext.OpenStore(storeName);
            context = new MyEntityContext(dataObjectStore);
            var foundPerson = context.Persons.Where(p => p.Name.Equals("Kal")).FirstOrDefault();
            //var foundPerson2 = context.Person2s.Where(p => p.Name.Equals("Jen")).FirstOrDefault();
            var foundSkill = context.Skills.FirstOrDefault(s => s.Name.Equals("Bar"));
            var foundCompany = context.Companies.FirstOrDefault(s => s.Name.Equals("NetworkedPlanet"));
            Assert.IsNotNull(foundPerson);
            //Assert.IsNotNull(foundPerson2);
            Assert.IsNotNull(foundSkill);
            Assert.IsNotNull(foundCompany);

            Assert.IsTrue(foundPerson.Id.StartsWith(Constants.GeneratedUriPrefix));
            Assert.IsTrue(foundCompany.Id.StartsWith(Constants.GeneratedUriPrefix));
            Guid guid;
            Assert.IsTrue(Guid.TryParse(foundSkill.Id, out guid));
        }
Exemplo n.º 8
0
        public void TestSetPropertiesThenAttach()
        {
            string storeName = Guid.NewGuid().ToString();
            var dataObjectStore = _dataObjectContext.CreateStore(storeName);
            var context = new MyEntityContext(dataObjectStore);
            var person = new Person {Name = "Kal", DateOfBirth = new DateTime(1970, 12, 12),
            Friends = new List<IPerson>
                          {
                              new Person{Name="Gra", Id = "http://example.org/people/1234"},
                              new Person{Name="Stu", Id = "http://example.org/people/456"}
                          }};
            person.Id = "http://example.org/people/123";
            person.Context = context;
            context.SaveChanges();

            dataObjectStore = _dataObjectContext.OpenStore(storeName);
            context = new MyEntityContext(dataObjectStore);
            var found = context.Persons.Where(p => p.Id.Equals("http://example.org/people/123")).FirstOrDefault();
            Assert.IsNotNull(found);
            Assert.AreEqual("Kal", found.Name);
            Assert.AreEqual(new DateTime(1970, 12, 12), found.DateOfBirth);

            found = context.Persons.Where(p => p.Id.Equals("http://example.org/people/1234")).FirstOrDefault();
            Assert.IsNotNull(found);
            Assert.AreEqual("Gra", found.Name);

            found = context.Persons.Where(p => p.Id.Equals("http://example.org/people/456")).FirstOrDefault();
            Assert.IsNotNull(found);
            Assert.AreEqual("Stu", found.Name);
        }
Exemplo n.º 9
0
        public void TestSetContextAndIdentityProperties()
        {
            string storeName = Guid.NewGuid().ToString();
            var dataObjectStore = _dataObjectContext.CreateStore(storeName);
            var context = new MyEntityContext(dataObjectStore);

            var person = new Person
                             {
                                 Context = context,
                                 Id = "http://example.org/people/123",
                                 Name = "Kal",
                                 DateOfBirth = new DateTime(1970, 12, 12)
                             };
            
            context.SaveChanges();

            dataObjectStore = _dataObjectContext.OpenStore(storeName);
            context = new MyEntityContext(dataObjectStore);
            var found = context.Persons.Where(p => p.Id.Equals("http://example.org/people/123")).FirstOrDefault();
            Assert.IsNotNull(found);
        }
Exemplo n.º 10
0
        public void TestCollectionUpdatedByInverseProperty()
        {
            var storeName = "TestCollectionUpdatedByInverseProperty_" + DateTime.Now.Ticks;
            using (var context = CreateEntityContext(storeName))
            {
                var dept = new Department {Name = "Research"};
                context.Departments.Add(dept);
                
                // Attach before property is set
                var alice = new Person {Name = "Alice"};
                context.Persons.Add(alice);
                alice.Department = dept;
                Assert.AreEqual(1, dept.Persons.Count);
                
                // Attach after property set
                var bob = new Person {Name = "Bob", Department = dept};
                context.Persons.Add(bob);
                Assert.AreEqual(2, dept.Persons.Count);

                // Attach after property set by explicit call
                var charlie = new Person { Name = "Charlie"};
                charlie.Department = dept;
                context.Persons.Add(charlie);
                Assert.AreEqual(3, dept.Persons.Count);

                // Not attached before checking inverse property
                var dave = new Person { Name = "Dave", Department = dept };
                Assert.AreEqual(3, dept.Persons.Count);
                context.Persons.Add(dave);
                Assert.AreEqual(4, dept.Persons.Count);
                
                context.SaveChanges();

                context.DeleteObject(bob);
                context.SaveChanges();

                Assert.AreEqual(3, dept.Persons.Count);
            }
        }
Exemplo n.º 11
0
        public void TestCustomTriplesQueryWithMultipleResultTypes()
        {
            string storeName = Guid.NewGuid().ToString();
            var people = new Person[10];
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var person = new Person { Age = 40 - i, Name = "Person #" + i };
                        context.Persons.Add(person);
                        people[i] = person;

                        if (i >= 5)
                        {
                            var session = new Session { Speaker = "Person #" + i };
                            context.Sessions.Add(session);
                        }
                    }
                    context.SaveChanges();
                }
            }


            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var query = @"
select  ?s ?p ?o
where {
   ?s ?p ?o.
   {?s a <http://www.example.org/schema/Person>}
    union
   {?s a <http://www.example.org/schema/Session>}

}

";
                    var results = context.ExecuteQueryToResultTypes(query);
                    var persons = results[typeof(Person)].Cast<Person>().ToList();
                    Assert.AreEqual(10, persons.Count);

                    var sessions = results[typeof(Session)].Cast<Session>().ToList();
                    Assert.AreEqual(5, sessions.Count);

                }
            }



        }
Exemplo n.º 12
0
        public void TestSetPropertiesThenAttach()
        {
            string storeName = Guid.NewGuid().ToString();
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    // ReSharper disable UseObjectOrCollectionInitializer
                    // Purposefully setting properties and then attaching Context property
                    var person = new Person
                        {
                            Name = "Kal",
                            DateOfBirth = new DateTime(1970, 12, 12),
                            Friends = new List<IPerson>
                                {
                                    new Person {Name = "Gra", Id = "http://example.org/people/1234"},
                                    new Person {Name = "Stu", Id = "http://example.org/people/456"}
                                }
                        };
                    person.Id = "http://example.org/people/123";
                    person.Context = context;
                    // ReSharper restore UseObjectOrCollectionInitializer
                    context.SaveChanges();
                }
            }
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var found =
                        context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/123"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Kal", found.Name);
                    Assert.AreEqual(new DateTime(1970, 12, 12), found.DateOfBirth);

                    found = context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/1234"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Gra", found.Name);

                    found = context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/456"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Stu", found.Name);
                }
            }
        }
Exemplo n.º 13
0
 public void TestLinqRetrieveId()
 {
     var connectionString = GetConnectionString("TestLinqRetrieveId");
     using (var context = new MyEntityContext(connectionString))
     {
         var alice = new Person {Id = "alice", Name = "Alice"};
         context.Persons.Add(alice);
         context.SaveChanges();
     
         var entity = context.Persons.First();
         Assert.That(entity.Id, Is.EqualTo("alice"));
         var id = context.Persons.Select(x=>x.Id).First();
         Assert.That(id, Is.EqualTo("alice"));
     }
 }