private static void SeedAttachmentField(PreservationContext dbContext, RequirementDefinition reqDef) { var attachmentField = new Field(reqDef.Plant, Guid.NewGuid().ToString(), FieldType.Attachment, 10); reqDef.AddField(attachmentField); dbContext.SaveChangesAsync().Wait(); }
public async Task <Result <int> > Handle(CreateRequirementDefinitionCommand request, CancellationToken cancellationToken) { var requirementType = await _requirementTypeRepository.GetByIdAsync(request.RequirementTypeId); var newRequirementDefinition = new RequirementDefinition( _plantProvider.Plant, request.Title, request.DefaultIntervalWeeks, request.Usage, request.SortKey); requirementType.AddRequirementDefinition(newRequirementDefinition); foreach (var field in request.Fields) { newRequirementDefinition.AddField(new Field( _plantProvider.Plant, field.Label, field.FieldType, field.SortKey, field.Unit, field.ShowPrevious)); } await _unitOfWork.SaveChangesAsync(cancellationToken); return(new SuccessResult <int>(newRequirementDefinition.Id)); }
protected Field AddAttachmentField(PreservationContext context, RequirementDefinition rd, string label) { var field = new Field(TestPlant, label, FieldType.Attachment, 0); rd.AddField(field); context.SaveChangesAsync().Wait(); return(field); }
protected Field AddNumberField(PreservationContext context, RequirementDefinition rd, string label, string unit, bool showPrevious) { var field = new Field(TestPlant, label, FieldType.Number, 0, unit, showPrevious); rd.AddField(field); context.SaveChangesAsync().Wait(); return(field); }
public void Setup() { _field = new Field(TestPlant, "L", FieldType.Info, 1); _rd1 = new RequirementDefinition(TestPlant, "RD1", 1, RequirementUsage.ForAll, 1); _rd1.SetProtectedIdForTesting(_reqDefId); _rd1.AddField(_field); var rd2 = new RequirementDefinition(TestPlant, "RD2", 1, RequirementUsage.ForAll, 2); var rd3 = new RequirementDefinition(TestPlant, "RD3", 1, RequirementUsage.ForAll, 3); var requirementType = new RequirementType(TestPlant, "C1", "T1", _reqIconOther, 0); requirementType.AddRequirementDefinition(_rd1); requirementType.AddRequirementDefinition(rd2); requirementType.AddRequirementDefinition(rd3); _requirementTypes = new List <RequirementType> { requirementType, new RequirementType(TestPlant, "C2", "T2", _reqIconOther, 0), new RequirementType(TestPlant, "C3", "T3", _reqIconOther, 0) }; _rtSetMock = _requirementTypes.AsQueryable().BuildMockDbSet(); ContextHelper .ContextMock .Setup(x => x.RequirementTypes) .Returns(_rtSetMock.Object); var requirementDefinitions = new List <RequirementDefinition> { _rd1, rd2, rd3 }; _rdSetMock = requirementDefinitions.AsQueryable().BuildMockDbSet(); ContextHelper .ContextMock .Setup(x => x.RequirementDefinitions) .Returns(_rdSetMock.Object); var fields = new List <Field> { _field }; _fieldSetMock = fields.AsQueryable().BuildMockDbSet(); ContextHelper .ContextMock .Setup(x => x.Fields) .Returns(_fieldSetMock.Object); _dut = new RequirementTypeRepository(ContextHelper.ContextMock.Object); }
private void CloneFields(RequirementDefinition sourceRD, RequirementDefinition targetRD) { foreach (var sourceField in sourceRD.Fields) { if (targetRD.Fields.SingleOrDefault(t => t.Label == sourceField.Label) == null) { var targetField = new Field( targetRD.Plant, sourceField.Label, sourceField.FieldType, sourceField.SortKey, sourceField.Unit, sourceField.ShowPrevious); targetRD.AddField(targetField); } } }
protected override void SetupNewDatabase(DbContextOptions <PreservationContext> dbContextOptions) { using (var context = new PreservationContext(dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider)) { var journey = AddJourneyWithStep(context, "J1", "S", AddMode(context, "M1", false), AddResponsible(context, "R1")); var requirementType1 = new RequirementType(TestPlant, _requirementType1Code, _requirementType1Title, _requirementType1Icon, 0); context.RequirementTypes.Add(requirementType1); var requirementType2 = new RequirementType(TestPlant, _requirementType2Code, _requirementType2Title, _requirementType2Icon, 0); context.RequirementTypes.Add(requirementType2); context.SaveChangesAsync().Wait(); var requirementDefinitionWithoutField = new RequirementDefinition(TestPlant, _requirementDefinitionWithoutFieldTitle, 2, RequirementUsage.ForAll, 1); requirementType1.AddRequirementDefinition(requirementDefinitionWithoutField); var requirementDefinitionWithOneInfo = new RequirementDefinition(TestPlant, _requirementDefinitionWithOneInfoTitle, 2, RequirementUsage.ForAll, 1); var infoField = new Field(TestPlant, "Label for Info", FieldType.Info, 0); requirementDefinitionWithOneInfo.AddField(infoField); requirementType1.AddRequirementDefinition(requirementDefinitionWithOneInfo); var requirementDefinitionWithOneAttachment = new RequirementDefinition(TestPlant, _requirementDefinitionWithOneAttachmentTitle, 2, RequirementUsage.ForAll, 1); var attachmentField = new Field(TestPlant, "Label for Attachment", FieldType.Attachment, 0); requirementDefinitionWithOneAttachment.AddField(attachmentField); requirementType1.AddRequirementDefinition(requirementDefinitionWithOneAttachment); var requirementDefinitionWithTwoCheckBoxes = new RequirementDefinition(TestPlant, _requirementDefinitionWithTwoCheckBoxesTitle, 2, RequirementUsage.ForAll, 1); var cbField1 = new Field(TestPlant, "Label for checkBox - second", FieldType.CheckBox, 10); var cbField2 = new Field(TestPlant, "Label for checkBox - first", FieldType.CheckBox, 2); requirementDefinitionWithTwoCheckBoxes.AddField(cbField1); requirementDefinitionWithTwoCheckBoxes.AddField(cbField2); requirementType2.AddRequirementDefinition(requirementDefinitionWithTwoCheckBoxes); var requirementDefinitionWithThreeNumberShowPrev = new RequirementDefinition(TestPlant, _requirementDefinitionWithThreeNumberShowPrevTitle, 2, RequirementUsage.ForAll, 1); var numberFieldPrev1 = new Field(TestPlant, "Label for number - third", FieldType.Number, 15, _unit, true); var numberFieldPrev2 = new Field(TestPlant, "Label for number - first", FieldType.Number, 2, _unit, true); var numberFieldPrev3 = new Field(TestPlant, "Label for number - second", FieldType.Number, 10, _unit, true); requirementDefinitionWithThreeNumberShowPrev.AddField(numberFieldPrev1); requirementDefinitionWithThreeNumberShowPrev.AddField(numberFieldPrev2); requirementDefinitionWithThreeNumberShowPrev.AddField(numberFieldPrev3); requirementType2.AddRequirementDefinition(requirementDefinitionWithThreeNumberShowPrev); var requirementDefinitionWithOneNumberNoPrev = new RequirementDefinition(TestPlant, _requirementDefinitionWithOneNumberNoPrevTitle, 2, RequirementUsage.ForAll, 1); var numberFieldNoPrev = new Field(TestPlant, "Label for number", FieldType.Number, 10, _unit, false); requirementDefinitionWithOneNumberNoPrev.AddField(numberFieldNoPrev); requirementType2.AddRequirementDefinition(requirementDefinitionWithOneNumberNoPrev); var requirementDefinitionForVoidedTest = new RequirementDefinition(TestPlant, _requirementDefinitionForVoidedTest, 2, RequirementUsage.ForAll, 1); var vField1 = new Field(TestPlant, "Label for field", FieldType.CheckBox, 10); requirementDefinitionForVoidedTest.AddField(vField1); requirementType2.AddRequirementDefinition(requirementDefinitionForVoidedTest); context.SaveChangesAsync().Wait(); var requirementWithoutField = new TagRequirement(TestPlant, _interval, requirementDefinitionWithoutField); var requirementWithOneInfo = new TagRequirement(TestPlant, _interval, requirementDefinitionWithOneInfo); var requirementWithOneAttachment = new TagRequirement(TestPlant, _interval, requirementDefinitionWithOneAttachment); var requirementWithTwoCheckBoxes = new TagRequirement(TestPlant, _interval, requirementDefinitionWithTwoCheckBoxes); var requirementWithOneNumberNoPrev = new TagRequirement(TestPlant, _interval, requirementDefinitionWithOneNumberNoPrev); var requirementWithThreeNumberShowPrev = new TagRequirement(TestPlant, _interval, requirementDefinitionWithThreeNumberShowPrev); var requirementThatIsVoided = new TagRequirement(TestPlant, _interval, requirementDefinitionForVoidedTest); requirementThatIsVoided.IsVoided = true; var tag = new Tag(TestPlant, TagType.Standard, "TagNo", "Description", journey.Steps.ElementAt(0), new List <TagRequirement> { requirementWithoutField, requirementWithOneInfo, requirementWithOneAttachment, requirementWithTwoCheckBoxes, requirementWithOneNumberNoPrev, requirementWithThreeNumberShowPrev, requirementThatIsVoided }); context.Tags.Add(tag); context.SaveChangesAsync().Wait(); _tagId = tag.Id; _requirementDefinitionWithOneAttachmentId = requirementDefinitionWithOneAttachment.Id; _requirementDefinitionWithTwoCheckBoxesId = requirementDefinitionWithTwoCheckBoxes.Id; _requirementDefinitionWithThreeNumberShowPrevId = requirementDefinitionWithThreeNumberShowPrev.Id; _requirementWithoutFieldId = requirementWithoutField.Id; _requirementWithOneInfoId = requirementWithOneInfo.Id; _requirementWithOneAttachmentId = requirementWithOneAttachment.Id; _requirementWithTwoCheckBoxesId = requirementWithTwoCheckBoxes.Id; _requirementWithThreeNumberShowPrevId = requirementWithThreeNumberShowPrev.Id; _requirementWithOneNumberNoPrevId = requirementWithOneNumberNoPrev.Id; _attachmentFieldId = attachmentField.Id; _firstCbFieldId = cbField2.Id; _secondCbFieldId = cbField1.Id; _firstNumberFieldId = numberFieldPrev2.Id; _secondNumberFieldId = numberFieldPrev3.Id; _thirdNumberFieldId = numberFieldPrev1.Id; } }
public void Setup() { // Arrange var requirementTypeId = 1; var requirementDefinitionId = 2; _fieldId = 3; var requirementTypeRepositoryMock = new Mock <IRequirementTypeRepository>(); _requirementType = new RequirementType(TestPlant, "Req type code", "Req type title", RequirementTypeIcon.Other, 10); _requirementType.SetProtectedIdForTesting(requirementTypeId); _requirementDefinition = new RequirementDefinition(TestPlant, _oldTitle, _oldDefaultWeeks, _oldUsage, _oldSortKey); _requirementDefinition.SetProtectedIdForTesting(requirementDefinitionId); _field = new Field(TestPlant, _oldLabel, _oldFieldType, _oldSortKey, _oldUnit, _oldShowPrevious); _field.SetProtectedIdForTesting(_fieldId); _requirementDefinition.AddField(_field); _requirementType.AddRequirementDefinition(_requirementDefinition); requirementTypeRepositoryMock.Setup(j => j.GetByIdAsync(requirementTypeId)) .Returns(Task.FromResult(_requirementType)); _updateCommandWithUpdateField = new UpdateRequirementDefinitionCommand( requirementTypeId, requirementDefinitionId, _newSortKey1, _newUsage, _newTitle, _newDefaultWeeks, _rowVersion, new List <UpdateFieldsForCommand> { new UpdateFieldsForCommand(_fieldId, _newLabel1, FieldType.Number, _newSortKey1, true, _rowVersion, _newUnit1, _newShowPrevious1) }, new List <FieldsForCommand>() ); _updateCommandWithDeleteField = new UpdateRequirementDefinitionCommand( requirementTypeId, requirementDefinitionId, _newSortKey1, _newUsage, _newTitle, _newDefaultWeeks, _rowVersion, new List <UpdateFieldsForCommand>(), new List <FieldsForCommand>() ); _updateCommandWithUpdateAndAddField = new UpdateRequirementDefinitionCommand( requirementTypeId, requirementDefinitionId, _newSortKey1, _newUsage, _newTitle, _newDefaultWeeks, _rowVersion, new List <UpdateFieldsForCommand> { new UpdateFieldsForCommand(_fieldId, _newLabel1, FieldType.Number, _newSortKey1, true, _rowVersion, _newUnit1, _newShowPrevious1) }, new List <FieldsForCommand> { new FieldsForCommand(_newLabel2, _newFieldType2, _newSortKey2, _newUnit2, _newShowPrevious2) } ); _dut = new UpdateRequirementDefinitionCommandHandler( requirementTypeRepositoryMock.Object, UnitOfWorkMock.Object, PlantProviderMock.Object); }
public void Setup() { var reqDefId = 0; _modeRepository = new ModeRepository(_plantProvider, _sourceModes); _sourceModes.Add(new Mode(_sourcePlant, "ModeA", false)); _sourceModes.Add(new Mode(_sourcePlant, "ModeB", false)); _requirementTypeRepository = new RequirementTypeRepository(_plantProvider, _sourceRequirementTypes); var requirementTypeA = new RequirementType(_sourcePlant, "RequirementTypeCodeA", "RequirementTypeTitleA", _requirementIconOther, 1); var reqDefA1 = new RequirementDefinition(_sourcePlant, "RequirementDefCodeA1", 1, RequirementUsage.ForAll, 2); reqDefA1.SetProtectedIdForTesting(++reqDefId); requirementTypeA.AddRequirementDefinition(reqDefA1); var reqDefA2 = new RequirementDefinition(_sourcePlant, "RequirementDefCodeA2", 3, RequirementUsage.ForAll, 4); reqDefA2.SetProtectedIdForTesting(++reqDefId); requirementTypeA.AddRequirementDefinition(reqDefA2); reqDefA1.AddField(new Field(_sourcePlant, "LabelA", FieldType.Number, 1, "UnitA", true)); var requirementTypeB = new RequirementType(_sourcePlant, "RequirementTypeCodeB", "RequirementTypeTitleB", _requirementIconOther, 2); _sourceRequirementTypes.Add(requirementTypeA); _sourceRequirementTypes.Add(requirementTypeB); _tagFunctionRepository = new TagFunctionRepository(_plantProvider, _sourceTagFunctions); var tagFunctionA = new TagFunction(_sourcePlant, _tfCodeA, "TagFunctionDescA", _regCodeA); tagFunctionA.AddRequirement(new TagFunctionRequirement(_sourcePlant, 1, reqDefA1)); tagFunctionA.AddRequirement(new TagFunctionRequirement(_sourcePlant, 2, reqDefA2)); var tagFunctionB = new TagFunction(_sourcePlant, _tfCodeB, "TagFunctionDescB", _regCodeB); _sourceTagFunctions.Add(tagFunctionA); _sourceTagFunctions.Add(tagFunctionB); _tagFunctionApiServiceMock = new Mock <ITagFunctionApiService>(); _tagFunctionApiServiceMock .Setup(t => t.TryGetTagFunctionAsync(TestPlant, _tfCodeA, _regCodeA)) .Returns(Task.FromResult(new PCSTagFunction { Code = _tfCodeA, RegisterCode = _regCodeA })); _tagFunctionApiServiceMock .Setup(t => t.TryGetTagFunctionAsync(TestPlant, _tfCodeB, _regCodeB)) .Returns(Task.FromResult(new PCSTagFunction { Code = _tfCodeB, RegisterCode = _regCodeB })); _command = new CloneCommand(_sourcePlant, TestPlant); _dut = new CloneCommandHandler( _plantProvider, _plantProvider, UnitOfWorkMock.Object, _modeRepository, _requirementTypeRepository, _tagFunctionRepository, _tagFunctionApiServiceMock.Object); UnitOfWorkMock .Setup(uiw => uiw.SaveChangesAsync(It.IsAny <CancellationToken>())) .Callback(() => { // Need this to simulate what EF Core do with Ids upon saving new Items _requirementTypeRepository.Save(); }); }
public void AddField_ShouldThrowExceptionTest_ForNullField() { Assert.ThrowsException <ArgumentNullException>(() => _dut.AddField(null)); Assert.AreEqual(0, _dut.Fields.Count); }