public void Map2PocoTests_NewObjectsInArray()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var query = from brewery in context.Query <Brewery>()
                                where brewery.Type == "brewery"
                                select
                                new
                    {
                        name = brewery.Name,
                        list =
                            new[]
                        { new { part = brewery.City }, new { part = brewery.State }, new { part = brewery.Code } }
                    };

                    foreach (var brewery in query.Take(10))
                    {
                        Console.WriteLine("Brewery {0} has address parts {1}", brewery.name,
                                          String.Join(", ", brewery.list.Select(p => p.part)));
                    }
                }
            }
        }
        public void AnyAllTests_AllNestedArrayPrefiltered()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    // Note: This query isn't very useful in the real world
                    // However, it does demonstrate how to prefilter the collection before all is run
                    // Which is behaviorly different then adding the Where predicate inside the All predicate
                    // In this example, all breweries which have NO address 563 Second Street will be returned

                    var breweries = (from b in context.Query <Brewery>()
                                     where
                                     b.Type == "brewery" &&
                                     b.Address.Where(p => p == "563 Second Street").All(p => p == "101 Fake Street")
                                     orderby b.Name
                                     select new { name = b.Name, address = b.Address }).
                                    ToList();

                    Assert.IsNotEmpty(breweries);
                    Assert.False(breweries.SelectMany(p => p.address).Any(p => p == "563 Second Street"));
                }
            }
        }
Exemple #3
0
        public async Task SetUp()
        {
            Cluster = await Couchbase.Cluster.ConnectAsync(TestConfigurations.DefaultConfig());

            await Cluster.WaitUntilReadyAsync(TimeSpan.FromSeconds(10));

            var bucket = await Cluster.BucketAsync("beer-sample");

            await EnsurePrimaryIndexExists(bucket);

            Bucket = bucket;
        }
