Example #1
0
        public void Should_Edit_Modality()
        {
            var context = GetDbContext();

            var id = Guid.NewGuid();

            context.Modalities.Add(new Modality
            {
                Id          = id,
                Name        = "Modality 1",
                DisplayName = "M1"
            });

            context.SaveChanges();

            var modalityToEdit = context.Modalities.FirstOrDefault(x => x.DisplayName == "M1");

            var sut = new Edit.Handler(context);

            var result = sut.Handle(new Edit.Command
            {
                Id          = id,
                Name        = "Updated Modality",
                DisplayName = "UM"
            }, CancellationToken.None).Result;

            var updatedLocation = context.Modalities.FirstOrDefault(x => x.Id == id);

            Assert.Equal("Updated Modality", updatedLocation.Name);
        }
Example #2
0
        public async Task Expect_Edit_Article()
        {
            var createCommand = new Create.Command(new Create.ArticleData()
            {
                Title       = "Test article dsergiu77",
                Description = "Description of the test article",
                Body        = "Body of the test article",
                TagList     = new string[] { "tag1", "tag2" }
            });

            var createdArticle = await ArticleHelpers.CreateArticle(this, createCommand);

            var command = new Edit.Command(new(new Edit.ArticleData()
            {
                Title = "Updated " + createdArticle.Title,
                Description = "Updated" + createdArticle.Description,
                Body = "Updated" + createdArticle.Body,
            }), createdArticle.Slug);

            // remove the first tag and add a new tag
            command.Model.Article.TagList = new string[] { createdArticle.TagList[1], "tag3" };

            var dbContext = GetDbContext();

            var articleEditHandler = new Edit.Handler(dbContext);
            var edited             = await articleEditHandler.Handle(command, new System.Threading.CancellationToken());

            Assert.NotNull(edited);
            Assert.Equal(edited.Article.Title, command.Model.Article.Title);
            Assert.Equal(edited.Article.TagList.Count(), command.Model.Article.TagList.Count());
            // use assert Contains because we do not know the order in which the tags are saved/retrieved
            Assert.Contains(edited.Article.TagList[0], command.Model.Article.TagList);
            Assert.Contains(edited.Article.TagList[1], command.Model.Article.TagList);
        }
Example #3
0
        public void Handle_IdIsNull_ThrowsNullReferenceException()
        {
            var token = new CancellationTokenSource().Token;

            _id = Guid.Empty;

            using (var context = new ExpenseTrackerDbContext(_options))
            {
                // Setup
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                Seed.SeedData(context);

                _editCommand = new Edit.Command
                {
                    Id      = _id,
                    Expense = _expenseDTO
                };
                _editHandler = new Edit.Handler(context, _mapper);

                // Attempt
                var action = _editHandler.Handle(_editCommand, token);

                Assert.ThrowsAsync <NullReferenceException>(() => action);
                Assert.Equal("Can't find the expense record.",
                             action.Exception.InnerException.Message);
            }
        }
Example #4
0
        public void Should_Edit_License()
        {
            //arrange
            var context = GetDbContext();

            var modalityId = Guid.NewGuid();

            context.Modalities.Add(new Domain.Modality {
                Id = modalityId, Name = "Test Modality"
            });
            context.SaveChanges();

            var licenseId = Guid.NewGuid();

            context.Licenses.Add(new Domain.License {
                Id = licenseId, Name = "LicensePreEdit", DisplayName = "LPR", ModalityId = modalityId
            });
            context.SaveChanges();

            //act
            var sut    = new Edit.Handler(context);
            var result = sut.Handle(new Edit.Command {
                Id = licenseId, Name = "LicensePostEdit", DisplayName = "LPO"
            }, CancellationToken.None).Result;

            var license = context.Licenses.FirstOrDefault(x => x.Id == licenseId);

            Assert.Equal("LicensePostEdit", license.Name);
            Assert.Equal("LPO", license.DisplayName);
        }
