public async Task DescriptionGroupCannotHaveEmptyName()
        {
            //arrange
            var server      = TestFactory.GetServer();
            var shopManager = server.GetRequiredService <IShopManager>();
            var validator   = server.GetRequiredService <IShopValidator <DescriptionGroup> >();

            var descGrpWithInvalidName1 = new DescriptionGroup()
            {
                Name = ""
            };
            var descGrpWithInvalidName2 = new DescriptionGroup()
            {
                Name = null
            };

            //act
            var result1 = await validator.ValidateAsync(shopManager, descGrpWithInvalidName1);

            var result2 = await validator.ValidateAsync(shopManager, descGrpWithInvalidName2);

            Assert.True(!result1.Succeeded &&
                        result1.Errors.Any(x => x.Code == nameof(OperationErrorDescriber.EmptyDescriptionGroupName)));
            Assert.True(!result2.Succeeded &&
                        result1.Errors.Any(x => x.Code == nameof(OperationErrorDescriber.EmptyDescriptionGroupName)));
        }
        public async Task DescriptionGroupAlreadyExists()
        {
            //arrange
            var server      = TestFactory.GetServer();
            var shopManager = server.GetRequiredService <IShopManager>();
            var validator   = server.GetRequiredService <IShopValidator <DescriptionGroup> >();
            var shopRepo    = server.GetShopRepository();

            var descGrp1 = new DescriptionGroup()
            {
                Name = "testGroup"
            };
            var descGrp2 = new DescriptionGroup()
            {
                Name = "testGroup"
            };

            //act
            await shopRepo.CreateDescriptionGroupAsync(descGrp1, CancellationToken.None);

            var result = await validator.ValidateAsync(shopManager, descGrp2);

            Assert.True(!result.Succeeded &&
                        result.Errors.Any(x => x.Code == nameof(OperationErrorDescriber.DuplicateDescriptionGroupName)));
        }
Esempio n. 3
0
        public async Task <OperationResult> CreateDescriptionGroupAsync(DescriptionGroup descriptionGroup)
        {
            ThrowIfDisposed();
            if (descriptionGroup == null)
            {
                throw new ArgumentNullException(nameof(descriptionGroup));
            }

            if (await _repository.DescriptionGroupItemsQueryable
                .AnyAsync(x => x.Name == descriptionGroup.Name, CancellationToken))
            {
                return(OperationResult.Failure(ErrorDescriber.DuplicateDescriptionGroupName()));
            }

            var result = await Validate(new[] { descriptionGroup }, DescriptionGroupValidators);

            if (!result.Succeeded)
            {
                return(result);
            }

            result = await _repository.CreateDescriptionGroupAsync(descriptionGroup, CancellationToken);

            if (!result.Succeeded)
            {
                return(result);
            }

            return(OperationResult.Success());
        }
        public async Task PassValidDescriptionItem()
        {
            //arrange
            var server      = TestFactory.GetServer();
            var shopManager = server.GetRequiredService <IShopManager>();
            var validator   = server.GetRequiredService <IShopValidator <DescriptionGroupItem> >();
            var shopRepo    = server.GetShopRepository();

            var product = new Product()
            {
                Name = "testProduct"
            };
            await shopRepo.CreateProductAsync(product, CancellationToken.None);

            var descGrp = new DescriptionGroup()
            {
                Name = "testGroup"
            };
            await shopRepo.CreateDescriptionGroupAsync(descGrp, CancellationToken.None);

            var descItem = new DescriptionGroupItem()
            {
                Name = "testGroupItem", DescriptionGroupId = descGrp.Id
            };

            //act
            var result = await validator.ValidateAsync(shopManager, descItem);

            Assert.True(result.Succeeded);
        }
        public async Task DescriptionWithoutExistingReferences()
        {
            //arrange
            var server      = TestFactory.GetServer();
            var shopManager = server.GetRequiredService <IShopManager>();
            var validator   = server.GetRequiredService <IShopValidator <Description> >();
            var shopRepo    = server.GetShopRepository();

            var product = new Product()
            {
                Name = "testProduct"
            };
            await shopRepo.CreateProductAsync(product, CancellationToken.None);

            var descGrp = new DescriptionGroup()
            {
                Name = "testGroup"
            };
            await shopRepo.CreateDescriptionGroupAsync(descGrp, CancellationToken.None);

            var descItem = new DescriptionGroupItem()
            {
                Name = "testGroupItem", DescriptionGroupId = descGrp.Id
            };
            await shopRepo.CreateDescriptionGroupItemAsync(descItem, CancellationToken.None);

            var desc1 = new Description()
            {
                Value = "testDescription", ProductId = product.Id, DescriptionGroupItemId = 322
            };
            var desc2 = new Description()
            {
                Value = "testDescription", ProductId = 322, DescriptionGroupItemId = descItem.Id
            };

            //act
            var result1 = await validator.ValidateAsync(shopManager, desc1);

            var result2 = await validator.ValidateAsync(shopManager, desc2);

            Assert.True(!result1.Succeeded &&
                        result1.Errors.Where(x => x.Code == nameof(OperationErrorDescriber.EntityNotFound)).Take(2).Count() == 1);
            Assert.True(!result1.Succeeded &&
                        result1.Errors.Where(x => x.Code == nameof(OperationErrorDescriber.EntityNotFound)).Take(2).Count() == 1);
        }
        public async Task PassValidDescriptionGroup()
        {
            //arrange
            var server      = TestFactory.GetServer();
            var shopManager = server.GetRequiredService <IShopManager>();
            var validator   = server.GetRequiredService <IShopValidator <DescriptionGroup> >();
            var shopRepo    = server.GetShopRepository();

            var descGrp = new DescriptionGroup()
            {
                Name = "testGroup"
            };

            //act
            var result = await validator.ValidateAsync(shopManager, descGrp);

            Assert.True(result.Succeeded);
        }
