Ejemplo n.º 1
0
        public void Map2PocoTests_NewObjectsInArray()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var query = from brewery in bucket.Queryable <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)));
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public void Test_POCO_Basic()
 {
     using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
     {
         using (var bucket = cluster.OpenBucket("beer-sample"))
         {
         }
     }
 }
Ejemplo n.º 3
0
        public void AggregateTests_SimpleCount()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var count = bucket.Queryable <Beer>().Count(p => p.Type == "beer");

                    Console.WriteLine("Number of beers is {0}", count);
                }
            }
        }
Ejemplo n.º 4
0
        public void AnyAllTests_AllOnMainDocument_ReturnsFalse()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var isAllBreweries = bucket.Queryable <Brewery>().All(p => p.Type == "brewery");

                    Assert.False(isAllBreweries);
                }
            }
        }
Ejemplo n.º 5
0
        public void AggregateTests_SimpleAverage()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var avg = bucket.Queryable <Beer>().Where(p => p.Type == "beer" && N1Ql.IsValued(p.Abv)).Average(p => p.Abv);

                    Console.WriteLine("Average ABV of all beers is {0}", avg);
                }
            }
        }
Ejemplo n.º 6
0
        protected void InitializeCluster(IContractResolver contractResolver = null)
        {
            if (contractResolver != null)
            {
                _contractResolver = contractResolver;
            }
            var config = TestConfigurations.DefaultConfig();

            //var config = new ClientConfiguration();
            //config.Servers.Add(new Uri("http://127.0.0.1:8091"));
            config.DeserializationSettings.ContractResolver = _contractResolver;
            config.SerializationSettings.ContractResolver   = _contractResolver;
            ClusterHelper.Initialize(config);
        }
Ejemplo n.º 7
0
        public void Single_HasResult()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>()
                                where beer.Name == "21A IPA"
                                select new { beer.Name };

                    Console.WriteLine(beers.Single().Name);
                }
            }
        }
Ejemplo n.º 8
0
        public void Test_Select_Children()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket())
                {
                    var query = from c in bucket.Queryable <Contact>()
                                select c.Children;

                    const string expected = "SELECT `Extent1`.`children` FROM `default` as `Extent1`";
                    Assert.AreEqual(expected, CreateN1QlQuery(bucket, query.Expression));
                }
            }
        }
Ejemplo n.º 9
0
        public void Map2PocoTests_Simple_Projections_TypeFilterAttribute()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = (from b in bucket.Queryable <BeerFiltered>()
                                 select new { type = b.Type }).
                                AsEnumerable();

                    Assert.True(beers.All(p => p.type == "beer"));
                }
            }
        }
Ejemplo n.º 10
0
        public void Map2PocoTests_Explain()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var explanation = (from b in bucket.Queryable <Beer>()
                                       where b.Type == "beer"
                                       select b).
                                      Explain();

                    Console.WriteLine(explanation);
                }
            }
        }
Ejemplo n.º 11
0
        public void SingleOrDefault_Empty()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>()
                                where beer.Type == "abcdefg"
                                select new { beer.Name };

                    var aBeer = beers.SingleOrDefault();
                    Assert.IsNull(aBeer);
                }
            }
        }
Ejemplo n.º 12
0
        public void AnyAllTests_AnyOnMainDocument_ReturnsFalse()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var hasFaketype = (from b in bucket.Queryable <Brewery>()
                                       where b.Type == "faketype"
                                       select new { name = b.Name, address = b.Address }).
                                      Any();

                    Assert.False(hasFaketype);
                }
            }
        }
Ejemplo n.º 13
0
        public void AnyAllTests_AllOnMainDocument_ReturnsTrue()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var allBreweriesHaveAddress = (from b in bucket.Queryable <Brewery>()
                                                   where b.Type == "brewery"
                                                   select new { b.Name })
                                                  .All(p => p.Name != "");

                    Assert.True(allBreweriesHaveAddress);
                }
            }
        }
Ejemplo n.º 14
0
        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 breweries = (from b in bucket.Queryable <Brewery>()
                                     select new { type = b.Type })
                                    .AsEnumerable();

                    Assert.True(breweries.All(p => p.type == "brewery"));
                }
            }
        }
Ejemplo n.º 15
0
        public void FirstOrDefault_HasResult()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>()
                                where beer.Type == "beer"
                                select new { beer.Name };

                    var aBeer = beers.FirstOrDefault();
                    Assert.IsNotNull(aBeer);
                    Console.WriteLine(aBeer.Name);
                }
            }
        }
