Ejemplo n.º 1
0
        public void MapEventParent_EntityWithAllAssociated_EverythingMapped()
        {
            var entity = new EventParent
            {
                Id          = 1,
                UserId      = "foo",
                Name        = "bar",
                Description = "baz",
                User        = new User {
                    Id = "foo"
                }
            };
            var expected = new EventParentModel
            {
                Id          = 1,
                Name        = "bar",
                Description = "baz"
            };

            var actual = target.Map <EventParentModel>(entity);

            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.Description, actual.Description);
        }
Ejemplo n.º 2
0
 public IActionResult Create([FromBody][CustomizeValidator(RuleSet = Constants.RuleSetNameForInsert)] EventParentModel model)
 {
     if (ModelState.IsValid)
     {
         service.Insert(model);
         return(CreatedAtRoute("GetEventParent", new { id = model.Id }, model));
     }
     return(BadRequest(ModelState));
 }
Ejemplo n.º 3
0
        public void Insert_ThrowsException_HandledByCatchBlock()
        {
            var model       = new EventParentModel();
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.InsertEventParent(It.IsAny <EventParent>())).Throws <Exception>();
            var target = InitializeTarget(contextMock.Object);

            target.Insert(model);
        }
Ejemplo n.º 4
0
        public void Update_ThrowsException_HandledByCatchBlock()
        {
            var model = new EventParentModel {
                Id = 1
            };
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.GetEventParentByIdTracked(It.IsAny <int>())).Throws <Exception>();
            var target = InitializeTarget(contextMock.Object);

            target.Update(model);
        }
Ejemplo n.º 5
0
 public IActionResult Update(int id, [FromBody][CustomizeValidator(RuleSet = Constants.RuleSetNameForUpdate)] EventParentModel model)
 {
     if (ModelState.IsValid)
     {
         if (id == model.Id)
         {
             service.Update(model);
             return(NoContent());
         }
         return(BadRequest());
     }
     return(BadRequest(ModelState));
 }
        public void Validate_ValidModel(int?id, string name, string ruleSet)
        {
            var model = new EventParentModel
            {
                Id   = id,
                Name = name
            };

            var result = target.Validate(model, ruleSet);

            Assert.IsTrue(result.IsValid);
            target.ShouldNotHaveValidationErrorFor(x => x.Id, model, ruleSet);
            target.ShouldNotHaveValidationErrorFor(x => x.Name, model, ruleSet);
        }
Ejemplo n.º 7
0
        public void Insert_ModelIsProvided_InsertsEntity()
        {
            int id          = 1;
            var model       = new EventParentModel();
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.InsertEventParent(It.IsAny <EventParent>())).Callback <EventParent>(x => x.Id = id).Verifiable();
            var target = InitializeTarget(contextMock.Object);

            target.Insert(model);

            contextMock.Verify();
            Assert.AreEqual(id, model.Id);
        }
Ejemplo n.º 8
0
        public void Update_InvalidId_ContextSaveChangesNotCalled()
        {
            var model = new EventParentModel {
                Id = 1
            };
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.GetEventParentByIdTracked(It.Is <int>(y => y == model.Id))).Returns <EventParent>(null).Verifiable();
            var target = InitializeTarget(contextMock.Object);

            target.Update(model);

            contextMock.Verify();
            contextMock.Verify(x => x.SaveChanges(), Times.Never());
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Inserts an <see cref="EventParentModel"/> class.
 /// </summary>
 /// <param name="model">The <see cref="EventParentModel"/> class to insert.</param>
 public void Insert(EventParentModel model)
 {
     try
     {
         var entity = (EventParent) new EventParent
         {
             UserId    = ApplicationUser.Id,
             CreatedOn = DateTime.Now
         }.InjectFrom <SmartInjection>(model);
         context.InsertEventParent(entity);
         model.Id = entity.Id;
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Updates an <see cref="EventParentModel"/> class.
 /// </summary>
 /// <param name="model">The <see cref="EventParentModel"/> class to update.</param>
 public void Update(EventParentModel model)
 {
     try
     {
         var entity = context.GetEventParentByIdTracked(model.Id.Value);
         if (entity != null &&
             entity.UserId == ApplicationUser.Id)
         {
             entity.InjectFrom <SmartInjection>(model);
             entity.UpdatedOn = DateTime.Now;
             context.SaveChanges();
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 11
0
        public void Update_ValidIdAndUserId_ContextSaveIsCalled()
        {
            var model = new EventParentModel {
                Id = 1
            };
            var entity = new EventParent {
                Id = model.Id.Value, UserId = User.Id
            };
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.GetEventParentByIdTracked(It.Is <int>(y => y == model.Id))).Returns(entity).Verifiable();
            contextMock.Setup(x => x.SaveChanges()).Verifiable();
            var target = InitializeTarget(contextMock.Object);

            target.Update(model);

            contextMock.Verify();
        }
Ejemplo n.º 12
0
        public void GetById_ValidIdAndUserId_ReturnsModel()
        {
            int id     = 1;
            var entity = new EventParent {
                Id = id, UserId = User.Id
            };
            var expected = new EventParentModel {
                Id = id
            };
            var contextMock = new Mock <IOnTaskDbContext>();

            contextMock.Setup(x => x.GetEventParentById(It.Is <int>(y => y == id))).Returns(entity).Verifiable();
            var mapperMock = new Mock <IMapperService>();

            mapperMock.Setup(x => x.Map <EventParentModel>(It.Is <EventParent>(y => y == entity), It.IsAny <object>())).Returns(expected).Verifiable();
            var target = InitializeTarget(contextMock.Object, mapperMock.Object);

            var actual = target.GetById(id);

            contextMock.Verify();
            Assert.AreEqual(expected.Id, actual.Id);
        }