public async Task Query() { // Verify that the query commands work. var endpoint = bucket.Configuration.GetEndPoint(); var bucketName = bucket.Configuration.BucketName; // Insert a couple documents into the database. var jack = new Person() { Id = 0, Name = "Jack", Age = 10, Data = new byte[] { 0, 1, 2, 3, 4 } }; var jill = new Person() { Id = 1, Name = "Jill", Age = 11, Data = new byte[] { 5, 6, 7, 8, 9 } }; await bucket.InsertSafeAsync(jack, persistTo : PersistTo.One); await bucket.InsertSafeAsync(jill, persistTo : PersistTo.One); await bucket.WaitForIndexerAsync(); bucket.Query <dynamic>($"select count(*) from `{bucket.Name}`;"); using (var runner = new ProgramRunner()) { // Query using the [http://...] target format and passing the query on the command line. var result = runner.Execute(Program.Main, "couchbase", "query", $"http://{endpoint.Address}:{endpoint.Port}@{username}:{password}:{bucketName}", $"select * from `{bucket.Name}`;"); Assert.Equal(0, result.ExitCode); Assert.Contains("Jack", result.OutputText); Assert.Contains("Jill", result.OutputText); // Query again, but using the using the [couchbase://...] target format. result = runner.Execute(Program.Main, "couchbase", "query", $"couchbase://{endpoint.Address}:{endpoint.Port}@{username}:{password}:{bucketName}", $"select * from `{bucket.Name}`;"); Assert.Equal(0, result.ExitCode); Assert.Contains("Jack", result.OutputText); Assert.Contains("Jill", result.OutputText); // Pass the query as a file. using (var tempFile = new TempFile()) { File.WriteAllText(tempFile.Path, $"select * from `{bucket.Name}`;"); result = runner.Execute(Program.Main, "couchbase", "query", $"couchbase://{endpoint.Address}:{endpoint.Port}@{username}:{password}:{bucketName}", $"@{tempFile.Path}"); Assert.Equal(0, result.ExitCode); Assert.Contains("Jack", result.OutputText); Assert.Contains("Jill", result.OutputText); } // Pass the query as STDIN. result = runner.ExecuteWithInput(Program.Main, $"select * from `{bucket.Name}`;", "couchbase", "query", $"couchbase://{endpoint.Address}:{endpoint.Port}@{username}:{password}:{bucketName}", "-"); Assert.Equal(0, result.ExitCode); Assert.Contains("Jack", result.OutputText); Assert.Contains("Jill", result.OutputText); } }
public async Task WriteReadList() { // Ensure that the database starts out empty. Assert.Empty(from doc in context.Query <object>() select doc); // Verify that the generated CreateKey() methods return the // correct values. Assert.Equal($"{Person.PersistedType}::0", Person.CreateKey("0")); Assert.Equal($"{City.PersistedType}::Woodinville", City.CreateKey("Woodinville")); // Verify that we can write generated entity models. var jack = new Person() { Id = 0, Name = "Jack", Age = 10, Gender = Gender.Male, Data = new byte[] { 0, 1, 2, 3, 4 } }; var jill = new Person() { Id = 1, Name = "Jill", Age = 11, Gender = Gender.Female, Data = new byte[] { 5, 6, 7, 8, 9 } }; Assert.Equal("Test.Neon.Models.Definitions.Person::0", jack.GetKey()); Assert.Equal("Test.Neon.Models.Definitions.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 <Person>(Person.CreateKey(0)); var jillRead = await bucket.GetSafeAsync <Person>(Person.CreateKey(1)); Assert.Equal("Test.Neon.Models.Definitions.Person::0", jackRead.GetKey()); Assert.Equal("Test.Neon.Models.Definitions.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 Person 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); await bucket.WaitForIndexerAsync(); //----------------------------------------------------------------- // Query for the people and verify var peopleQuery = (from doc in context.Query <Person>() 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()); // $todo(jeff.lill): https://github.com/nforgeio/neonKUBE/issues/475 //----------------------------------------------------------------- // 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(Person.SameTypeAs(jack)); Assert.False(Person.SameTypeAs(city)); Assert.False(Person.SameTypeAs(null)); }