Exemple #4
0
        protected void InitializeCluster(IContractResolver contractResolver = null)
        {
            if (contractResolver != null)
            {
                _contractResolver = contractResolver;
            }

            var config = TestConfigurations.DefaultConfig();

            config.DeserializationSettings.ContractResolver = _contractResolver;
            config.SerializationSettings.ContractResolver   = _contractResolver;
            ClusterHelper.Initialize(config);
        }
        public void AnyAllTests_AllOnMainDocument_ReturnsFalse()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var isAllBreweries = context.Query <Brewery>().All(p => p.Type == "brewery");

                    Assert.False(isAllBreweries);
                }
            }
        }
        public void AggregateTests_SimpleAverage()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var avg = context.Query <Beer>().Where(p => p.Type == "beer" && N1QlFunctions.IsValued(p.Abv)).Average(p => p.Abv);

                    Console.WriteLine("Average ABV of all beers is {0}", avg);
                }
            }
        }
        public void AggregateTests_SimpleCount()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var count = context.Query <Beer>().Count(p => p.Type == "beer");

                    Console.WriteLine("Number of beers is {0}", count);
                }
            }
        }
        public void Map2PocoTests_Simple_Projections_TypeFilterAttribute()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = (from b in context.Query <BeerFiltered>()
                                 select new { type = b.Type }).
                                AsEnumerable();

                    Assert.True(beers.All(p => p.type == "beer"));
                }
            }
        }
        public void First_HasResult()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                where beer.Type == "beer"
                                select new { beer.Name };

                    Console.WriteLine(beers.First().Name);
                }
            }
        }
        public void AnyAllTests_AnyOnMainDocument_ReturnsFalse()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var hasFaketype = (from b in context.Query <Brewery>()
                                       where b.Type == "faketype"
                                       select new { name = b.Name, address = b.Address }).
                                      Any();

                    Assert.False(hasFaketype);
                }
            }
        }
        public void Map2PocoTests_Explain_QueryWithPropertyExtraction()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var explanation = (from b in context.Query <Beer>()
                                       where b.Type == "beer"
                                       select b.Abv).
                                      Explain();

                    Console.WriteLine(explanation);
                }
            }
        }
        public void AnyAllTests_AllOnMainDocument_ReturnsTrue()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var allBreweriesHaveAddress = (from b in context.Query <Brewery>()
                                                   where b.Type == "brewery"
                                                   select new { b.Name })
                                                  .All(p => p.Name != "");

                    Assert.True(allBreweriesHaveAddress);
                }
            }
        }
        public void FirstOrDefault_Empty()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                where beer.Type == "abcdefg"
                                select new { beer.Name };

                    var aBeer = beers.FirstOrDefault();
                    Assert.IsNull(aBeer);
                }
            }
        }
        public void Map2PocoTests_Simple_Projections_TypeFilterRuntime()
        {
            DocumentFilterManager.SetFilter(new BreweryFilter());

            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries = (from b in context.Query <Brewery>()
                                     select new { type = b.Type })
                                    .AsEnumerable();

                    Assert.True(breweries.All(p => p.type == "brewery"));
                }
            }
        }
        public void Map2PocoTests_Simple_Projections()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from b in context.Query <Beer>()
                                select new { name = b.Name, abv = b.Abv };

                    foreach (var b in beers)
                    {
                        Console.WriteLine("{0} has {1} ABV", b.name, b.abv);
                    }
                }
            }
        }
        public void SingleOrDefault_HasResult()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                where beer.Name == "21A IPA"
                                select new { beer.Name };

                    var aBeer = beers.SingleOrDefault();
                    Assert.IsNotNull(aBeer);
                    Console.WriteLine(aBeer.Name);
                }
            }
        }
        public void AnyAllTests_AnyNestedArray()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries = (from b in context.Query <Brewery>()
                                     where b.Type == "brewery" && b.Address.Any()
                                     select new { name = b.Name, address = b.Address }).
                                    ToList();

                    Assert.IsNotEmpty(breweries);
                    Assert.True(breweries.All(p => p.address.Any()));
                }
            }
        }
        public void NestTests_Unnest_Simple()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries = from brewery in context.Query <Brewery>()
                                    from address in brewery.Address
                                    select new { name = brewery.Name, address };

                    foreach (var b in breweries.Take(10))
                    {
                        Console.WriteLine("Brewery {0} has address line {1}", b.name, b.address);
                    }
                }
            }
        }
        public void Map2PocoTests_Simple_Projections_WhereDateTime()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from b in context.Query <Beer>()
                                where (b.Type == "beer") && (b.Updated >= new DateTime(2010, 1, 1))
                                select new { name = b.Name, updated = b.Updated };

                    foreach (var b in beers.Take(20))
                    {
                        Console.WriteLine("{0} last updated {1:g}", b.name, b.updated);
                    }
                }
            }
        }
        public void Map2PocoTests()
        {
            using (var cluster = new Cluster(
                       TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from b in context.Query <Beer>()
                                select b;

                    foreach (var beer in beers)
                    {
                        Console.WriteLine(beer.Name);
                    }
                }
            }
        }
        public void First_Empty()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                where beer.Type == "abcdefg"
                                select new { beer.Name };

                    Assert.Throws <InvalidOperationException>(() =>
                    {
                        // ReSharper disable once UnusedVariable
                        var temp = beers.First();
                    });
                }
            }
        }
        public void SubqueryTests_ArraySubqueryWithFilter()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries = from brewery in context.Query <Brewery>()
                                    where brewery.Type == "brewery"
                                    orderby brewery.Name
                                    select new { name = brewery.Name, addresses = brewery.Address.Where(p => p.Length > 3) };

                    foreach (var b in breweries.Take(10))
                    {
                        Console.WriteLine("Brewery {0} has address {1}", b.name, string.Join(", ", b.addresses));
                    }
                }
            }
        }
        public void UseKeys_SelectDocuments()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var query =
                        from brewery in
                        context.Query <Brewery>().UseKeys(new[] { "21st_amendment_brewery_cafe", "357" })
                        select new { name = brewery.Name };

                    foreach (var brewery in query)
                    {
                        Console.WriteLine("Brewery {0}", brewery.name);
                    }
                }
            }
        }
        public void JoinTests_InnerJoin_Simple()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                join brewery in context.Query <Brewery>()
                                on beer.BreweryId equals N1QlFunctions.Key(brewery)
                                select new { beer.Name, beer.Abv, BreweryName = brewery.Name };

                    foreach (var b in beers.Take(10))
                    {
                        Console.WriteLine("Beer {0} with ABV {1} is from {2}", b.Name, b.Abv, b.BreweryName);
                    }
                }
            }
        }
        public void Map2PocoTests_Simple_Projections_MetaId()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = (from b in context.Query <Beer>()
                                 where b.Type == "beer"
                                 select new { name = b.Name, id = N1QlFunctions.Meta(b).Id }).
                                Take(10);

                    foreach (var b in beers)
                    {
                        Console.WriteLine("{0} has id {1}", b.name, b.id);
                    }
                }
            }
        }
        public void NoProjection_Number()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = (from b in context.Query <Beer>()
                                 where b.Type == "beer"
                                 select b.Abv).
                                Take(10);

                    foreach (var b in beers)
                    {
                        Console.WriteLine(b);
                    }
                }
            }
        }
        public void JoinTests_InnerJoin_Prefiltered()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>().Where(p => p.Type == "beer")
                                join brewery in context.Query <Brewery>().Where(p => p.Type == "brewery")
                                on beer.BreweryId equals N1QlFunctions.Key(brewery)
                                    where brewery.Geo.Longitude > -80
                                orderby beer.Name
                                select new { beer.Name, beer.Abv, BreweryName = brewery.Name };

                    foreach (var b in beers.Take(10))
                    {
                        Console.WriteLine("Beer {0} with ABV {1} is from {2}", b.Name, b.Abv, b.BreweryName);
                    }
                }
            }
        }
        public void AggregateTests_JoinBeforeGroupByAndMultipartKey()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries =
                        from beer in context.Query <Beer>()
                        join brewery in context.Query <Brewery>() on beer.BreweryId equals N1QlFunctions.Key(brewery)
                            where beer.Type == "beer"
                        group beer by new { breweryid = beer.BreweryId, breweryName = brewery.Name }
                    into g
                        select new { g.Key.breweryName, count = g.Count(), avgAbv = g.Average(p => p.Abv) };

                    foreach (var brewery in breweries)
                    {
                        Console.WriteLine("Brewery {0} has {1} beers with {2:f2} average ABV", brewery.breweryName, brewery.count, brewery.avgAbv);
                    }
                }
            }
        }
        public void AggregateTests_OrderByAggregate()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var breweries =
                        from beer in context.Query <Beer>()
                        where beer.Type == "beer"
                        group beer by beer.BreweryId
                        into g
                        orderby g.Count() descending
                        select new { breweryid = g.Key, count = g.Count(), avgAbv = g.Average(p => p.Abv) };

                    foreach (var brewery in breweries)
                    {
                        Console.WriteLine("Brewery {0} has {1} beers with {2:f2} average ABV", brewery.breweryid, brewery.count, brewery.avgAbv);
                    }
                }
            }
        }
        public void JoinTests_LeftJoin_SortAndFilter()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var context = new BucketContext(bucket);

                    var beers = from beer in context.Query <Beer>()
                                join breweryGroup in context.Query <Brewery>()
                                on beer.BreweryId equals N1QlFunctions.Key(breweryGroup) into bg
                                from brewery in bg.DefaultIfEmpty()
                                    where beer.Abv > 4
                                orderby brewery.Name, beer.Name
                        select new { beer.Name, beer.Abv, BreweryName = brewery.Name };

                    foreach (var b in beers.Take(10))
                    {
                        Console.WriteLine("Beer {0} with ABV {1} is from {2}", b.Name, b.Abv, b.BreweryName);
                    }
                }
            }
        }