Ejemplo n.º 1
0
        public void ModifiesSystemProperties_BasicTests(string op, string path, string value, bool expected)
        {
            // Arrange
            var patchDoc = new JsonPatchDocument <InMemoryEntity>();

            if (path.Equals("/updatedAt", StringComparison.OrdinalIgnoreCase) && value.EndsWith(".000Z"))
            {
                patchDoc.Operations.Add(new Operation <InMemoryEntity>(op, path, null, DateTime.Parse(value)));
            }
            else
            {
                patchDoc.Operations.Add(new Operation <InMemoryEntity>(op, path, null, value));
            }

            var entity = new InMemoryEntity
            {
                Id        = "test",
                UpdatedAt = DateTimeOffset.Parse("2021-12-31T05:30:00.000Z"),
                Version   = new byte[] { 0x01, 0x00, 0x42, 0x22, 0x47, 0x8F }
            };

            // Act
            var actual = patchDoc.ModifiesSystemProperties(entity, out Dictionary <string, string> validationErrors);

            // Assert
            Assert.Equal(expected, actual);
            if (expected)
            {
                Assert.Equal(2, validationErrors.Count);
            }
        }
        private InMemoryEntity SaveConstant(Constant entity)
        {
            var entityType = entity.GetType();

            var collection = Collection(entityType);

            if (collection.list.Count > 1)
            {
                throw new InvalidOperationException($"Whoops, constant [{entityType}] was duplicated");
            }

            var inMemoryEntity = new InMemoryEntity
            {
                entityType = entityType
            };

            var existingItem = collection.list.SingleOrDefault();

            inMemoryEntity.revision =
                new InMemoryEntityRevision(inMemoryEntity, existingItem?.revision, new Dictionary <string, object>
            {
                [nameof(Constant.ЗначениеНетипизированное)] = entity.ЗначениеНетипизированное
            });

            collection.list.Clear();
            collection.list.Add(inMemoryEntity);
            collection.revision++;
            return(inMemoryEntity);
        }
Ejemplo n.º 3
0
 public InMemoryEntityRevision(InMemoryEntity inMemoryEntity, InMemoryEntityRevision previous, Dictionary <string, object> properties)
 {
     this.inMemoryEntity = inMemoryEntity;
     this.previous       = previous;
     this.properties     = properties;
     Writable            = true;
 }
Ejemplo n.º 4
0
        private static object CreateEntity(Type type, InMemoryEntity entity)
        {
            var result = (Abstract1CEntity)FormatterServices.GetUninitializedObject(type);

            result.Controller = new EntityController(entity.revision);
            return(result);
        }
        private static object CreateConstantEntity(Type type, InMemoryEntity entity)
        {
            var result = (Constant)FormatterServices.GetUninitializedObject(type);

            result.ЗначениеНетипизированное =
                entity.revision.TryLoadValue(nameof(result.ЗначениеНетипизированное), typeof(object), out var item)
                    ? item
                    : throw new InvalidOperationException($"Constant [{type.Name}] value is empty");
            return(result);
        }
        public void GetETag_Null_WhenEmptyVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Array.Empty <byte>()
            };

            // Act
            string actual = entity.GetETag();

            // Assert
            Assert.Null(actual);
        }
        public void HasValidVersion_False_OnNullVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = null
            };

            // Act
            bool actual = entity.HasValidVersion();

            // Assert
            Assert.False(actual);
        }
        public void GetETag_Null_WhenNullVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = null
            };

            // Act
            string actual = entity.GetETag();

            // Assert
            Assert.Null(actual);
        }
        public void ToEntityTagHeaderValue_Null_WhenEmptyVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Array.Empty <byte>()
            };

            // Act
            EntityTagHeaderValue actual = entity.ToEntityTagHeaderValue();

            // Assert
            Assert.Null(actual);
        }
Ejemplo n.º 10
0
        public void ToEntityTagHeaderValue_Null_WhenNullVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = null
            };

            // Act
            EntityTagHeaderValue actual = entity.ToEntityTagHeaderValue();

            // Assert
            Assert.Null(actual);
        }
Ejemplo n.º 11
0
        public void HasValidVersion_True_OnFilledVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Guid.NewGuid().ToByteArray()
            };

            // Act
            bool actual = entity.HasValidVersion();

            // Assert
            Assert.True(actual);
        }
Ejemplo n.º 12
0
        public void HasValidVersion_False_OnEmptyVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Array.Empty <byte>()
            };

            // Act
            bool actual = entity.HasValidVersion();

            // Assert
            Assert.False(actual);
        }
Ejemplo n.º 13
0
        public void GetETag_Valid_WhenFilledVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Guid.NewGuid().ToByteArray()
            };

            // Act
            string actual = entity.GetETag();

            // Assert
            Assert.NotEmpty(actual);
            Assert.Matches("^\"[a-zA-Z0-9+/=]{24}\"$", actual);
        }