Ejemplo n.º 16
0
        public void Map2PocoTests_Simple_Projections()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from b in bucket.Queryable <Beer>()
                                select new { name = b.Name, abv = b.Abv };

                    foreach (var b in beers)
                    {
                        Console.WriteLine("{0} has {1} ABV", b.name, b.abv);
                    }
                }
            }
        }
Ejemplo n.º 17
0
        public void AnyAllTests_AnyNestedArray()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var breweries = (from b in bucket.Queryable <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()));
                }
            }
        }
Ejemplo n.º 18
0
        public void NestTests_Unnest_Simple()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var breweries = from brewery in bucket.Queryable <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);
                    }
                }
            }
        }
Ejemplo n.º 19
0
        public void Map2PocoTests()
        {
            using (var cluster = new Cluster(
                       TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from b in bucket.Queryable <Beer>()
                                select b;

                    foreach (var beer in beers)
                    {
                        Console.WriteLine(beer.Name);
                    }
                }
            }
        }
Ejemplo n.º 20
0
        public void Map2PocoTests_Simple_Projections_WhereDateTime()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from b in bucket.Queryable <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);
                    }
                }
            }
        }
Ejemplo n.º 21
0
        public void Map2PocoTests_Simple_Projections_MetaWhere()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = (from b in bucket.Queryable <Beer>()
                                 where b.Type == "beer" && N1Ql.Meta(b).Type == "json"
                                 select new { name = b.Name }).
                                Take(10);

                    foreach (var b in beers)
                    {
                        Console.WriteLine("{0} is a JSON document", b.name);
                    }
                }
            }
        }
Ejemplo n.º 22
0
        public void UseKeys_SelectDocuments()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var query =
                        from brewery in
                        bucket.Queryable <Brewery>().UseKeys(new[] { "21st_amendment_brewery_cafe", "357" })
                        select new { name = brewery.Name };

                    foreach (var brewery in query)
                    {
                        Console.WriteLine("Brewery {0}", brewery.name);
                    }
                }
            }
        }
Ejemplo n.º 23
0
        public void SingleOrDefault_HasManyResults()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>()
                                where beer.Type == "beer"
                                select new { beer.Name };

                    Assert.Throws <InvalidOperationException>(() =>
                    {
                        // ReSharper disable once UnusedVariable
                        var temp = beers.SingleOrDefault();
                    });
                }
            }
        }
Ejemplo n.º 24
0
        public void Map2PocoTests_Simple_Projections_MetaId()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = (from b in bucket.Queryable <Beer>()
                                 where b.Type == "beer"
                                 select new { name = b.Name, id = N1Ql.Meta(b).Id }).
                                Take(10);

                    foreach (var b in beers)
                    {
                        Console.WriteLine("{0} has id {1}", b.name, b.id);
                    }
                }
            }
        }
Ejemplo n.º 25
0
        public void JoinTests_InnerJoin_Simple()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>()
                                join brewery in bucket.Queryable <Brewery>()
                                on beer.BreweryId equals N1Ql.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);
                    }
                }
            }
        }
Ejemplo n.º 26
0
        public void SubqueryTests_ArraySubqueryWithFilter()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var breweries = from brewery in bucket.Queryable <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));
                    }
                }
            }
        }
Ejemplo n.º 27
0
        public void JoinTests_InnerJoin_Prefiltered()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var beers = from beer in bucket.Queryable <Beer>().Where(p => p.Type == "beer")
                                join brewery in bucket.Queryable <Brewery>().Where(p => p.Type == "brewery")
                                on beer.BreweryId equals N1Ql.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);
                    }
                }
            }
        }
Ejemplo n.º 28
0
        public void Test_AnonymousType_In_Projection()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket())
                {
                    var query = from c in QueryFactory.Queryable <Contact>(bucket)
                                select new
                    {
                        age   = c.Age,
                        fname = c.FirstName
                    };

                    const string expected = "SELECT `Extent1`.`age` as `age`, `Extent1`.`fname` as `fname` FROM `default` as `Extent1`";

                    var N1QLQuery = CreateN1QlQuery(bucket, query.Expression);

                    Assert.AreEqual(expected, N1QLQuery);
                }
            }
        }
Ejemplo n.º 29
0
        public void AggregateTests_JoinBeforeGroupByAndMultipartKey()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var breweries =
                        from beer in bucket.Queryable <Beer>()
                        join brewery in bucket.Queryable <Brewery>() on beer.BreweryId equals N1Ql.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);
                    }
                }
            }
        }
Ejemplo n.º 30
0
        public void AggregateTests_OrderByAggregate()
        {
            using (var cluster = new Cluster(TestConfigurations.DefaultConfig()))
            {
                using (var bucket = cluster.OpenBucket("beer-sample"))
                {
                    var breweries =
                        from beer in bucket.Queryable <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);
                    }
                }
            }
        }