Example #5
0
        public async Task Edit_Inexistent_User_WithValidInput_ShouldReturn_RestException()
        {
            var mockedContext            = new EletronicPartsCatalogContextMock().GetMockedContextWithData();
            var mockedHasher             = new Mock <IPasswordHasher>();
            var mockedCurrentUserAcessor = new Mock <ICurrentUserAccessor>();

            mockedCurrentUserAcessor.Setup(mcua => mcua.GetCurrentUsername()).Returns("test");
            var mockedMapper = new Mock <IMapper>();

            var sut = new Edit.Handler(mockedContext, mockedHasher.Object, mockedCurrentUserAcessor.Object, mockedMapper.Object);

            var message = new Edit.Command
            {
                User = new Edit.UserData
                {
                    Username = "******",
                    Bio      = "some bio",
                    Email    = "*****@*****.**",
                    Password = "******"
                }
            };

            var result = await sut.Handle(message, CancellationToken.None);

            Assert.Null(result.User);
        }
Example #6
0
        public void Should_Edit_Shift()
        {
            var shiftIds = Create_Shift_For_Edit_Tests_And_Return_Ids();

            var sut   = new Edit.Handler(context);
            var resut = sut.Handle(new Edit.Command
            {
                Id             = shiftIds.ShiftId,
                Start          = DateTime.Now,
                End            = DateTime.Now.AddHours(8),
                TechnologistId = shiftIds.UpdateTechnologistId,
                ModalityId     = shiftIds.ModalityID,
                LicenseId      = shiftIds.UpdateLicenseId,
                LocationId     = shiftIds.LocationId,
                RoomId         = shiftIds.RoomId
            }, CancellationToken.None).Result;

            var editedShift = context.Set <Shift>().AsNoTracking()
                              .Include(s => s.License)
                              .Include(s => s.Modality)
                              .Include(s => s.Location)
                              .Include(s => s.Room)
                              .Include(s => s.Technologist)
                              .FirstOrDefault(x => x.Id == shiftIds.ShiftId);


            Assert.Equal(shiftIds.UpdateTechnologistId, editedShift.TechnologistId);
            Assert.Equal(shiftIds.UpdateLicenseId, editedShift.LicenseId);
        }
Example #7
0
        public void Should_Fail_Edit_License_W_Invalid_Id()
        {
            var context    = GetDbContext();
            var modalityId = Guid.NewGuid();

            context.Modalities.Add(new Domain.Modality {
                Id = modalityId, Name = "Test Modality"
            });
            context.SaveChanges();

            var licenseId1 = Guid.NewGuid();

            context.Licenses.Add(new Domain.License {
                Id = licenseId1, Name = "Test License", DisplayName = "TL", ModalityId = modalityId
            });

            var sut = new Edit.Handler(context);

            var licenseId2 = Guid.NewGuid();
            var ex         = Assert.ThrowsAsync <RestException>(() => sut.Handle(new Edit.Command {
                Id = licenseId2, Name = "LicensePostEdit", DisplayName = "LPO"
            }, CancellationToken.None));

            var thrownError   = ex.Result.Errors.ToString();
            var expectedError = (new { license = "License not found" }).ToString();

            Assert.Equal(expectedError, thrownError);
        }
Example #8
0
        public void Should_Edit_Location()
        {
            var context = GetDbContext();
            var id      = Guid.NewGuid();

            context.Locations.Add(new Location
            {
                Id   = id,
                Name = "Test Location"
            });

            context.SaveChanges();


            var sut = new Edit.Handler(context);

            var result = sut.Handle(
                new Edit.Command
            {
                Id   = id,
                Name = "Updated Location"
            }, CancellationToken.None).Result;

            var location = context.Locations.FirstOrDefaultAsync(x => x.Id == id).Result;

            Assert.Equal("Updated Location", location.Name);
        }
