Esempio n. 1
0
        public async void Build_NoQueryString_ShouldReturnAllProductsWithDateAscOrder()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 3. qs dummy
                    string noQueryString        = "";
                    NameValueCollection qsDummy = HttpUtility.ParseQueryString("");

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    var result = products.Select(p => p.CreationDate).ToList();

                    _output.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));

                    // assert
                    Assert.True(result.Zip(result.Skip(1), (a, b) => new { a, b }).All(p => DateTime.Compare(p.a.Date, p.b.Date) <= 0));
                }
        }
Esempio n. 2
0
        public async void Build_KeyWordQueryString_ShouldReturnAllProductsWhoseKeyWordMatchTheQueryString()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 2.2. dummy keyword
                    string keywordDummy = "Table";

                    // 3. qs dummy
                    string KeyWordQueryString   = "?keyword=" + keywordDummy;
                    NameValueCollection qsDummy = HttpUtility.ParseQueryString(KeyWordQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    var result = products.All(p => p.Name.Contains(keywordDummy) || p.Description.Contains(keywordDummy));
                    _output.WriteLine(JsonConvert.SerializeObject(products, Formatting.Indented));

                    // assert
                    Assert.NotEmpty(products);
                    Assert.True(result);
                }
        }
Esempio n. 3
0
        public async void Build_IQueryable_ShouldConstructedWithoutWhereAndOrderByClause()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve context
                    _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // expectedResult
                    var expectedResult = ProductFaker.GetProductList(50).Count;

                    // 3. qs dummy
                    NameValueCollection qsDummy = HttpUtility.ParseQueryString("");

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var result = await productQueryBuilder.Build(qsDummy);

                    var resultCount = result.Count;

                    // assert
                    Assert.Equal(expectedResult, resultCount);
                }
        }
Esempio n. 4
0
        public async void Build_MinPriceQueryString_ShouldReturnAllProductsWhoseMinPriceMatchTheQueryString()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 2.2. dummy minprice
                    decimal minpriceDummy = 30000m;

                    // 3. qs dummy
                    string MinPriceQueryString  = "?minprice=" + minpriceDummy;
                    NameValueCollection qsDummy = HttpUtility.ParseQueryString(MinPriceQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    //var averageList = products.Select(p => p.Reviews.DefaultIfEmpty().Average(r => (int)r.Score));
                    var result = products.All(p => p.Price > minpriceDummy);
                    _output.WriteLine(JsonConvert.SerializeObject(products, Formatting.Indented));

                    // assert
                    Assert.NotEmpty(products);
                    Assert.True(result);
                }
        }
Esempio n. 5
0
        public async void Build_SubCategoryQueryString_ShouldReturnAllProductsWhoseSubCategoryMatchTheQueryString()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 3. qs dummy
                    string SubCategoryQueryString = "?subcategory=40";
                    NameValueCollection qsDummy   = HttpUtility.ParseQueryString(SubCategoryQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    var result = products.All(p => (int)p.SubCategory.Id == 40);

                    _output.WriteLine(JsonConvert.SerializeObject(products, Formatting.Indented));

                    // assert
                    Assert.NotEmpty(products);
                    Assert.True(result);
                }
        }
Esempio n. 6
0
        public async void Build_ReviewScoreDesc9SortQueryString_ShouldReturnAllProductsWithTheSortOrder()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 3. qs dummy
                    string ReviewScoreDescSortQueryString = "?sort=9";
                    NameValueCollection qsDummy           = HttpUtility.ParseQueryString(ReviewScoreDescSortQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    var result = products.Select(p => p.Reviews.Average(r => (int?)r.Score) ?? 0).ToList();

                    _output.WriteLine(JsonConvert.SerializeObject(result));

                    // assert
                    Assert.True(result.Zip(result.Skip(1), (a, b) => new { a, b }).All(p => p.a <= p.b));
                }
        }
