コード例 #1
0
        public void WeirdDtoTests()
        {
            DtoInfo <WeirdDto> info = DtoInfo.GetInfo <WeirdDto>();

            info.Properties.Count.Should().Be(2);
            Invoking(() => info.CreateNew()).Should().Throw <InvalidOperationException>();

            WeirdDto dto = new(1, 2);

            Invoking(() => info.ShallowClone(dto)).Should().Throw <InvalidOperationException>();

            var property = info.GetProperty <int>("IntegerProperty");

            info.GetProperty(x => x.IntegerProperty).Should().Be(property);
            property.Name.Should().Be("IntegerProperty");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeTrue();
            ((PropertyInfo)property.MemberInfo).GetMethod !.Name.Should().Be("get_IntegerProperty");
            property.GetValue(dto).Should().Be(dto.IntegerProperty);
            Invoking(() => property.SetValue(dto, 24)).Should().Throw <InvalidOperationException>();

            var field = info.GetProperty <int>("IntegerField");

            info.GetProperty(x => x.IntegerField).Should().Be(field);
            field.Name.Should().Be("IntegerField");
            field.ValueType.Should().Be(typeof(int));
            field.IsReadOnly.Should().BeTrue();
            ((FieldInfo)field.MemberInfo).Name.Should().Be("IntegerField");
            field.GetValue(dto).Should().Be(dto.IntegerField);
            Invoking(() => field.SetValue(dto, 24)).Should().Throw <InvalidOperationException>();
        }
コード例 #2
0
        public void StrongColor()
        {
            var info = DtoInfo.GetInfo <Color>();

            info.CreateNew().Should().Be(new Color(0, 0, 0));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3)).Should().Be(new Color(255, 1, 2, 3));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3), ("a", (byte)4)).Should().Be(new Color(4, 1, 2, 3));
        }
コード例 #3
0
        public void WeakColor()
        {
            var info = DtoInfo.GetInfo(typeof(Color));

            info.CreateNew().Should().Be(new Color(0, 0, 0));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3)).Should().Be(new Color(255, 1, 2, 3));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3), ("a", (byte)4)).Should().Be(new Color(4, 1, 2, 3));
        }
コード例 #4
0
        public void WeakNullablePoint()
        {
            var info = DtoInfo.GetInfo(typeof(Point?));

            info.CreateNew().Should().Be(default(Point));
            info.CreateNew(("x", 1)).Should().Be(new Point {
                X = 1
            });
        }
コード例 #5
0
        public void StrongInitOnly()
        {
            var info = DtoInfo.GetInfo <InitOnlyDto>();
            var dto  = info.CreateNew(("a", 1), ("b", 2));

            dto.A.Should().Be(1);
            dto.B.Should().Be(2);
            info.ShallowClone(dto).A.Should().Be(1);
        }
コード例 #6
0
        public void StrongNullablePoint()
        {
            var info = DtoInfo.GetInfo <Point?>();

            info.CreateNew().Should().Be(default(Point));
            info.CreateNew(("x", 1)).Should().Be(new Point {
                X = 1
            });
        }
        public static Assertable <TResponse> AssertResponse <TResponse>(this TResponse response)
            where TResponse : AutoWebServiceResponse
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            var exception        = response.CreateException();
            var statusCodeString = exception.ResponseStatusCode?.ToString();

            var properties = DtoInfo.GetInfo(typeof(TResponse))
                             .Properties
                             .Select(p => (p.Name, IsContent: IsContentProperty(p.Name), Value: p.GetValue(response)))
                             .Where(p => !IsDefault(p.Value))
                             .ToList();

            var assertable = AssertEx.HasValue(response, "response")
                             .Context(GetContext(exception, properties));

            if (properties.Any(p => p.IsContent && p.Value.GetType().IsClass))
            {
                assertable = assertable.WithExtrator(TryExtractStatusProperty);
            }

            return(assertable);

            bool TryExtractStatusProperty(
                LambdaExpression sourceExpression,
                out LambdaExpression hasValueExpression,
                out LambdaExpression remainingExpression)
            {
                hasValueExpression  = null;
                remainingExpression = null;

                var sourceParameter = sourceExpression.Parameters.Single();
                var visitor         = new MemberReplacingExpressionVisitor(sourceParameter, "response");
                var replacedBody    = visitor.Visit(sourceExpression.Body);

                if (!visitor.TryGetSingleClassParameter(out var memberInfo, out var responseParameter) || !IsContentProperty(memberInfo.Name))
                {
                    return(false);
                }

                hasValueExpression  = Expression.Lambda(Expression.MakeMemberAccess(sourceParameter, memberInfo), sourceParameter);
                remainingExpression = Expression.Lambda(replacedBody, responseParameter);

                return(true);
            }

            // Logic matches https://github.com/Faithlife/FaithlifeWebRequests/blob/5d04e85c62ae0ccea2ca7e45f5d40d650a7acd0b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs#L142
            bool IsContentProperty(string propertyName)
            => string.Equals(statusCodeString, propertyName, StringComparison.OrdinalIgnoreCase);
        }
