public void AddProjectDataModel_DuplicateItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var exception = Record.ExceptionAsync(() => projectDataModelService.AddProjectDataModel(1, "Product", null, null, null, null));

            Assert.IsType <DuplicateProjectDataModelException>(exception?.Result);
        }
        public void AddDataModelProperty_DuplicateItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var exception = Record.ExceptionAsync(() => projectDataModelService.AddDataModelProperty(1, "Name", "Name", "string", "input-text", true, null, null, null));

            Assert.IsType <DuplicateProjectDataModelPropertyException>(exception?.Result);
        }
        public async void DeleteDataModelProperty_ValidItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            await projectDataModelService.DeleteDataModelProperty(1);

            Assert.Empty(_dataProperty);
        }
        public async void GetProjectDataModelById_ReturnNull()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var projectDataModel        = await projectDataModelService.GetProjectDataModelById(2);

            Assert.Null(projectDataModel);
        }
        public async void GetDataModelProperties_ReturnEmpty()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var dataModelProperties     = await projectDataModelService.GetDataModelProperties(2);

            Assert.Empty(dataModelProperties);
        }
        public void DeleteDataModel_RelatedModelException()
        {
            var relatedModel = new ProjectDataModel
            {
                Name       = "Category",
                ProjectId  = 1,
                Properties = new List <ProjectDataModelProperty>
                {
                    new ProjectDataModelProperty
                    {
                        Name = "Products",
                        RelatedProjectDataModelId = 1
                    }
                }
            };

            _data.Add(relatedModel);

            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);

            var exception = Record.ExceptionAsync(() => projectDataModelService.DeleteDataModel(1));

            Assert.IsType <RelatedProjectDataModelException>(exception?.Result);
            Assert.Equal($"The data model \"Product\" is referenced by the following models: Category", exception?.Result.Message);
        }
        public async void GetProjectDataModels_ReturnItems()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var dataModels = await projectDataModelService.GetProjectDataModels(1, false);

            Assert.NotEmpty(dataModels);
        }
        public void AddProjectDataModel_InvalidProject()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var exception = Record.ExceptionAsync(() => projectDataModelService.AddProjectDataModel(2, "Category", null, null, null, null));

            Assert.IsType <ProjectNotFoundException>(exception?.Result);
        }
        public void AddDataModelProperty_InvalidDataModel()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var exception = Record.ExceptionAsync(() => projectDataModelService.AddDataModelProperty(2, "Price", "Price", "int", "input-text", false, null, null, null));

            Assert.IsType <ProjectDataModelNotFoundException>(exception?.Result);
        }
        public async void GetProjectDataModelPropertyByName_ReturnNull()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var property = await projectDataModelService.GetProjectDataModelPropertyByName(2, "Name");

            Assert.Null(property);
        }
        public async void AddDataModelProperty_ValidItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            int newId = await projectDataModelService.AddDataModelProperty(1, "Price", "Price", "int", "input-text", false, null, null, null);

            Assert.True(newId > 1);
            Assert.True(_dataProperty.Count > 1);
        }
        public async void GetProjectDataModelPropertyById_ReturnItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var property = await projectDataModelService.GetProjectDataModelPropertyById(1);

            Assert.NotNull(property);
            Assert.Equal(1, property.Id);
        }
        public async void GetProjectDataModelByName_ReturnItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var projectDataModel        = await projectDataModelService.GetProjectDataModelByName(1, "Product");

            Assert.NotNull(projectDataModel);
            Assert.Equal(1, projectDataModel.Id);
        }
        public async void AddProjectDataModel_ValidItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            int newId = await projectDataModelService.AddProjectDataModel(1, "Category", null, null, null, null);

            Assert.True(newId > 1);
            Assert.True(_data.Count > 1);

            var newData = _data.First(a => a.Id == newId);

            Assert.NotNull(newData.Label);
        }
        public async void RenameDataModel_ValidItem()
        {
            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            await projectDataModelService.UpdateDataModel(new ProjectDataModel
            {
                Id   = 1,
                Name = "newName"
            });

            var dataModel = _data.First(d => d.Id == 1);

            Assert.Equal("newName", dataModel.Name);
        }
        public void DeleteDataModels_RelatedModelException()
        {
            _data.Add(new ProjectDataModel
            {
                Id         = 2,
                Name       = "Category",
                ProjectId  = 1,
                Properties = new List <ProjectDataModelProperty>
                {
                    new ProjectDataModelProperty
                    {
                        Name = "Products",
                        RelatedProjectDataModelId = 1,
                        RelatedProjectDataModel   = new ProjectDataModel
                        {
                            Id   = 1,
                            Name = "Product"
                        }
                    }
                },
            });

            _data.Add(new ProjectDataModel
            {
                Id         = 3,
                Name       = "Service",
                ProjectId  = 1,
                Properties = new List <ProjectDataModelProperty>
                {
                    new ProjectDataModelProperty
                    {
                        Name = "Category",
                        RelatedProjectDataModelId = 2,
                        RelatedProjectDataModel   = new ProjectDataModel
                        {
                            Id   = 1,
                            Name = "Category"
                        }
                    }
                },
            });

            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);

            var exception = Record.ExceptionAsync(() => projectDataModelService.DeleteDataModels(1, new int[] { 1, 2 }));

            Assert.IsType <RelatedProjectDataModelException>(exception?.Result);
            Assert.Equal($"Cannot perform bulk delete because the following models depend on the models to be deleted: Service", exception?.Result.Message);
        }
        public void RenameDataModel_DuplicateItem()
        {
            _data.Add(new ProjectDataModel
            {
                Id        = 2,
                ProjectId = 1,
                Name      = "newName"
            });

            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            var exception = Record.ExceptionAsync(() => projectDataModelService.UpdateDataModel(new ProjectDataModel
            {
                Id   = 1,
                Name = "newName"
            }));

            Assert.IsType <DuplicateProjectDataModelException>(exception?.Result);
        }
        public async void DeleteDataModels_ValidItem()
        {
            var relatedModel = new ProjectDataModel
            {
                Id         = 2,
                Name       = "Category",
                ProjectId  = 1,
                Properties = new List <ProjectDataModelProperty>
                {
                    new ProjectDataModelProperty
                    {
                        Name = "Products",
                        RelatedProjectDataModelId = 1
                    }
                }
            };

            _data.Add(relatedModel);

            int callOrder = 0;

            _dataModelRepository.Setup(r => r.Delete(2, It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask).Callback((int id, CancellationToken cancellationToken) =>
            {
                Assert.Equal(0, callOrder++);
            });

            _dataModelRepository.Setup(r => r.Delete(1, It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask).Callback((int id, CancellationToken cancellationToken) =>
            {
                Assert.Equal(1, callOrder);
            });

            var projectDataModelService = new ProjectDataModelService(_dataModelRepository.Object, _propertyRepository.Object, _projectRepository.Object);
            await projectDataModelService.DeleteDataModels(1, new int[] { 1, 2 });
        }