Esempio n. 7
0
        public async Task <OperationResult> CreateDescriptionGroupAsync(DescriptionGroup descriptionGroup,
                                                                        CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (descriptionGroup == null)
            {
                throw new ArgumentNullException(nameof(descriptionGroup));
            }

            Context.Entry(descriptionGroup).State = EntityState.Added;
            try
            {
                await SaveChangesAsync(ct);
            }
            catch (DbUpdateException)
            {
                return(OperationResult.Failure(ErrorDescriber.DbUpdateFailure()));
            }

            return(OperationResult.Success());
        }
Esempio n. 8
0
        public async Task <OperationResult> UpdateDescriptionGroupAsync(DescriptionGroup descriptionGroup,
                                                                        CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (descriptionGroup == null)
            {
                throw new ArgumentNullException(nameof(descriptionGroup));
            }

            var existing = await DescGroupsSet.FindAsync(new object[] { descriptionGroup.Id }, ct);

            if (existing == null)
            {
                Context.Entry(descriptionGroup).State = EntityState.Modified;
            }
            else
            {
                existing.Name        = descriptionGroup.Name;
                existing.Description = descriptionGroup.Description;
            }

            try
            {
                await SaveChangesAsync(ct);
            }
            catch (DbUpdateConcurrencyException)
            {
                return(OperationResult.Failure(ErrorDescriber.ConcurrencyFailure()));
            }
            catch (DbUpdateException)
            {
                return(OperationResult.Failure(ErrorDescriber.DbUpdateFailure()));
            }

            return(OperationResult.Success());
        }
Esempio n. 9
0
        public async Task <OperationResult> UpdateDescriptionGroupAsync(DescriptionGroup descriptionGroup)
        {
            ThrowIfDisposed();
            if (descriptionGroup == null)
            {
                throw new ArgumentNullException(nameof(descriptionGroup));
            }

            var result = await Validate(new[] { descriptionGroup }, DescriptionGroupValidators);

            if (!result.Succeeded)
            {
                return(result);
            }

            result = await _repository.UpdateDescriptionGroupAsync(descriptionGroup, CancellationToken);

            if (!result.Succeeded)
            {
                return(result);
            }

            return(OperationResult.Success());
        }