public void TestEvaluateSubclass() { Person person = new CustomPerson(); ICriterion before = Restrictions.Eq("Father", person); ICriterion after = ExpressionProcessor.ProcessExpression <Person>(p => p.Father == person); Assert.AreEqual(before.ToString(), after.ToString()); }
public async Task QueryCustom() { // Ensure that the database starts out empty. Assert.Empty(from doc in context.Query <object>() select doc); // Persist some documents for querying. var jack = new CustomPerson() { Id = 0, Name = "Jack-Custom", Age = 10, Gender = Gender.Male }; await bucket.UpsertSafeAsync(jack, persistTo : PersistTo.One); var jill = new CustomPerson() { Id = 1, Name = "Jill-Custom", Age = 11, Gender = Gender.Female }; await bucket.UpsertSafeAsync(jill, persistTo : PersistTo.One); var john = new CustomPerson() { Id = 2, Name = "John-Custom", Age = 12, Gender = Gender.Male }; await bucket.UpsertSafeAsync(john, persistTo : PersistTo.One); // Wait for the indexer to catch up. await bucket.WaitForIndexerAsync(); //----------------------------------------------------------------- var gotJack = false; var gotJill = false; var gotJohn = false; foreach (var person in (from item in context.Query <CustomPerson>() select item)) { gotJack = gotJack || person.Name == jack.Name; gotJill = gotJill || person.Name == jill.Name; gotJohn = gotJohn || person.Name == john.Name; } Assert.True(gotJack); Assert.True(gotJill); Assert.True(gotJohn); //----------------------------------------------------------------- gotJack = false; gotJill = false; gotJohn = false; foreach (var person in (from item in context.Query <CustomPerson>() where item.Name == "Jack-Custom" select item)) { gotJack = gotJack || person.Name == jack.Name; gotJill = gotJill || person.Name == jill.Name; gotJohn = gotJohn || person.Name == john.Name; } Assert.True(gotJack); Assert.False(gotJill); Assert.False(gotJohn); //----------------------------------------------------------------- gotJack = false; gotJill = false; gotJohn = false; foreach (var person in (from item in context.Query <CustomPerson>() where item.Age >= 11 select item)) { gotJack = gotJack || person.Name == jack.Name; gotJill = gotJill || person.Name == jill.Name; gotJohn = gotJohn || person.Name == john.Name; } Assert.False(gotJack); Assert.True(gotJill); Assert.True(gotJohn); }
public async Task CustomNames() { // Ensure that the database starts out empty. Assert.Empty(from doc in context.Query <object>() select doc); // Verify that we can write generated entity models. var jack = new CustomPerson() { Id = 0, Name = "Jack", Age = 10, Gender = Gender.Male, Data = new byte[] { 0, 1, 2, 3, 4 } }; var jill = new CustomPerson() { Id = 1, Name = "Jill", Age = 11, Gender = Gender.Female, Data = new byte[] { 5, 6, 7, 8, 9 } }; Assert.Equal("custom-person::0", jack.GetKey()); Assert.Equal("custom-person::1", jill.GetKey()); await bucket.UpsertSafeAsync(jack, persistTo : PersistTo.One); await bucket.UpsertSafeAsync(jill, persistTo : PersistTo.One); // Verify that we can read them. var jackRead = await bucket.GetSafeAsync <CustomPerson>(CustomPerson.CreateKey(0)); var jillRead = await bucket.GetSafeAsync <CustomPerson>(CustomPerson.CreateKey(1)); Assert.Equal("custom-person::0", jackRead.GetKey()); Assert.Equal("custom-person::1", jillRead.GetKey()); Assert.True(jack == jackRead); Assert.True(jill == jillRead); //----------------------------------------------------------------- // Persist a [City] entity (which has a different entity type) and then // perform a N1QL query to list the [CustomPerson] entities and verify that // we get only Jack and Jill back. This verifies the the [TypeFilter] // attribute is generated and working correctly. var city = new City() { Name = "Woodinville", Population = 12345 }; var result = await bucket.UpsertAsync(city); bucket.WaitForIndexer(); //----------------------------------------------------------------- // Query for the people and verify var peopleQuery = (from doc in context.Query <CustomPerson>() select doc); var people = peopleQuery.ToList(); Assert.Equal(2, people.Count); Assert.Contains(people, p => p.Name == "Jack"); Assert.Contains(people, p => p.Name == "Jill"); //----------------------------------------------------------------- // Query for the city and verify. var cityQuery = from doc in context.Query <City>() select doc; var cities = cityQuery.ToList(); Assert.Single(cities); Assert.Contains(cities, p => p.Name == "Woodinville"); //----------------------------------------------------------------- // Query for documents that don't exist and verify. var rawResults = await bucket.QueryAsync <object>($"select * from `{bucket.Name}` where __T=\"Test.Neon.Models.Definitions.Country\";"); var countryQuery = from doc in context.Query <Country>() select doc; Assert.Empty(rawResults.ToList()); Assert.Empty(countryQuery.ToList()); //----------------------------------------------------------------- // Verify that plain old object serialization still works. var poo = new PlainOldObject() { Foo = "bar" }; await bucket.UpsertSafeAsync("poo", poo, persistTo : PersistTo.One); poo = await bucket.GetSafeAsync <PlainOldObject>("poo"); Assert.Equal("bar", poo.Foo); //----------------------------------------------------------------- // Extra credit #1: Verify that [DeepClone()] works. var clone = jack.DeepClone(); Assert.Equal(jack.Name, clone.Name); Assert.Equal(jack.Age, clone.Age); Assert.Equal(jack.Gender, clone.Gender); Assert.NotSame(jack.Data, clone.Data); //----------------------------------------------------------------- // Extra credit #2: Verify that [SameTypeAs()] works. Assert.True(CustomPerson.SameTypeAs(jack)); Assert.False(CustomPerson.SameTypeAs(city)); Assert.False(CustomPerson.SameTypeAs(null)); }