Ejemplo n.º 14
0
        public void ToEntityTagHeaderValue_Valid_WhenFilledVersion()
        {
            // Arrange
            ITableData entity = new InMemoryEntity {
                Version = Guid.NewGuid().ToByteArray()
            };

            // Act
            EntityTagHeaderValue actual = entity.ToEntityTagHeaderValue();

            // Assert
            Assert.NotNull(actual);
            Assert.False(actual.IsWeak);
            Assert.Matches("^\"[a-zA-Z0-9+/=]{24}\"$", actual.Tag.ToString());
        }
        public void AddFromEntity_LastModified_WhenUpdatedAt()
        {
            // Arrange
            var headers = new HeaderDictionary();
            var entity  = new InMemoryEntity {
                Version = null, UpdatedAt = DateTimeOffset.Parse("2019-01-30T13:30:15Z")
            };

            // Act
            headers.AddFromEntity(entity);

            // Assert
            Assert.False(headers.ContainsKey("ETag"));
            Assert.Single(headers["Last-Modified"]);
            Assert.Equal("Wed, 30 Jan 2019 13:30:15 GMT", headers["Last-Modified"][0]);
        }
        public void AddFromEntity_BothHeaders_WhenBothSet()
        {
            // Arrange
            var headers = new HeaderDictionary();
            var entity  = new InMemoryEntity {
                Version = testVersion, UpdatedAt = DateTimeOffset.Parse("2019-01-30T13:30:15Z")
            };

            // Act
            headers.AddFromEntity(entity);

            // Assert
            Assert.Single(headers["ETag"]);
            Assert.Equal(testETag, headers["ETag"][0]);
            Assert.Single(headers["Last-Modified"]);
            Assert.Equal("Wed, 30 Jan 2019 13:30:15 GMT", headers["Last-Modified"][0]);
        }
Ejemplo n.º 17
0
        private InMemoryEntity Save(Abstract1CEntity entity, bool isTableSection)
        {
            if (entity == null)
            {
                return(null);
            }
            var changed = entity.Controller.Changed;

            if (changed != null)
            {
                var keys = changed.Keys.ToArray();
                foreach (var k in keys)
                {
                    var abstract1CEntity = changed[k] as Abstract1CEntity;
                    if (abstract1CEntity != null)
                    {
                        changed[k] = Save(abstract1CEntity, false);
                        continue;
                    }
                    var list = changed[k] as IList;
                    if (list != null)
                    {
                        changed[k] = ConvertList(list);
                        continue;
                    }
                    var syncList = changed[k] as SyncList;
                    if (syncList != null)
                    {
                        changed[k] = ConvertList(syncList.Current);
                    }
                }
            }
            InMemoryEntity inMemoryEntity;

            if (!entity.Controller.IsNew)
            {
                var inmemoryEntityRevision = (InMemoryEntityRevision)entity.Controller.ValueSource;
                inMemoryEntity = inmemoryEntityRevision.inMemoryEntity;
                if (changed != null)
                {
                    inMemoryEntity.revision = new InMemoryEntityRevision(inMemoryEntity, inmemoryEntityRevision, changed);
                    Collection(entity.GetType()).revision++;
                }
            }
            else
            {
                if (changed == null)
                {
                    changed = new Dictionary <string, object>();
                }
                inMemoryEntity = new InMemoryEntity();
                var revision = new InMemoryEntityRevision(inMemoryEntity, null, changed);
                inMemoryEntity.entityType = entity.GetType();
                inMemoryEntity.revision   = revision;
                if (!isTableSection)
                {
                    var configurationName = ConfigurationName.Get(entity.GetType());
                    if (configurationName.Scope == ConfigurationScope.Справочники)
                    {
                        AssignNewGuid(entity, changed, "Код");
                    }
                    else if (configurationName.Scope == ConfigurationScope.Документы)
                    {
                        AssignNewGuid(entity, changed, "Номер");
                    }
                    if (entity.Controller.IsNew && configurationName.HasReference)
                    {
                        var idProperty = entity.GetType().GetProperty(EntityHelpers.idPropertyName);
                        if (idProperty == null)
                        {
                            throw new InvalidOperationException("assertion failure");
                        }
                        AssignValue(entity, changed, idProperty, Guid.NewGuid());
                    }
                    var inMemoryEntityCollection = Collection(entity.GetType());
                    inMemoryEntityCollection.revision++;
                    inMemoryEntityCollection.list.Add(inMemoryEntity);
                }
            }
            entity.Controller.ResetDirty(inMemoryEntity.revision);
            return(inMemoryEntity);
        }