Example #9
0
        public void Should_Edit_Activity()
        {
            var context = GetDbContext();

            var activity = new Activity
            {
                Id          = 1,
                Title       = "Test Activity",
                Description = "Activity Description",
//                GeoCoordinate = new GeoCoordinate
//                {
//                    Latitude = 20,
//                    Longitude = 40
//                }
            };

            context.Activities.Add(activity);
            context.SaveChanges();

            var activityEditCommand = new Edit.Command
            {
                Activity = new Edit.ActivityData
                {
                    Title = "Updated Title"
                },
                Id = 1
            };

            var sut    = new Edit.Handler(context, _mapper);
            var result = sut.Handle(activityEditCommand, CancellationToken.None).Result;

            Assert.Equal("Updated Title", result.Title);
            Assert.Equal("Activity Description", result.Description);
        }
Example #10
0
        public void Handle_UpdateFailed_ThrowsDbUpdateException()
        {
            var token = new CancellationTokenSource().Token;

            _id = new Guid("fc0d6a3f-1a7e-4c5a-aa2f-2d952c4c8712");

            using (var context = new ExpenseTrackerDbContext(_options))
            {
                // Setup
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                Seed.SeedData(context);

                _editCommand = new Edit.Command {
                    Id = _id, Expense = _expenseDTO
                };
                _editHandler = new Edit.Handler(context, _mapper);

                // Attempt
                var action = _editHandler.Handle(_editCommand, token);

                // Assert
                Assert.ThrowsAsync <DbUpdateException>(() => action);
                Assert.Equal("Problem saving changes.",
                             action.Exception.InnerException.Message);
            }
        }
Example #11
0
        public void Handle_EditExpense_ShouldUpdateTheRecord()
        {
            var token = new CancellationTokenSource().Token;

            _id = new Guid("fc0d6a3f-1a7e-4c5a-aa2f-2d952c4c8712");

            using (var context = new ExpenseTrackerDbContext(_options))
            {
                // Setup
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                Seed.SeedData(context);

                var currentExpense = context.Expenses.FindAsync(_id);
                _editCommand = new Edit.Command
                {
                    Id      = _id,
                    Expense = _expenseDTO
                };
                _editHandler = new Edit.Handler(context, _mapper);

                // Attempt
                _editHandler.Handle(_editCommand, token).Wait();

                var actual   = JsonConvert.SerializeObject(currentExpense.Result);
                var expected = JsonConvert.SerializeObject(_expenseDTO);

                Assert.Equal(expected, actual);
            }
        }
Example #12
0
        public void Should_Fail_To_Edit_Room_With_Invalid_Id()
        {
            var context    = GetDbContext();
            var locationId = Guid.NewGuid();

            context.Locations.Add(new Domain.Location {
                Id = locationId, Name = "Test Location"
            });
            context.SaveChanges();

            context.Rooms.Add(new Domain.Room {
                Id = Guid.NewGuid(), Name = "Test Room", LocationId = locationId
            });

            var sut = new Edit.Handler(context);

            var nonExistingRoomId = Guid.NewGuid();
            var ex = Assert.ThrowsAsync <RestException>(() => sut.Handle(new Edit.Command {
                Id = nonExistingRoomId, Name = "Edit Room"
            }, CancellationToken.None));

            var thrownError   = ex.Result.Errors.ToString();
            var expectedError = (new { room = "Room Not Found" }).ToString();

            Assert.Equal(expectedError, thrownError);
        }
Example #13
0
        public void Should_Edit_Room()
        {
            //arrange
            var context = GetDbContext();

            var locationID = Guid.NewGuid();

            context.Locations.Add(new Location {
                Id = locationID, Name = "Test Location"
            });

            context.SaveChanges();

            var roomId = Guid.NewGuid();

            context.Rooms.Add(new Room {
                Id = roomId, Name = "Room BeforeEdit", LocationId = locationID
            });

            context.SaveChanges();

            //act
            var sut    = new Edit.Handler(context);
            var result = sut.Handle(new Edit.Command
            {
                Id   = roomId,
                Name = "Room PostEdit"
            }, CancellationToken.None).Result;

            var room = context.Rooms.Find(roomId);

            Assert.Equal("Room PostEdit", room.Name);
        }