コード例 #8
0
        public void OnePropertyInfoTests()
        {
            DtoInfo <OneProperty> info = DtoInfo.GetInfo <OneProperty>();

            info.Properties.Count.Should().Be(1);
            info.CreateNew().GetType().Should().Be(typeof(OneProperty));

            OneProperty dto = new() { Integer = 42 };

            info.ShallowClone(dto).Integer.Should().Be(dto.Integer);
        }
コード例 #9
0
        protected override ActivityExecutionResult OnExecute(WorkflowExecutionContext context)
        {
            var entityInfo = context.GetVariable <EntityInfo>("EntityInfo");
            var option     = context.GetVariable <object>("Option") as CrudCommandOption;

            try
            {
                string[] actionNames = { string.Empty, string.Empty, string.Empty };

                if (option != null && option.SeparateDto)
                {
                    actionNames[1] = "Create";
                    actionNames[2] = "Update";
                }
                else
                {
                    actionNames[1] = "CreateUpdate";
                    actionNames[2] = actionNames[1];
                }

                string[] typeNames = new string[actionNames.Length];

                var useEntityPrefix = option != null && option.EntityPrefixDto;
                var dtoSubfix       = option?.DtoSuffix ?? "Dto";

                for (int i = 0; i < typeNames.Length; i++)
                {
                    typeNames[i] = useEntityPrefix
                        ? $"{entityInfo.Name}{actionNames[i]}{dtoSubfix}"
                        : $"{actionNames[i]}{entityInfo.Name}{dtoSubfix}";
                }

                DtoInfo dtoInfo = new DtoInfo(typeNames[0], typeNames[1], typeNames[2]);

                context.SetLastResult(dtoInfo);
                context.SetVariable("DtoInfo", dtoInfo);
                LogOutput(() => dtoInfo);

                return(Done());
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Building DTO info failed.");
                if (e is ParseException pe)
                {
                    foreach (var error in pe.Errors)
                    {
                        Logger.LogError(error);
                    }
                }
                throw;
            }
        }
コード例 #10
0
        public void EmptyDtoTests()
        {
            DtoInfo <EmptyDto> info = DtoInfo.GetInfo <EmptyDto>();

            info.Properties.Should().BeEmpty();
            info.CreateNew().GetType().Should().Be(typeof(EmptyDto));
            info.ShallowClone(new EmptyDto()).Should().NotBeNull();
            Invoking(() => info.GetProperty("Nope")).Should().Throw <ArgumentException>();
            info.TryGetProperty("Nope").Should().BeNull();
            Invoking(() => info.GetProperty <int>("Nope")).Should().Throw <ArgumentException>();
            info.TryGetProperty <int>("Nope").Should().BeNull();
        }
コード例 #11
0
        public void StrongMixedDto()
        {
            var info = DtoInfo.GetInfo <MixedDto>();

            info.CreateNew().Should().BeEquivalentTo(new MixedDto());
            info.CreateNew(("string", "hey")).Should().BeEquivalentTo(new MixedDto {
                String = "hey"
            });
            info.CreateNew(("integer", 1), ("string", "wow")).Should().BeEquivalentTo(new MixedDto(1)
            {
                String = "wow"
            });
        }
コード例 #12
0
        public void WeakMixedDto()
        {
            IDtoInfo info = DtoInfo.GetInfo(typeof(MixedDto));

            info.CreateNew().Should().BeEquivalentTo(new MixedDto());
            info.CreateNew(("string", "hey")).Should().BeEquivalentTo(new MixedDto {
                String = "hey"
            });
            info.CreateNew(("integer", 1), ("string", "wow")).Should().BeEquivalentTo(new MixedDto(1)
            {
                String = "wow"
            });
        }