Esempio n. 7
0
        public async void Build_PriceDesc3SortQueryString_ShouldReturnAllProductsWithTheSortOrder()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDB(_context);

                    // 3. qs dummy
                    string priceDescSortQueryString = "?sort=3";
                    NameValueCollection qsDummy     = HttpUtility.ParseQueryString(priceDescSortQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    var result = products.Select(p => p.Price).ToList();

                    // assert
                    Assert.True(result.Zip(result.Skip(1), (a, b) => new { a, b }).All(p => p.a > p.b));
                }
        }
Esempio n. 8
0
        public async void Build_AllQueryString_ShouldReturnAllProductsWhoseAllMatchTheQueryString()
        {
            // 2.1. resolve dependency
            using (var container = _builder.Build())
                using (var scope = container.BeginLifetimeScope())
                {
                    // resolve _context
                    CatalogApiDbContext _context = scope.Resolve <CatalogApiDbContext>();
                    // seed initial data in inmemory
                    _context = await SetupInitialDBForAllQueryString(_context);

                    // 2.2. dummy all
                    int     categoryIdDummy    = 0;
                    int     subCategoryIdDummy = 0;
                    string  keywordDummy       = "collaborative";
                    int     reviewscoreDummy   = 5;
                    decimal maxpriceDummy      = 70000m;
                    decimal minpriceDummy      = 60000m;

                    // 3. qs dummy
                    string AllQueryString = "?category=" + categoryIdDummy
                                            + "&subcategory=" + subCategoryIdDummy
                                            + "&keyword=" + keywordDummy
                                            + "&reviewscore=" + reviewscoreDummy
                                            + "&maxprice=" + maxpriceDummy
                                            + "&minprice=" + minpriceDummy;

                    NameValueCollection qsDummy = HttpUtility.ParseQueryString(AllQueryString);

                    // act
                    IQueryBuilder <Product> productQueryBuilder = scope.Resolve <IQueryBuilder <Product> >();
                    var products = await productQueryBuilder.Build(qsDummy);

                    //var averageList = products.Select(p => p.Reviews.DefaultIfEmpty().Average(r => (int)r.Score));
                    var result = products.All(p =>
                    {
                        return((int)p.SubCategory.CategoryId == categoryIdDummy &&
                               (int)p.SubCategory.Id == subCategoryIdDummy &&
                               p.Name.Contains(keywordDummy) || p.Description.Contains(keywordDummy) &&
                               Math.Round(p.Reviews.DefaultIfEmpty().Average(r => (int)r.Score)) == reviewscoreDummy &&
                               p.Price < maxpriceDummy &&
                               p.Price > minpriceDummy);
                    });
                    _output.WriteLine(JsonConvert.SerializeObject(_context.Products, Formatting.Indented));

                    // assert
                    Assert.NotEmpty(products);
                    Assert.True(result);
                }
        }
Esempio n. 9
0
        private async Task <CatalogApiDbContext> SetupInitialDBForAllQueryString(CatalogApiDbContext context)
        {
            // this make sure clearing up the data from previous test
            context.Database.EnsureDeleted();
            // recreate database for current test
            context.Database.EnsureCreated();
            // 1.2. seed test data
            var productsTestData = AllQueryStringProductTestData.GetProducts();

            // wrap with Async IQueryable and add it to context
            context.AddRange(productsTestData);
            await context.SaveChangesAsync();

            return(context);
        }
Esempio n. 10
0
 public ProductQueryBuilder(CatalogApiDbContext context, IIndex <SortConstants, IOrderClauseStrategy> orderStrategyFactory, ISpecificationBuilder <Product> specificationBuilder)
 {
     _context = context;
     _orderStrategyFactory = orderStrategyFactory;
     _specificationBuilder = specificationBuilder;
 }
Esempio n. 11
0
 public ProductQueryBuilderTest(ITestOutputHelper output)
 {
     _output  = output;
     _builder = DISetup.GetAutofacContainerBuilder();
     _context = null;
 }