Example #14
0
        public void Should_Fail_To_Edit_Shift_With_Invalid_Id()
        {
            var shiftIds = Create_Shift_For_Edit_Tests_And_Return_Ids();
            var sut      = new Edit.Handler(context);


            var nonExistingShiftId = Guid.NewGuid();

            var ex = Assert.ThrowsAsync <RestException>(() => sut.Handle(new Edit.Command
            {
                Id             = Guid.NewGuid(),
                Start          = DateTime.Now,
                End            = DateTime.Now.AddHours(8),
                TechnologistId = shiftIds.UpdateTechnologistId,
                ModalityId     = shiftIds.ModalityID,
                LicenseId      = shiftIds.UpdateLicenseId,
                LocationId     = shiftIds.LocationId,
                RoomId         = shiftIds.RoomId
            }, CancellationToken.None));

            var thrownError   = ex.Result.Errors.ToString();
            var expectedError = (new { shift = "Shift Not Found" }).ToString();

            Assert.Equal(expectedError, thrownError);
        }
Example #15
0
        public void Should_Update_Technologist_With_New_License()
        {
            var context = GetDbContext();

            var modalityId = Guid.NewGuid();

            context.Modalities.Add(new Domain.Modality {
                Id = modalityId, Name = "Test Modality", DisplayName = "TM"
            });
            context.SaveChanges();

            var technologistId = Guid.NewGuid();

            context.Technologists.Add(new Domain.Technologist
            {
                Id         = technologistId,
                Name       = "Test Technologist",
                ModalityId = modalityId,
                Initial    = "TT",
            });

            var licenceId1 = Guid.NewGuid();
            var licenseId2 = Guid.NewGuid();

            context.Licenses.Add(new Domain.License {
                Id = licenceId1, Name = "License 1", DisplayName = "L1", ModalityId = modalityId
            });
            context.Licenses.Add(new Domain.License {
                Id = licenseId2, Name = "License 2", DisplayName = "L2", ModalityId = modalityId
            });


            context.SaveChanges();

            var sut           = new Edit.Handler(context);
            var licenseIdList = new List <Guid> {
                licenceId1, licenseId2
            };
            var result = sut.Handle(new Edit.Command
            {
                Id            = technologistId,
                Name          = "Updated Technologist",
                Initial       = "UT",
                LicenseIdList = licenseIdList
            }, CancellationToken.None).Result;

            var editedTechnologist        = context.Technologists.Include(x => x.Licenses).ThenInclude(x => x.License).FirstOrDefault(x => x.Id == technologistId);
            var editedTechnologistLicense = editedTechnologist.Licenses.FirstOrDefault(x => x.LicenseId == licenceId1);

            Assert.Equal("Updated Technologist", editedTechnologist.Name);
            Assert.Equal(2, editedTechnologist.Licenses.Count);
            Assert.NotNull(editedTechnologistLicense);
            Assert.Equal("License 1", editedTechnologistLicense.License.Name);
        }
Example #16
0
        public void EditJob_WithValidInput_ShouldEditJob()
        {
            //Arrange
            var handler = new Edit.Handler(testContext);
            //Act
            var result = handler.Handle(_jobEditCommand, CancellationToken.None).Result;

            //Assert
            Assert.NotNull(result);
            Assert.That(result.Equals(Unit.Value));
        }
Example #17
0
        public void EditJob_WithEmptyJobId_ShouldThrowException()
        {
            //Arrange
            _jobEditCommand.Id = 0;
            var handler = new Edit.Handler(testContext);
            //Act+Assert
            var ex = Assert.CatchAsync <Exception>(() => handler.Handle(_jobEditCommand, CancellationToken.None));

            if (ex.Equals(typeof(Exception)))
            {
                Assert.That(ex.Message, Is.EqualTo("problem saving changes"));
            }
        }
Example #18
0
        public async Task Edit_Inexistent_Project_WithValidInput_ShouldReturn_RestException()
        {
            var mockedContext = new EletronicPartsCatalogContextMock().GetMockedContextWithData();

            var sut = new Edit.Handler(mockedContext);

            var message = new Edit.Command
            {
                Component = new Edit.ComponentData()
                {
                    Description = "Description",
                },
                Slug = "slug"
            };

            await Assert.ThrowsAsync <RestException>(() => sut.Handle(message, CancellationToken.None));
        }