コード例 #13
0
        public void OnePropertyWeakPropertyTests()
        {
            DtoInfo <OneProperty>      info     = DtoInfo.GetInfo <OneProperty>();
            IDtoProperty <OneProperty> property = info.GetProperty("Integer");

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((PropertyInfo)property.MemberInfo).GetMethod !.Name.Should().Be("get_Integer");

            OneProperty dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
コード例 #14
0
        public void OneFieldWeakestFieldTests()
        {
            DtoInfo <OneField> info     = DtoInfo.GetInfo <OneField>();
            IDtoProperty       property = info.Properties.Single();

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((FieldInfo)property.MemberInfo).Name.Should().Be("Integer");

            OneField dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
コード例 #15
0
        public void OnePropertyWeakInfoTests()
        {
            IDtoInfo info = DtoInfo.GetInfo(typeof(OneProperty));

            info.Properties.Count.Should().Be(1);
            info.CreateNew().GetType().Should().Be(typeof(OneProperty));

            OneProperty dto = new() { Integer = 42 };

            ((OneProperty)info.ShallowClone(dto)).Integer.Should().Be(dto.Integer);

            info.GetProperty("Integer").Name.Should().Be("Integer");
            info.TryGetProperty("Integer") !.Name.Should().Be("Integer");

            info.GetProperty("integer").Name.Should().Be("Integer");
            info.TryGetProperty("integer") !.Name.Should().Be("Integer");
        }
コード例 #16
0
        public void WeakAnonymousType()
        {
            var obj  = new { Integer = 3, String = "three" };
            var info = DtoInfo.GetInfo(obj.GetType());

            info.Properties.Should().HaveCount(2);
            var property = info.GetProperty("Integer");

            property.IsReadOnly.Should().BeTrue();
            property.GetValue(obj).Should().Be(3);
            property.GetValue(info.ShallowClone(obj)).Should().Be(3);
            property.GetValue(info.CreateNew()).Should().Be(0);
            property.GetValue(info.CreateNew((property, 4))).Should().Be(4);
            property.GetValue(info.CreateNew(("integer", 4))).Should().Be(4);
            Invoking(() => info.CreateNew(("nope", 4))).Should().Throw <ArgumentException>();
            Invoking(() => info.CreateNew(("integer", "4"))).Should().Throw <ArgumentException>();
        }
コード例 #17
0
        public void OneFieldStrongFieldTests()
        {
            DtoInfo <OneField>          info     = DtoInfo.GetInfo <OneField>();
            DtoProperty <OneField, int> property = info.GetProperty <int>("Integer");

            info.GetProperty(x => x.Integer).Should().Be(property);
            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((FieldInfo)property.MemberInfo).Name.Should().Be("Integer");

            OneField dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
        public void TestSetup()
        {
            this.resolveService = new ResolveService();
            this.dataModelUtils = new DataModelUtils();
            this.requestUtils   = new RequestUtils();

            this.resolveService.DataModelUtils = this.dataModelUtils;

            this.engineeringModelIid = Guid.NewGuid();

            this.siteDirectoryInfo      = new DtoInfo(typeof(SiteDirectory).Name, Guid.NewGuid());
            this.simpleQuantityKindInfo = new DtoInfo(typeof(SimpleQuantityKind).Name, Guid.NewGuid());

            this.engineeringModelInfo = new DtoInfo(typeof(EngineeringModel).Name, Guid.NewGuid());
            this.bookInfo             = new DtoInfo(typeof(Book).Name, Guid.NewGuid());

            this.optionInfo    = new DtoInfo(typeof(Option).Name, Guid.NewGuid());
            this.parameterInfo = new DtoInfo(typeof(Parameter).Name, Guid.NewGuid());
            this.aliasInfo     = new DtoInfo(typeof(Alias).Name, Guid.NewGuid());
        }
コード例 #19
0
 public void DtoInfoCreateNew()
 {
     m_dto = DtoInfo.GetInfo <BenchmarkDto>().CreateNew(("Id", 1L), ("Name", "one"));
 }
コード例 #20
0
 public void DtoInfoCreateNew()
 {
     m_dto = DtoInfo.GetInfo <BenchmarkDto>().CreateNew();
 }
コード例 #21
0
        public void ShallowCloneThrowsOnNull()
        {
            DtoInfo <EmptyDto> info = DtoInfo.GetInfo <EmptyDto>();

            Invoking(() => info.ShallowClone(null !)).Should().Throw <ArgumentNullException>();
        }