Ejemplo n.º 1
0
 public void CanParseAndFormatCustomType(string input, string expected)
 {
     var schema = new FieldSchema { CustomType = "taxnumber" };
     var field = new Field(schema, input);
     var actual = field.Format();
     StringAssert.AreEqualIgnoringCase(actual, expected);
 }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public ITableSchemaBuilder WithFieldsFrom <TRecord>() where TRecord : class, new()
        {
            IEnumerable <PropertyInfo> properties = (typeof(TRecord)).GetTypeInfo().DeclaredProperties;

            foreach (PropertyInfo propertyInfo in properties)
            {
                if (string.Compare(propertyInfo.Name, "id", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    if (!propertyInfo.PropertyType.GetTypeInfo().IsValueType)
                    {
                        throw new NotSupportedException("Field 'id' must be of a value type");
                    }

                    WithKeyField(propertyInfo.Name);
                    continue;
                }

                string typeName = TypeMap.GetTypeName(propertyInfo.PropertyType);

                FieldSchema field = new FieldSchema
                {
                    Name     = propertyInfo.Name,
                    Required = false,
                    Type     = typeName
                };

                fields.Add(field);
            }

            return(this);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the type that the field is an array of (assuming the field is for an array)
        /// </summary>
        /// <param name="field"></param>
        /// <param name="parentName"></param>
        /// <returns></returns>
        public Type GetArrayType(FieldSchema field)
        {
            var parentName   = Document.TypeLookup.ParentNameBySchema[field];
            var templateType = TemplateTypeByName.GetValueOrDefault(parentName);

            return(GetTemplatedType(field.Type, field.Template, templateType));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the type associated with a field, with the
        /// </summary>
        /// <param name="field"></param>
        /// <param name="parentName">The name of the compound or niObject that this object belongs to</param>
        /// <returns></returns>
        public Type GetFieldType(FieldSchema field)
        {
            var parentName   = Document.TypeLookup.ParentNameBySchema[field];
            var templateType = TemplateTypeByName.GetValueOrDefault(parentName);

            return(GetDimensionalType(field.Type, field.Template, field.Dimensions.Count, templateType));
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public ITableSchemaBuilder WithKeyField(string fieldName)
        {
            FieldSchema field = fields.FirstOrDefault(x => x.Name == fieldName);

            if (field == null)
            {
                field = new FieldSchema
                {
                    Name          = fieldName,
                    Required      = true,
                    Type          = "id",
                    IsPrimaryKey  = true,
                    AutoIncrement = true
                };
                fields.Add(field);
            }
            else
            {
                field.Required      = true;
                field.Type          = "id";
                field.IsPrimaryKey  = true;
                field.AutoIncrement = true;
            }

            return(this);
        }
Ejemplo n.º 6
0
        public StringBuilder GenerateFileds(int tabnum = 1)
        {
            StringBuilder sb = new StringBuilder();

            foreach (DataColumn column in options.Schema.Columns)
            {
                FieldSchema field = new FieldSchema()
                {
                    name    = headRow[column].ToString(),
                    type    = typeRow[column].ToString(),
                    comment = commentRow[column].ToString()
                };

                if (IDField == null)
                {
                    IDField = field;
                }
                var tab = new String('\t', tabnum);
                sb.AppendLine($"{tab}/// <summary>");
                sb.AppendLine($"{tab}/// {field.comment}");
                sb.AppendLine($"{tab}/// </summary>");
                sb.AppendLine($"{tab}public {field.type} {field.name} {{ get; set; }}");
                sb.AppendLine();
            }
            return(sb);
        }
Ejemplo n.º 7
0
        public bool CheckIncludedInType(FieldSchema fieldSchema, NiObjectSchema parent)
        {
            if (parent == null)
            {
                return(true);
            }

            if (NiObjectInheritance.TryGetValue(parent, out var inheritance))
            {
                if (fieldSchema.OnlyT != null)
                {
                    if (!inheritance.Any(inheritedNi => inheritedNi.Name == fieldSchema.OnlyT))
                    {
                        return(false);
                    }
                }

                if (fieldSchema.ExcludeT != null)
                {
                    if (inheritance.Any(inheritedNi => inheritedNi.Name == fieldSchema.ExcludeT))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            return(true);
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public ITableSchemaBuilder WithFieldsFrom <TRecord>() where TRecord : class, new()
        {
            PropertyInfo[] properties = typeof(TRecord).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo propertyInfo in properties)
            {
                if (string.Compare(propertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    if (!propertyInfo.PropertyType.IsValueType)
                    {
                        throw new NotSupportedException("Field 'id' must be of a value type");
                    }

                    WithKeyField(propertyInfo.Name);
                    continue;
                }

                string typeName = TypeMap.GetTypeName(propertyInfo.PropertyType);

                FieldSchema field = new FieldSchema
                {
                    name     = propertyInfo.Name,
                    required = false,
                    type     = typeName
                };

                fields.Add(field);
            }

            return(this);
        }
Ejemplo n.º 9
0
        Data ReadField(FieldSchema fieldSchema, Interpreter interpreter, ReadingContext parentContext)
        {
            var type         = parentContext.ReadType(fieldSchema.Type);
            var argument     = Expressions.GetArgument(fieldSchema, interpreter);
            var childContext = parentContext.Extend(fieldSchema, argument);

            if (!parentContext.Version.MatchesVersionConstraint(fieldSchema.MinVersion, fieldSchema.MaxVersion))
            {
                return(null);
            }
            if (!Expressions.CheckCondition(fieldSchema, interpreter))
            {
                return(null);
            }
            if (!Expressions.CheckVersionCondition(fieldSchema, parentContext.VersionInterpreter))
            {
                return(null);
            }
            if (!Inheritance.CheckIncludedInType(fieldSchema, parentContext.Parent))
            {
                return(null);
            }

            if (fieldSchema.IsMultiDimensional)
            {
                var count    = Expressions.GetCount(fieldSchema, interpreter);
                var elements = new List <Data>(count);
                for (int i = 0; i < count; i++)
                {
                    elements.Add(ReadData(type, childContext));
                }
                return(new ListData(elements));
            }
            return(ReadData(type, childContext));
        }
Ejemplo n.º 10
0
 public void CanParseAndFormat(DataType type, object input, string format, string expected)
 {
     var schema = new FieldSchema { DataType = type, DataFormat = format };
     var field = new Field(schema, input + "");
     var actual = field.Format();
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 11
0
 public long GetArgument(FieldSchema field, Interpreter interpreter)
 {
     if (Arguments.TryGetValue(field, out var argument))
     {
         return(interpreter.Evaluate(argument));
     }
     return(0);
 }
Ejemplo n.º 12
0
 public bool CheckVersionCondition(FieldSchema field, Interpreter interpreter)
 {
     if (VersionConditions.TryGetValue(field, out var expression))
     {
         return(interpreter.Evaluate(expression) != 0);
     }
     return(true);
 }
Ejemplo n.º 13
0
        public Instruction LoadCondition(FieldSchema schema, ExpressionWriter.State state)
        {
            if (!DocBuilder.Document.ExpressionLookup.Conditions.TryGetValue(schema, out var expression))
            {
                return(new Instruction.LoadInt32(1));
            }

            return(new Instruction.InstructionWriter(il => new ExpressionWriter(il, state).WriteExpression(expression)));
        }
Ejemplo n.º 14
0
        public FieldRepo(FieldSchema fieldSchema, TypeRepo typeRepo = null)
        {
            FieldSchema = fieldSchema;
            TypeRepo    = typeRepo;

            if (typeRepo?.Serializer.PublicOnly == true && FieldSchema.IsPrivate)
            {
                FieldSchema.IsLoadable = false;
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes a new field.
 /// </summary>
 /// <param name="id">The ID of the field.</param>
 /// <param name="name">The name.</param>
 /// <param name="fieldType">The type of field; whether custom or JIRA.</param>
 /// <param name="orderable">Whether or not the field is orderable.</param>
 /// <param name="navigable">Whether or not the field is navigable.</param>
 /// <param name="searchable">Whether or not the field is searchable.</param>
 /// <param name="schema">The schema.</param>
 internal Field(string id, string name, Type fieldType, bool orderable, bool navigable, bool searchable, FieldSchema schema)
 {
     Id         = id;
     Name       = name;
     FieldType  = fieldType;
     Orderable  = orderable;
     Navigable  = navigable;
     Searchable = searchable;
     Schema     = schema;
 }
Ejemplo n.º 16
0
 public int GetCount(FieldSchema field, Interpreter interpreter)
 {
     if (Dimensions.TryGetValue(field, out var dimensions))
     {
         return((int)dimensions
                .Select(dimension => interpreter.Evaluate(dimension))
                .Aggregate((a, b) => a * b));
     }
     return(-1);
 }
Ejemplo n.º 17
0
        /// <summary>
        /// 获取子表结构
        /// </summary>
        /// <param name="Schema"></param>
        /// <param name="Field"></param>
        /// <returns></returns>
        private BizObjectSchema GetChildSchema(BizObjectSchema schema, FieldSchema field)
        {
            PropertySchema property = schema.GetProperty(field.Name);

            if (property != null && property.ChildSchema != null)
            {
                return(property.ChildSchema);
            }
            return(this.Engine.BizObjectManager.GetPublishedSchema(field.ChildSchemaCode));
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Initializes a new field.
 /// </summary>
 /// <param name="id">The ID of the field.</param>
 /// <param name="name">The name.</param>
 /// <param name="fieldType">The type of field; whether custom or JIRA.</param>
 /// <param name="orderable">Whether or not the field is orderable.</param>
 /// <param name="navigable">Whether or not the field is navigable.</param>
 /// <param name="searchable">Whether or not the field is searchable.</param>
 /// <param name="schema">The schema.</param>
 internal Field(string id, string name, Type fieldType, bool orderable, bool navigable, bool searchable, FieldSchema schema)
 {
     Id = id;
     Name = name;
     FieldType = fieldType;
     Orderable = orderable;
     Navigable = navigable;
     Searchable = searchable;
     Schema = schema;
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Get the type associated with the template argument of a field
        /// </summary>
        /// <param name="field"></param>
        /// <param name="parentName"></param>
        /// <returns></returns>
        public Type GetFieldTemplateType(FieldSchema field)
        {
            if (field.Template == null)
            {
                return(null);
            }
            var parentName   = Document.TypeLookup.ParentNameBySchema[field];
            var templateType = TemplateTypeByName.GetValueOrDefault(parentName);

            return(GetBaseType(field.Template, templateType));
        }
Ejemplo n.º 20
0
        public void ShouldDescribeFieldAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act
            FieldSchema schema = databaseApi.DescribeFieldAsync("staff", "field").Result;

            // Assert
            schema.name.ShouldBe("field");
        }
Ejemplo n.º 21
0
        public Instruction ConstructArray(Instruction loadLength, FieldSchema fieldSchema)
        {
            var fieldType = DocBuilder.SchemaTypes.GetArrayType(fieldSchema);

            return(new Instruction.InstructionWriter(il =>
            {
                il.Emit(OpCodes.Ldarg_0);
                loadLength.Write(il);
                il.Emit(OpCodes.Conv_Ovf_I);
                il.Emit(OpCodes.Newobj, fieldType);
            }));
        }
Ejemplo n.º 22
0
        public Instruction LoadFieldValue(FieldSchema field)
        {
            if (DocBuilder.Document.Compounds.TryGetValue(field.Type, out var compoundSchema))
            {
                return(ReadCompoundValue(field, compoundSchema));
            }
            if (DocBuilder.Document.Basics.TryGetValue(field.Type, out var basicSchema))
            {
                return(ReadBasicValue(basicSchema));
            }

            return(new Instruction.LoadInt32(0));
        }
Ejemplo n.º 23
0
        public void ShouldDescribeFieldAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act
            FieldSchema schema = databaseApi.DescribeFieldAsync("staff", "field").Result;

            // Assert
            schema.Name.ShouldBe("field");

            Should.Throw <ArgumentNullException>(() => databaseApi.DescribeFieldAsync(null, "field"));
            Should.Throw <ArgumentNullException>(() => databaseApi.DescribeFieldAsync("staff", null));
        }
Ejemplo n.º 24
0
        Instruction WithCondition(Instruction onSuccess, FieldSchema schema, ExpressionWriter.State state)
        {
            if (schema.Condition == null)
            {
                return(onSuccess);
            }

            var conditionalInstruction = new Instruction.IfBranch(
                LoadCondition(schema, state),
                onSuccess
                );

            return(conditionalInstruction);
        }
Ejemplo n.º 25
0
        /// <inheritdoc />
        public ITableSchemaBuilder WithKeyField(string fieldName)
        {
            FieldSchema field = new FieldSchema
            {
                name           = fieldName,
                required       = true,
                type           = "id",
                is_primary_key = true,
                auto_increment = true
            };

            fields.Add(field);
            return(this);
        }
        public void ShouldBuildWithKeyField()
        {
            // Arrange
            ITableSchemaBuilder builder = new TableSchemaBuilder();

            // Act
            TableSchema schema = builder.WithKeyField("index").Build();

            // Assert
            FieldSchema field = schema.Field.First();

            field.IsPrimaryKey.ShouldBe(true);
            field.Type.ShouldBe("id");
            field.Name.ShouldBe("index");
        }
Ejemplo n.º 27
0
        /// <inheritdoc />
        public ITableSchemaBuilder WithField <TField>(string fieldName, bool required = false, TField defaultValue = default(TField))
        {
            string typeName = TypeMap.GetTypeName(typeof(TField));

            FieldSchema field = new FieldSchema
            {
                Name         = fieldName,
                Required     = required,
                DefaultValue = defaultValue.ToString().ToLowerInvariant(),
                Type         = typeName
            };

            fields.Add(field);
            return(this);
        }
Ejemplo n.º 28
0
        public Instruction ConstructField(FieldSchema schema, FieldBuilder builder, ExpressionWriter.State state)
        {
            if (schema.Dimensions.Count > 0)
            {
                return(new Instruction.NoOperation());
            }

            var setFieldInstruction = new Instruction.Set(
                new Instruction.LoadThis(),
                LoadFieldValue(schema),
                new Instruction.SetField(builder)
                );

            return(WithCondition(setFieldInstruction, schema, state));
        }
        public void ShouldBuildWithField()
        {
            // Arrange
            ITableSchemaBuilder builder = new TableSchemaBuilder();

            // Act
            TableSchema schema = builder.WithField("name", true, 123).Build();

            // Assert
            FieldSchema field = schema.Field.First();

            field.IsPrimaryKey.ShouldBe(null);
            field.Required.ShouldBe(true);
            field.Name.ShouldBe("name");
            field.DefaultValue.ShouldBe("123");
        }
Ejemplo n.º 30
0
        public void ShouldBuildWithField()
        {
            // Arrange
            ITableSchemaBuilder builder = new TableSchemaBuilder();

            // Act
            TableSchema schema = builder.WithField("name", true, 123).Build();

            // Assert
            FieldSchema field = schema.field.First();

            field.is_primary_key.ShouldBe(null);
            field.required.ShouldBe(true);
            field.name.ShouldBe("name");
            field.default_value.ShouldBe("123");
        }
Ejemplo n.º 31
0
        Instruction WithArray(Instruction loadValue, FieldSchema fieldSchema, FieldBuilder builder)
        {
            if (!fieldSchema.IsMultiDimensional)
            {
                return(new Instruction.Set(
                           new Instruction.LoadThis(),
                           loadValue,
                           new Instruction.SetField(builder)
                           ));
            }

            return(new Instruction.Set(
                       new Instruction.LoadThis(),
                       //ConstructArray(),
                       new Instruction.SetField(builder)
                       ));
        }
Ejemplo n.º 32
0
        public void CanValidateByRule(DataType dataType, object value, string ruleType, string ruleContent, bool expected)
        {
            var rule = new Rule {Type = ruleType, Content = ruleContent};
            var schema = new FieldSchema { DataType = dataType, Rules = new[]{rule} };
            var field = new Field(schema, value);
            field.Validate();

            if (expected)
            {
                Assert.AreEqual(0, field.Errors.Count());
                Assert.AreEqual(NodeState.Valid, field.State);
            }
            else
            {
                Assert.AreEqual(1, field.Errors.Count());
                Assert.AreEqual(ruleType, field.Errors.First().Type);
            }
        }
Ejemplo n.º 33
0
        private static MvcBizObject LoadBizObjectData(InstanceData Data, FieldSchema field)
        {
            BizObject    bo  = Data[field.Name].Value as BizObject;
            MvcBizObject mbo = new MvcBizObject();

            if (bo == null)
            {
                BizObjectSchema schema = field.Schema;
                if (schema == null)
                {
                    schema = AppUtility.Engine.BizObjectManager.GetPublishedSchema(field.ChildSchemaCode);
                }
                bo = new BizObject(AppUtility.Engine, schema, "");
            }
            foreach (PropertySchema p in bo.Schema.Properties)
            {
                mbo.DataItems.Add(field.Name + "." + p.Name, GetMvcDataFromProperty(field, p, bo));
            }
            return(mbo);
        }
Ejemplo n.º 34
0
        public Instruction ReadCompoundValue(FieldSchema field, CompoundSchema compound)
        {
            var fieldType   = DocBuilder.SchemaFields.FieldTypes[field];
            var constructor = DocBuilder.SchemaConstructors.CompoundConstructors[compound] as ConstructorInfo;

            if (field.Template != null)
            {
                constructor = TypeBuilder.GetConstructor(fieldType, constructor);
            }

            if (constructor == null)
            {
                throw new Exception();
            }

            return(new Instruction.Set(
                       new Instruction.LoadArgument(1),
                       new Instruction.LoadInt64(0),
                       new Instruction.NewObject(constructor)
                       ));
        }
        private static IEnumerable<object> ParseAllowedValues(JsonArrayObjects json, FieldSchema schema)
        {
            if (json == null)
            {
                return null;
            }

            if (json.Count == 0)
            {
                return new object[0];
            }

            var parser = GetParserFor(schema);
            if (parser != null)
            {
                return json.ConvertAll(parser.Invoke);
            }

            // Fallback to returning an array of JsonObjects
            return json;
        }
        private static Func<JsonObject, object> GetParserFor(FieldSchema schema)
        {
            var customFieldTypesWithFieldOption = new List<string>
            {
                "com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes",
                "com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons",
                "com.atlassian.jira.plugin.system.customfieldtypes:select",
                "com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect",
                "com.atlassian.jira.plugin.system.customfieldtypes:multiselect"
            };

            var type = schema.Type == "array" ? schema.Items : schema.Type;
            if (schema.Custom != null && customFieldTypesWithFieldOption.Contains(schema.Custom))
            {
                // TODO: Implement custom fiels option parser!
                type = "customFieldOption";
            }

            Func<JsonObject, object> parser;
            RegisteredAllowedValueParsers.TryGetValue(type, out parser);
            return parser;
        }
Ejemplo n.º 37
0
        public void CanParseBigDecimal(string input, string expectedStr, string culture, bool expectSuccess)
        {
            var schema = new FieldSchema { DataType = DataType.BigDecimal };
            var field = new Field(schema, input);
            field.Context.Config.CultureName = culture;
            var actual = (Decimal)field.Value;
            var expected = Decimal.Parse(expectedStr);

            if (expectSuccess)
            {
                Assert.IsNull(field.Errors);
                Assert.AreEqual(NodeState.Resolved, field.State);
                Assert.AreEqual(expected, actual);
            }
            else
            {
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(NodeState.Fault, field.State);
                Assert.IsNotNull(field.Errors);
                Assert.IsTrue(field.Errors.Any());
                Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
            }
        }
Ejemplo n.º 38
0
        public void CanValidateMultiple()
        {
            var gte10 = new Rule {Type = ValidationType.Min, Content = "10"};
            var lte100 = new Rule {Type = ValidationType.Max, Content = "100"};
            var gte120 = new Rule {Type = ValidationType.Min, Content = "120"};
            var schema1 = new FieldSchema
            {
                DataType = DataType.Integer,
                Rules = new List<Rule>{gte10, lte100}
            };
            var field1 = new Field(schema1, "20");
            var schema2 = new FieldSchema
            {
                DataType = DataType.Integer,
                Rules = new List<Rule> { lte100, gte120 }
            };
            var field2 = new Field(schema2, "101");

            field1.Validate();
            Assert.AreEqual(0, field1.Errors.Count());
            field2.Validate();
            Assert.AreEqual(2, field2.Errors.Count());
        }
Ejemplo n.º 39
0
        public void CanValidateCustom()
        {
            var schema = new FieldSchema
            {
                Rules = new List<Rule>
                {
                    new Rule { Type = "taxNumber" }
                }
            };
            var field1 = new Field(schema, "1234567890");
            field1.Validate();

            Assert.AreEqual(1, field1.Errors.Count());
            Assert.AreEqual("taxNumber", field1.Errors.First().Type);

            var field2 = new Field(schema, "4400112594");
            field2.Validate();
            Assert.AreEqual(0, field2.Errors.Count());
        }
Ejemplo n.º 40
0
        string createProperty(FieldSchema fs)
        {
            string sProperty = string.Empty;

            sProperty += indent;
            sProperty += indent;

            string sType = fs.DataType.ToCSharpType();

            if (fs.DataType != typeof(string) && fs.AllowDBNull == true)
                sType += "?";

            //AllowDBNull

            sProperty += string.Format("public {0} {1} {{ get; set; }}", sType, fs.ColumnName);

            sProperty += "\n";

            return sProperty;
        }
Ejemplo n.º 41
0
        public void CanParseDateTime(string input, string expectedStr, string format, string cfgFormat, string culture, bool expectSuccess)
        {
            var schema = new FieldSchema
            {
                DataType = DataType.DateTime,
                DataFormat = format
            };
            var field = new Field(schema, input);
            field.Context.Config.DateTimeFormat = cfgFormat;
            field.Context.Config.CultureName = culture;

            var actual = field.Value;
            if (expectSuccess)
            {
                Assert.AreEqual(NodeState.Resolved, field.State);
                Assert.IsNull(field.Errors);
                var expected = new DateTimeOffset(DateTime.Parse(expectedStr), TimeZoneInfo.Local.BaseUtcOffset);
                Assert.AreEqual(expected, actual);
            }
            else
            {
                Assert.AreEqual(NodeState.Fault, field.State);
                Assert.IsNotNull(field.Errors);
                Assert.IsTrue(field.Errors.Any());
                Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
                Assert.AreEqual(DateTimeOffset.MinValue, actual);
            }
        }
Ejemplo n.º 42
0
 public void UnsetPropertyOrUnsupportedTypeShouldReturnDefaultValueWhenGet()
 {
     var node = new FieldSchema
     {
         Properties = new Dictionary<string, string>
         {
             {"bool", "xxx"},
             {"int", "xxx"},
             {"decimal","xxx"},
             {"dateTime","xxx"},
         }
     };
     Assert.AreEqual(false, node.Property<bool>("bool"));
     Assert.AreEqual(0, node.Property<int>("int"));
     Assert.AreEqual(0, node.Property<double>("decimal"));
     Assert.AreEqual(DateTime.MinValue, node.Property<DateTime>("dateTime"));
     Assert.AreEqual(null, node.Property<string>("string"));
     Assert.AreEqual(null, node.Property<byte[]>("bytes"));
 }
Ejemplo n.º 43
0
 public void ShouldValidateEvaluation(DataType type, string expression, bool expected)
 {
     var schema = new FieldSchema { DataType = type, Default = expression };
     var field = new Field(schema);
     field.Validate();
     var actual = field.State == NodeState.Valid;
     Assert.AreEqual(expected, actual);
     if (actual)
     {
         CollectionAssert.IsEmpty(field.Errors);
     }
     else
     {
         CollectionAssert.IsNotEmpty(field.Errors);
         Assert.AreEqual(ValidationType.Evaluation, field.Errors.First().Type);
     }
 }
Ejemplo n.º 44
0
        public void ShouldStopValidationOnFormatIfFailed()
        {
            var schema = new FieldSchema
            {
                DataType = DataType.Integer,
                Rules = new List<Rule>
                {
                    new Rule
                    {
                        Type = ValidationType.Max,
                        Content = "100",
                    }
                }
            };
            var field = new Field(schema, "abc123");
            field.Validate();

            Assert.AreEqual(1, field.Errors.Count());
            Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
        }
Ejemplo n.º 45
0
 public void FieldSchemaShouldOnlyAcceptCertainDataType(DataType input, DataType expected)
 {
     var fs = new FieldSchema(null, input);
     Assert.AreEqual(expected, fs.DataType);
     fs.DataType = input;
     Assert.AreEqual(expected, fs.DataType);
 }
Ejemplo n.º 46
0
 public void FieldSchemaWithCustomTypeStrShouldBeOfTypeObject()
 {
     var info = new FieldSchema { CustomType = "NumberRange" };
     Assert.AreEqual(info.DataType, DataType.Object);
 }
Ejemplo n.º 47
0
 public void CanGetProperty()
 {
     var node = new FieldSchema
     {
         Properties = new Dictionary<string, string>
         {
             {"boolVal", "true"},
             {"intVal", "12"},
             {"decimalVal","23.5"},
             {"dateTimeVal","02/29/2012"},
             {"stringVal","abc"}
         }
     };
     Assert.AreEqual("abc", node.Property<string>("stringVal"));
     Assert.AreEqual(new DateTime(2012, 2, 29), node.Property<DateTime>("dateTimeVal"));
     Assert.AreEqual(23.5, node.Property<double>("decimalVal"));
     Assert.AreEqual(12, node.Property<int>("intVal"));
     Assert.AreEqual(true, node.Property<bool>("boolVal"));
 }
Ejemplo n.º 48
0
 public void CanQueryPropertyExistence()
 {
     var node = new FieldSchema
     {
         Properties = new Dictionary<string, string>
         {
             {"bool", "xxx"},
             {"int", "xxx"},
             {"decimal","xxx"},
             {"dateTime","xxx"},
         }
     };
     Assert.IsTrue(node.HasProperty("bool"));
     Assert.IsTrue(node.HasProperty("decimal"));
     Assert.IsTrue(node.HasProperty("int"));
     Assert.IsTrue(node.HasProperty("dateTime"));
     Assert.IsFalse(node.HasProperty("string"));
 }
Ejemplo n.º 49
0
 public void CanDetectIsComputed(string defVal, bool expected)
 {
     var info = new FieldSchema { Default = defVal };
     var actual = info.IsComputed();
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 50
0
        public void CanReadProperties()
        {
            var node = new FieldSchema
            {
                Properties = new Dictionary<string, string>
                {
                    {"str","abc"},
                    {"int","12"},
                    {"bool","true"},
                    {"double","12.5"},
                }
            };

            Assert.IsTrue(node.HasProperty("double"));
            Assert.AreEqual("12.5",node.Property("double"));
            Assert.AreEqual(12.5, node.Property<double>("double"));
            Assert.AreEqual(0d, node.Property<double>("double2"));

            Assert.IsTrue(node.HasProperty("int"));
            Assert.AreEqual("12", node.Property("int"));
            Assert.AreEqual(12, node.Property<int>("int"));
            Assert.AreEqual(0, node.Property<int>("int2"));

            Assert.IsTrue(node.HasProperty("str"));
            Assert.AreEqual("abc", node.Property("str"));
            Assert.AreEqual("abc", node.Property<string>("str"));
            Assert.AreEqual(null, node.Property<string>("str2"));

            Assert.IsTrue(node.HasProperty("bool"));
            Assert.AreEqual("true", node.Property("bool"));
            Assert.AreEqual(true, node.Property<bool>("bool"));
            Assert.AreEqual(false, node.Property<bool>("bool2"));
        }
Ejemplo n.º 51
0
        public void CanSetProperty()
        {
            var node = new FieldSchema();

            node.Property("string", "abc");
            node.Property("int", 12);
            node.Property("dateTime", new DateTime(2012, 2, 29));
            node.Property("timeSpan", TimeSpan.FromDays(1));
            node.Property("bytes", new byte[] {0x0a, 0x0d});

            Assert.AreEqual("abc", node.Property("string"));
            Assert.AreEqual(12, node.Property<int>("int"));
            Assert.AreEqual("12", node.Property("int"));
            Assert.AreEqual(new DateTime(2012, 2, 29), node.Property<DateTime>("dateTime"));
            Assert.AreEqual(@"2/29/2012 12:00:00 AM", node.Property("dateTime"));
            Assert.AreEqual(TimeSpan.Parse("00:00:00"), node.Property<TimeSpan>("timeSpan"));
            Assert.AreEqual("1.00:00:00", node.Property("timeSpan"));
            Assert.AreEqual(null, node.Property<byte[]>("bytes"));
            Assert.AreEqual("System.Byte[]", node.Property("bytes"));
        }
Ejemplo n.º 52
0
        public void CanParseUri(string input, string expectedStr, bool expectSuccess)
        {
            var schema = new FieldSchema { DataType = DataType.Link };
            var field = new Field(schema, input);
            var actual = (Uri)field.Value;
            var expected = new Uri(expectedStr);

            if (expectSuccess)
            {
                Assert.IsNull(field.Errors);
                Assert.AreEqual(NodeState.Resolved, field.State);
                Assert.AreEqual(expected, actual);
            }
            else
            {
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(NodeState.Fault, field.State);
                Assert.IsNotNull(field.Errors);
                Assert.IsTrue(field.Errors.Any());
                Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
            }
        }
Ejemplo n.º 53
0
        public void ShouldStopValidationOnEvaluationIfFailed()
        {
            var schema = new FieldSchema
            {
                DataType = DataType.Integer,
                Rules = new List<Rule>
                {
                    new Rule
                    {
                        Type = ValidationType.Max,
                        Content = "100",
                    }
                },
                Default = "=x^2+2*x+1"
            };
            var field = new Field(schema);
            field.Validate();

            Assert.AreEqual(1, field.Errors.Count());
            Assert.AreEqual(ValidationType.Evaluation, field.Errors.First().Type);
        }
Ejemplo n.º 54
0
 public void CanParseBoolean(string input, bool expected, string format, string cfgFormat, bool expectSuccess)
 {
     var schema = new FieldSchema { DataType = DataType.Boolean, DataFormat = format };
     var field = new Field(schema, input);
     field.Context.Config.BooleanFormat = cfgFormat;
     var actual = (bool)field.Value;
     if (expectSuccess)
     {
         Assert.IsNull(field.Errors);
         Assert.AreEqual(NodeState.Resolved, field.State);
         Assert.AreEqual(expected, actual);
     }
     else
     {
         Assert.AreEqual(expected, actual);
         Assert.AreEqual(NodeState.Fault, field.State);
         Assert.IsNotNull(field.Errors);
         Assert.IsTrue(field.Errors.Any());
         Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
     }
 }
Ejemplo n.º 55
0
        public void ShouldStopValidationOnIsRequiredIfFailed()
        {
            var schema = new FieldSchema
            {
                Rules = new List<Rule>
                {
                    new Rule { Type = ValidationType.MinSize, Content = "2"},
                    new Rule { Type = ValidationType.Exist },
                }
            };
            var field = new Field(schema, "");
            field.Validate();

            Assert.AreEqual(1, field.Errors.Count());
            Assert.AreEqual(ValidationType.Exist, field.Errors.First().Type);
        }
Ejemplo n.º 56
0
 public void FormatForInvalidInputReturnsDefaultValue(DataType type, object input, string expected)
 {
     var schema = new FieldSchema { DataType = type };
     var field = new Field(schema, input);
     var actual = field.Format();
     Assert.AreEqual(NodeState.Fault, field.State);
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 57
0
 public void CanParseGuid(string input, string format, string cfgFormat, string expectedStr, bool expectSuccess)
 {
     var schema = new FieldSchema { DataType = DataType.Guid, DataFormat = format };
     var field = new Field(schema, input);
     field.Context.Config.GuidFormat = cfgFormat;
     var actual = (Guid)field.Value;
     var expected = Guid.Parse(expectedStr);
     if (expectSuccess)
     {
         Assert.AreEqual(expected, actual);
         Assert.AreEqual(NodeState.Resolved, field.State);
         Assert.IsNull(field.Errors);
         Assert.AreEqual(expected, actual);
     }
     else
     {
         Assert.AreEqual(expected, actual);
         Assert.AreEqual(NodeState.Fault, field.State);
         Assert.IsNotNull(field.Errors);
         Assert.AreEqual(1, field.Errors.Count());
         Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
     }
 }
Ejemplo n.º 58
0
 public void CanParseComplexObject()
 {
     var schema = new FieldSchema
     {
         CustomType = "taxNumber"
     };
     var field = new Field(schema,"4400112594");
     var actual = (TaxNumber) field.Value;
     Assert.IsNotNull(actual);
     Assert.IsNotNullOrEmpty(actual.Body);
     Assert.AreEqual(actual.Body, "4400112594");
     Assert.IsNull(actual.Extension);
 }