Example #19
0
        public void Should_Fail_Edit_Location_W_Invalid_Id()
        {
            var context = GetDbContext();


            context.Locations.Add(new Location {
                Id = Guid.NewGuid(), Name = "Test Location"
            });

            var sut = new Edit.Handler(context);

            var nonExistingLocationId = Guid.NewGuid();
            var ex = Assert.ThrowsAsync <RestException>(() => sut.Handle(new Edit.Command {
                Id = nonExistingLocationId, Name = "LocationEdit"
            }, CancellationToken.None));

            var thrownError   = ex.Result.Errors.ToString();
            var expectedError = (new { location = "Location Not Found" }).ToString();

            Assert.Equal(expectedError, thrownError);
        }
Example #20
0
        public void Should_Fail_To_Edit_Modality_With_Invalid_Id()
        {
            var context = GetDbContext();


            context.Modalities.Add(new Modality {
                Id = Guid.NewGuid(), Name = "Test Modality", DisplayName = "TM"
            });

            var sut = new Edit.Handler(context);

            var nonExistingModalityId = Guid.NewGuid();
            var ex = Assert.ThrowsAsync <RestException>(() => sut.Handle(new Edit.Command {
                Id = nonExistingModalityId, Name = "Edited Modality", DisplayName = "EM"
            }, CancellationToken.None));

            var thrownError   = ex.Result.Errors.ToString();
            var expectedError = (new { modality = "Modality Not Found" }).ToString();

            Assert.Equal(expectedError, thrownError);
        }
Example #21
0
        public async Task EditTest_ValidData_ExpectSuccess()
        {
            var createCmd = new Create.Command()
            {
                Category = new Create.CategoryData()
                {
                    ParentCategory = 0,
                    Name           = "TestCategory",
                    Description    = "This is a test Category"
                }
            };


            var createdArticle = await CategoryHelpers.CreateCategory(this, createCmd);

            var command = new Edit.Command()
            {
                Category = new Domain.Category()
                {
                    ParentCategoryId    = 1,
                    CategoryName        = "Updated " + createdArticle.CategoryName,
                    CategoryDescription = "Updated " + createdArticle.CategoryDescription,
                    CategoryId          = createdArticle.CategoryId
                },
            };

            var dbContext = GetDbContext();

            var articleEditHandler = new Edit.Handler(dbContext);
            var edited             = await articleEditHandler.Handle(command, new System.Threading.CancellationToken());

            Assert.NotNull(edited);
            Assert.Equal(edited.Category.ParentCategoryId, command.Category.ParentCategoryId);
            Assert.Contains(edited.Category.CategoryName, command.Category.CategoryName);
            // use assert Contains because we do not know the order in which the tags are saved/retrieved
            Assert.Contains(edited.Category.CategoryDescription, command.Category.CategoryDescription);
            Assert.Equal(edited.Category.CategoryId, command.Category.CategoryId);
        }
Example #22
0
        public void Should_Update_Technologist()
        {
            var context = GetDbContext();

            var modalityId = Guid.NewGuid();

            context.Modalities.Add(new Domain.Modality {
                Id = modalityId, Name = "Test Modality", DisplayName = "TM"
            });
            context.SaveChanges();

            var technologistId = Guid.NewGuid();

            context.Technologists.Add(new Domain.Technologist
            {
                Id         = technologistId,
                Name       = "Test Technologist",
                ModalityId = modalityId,
                Initial    = "TT",
            });
            context.SaveChanges();

            var sut = new Edit.Handler(context);

            var result = sut.Handle(new Edit.Command
            {
                Id            = technologistId,
                Name          = "Updated Technologist",
                Initial       = "UT",
                LicenseIdList = new List <Guid> {
                }
            }, CancellationToken.None).Result;

            var editedTechnologist = context.Technologists.FirstOrDefault(x => x.Id == technologistId);

            Assert.Equal("Updated Technologist", editedTechnologist.Name);
            Assert.Equal("UT", editedTechnologist.Initial);
        }