public void TestCreateAndRetrieve() { string storeName = MakeStoreName("TestCreateAndRetrieve", Configuration.PersistenceType); string personId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.Create(); Assert.IsNotNull(person); context.SaveChanges(); Assert.IsNotNull(person.Id); personId = person.Id; } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Id == personId); Assert.IsNotNull(person); } } }
public void TestCreateAndRetrieve() { string storeName = Guid.NewGuid().ToString(); string personId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.Create(); Assert.IsNotNull(person); context.SaveChanges(); Assert.IsNotNull(person.Id); personId = person.Id; } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Id == personId); Assert.IsNotNull(person); } } }
private void CreateBatch(int batchNumber) { using (var context = new MyEntityContext(_storeConnectionString)) { for (int i = 0; i < BatchSize; i++) { CreatePerson(context, (batchNumber*BatchSize) + i); } context.SaveChanges(); } }
public void TestLoadAndQuery25K(PersistenceType persistenceType) { var storeName = MakeStoreName("TestLoadAndQuery10K", persistenceType); const int personCount = 25000; var hugosCount = personCount/TestDataLists.Firstnames.Count; // Estimated number of instances with name 'HUGO' we will generate using (var doStore = CreateStore(storeName, persistenceType)) { using (var context = new MyEntityContext(doStore)) { var last10 = new IFoafPerson[10]; for (int i = 0; i < personCount; i++) { // Create an entity with properties set var person = context.FoafPersons.Create(); person.Name = TestDataLists.Firstnames[i%TestDataLists.Firstnames.Count]; person.Organisation = TestDataLists.Organizations[i%TestDataLists.Organizations.Count]; foreach (var friend in last10.Where(friend => friend != null)) { person.Knows.Add(friend); } last10[i%10] = person; } context.SaveChanges(); // Attempt a query using the same store handle var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList(); Assert.IsTrue(hugos.Count >= hugosCount, "Expected at least {0} foafPerson instances returned with name HUGO", hugosCount); hugosCount = hugos.Count; // Acual number may be one higher depending on where the iteration leaves off. } } // Try query with a new store context using (var doStore = OpenStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList(); Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a second context instance"); } } // Try query with a completely new client session BrightstarDB.Client.BrightstarService.Shutdown(); using (var doStore = OpenStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList(); Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a third context instance"); } } }
public HttpResponseMessage SaveUserPreference(string user, string preferences) { string connectionString = Constants.storeConnectionString; var client = BrightstarService.GetClient(connectionString); UserPreferenceModel userPreferencesModel = new JavaScriptSerializer().Deserialize<UserPreferenceModel>(preferences); using (var ctx = new MyEntityContext(connectionString)) { var userPreference = ctx.UserPreferences.Create(); List<IInterest> interestsList = new List<IInterest>(); foreach (var item in userPreferencesModel.Interests) { var interests = ctx.Interests.Create(); interests.Name = item.Name; interests.Checked = item.Checked; interestsList.Add(interests); } //add Interests collection ICollection<IInterest> interestsCollection = interestsList; userPreference.Interests = interestsCollection; List<IInterest> cuisineList = new List<IInterest>(); foreach (var item in userPreferencesModel.Cuisine) { var interests = ctx.Interests.Create(); interests.Name = item.Name; interests.Checked = item.Checked; cuisineList.Add(interests); } //add Cuisine collection ICollection<IInterest> cuisineCollection = cuisineList; userPreference.Cuisine = cuisineCollection; userPreference.NeedWheelchair = userPreferencesModel.NeedWheelchair; var currentUser = ctx.Users.FirstOrDefault(x => x.Email == user); IUserPreference usrPreferences = userPreference; currentUser.UserPreference = usrPreferences; ctx.SaveChanges(); return new HttpResponseMessage(HttpStatusCode.OK);//200 } }
public Collection<BaprLocation> Favorites(string user) { string connectionString = Constants.storeConnectionString; var client = BrightstarService.GetClient(Constants.storeConnectionString); using (var ctx = new MyEntityContext(connectionString)) { IUser usr = ctx.Users.Single(x => x.Email == user); ICollection<ILocation> result; if (!usr.MarkedLocations.Any()) result = new Collection<ILocation>(); else result = usr.MarkedLocations.Where(x => x.IsFavorite == true).ToList(); Collection<BaprLocation> baprLocations = Convert(result); return baprLocations; } }
public HttpResponseMessage MarkLocation([FromBody] FormDataCollection formVars) { string user = formVars["user"]; string type = formVars["type"]; string location = formVars["location"]; string connectionString = Constants.storeConnectionString; var client = BrightstarService.GetClient(Constants.storeConnectionString); ILocation markedLocation = JsonConvert.DeserializeObject<Location>(location); using (var ctx = new MyEntityContext(connectionString)) { IUser usr = ctx.Users.Single(x => x.Email == user); if (!usr.MarkedLocations.Any(x => x.Equals(markedLocation))) { var marker = ctx.Locations.Create(); marker.name = markedLocation.name; marker.latitude = markedLocation.latitude; marker.longitude = markedLocation.longitude; marker.attributes = markedLocation.attributes; if(type == "fav") marker.IsFavorite = markedLocation.IsFavorite; else marker.IsVisited = markedLocation.IsVisited; usr.MarkedLocations.Add(marker); } else { //update existing location ILocation existingLocation = usr.MarkedLocations.Single(x => x.Equals(markedLocation)); if (type == "fav") existingLocation.IsFavorite = markedLocation.IsFavorite; else existingLocation.IsVisited = markedLocation.IsVisited; } ctx.SaveChanges(); return new HttpResponseMessage(HttpStatusCode.OK);//200 } }
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 string GetUserPreference(string user) { UserPreferenceModel usrPreference = null; string connectionString = "type=embedded;storesdirectory=" + Constants.StoreLocation + ";storename=Users"; var client = BrightstarService.GetClient(connectionString); using (var ctx = new MyEntityContext(connectionString)) { var currentUser = ctx.Users.FirstOrDefault(x => x.Email == user); if (currentUser != null && currentUser.UserPreference != null) { List<IInterest> listIInterests = currentUser.UserPreference.Interests.ToList<IInterest>(); List<Interest> listInterest = listIInterests.ConvertAll(obj => (Interest)obj); listInterest.Sort(delegate(Interest interest1, Interest interest2) { return interest1.Name.CompareTo(interest2.Name); }); ICollection<Interest> interestsCollection = listInterest; listIInterests = currentUser.UserPreference.Cuisine.ToList<IInterest>(); listInterest = listIInterests.ConvertAll(obj => (Interest)obj); listInterest.Sort(delegate(Interest interest1, Interest interest2) { return interest1.Name.CompareTo(interest2.Name); }); ICollection<Interest> cuisineCollection = listInterest; usrPreference = new UserPreferenceModel() { NeedWheelchair = currentUser.UserPreference.NeedWheelchair, Cuisine = cuisineCollection, Interests = interestsCollection, }; } var json = JsonConvert.SerializeObject(usrPreference, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); return json; } }
public void TestManyToOneInverse() { string storeName = Guid.NewGuid().ToString(); string rootId, skillAId, skillBId, childSkillId, childSkill2Id; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var root = context.Skills.Create(); root.Name = "Root"; var skillA = context.Skills.Create(); skillA.Parent = root; skillA.Name = "Skill A"; var skillB = context.Skills.Create(); skillB.Parent = root; skillB.Name = "Skill B"; Assert.IsNotNull(root.Children); Assert.AreEqual(2, root.Children.Count); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillA.Id))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillB.Id))); context.SaveChanges(); rootId = root.Id; skillAId = skillA.Id; skillBId = skillB.Id; } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId)); Assert.IsNotNull(root); var childSkill = context.Skills.Create(); childSkill.Name = "Child Skill"; childSkill.Parent = root; Assert.AreEqual(3, root.Children.Count); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill.Id))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId))); context.SaveChanges(); childSkillId = childSkill.Id; } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId)); Assert.IsNotNull(root); Assert.AreEqual(3, root.Children.Count); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkillId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId))); var childSkill2 = context.Skills.Create(); childSkill2.Name = "Child Skill 2"; childSkill2.Parent = root; Assert.AreEqual(4, root.Children.Count); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill2.Id))); context.SaveChanges(); childSkill2Id = childSkill2.Id; } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId)); var skillA = context.Skills.FirstOrDefault(x => x.Id.Equals(skillAId)); var childSkill2 = context.Skills.FirstOrDefault(x => x.Id.Equals(childSkill2Id)); Assert.IsNotNull(root); Assert.IsNotNull(skillA); Assert.IsNotNull(childSkill2); Assert.AreEqual(4, root.Children.Count); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkillId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId))); Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill2Id))); // Move a skill to a new parent childSkill2.Parent = skillA; Assert.AreEqual(3, root.Children.Count); Assert.IsFalse(root.Children.Any(x => x.Id.Equals(childSkill2Id))); Assert.AreEqual(1, skillA.Children.Count); Assert.IsTrue(skillA.Children.Any(x => x.Id.Equals(childSkill2Id))); context.SaveChanges(); } } // Check the move has persisted using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId)); var skillA = context.Skills.FirstOrDefault(x => x.Id.Equals(skillAId)); var childSkill2 = context.Skills.FirstOrDefault(x => x.Id.Equals(childSkill2Id)); Assert.IsNotNull(root); Assert.IsNotNull(skillA); Assert.IsNotNull(childSkill2); Assert.AreEqual(3, root.Children.Count); Assert.IsFalse(root.Children.Any(x => x.Id.Equals(childSkill2Id))); Assert.AreEqual(1, skillA.Children.Count); Assert.IsTrue(skillA.Children.Any(x => x.Id.Equals(childSkill2Id))); Assert.AreEqual(skillA.Id, childSkill2.Parent.Id); Assert.AreEqual(root.Id, childSkill2.Parent.Parent.Id); } } }
public void TestGetAndSetDateTimeProperty() { string storeName = Guid.NewGuid().ToString(); string personId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var person = context.Persons.Create(); Assert.IsNotNull(person); person.Name = "Kal"; person.DateOfBirth = new DateTime(1970, 12, 12); context.SaveChanges(); personId = person.Id; } // Test that the property is still there when we retrieve the object again using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Id == personId); Assert.IsNotNull(person); Assert.IsNotNull(person.Name, "person.Name was NULL when retrieved back from server"); Assert.AreEqual("Kal", person.Name, "Unexpected Name property value"); Assert.IsTrue(person.DateOfBirth.HasValue); Assert.AreEqual(1970, person.DateOfBirth.Value.Year); Assert.AreEqual(12, person.DateOfBirth.Value.Month); Assert.AreEqual(12, person.DateOfBirth.Value.Day); } } // Test we can also use the simple property in a LINQ query using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.DateOfBirth == new DateTime(1970, 12, 12)); Assert.IsNotNull(person, "Could not find person by Date of Birth"); Assert.AreEqual(personId, person.Id, "Query for person by date of birth returned an unexpected person entity"); // Test we can set a nullable property back to null person.DateOfBirth = null; context.SaveChanges(); } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Name.Equals("Kal")); Assert.IsNotNull(person); Assert.IsNull(person.DateOfBirth); } } }
public void TestOneToOneInverse() { string storeName = Guid.NewGuid().ToString(); string aliceId, bobId, carolId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var alice = context.Persons.Create(); alice.Name = "Alice"; var bob = context.Animals.Create(); alice.Pet = bob; context.SaveChanges(); aliceId = alice.Id; bobId = bob.Id; } } // See if we can access the inverse property using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var bob = context.Animals.FirstOrDefault(a => a.Id.Equals(bobId)); Assert.IsNotNull(bob); Assert.IsNotNull(bob.Owner); Assert.AreEqual(aliceId, bob.Owner.Id); // see if we can access an item by querying against its inverse property bob = context.Animals.FirstOrDefault(a => a.Owner.Id.Equals(aliceId)); Assert.IsNotNull(bob); // check that alice.Pet refers to the same object as bob bob.Name = "Bob"; Assert.IsNotNull(bob.Name); Assert.AreEqual("Bob", bob.Name); var alice = context.Persons.FirstOrDefault(a => a.Id.Equals(aliceId)); Assert.IsNotNull(alice); var alicePet = alice.Pet; Assert.IsNotNull(alicePet); Assert.AreEqual(bob, alicePet); Assert.AreEqual("Bob", alicePet.Name); // Transfer object by changing the forward property var carol = context.Persons.Create(); carol.Name = "Carol"; carol.Pet = bob; carolId = carol.Id; Assert.AreEqual(carol, bob.Owner); Assert.IsNull(alice.Pet, "Expected alice.Pet to be null after owner change"); context.SaveChanges(); } } // Check that changes to forward properties get persisted using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var bob = context.Animals.FirstOrDefault(a => a.Owner.Id.Equals(carolId)); Assert.IsNotNull(bob); Assert.AreEqual("Bob", bob.Name); var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId)); Assert.IsNotNull(alice); Assert.IsNull(alice.Pet); var carol = context.Persons.FirstOrDefault(p => p.Id.Equals(carolId)); Assert.IsNotNull(carol); Assert.IsNotNull(carol.Pet); Assert.AreEqual(bob, carol.Pet); // Transfer object by changing inverse property bob.Owner = alice; Assert.IsNotNull(alice.Pet); Assert.IsNull(carol.Pet); context.SaveChanges(); } } // Check that changes to inverse properties get persisted using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var bob = context.Animals.FirstOrDefault(a => a.Id.Equals(bobId)); Assert.IsNotNull(bob); Assert.AreEqual("Bob", bob.Name); Assert.AreEqual(aliceId, bob.Owner.Id); var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId)); Assert.IsNotNull(alice); Assert.IsNotNull(alice.Pet); Assert.AreEqual(bob, alice.Pet); var carol = context.Persons.FirstOrDefault(p => p.Id.Equals(carolId)); Assert.IsNotNull(carol); Assert.IsNull(carol.Pet); } } }
public void TestManyToManyInverse() { string storeName = Guid.NewGuid().ToString(); string aliceId, bobId, jsId, cssId, jqueryId, rdfId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.Create(); alice.Name = "Alice"; var bob = context.Persons.Create(); bob.Name = "Bob"; var js = context.Skills.Create(); js.Name = "Javascript"; var css = context.Skills.Create(); css.Name = "CSS"; var jquery = context.Skills.Create(); jquery.Name = "JQuery"; var rdf = context.Skills.Create(); rdf.Name = "RDF"; alice.Skills.Add(js); alice.Skills.Add(css); bob.Skills.Add(js); bob.Skills.Add(jquery); context.SaveChanges(); aliceId = alice.Id; bobId = bob.Id; jsId = js.Id; cssId = css.Id; jqueryId = jquery.Id; rdfId = rdf.Id; } // See if we can access the inverse properties correctly using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var js = context.Skills.FirstOrDefault(x => x.Id.Equals(jsId)); Assert.IsNotNull(js); Assert.AreEqual(2, js.SkilledPeople.Count); Assert.IsTrue(js.SkilledPeople.Any(x => x.Id.Equals(aliceId))); Assert.IsTrue(js.SkilledPeople.Any(x => x.Id.Equals(bobId))); var css = context.Skills.FirstOrDefault(x => x.Id.Equals(cssId)); Assert.IsNotNull(css); Assert.AreEqual(1, css.SkilledPeople.Count); Assert.IsTrue(css.SkilledPeople.Any(x => x.Id.Equals(aliceId))); var jquery = context.Skills.FirstOrDefault(x => x.Id.Equals(jqueryId)); Assert.IsNotNull(jquery); Assert.AreEqual(1, jquery.SkilledPeople.Count); Assert.IsTrue(jquery.SkilledPeople.Any(x => x.Id.Equals(bobId))); var rdf = context.Skills.FirstOrDefault(x => x.Id.Equals(rdfId)); Assert.IsNotNull(rdf); Assert.AreEqual(0, rdf.SkilledPeople.Count); // Test adding to an inverse property with some existing values and an inverse property with no existing values var bob = context.Persons.FirstOrDefault(x => x.Id.Equals(bobId)); Assert.IsNotNull(bob); var alice = context.Persons.FirstOrDefault(x => x.Id.Equals(aliceId)); Assert.IsNotNull(alice); css.SkilledPeople.Add(bob); Assert.AreEqual(2, css.SkilledPeople.Count); Assert.IsTrue(css.SkilledPeople.Any(x => x.Id.Equals(bobId))); Assert.AreEqual(3, bob.Skills.Count); Assert.IsTrue(bob.Skills.Any(x => x.Id.Equals(cssId))); rdf.SkilledPeople.Add(alice); Assert.AreEqual(1, rdf.SkilledPeople.Count); Assert.IsTrue(rdf.SkilledPeople.Any(x => x.Id.Equals(aliceId))); Assert.AreEqual(3, alice.Skills.Count); Assert.IsTrue(alice.Skills.Any(x => x.Id.Equals(rdfId))); } } }
public void TestSetRelatedEntitiesToNullThrowsArgumentNullException() { var storeName = "TestSetRelatedEntitiesToNullThrowsArgumentNullException_" + DateTime.Now.Ticks; string aliceId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.Create(); var bob = context.Persons.Create(); var carol = context.Persons.Create(); alice.Friends = new List<IPerson> { bob, carol }; context.SaveChanges(); aliceId = alice.Id; } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId)); Assert.IsNotNull(alice); Assert.AreEqual(2, alice.Friends.Count); alice.Friends = null; // throws } }
public void TestClearRelatedObjects() { var storeName = "TestClearRelatedObjects_" + DateTime.Now.Ticks; string aliceId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.Create(); var bob = context.Persons.Create(); var carol = context.Persons.Create(); alice.Friends = new List<IPerson> { bob, carol }; context.SaveChanges(); aliceId = alice.Id; } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId)); Assert.IsNotNull(alice); Assert.AreEqual(2, alice.Friends.Count); alice.Friends.Clear(); Assert.AreEqual(0, alice.Friends.Count); context.SaveChanges(); } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId)); Assert.IsNotNull(alice); Assert.AreEqual(0, alice.Friends.Count); } }
public void TestGuidAndNullableGuidDefaults() { var storeName = "TestGuidAndNullableGuidDefaults_" + DateTime.Now.Ticks; string testEntityId; using (var doStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var testEntity = context.Entities.Create(); testEntityId = testEntity.Id; context.SaveChanges(); } } using (var doStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var testEntity = context.Entities.FirstOrDefault(x => x.Id.Equals(testEntityId)); Assert.IsNotNull(testEntity); Assert.IsNotNull(testEntity.SomeGuid); Assert.AreEqual(Guid.Empty, testEntity.SomeGuid); Assert.IsFalse(testEntity.SomeNullableGuid.HasValue); } } }
public void TestSetGuid() { var storeName = "SetGuid_" + DateTime.Now.Ticks; var testGuid = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); using (var doStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var testEntity = context.Entities.Create(); testEntity.SomeGuid = testGuid; context.SaveChanges(); } } using (var doStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(doStore)) { var testEntity = context.Entities.FirstOrDefault(); Assert.IsNotNull(testEntity); var testEntityId = testEntity.Id; Assert.AreEqual(testGuid, testEntity.SomeGuid); // Verify we can use a Guid value in a search testEntity = context.Entities.FirstOrDefault(e => e.SomeGuid.Equals(testGuid)); Assert.IsNotNull(testEntity); Assert.AreEqual(testEntityId, testEntity.Id); Assert.IsNull(context.Entities.FirstOrDefault(e => e.SomeGuid.Equals(Guid.Empty))); } } }
private int LinqFindById() { using (var context = new MyEntityContext(_storeConnectionString)) { var rng = new Random(); for (int i = 0; i < CycleCount; i++) { var personId = rng.Next(_personCount); var person = context.FoafPersons.FirstOrDefault(p => p.Id.Equals(personId.ToString())); if (person == null) { throw new BenchmarkAssertionException("Expected LINQ query to return a non-null result."); } } return CycleCount; } }
public void TestBaseResourceAddress() { string storeName = Guid.NewGuid().ToString(); using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var skill = new Skill { Id = "foo", Name = "Foo" }; context.Skills.Add(skill); var otherSkill = new Skill { Id = "bar", Name = "Bar", Context = context }; var yetAnotherSkill = new Skill { Name = "Bletch" }; context.Skills.Add(yetAnotherSkill); context.SaveChanges(); var found = context.Skills.FirstOrDefault(s => s.Id.Equals("foo")); Assert.IsNotNull(found); Assert.AreEqual("foo", found.Id); Assert.AreEqual("Foo", found.Name); found = context.Skills.FirstOrDefault(s => s.Id.Equals("bar")); Assert.IsNotNull(found); Assert.AreEqual("bar", found.Id); Assert.AreEqual("Bar", found.Name); found = context.Skills.FirstOrDefault(s => s.Name.Equals("Bletch")); Assert.IsNotNull(found); Guid foundId; Assert.IsTrue(Guid.TryParse(found.Id, out foundId)); } } }
public void TestSetContextAndIdentityProperties() { string storeName = Guid.NewGuid().ToString(); using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (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(); } } 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); } } }
public void TestSetAndGetSimpleProperty() { string storeName = Guid.NewGuid().ToString(); string personId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.Create(); Assert.IsNotNull(person); person.Name = "Kal"; context.SaveChanges(); personId = person.Id; } } // Test that the property is still there when we retrieve the object again using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Id == personId); Assert.IsNotNull(person); Assert.IsNotNull(person.Name, "person.Name was NULL when retrieved back from server"); Assert.AreEqual("Kal", person.Name, "Unexpected Name property value"); } } // Test we can also use the simple property in a LINQ query using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var person = context.Persons.FirstOrDefault(p => p.Name == "Kal"); Assert.IsNotNull(person, "Could not find person by Name"); Assert.AreEqual(personId, person.Id, "Query for person by name returned an unexpected person entity"); // Test we can use ToList() var people = context.Persons.Where(p => p.Name == "Kal").ToList(); Assert.IsNotNull(people); Assert.AreEqual(1, people.Count); Assert.AreEqual(personId, people[0].Id); } } }
public void TestOrderingOfResults() { string storeName = Guid.NewGuid().ToString(); var peterDob = DateTime.Now.AddYears(-35); var anneDob = DateTime.Now.AddYears(-28); using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var joDob = DateTime.Now.AddYears(-34); var mirandaDob = DateTime.Now.AddYears(-32); var jo = context.Persons.Create(); Assert.IsNotNull(jo); jo.Name = "Jo"; jo.DateOfBirth = joDob; jo.Age = 34; var peter = context.Persons.Create(); Assert.IsNotNull(peter); peter.Name = "Peter"; peter.DateOfBirth = peterDob; peter.Age = 35; var miranda = context.Persons.Create(); Assert.IsNotNull(miranda); miranda.Name = "Miranda"; miranda.DateOfBirth = mirandaDob; miranda.Age = 32; var anne = context.Persons.Create(); Assert.IsNotNull(anne); anne.Name = "Anne"; anne.DateOfBirth = anneDob; anne.Age = 28; context.SaveChanges(); } } using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var people = context.Persons.ToList(); Assert.AreEqual(4, people.Count, "Amount of people in context was not 4"); var orderedByName = context.Persons.OrderBy(p => p.Name).ToList(); var orderedByAge = context.Persons.OrderBy(p => p.Age).ToList(); var orderedByDob = context.Persons.OrderBy(p => p.DateOfBirth).ToList(); Assert.AreEqual("Anne", orderedByName[0].Name, "First in list was not alphabetically first"); Assert.AreEqual("Peter", orderedByName[3].Name, "Last in list was not alphabetically last"); Assert.AreEqual(28, orderedByAge[0].Age, "First in list was not numerically first"); Assert.AreEqual(35, orderedByAge[3].Age, "Last in list was not numerically last"); Assert.AreEqual(peterDob, orderedByDob[0].DateOfBirth, "First in list was not first by date"); Assert.AreEqual(anneDob, orderedByDob[3].DateOfBirth, "Last in list was not last by date"); } } }
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); } } }
public void TestLoopThroughEntities() { string storeName = Guid.NewGuid().ToString(); string homerId, bartId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var bart = context.Persons.Create(); bart.Name = "Bart Simpson"; var homer = context.Persons.Create(); homer.Name = "Homer Simpson"; bart.Father = homer; var marge = context.Persons.Create(); marge.Name = "Marge Simpson"; bart.Mother = marge; context.SaveChanges(); homerId = homer.Id; bartId = bart.Id; } } // Query with results converted to a list using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var homersKids = context.Persons.Where(p => p.Father.Id == homerId).ToList(); Assert.AreEqual(1, homersKids.Count, "Could not find Bart with SPARQL query for Homer's kids"); Assert.AreEqual(bartId, homersKids.First().Id); } } }
public void TestSkipAndTake() { 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)) { // 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); } } }
public void TestSetAndGetSingleRelatedObject() { string storeName = Guid.NewGuid().ToString(); string bartId, homerId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var bart = context.Persons.Create(); bart.Name = "Bart Simpson"; var homer = context.Persons.Create(); homer.Name = "Homer Simpson"; bart.Father = homer; context.SaveChanges(); homerId = homer.Id; bartId = bart.Id; } // Test that the property is still there when we retrieve the object again using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var bart = context.Persons.FirstOrDefault(p => p.Id == bartId); Assert.IsNotNull(bart, "Could not find Bart by ID"); var bartFather = bart.Father; Assert.IsNotNull(bartFather, "Father property was not present on the returned person object"); Assert.AreEqual(homerId, bartFather.Id, "Incorrect Father property value on returned object"); } } // See if we can use the property in a query using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var homersKids = context.Persons.Where(p => p.Father.Id == homerId).ToList(); Assert.AreEqual(1, homersKids.Count, "Could not find Bart with SPARQL query for Homer's kids"); Assert.AreEqual(bartId, homersKids.First().Id); } } }
private int LinqFindByName() { var rng = new Random(); using (var context = new MyEntityContext(_storeConnectionString)) { for (var i = 0; i < CycleCount; i++) { var targetName = Firstnames[rng.Next(Firstnames.Count)]; var results = context.FoafPersons.Where(p => p.GivenName.Equals(targetName)).ToList(); if (results.Count == 0) { throw new BenchmarkAssertionException( "Expected at least one result from LINQ query on GivenName"); } } } return CycleCount; }
public void TestPopulateEntityCollectionWithExistingEntities() { string storeName = Guid.NewGuid().ToString(); string aliceId, bobId, carolId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.Create(); alice.Name = "Alice"; var bob = context.Persons.Create(); bob.Name = "Bob"; var carol = context.Persons.Create(); carol.Name = "Carol"; alice.Friends.Add(bob); alice.Friends.Add(carol); context.SaveChanges(); aliceId = alice.Id; bobId = bob.Id; carolId = carol.Id; } // See if we can access the collection on a loaded object using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId); Assert.IsNotNull(alice); var friends = alice.Friends as IEntityCollection<IPerson>; Assert.IsNotNull(friends); Assert.IsFalse(friends.IsLoaded); friends.Load(); Assert.IsTrue(friends.IsLoaded); Assert.AreEqual(2, alice.Friends.Count); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(bobId))); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(carolId))); } } }
private void CreatePerson(MyEntityContext context, int personNumber) { var givenName = Firstnames[personNumber%Firstnames.Count]; var familyName = Surnames[(personNumber/Firstnames.Count)%Surnames.Count]; var p = new FoafPerson { Id = personNumber.ToString(), Name = givenName + " " + familyName, GivenName = givenName, FamilyName = familyName, Organisation = Organizations[personNumber%Organizations.Count], Age = 18 + (personNumber%60), }; context.FoafPersons.Add(p); foreach (var friend in _last10) { if (friend != null) { p.Knows.Add(friend); } } _last10[personNumber%10] = p; }
public void TestSetEntityCollection() { string storeName = Guid.NewGuid().ToString(); string aliceId, bobId, carolId, daveId, edwinaId; using (var dataObjectStore = _dataObjectContext.CreateStore(storeName)) { var context = new MyEntityContext(dataObjectStore); var alice = context.Persons.Create(); var bob = context.Persons.Create(); var carol = context.Persons.Create(); alice.Friends = new List<IPerson> { bob, carol }; context.SaveChanges(); aliceId = alice.Id; bobId = bob.Id; carolId = carol.Id; } // See if we can access the collection on a loaded object using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId); Assert.IsNotNull(alice); var friends = alice.Friends as IEntityCollection<IPerson>; Assert.IsNotNull(friends); Assert.IsFalse(friends.IsLoaded); friends.Load(); Assert.IsTrue(friends.IsLoaded); Assert.AreEqual(2, alice.Friends.Count); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(bobId))); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(carolId))); var dave = context.Persons.Create(); var edwina = context.Persons.Create(); alice.Friends = new List<IPerson> { dave, edwina }; context.SaveChanges(); daveId = dave.Id; edwinaId = edwina.Id; } } // See if we can access the collection on a loaded object using (var dataObjectStore = _dataObjectContext.OpenStore(storeName)) { using (var context = new MyEntityContext(dataObjectStore)) { var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId); Assert.IsNotNull(alice); var friends = alice.Friends as IEntityCollection<IPerson>; Assert.IsNotNull(friends); Assert.IsFalse(friends.IsLoaded); friends.Load(); Assert.IsTrue(friends.IsLoaded); Assert.AreEqual(2, alice.Friends.Count); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(daveId))); Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(edwinaId))); } } }