Example #1
0
        public void WithChanges_WhenChangesAreValid_SetsBeforeAndAfterJson()
        {
            var before = "before_changes";
            var after  = "after_changes";
            AuditedOperationTypeEnum type = AuditedOperationTypeEnum.Update;
            var model = new ModelToAudit {
                Id = Guid.NewGuid().ToString()
            };

            var mockChanges = new Mock <IChangeDetectionService>();

            mockChanges
            .Setup(c => c.GetChangeType(It.IsAny <EntityEntry>()))
            .Returns(type);

            mockChanges
            .Setup(c => c.SerializeEntityChanges(It.IsAny <AuditedOperationTypeEnum>(), It.IsAny <EntityEntry>()))
            .Returns((before, after));


            AuditService <FakeContext> auditService = GenerateAuditService(mockChanges.Object);
            AuditEntryModel            entry        = auditService.GenerateEntry <AuditEntryModel, ModelToAudit>(model);

            Assert.AreEqual(type, entry.Type);
            Assert.AreEqual(before, entry.EntityBeforeJson);
            Assert.AreEqual(after, entry.EntityAfterJson);
        }
Example #2
0
        public void SerializeEntityChanges_WhenChangeTypeInvalid_ThrowsArgumentOutOfRangeException()
        {
            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel(false);

            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);

            Assert.Throws <ArgumentOutOfRangeException>(() => cds.SerializeEntityChanges((AuditedOperationTypeEnum)5, entity));
        }
Example #3
0
        public void GetChangeType_WhenModelMemberIsUpdated_ReturnsUpdate()
        {
            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel();

            model.Name = Guid.NewGuid().ToString();
            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);
            AuditedOperationTypeEnum changeType = cds.GetChangeType(entity);

            Assert.AreEqual(AuditedOperationTypeEnum.Update, changeType);
        }
Example #4
0
        public void SerializeEntityChanges_WhenEntityIsDeleted_SerializesEntityToBefore()
        {
            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel();

            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);

            (string before, string after) = cds.SerializeEntityChanges(AuditedOperationTypeEnum.Delete, entity);

            Assert.IsNotNull(before);
            Assert.IsNull(after);
        }
Example #5
0
        public void GetChangeType_WhenModelIsAttachedWithId_ReturnsUpdate()
        {
            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel(false);

            Context.Attach(model);
            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);

            AuditedOperationTypeEnum changeType = cds.GetChangeType(entity);

            Assert.AreEqual(AuditedOperationTypeEnum.Update, changeType);
        }
Example #6
0
        protected ModelToAudit GenerateModel(bool modelIsInDatabase = true)
        {
            var model = new ModelToAudit
            {
                Id   = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString()
            };

            if (modelIsInDatabase)
            {
                Context.Add(model);
                Context.SaveChanges();
            }

            return(model);
        }
Example #7
0
        public void GenerateEntry_WhenIdIsNotDbGenerated_SetsEntityIdOnSave()
        {
            var model = new ModelToAudit();

            Context.Add(model);

            AuditService <FakeContext> auditService = GenerateAuditService();
            AuditEntryModel            newEntry     = auditService.GenerateEntry <AuditEntryModel, ModelToAudit>(model);

            model.Audits = new List <AuditEntryModel>
            {
                newEntry
            };

            Context.SaveChanges();

            Assert.AreEqual(model.Id, newEntry.EntityId);
        }
Example #8
0
        public void SerializeEntityChanges_WhenModelIsSelfReferencing_IgnoresSelfReferencing()
        {
            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel();

            model.OneToOneChildModel = new OneToOneChildModel {
                Parent = model
            };

            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);

            (string _, string afterJson) = cds.SerializeEntityChanges(AuditedOperationTypeEnum.Insert, entity);
            Dictionary <string, object> after = JsonConvert.DeserializeObject <Dictionary <string, object> >(afterJson);

            var oneToOne = after[nameof(model.OneToOneChildModel)] as JObject;

            Assert.IsNotNull(oneToOne);
            Assert.IsNull(oneToOne[nameof(OneToOneChildModel.Parent)]);
        }
Example #9
0
        public void Write_WhenAuditIsValid_WritesAuditToDatabase()
        {
            AuditedOperationTypeEnum type = AuditedOperationTypeEnum.Update;
            var model = new ModelToAudit();

            var mockChanges = new Mock <IChangeDetectionService>();

            mockChanges
            .Setup(c => c.GetChangeType(It.IsAny <EntityEntry>()))
            .Returns(type);

            mockChanges
            .Setup(c => c.SerializeEntityChanges(It.IsAny <AuditedOperationTypeEnum>(), It.IsAny <EntityEntry>()))
            .Returns((string.Empty, string.Empty));

            AuditService <FakeContext> auditService = GenerateAuditService(mockChanges.Object);
            var fluent = auditService.GenerateEntry <AuditEntryModel, ModelToAudit>(model);

            Context.SaveChanges();

            Assert.AreEqual(1, Context.AuditEntries.Count());
        }
Example #10
0
        public void SerializeEntityChanges_WhenEntityIsUpdated_SerializesOnlyChangedMembers()
        {
            var    newName = "New Name";
            string originalName;

            ChangeDetectionService cds   = MakeService();
            ModelToAudit           model = GenerateModel();

            originalName = model.Name;
            model.Name   = newName;
            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <ModelToAudit> entity = Context.Entry(model);

            (string beforeJson, string afterJson) = cds.SerializeEntityChanges(AuditedOperationTypeEnum.Update, entity);
            Dictionary <string, object> before = JsonConvert.DeserializeObject <Dictionary <string, object> >(beforeJson);
            Dictionary <string, object> after  = JsonConvert.DeserializeObject <Dictionary <string, object> >(afterJson);

            Assert.IsTrue(before.Count == 1);
            Assert.AreEqual(nameof(model.Name), before.First().Key);
            Assert.AreEqual(originalName, before.First().Value);

            Assert.IsTrue(after.Count == 1);
            Assert.AreEqual(nameof(model.Name), after.First().Key);
            Assert.AreEqual(newName, after.First().Value);
        }