Exemplo n.º 1
0
        public void ParseValList_1()
        {
            var sut = FieldAttribute.ParseValueListString("a:apple, b: banana");

            Aver.IsNotNull(sut);
            Aver.IsFalse(sut.CaseSensitive);
            Aver.AreEqual(2, sut.Count);
            Aver.AreEqual("apple", sut["a"].AsString());
            Aver.AreEqual("banana", sut["b"].AsString());
            Aver.AreEqual("apple", sut["A"].AsString());
            Aver.AreEqual("banana", sut["B"].AsString());
        }
Exemplo n.º 2
0
        public void ParseValList_2_withalt()
        {
            var sut = FieldAttribute.ParseValueListString("01|1|a|A: one, 2|02: two", caseSensitiveKeys: true);//no error because of case sensitive

            Aver.IsNotNull(sut);
            Aver.IsTrue(sut.CaseSensitive);
            Aver.AreEqual(6, sut.Count);
            Aver.AreEqual("one", sut["1"].AsString());
            Aver.AreEqual("one", sut["01"].AsString());
            Aver.AreEqual("one", sut["a"].AsString());
            Aver.AreEqual("one", sut["A"].AsString());
            Aver.AreEqual("two", sut["2"].AsString());
            Aver.AreEqual("two", sut["02"].AsString());
        }
Exemplo n.º 3
0
        public void ParseValList_5()
        {
            var sut = FieldAttribute.ParseValueListString("1|01|911:fire;02|912:police;03:ambulance");//notice different delimiters and spaces

            Aver.IsNotNull(sut);
            Aver.AreEqual(6, sut.Count);
            Aver.AreEqual("fire", sut["1"].AsString());
            Aver.AreEqual("fire", sut["01"].AsString());
            Aver.AreEqual("fire", sut["911"].AsString());

            Aver.AreEqual("police", sut["02"].AsString());
            Aver.AreEqual("police", sut["912"].AsString());

            Aver.AreEqual("ambulance", sut["03"].AsString());
        }
Exemplo n.º 4
0
        public void ParseValList_7()
        {
            var sut = FieldAttribute.ParseValueListString("1|| ||   | | | |||| || 01 |   911 : fire, 02 | 912: police,03:ambulance");

            Aver.IsNotNull(sut);
            Aver.AreEqual(6, sut.Count);
            Aver.AreEqual("fire", sut["1"].AsString());
            Aver.AreEqual("fire", sut["01"].AsString());
            Aver.AreEqual("fire", sut["911"].AsString());

            Aver.AreEqual("police", sut["02"].AsString());
            Aver.AreEqual("police", sut["912"].AsString());

            Aver.AreEqual("ambulance", sut["03"].AsString());
        }
Exemplo n.º 5
0
        public void Record_ValidInitJSON()
        {
            var json =
                @"{
  'OK': true,
  'ID': '39a833dd-b48f-46c1-83a6-96603cc962a6',
  'ISOLang': 'eng',
  '__FormMode': 'Edit',
  '__CSRFToken': '1kk_qzXPLyScAa2A5y5GLTo9IlCqjuP',
  '__Roundtrip': '{\'GDID\':\'0:1:5\'}',
  'fields': [
      {
        'def': {
          'Name': 'Mnemonic',
          'Type': 'string',
          'Description': 'Mnemonic',
          'Required': true,
          'MinSize': 1,
          'Size': 25,
          'Placeholder': 'Mnemonic',
          'Stored': false
        },
        'val': 'Dns'
      },
      {
        'def': {
          'Name': 'Vertical_ID',
          'Type': 'string',
          'Description': 'Vertical',
          'Required': false,
          'Visible': false,
          'Size': 15,
          'DefaultValue': 'HAB',
          'Key': true,
          'Case': 'Upper',
          'LookupDict': {
            'HAB': 'HAB.rs Health and Beauty',
            'READRS': 'READ.rs Book Business',
            'SLRS': 'SL.RS General Business'
          }
        },
        'val': 'HAB'
      },
      {
        'def': {
          'Name': 'Table_ID',
          'Type': 'int',
          'Key': true,
          'Description': 'Table',
          'Required': true,
          'Visible': false,
          'MinValue': 1,
          'MaxValue': 123,
          'DefaultValue': 15,
          'Kind': 'Number',
          'Stored': true
        },
        'val': 2
      }
    ]
}";

            var init = JSONReader.DeserializeDataObject(json) as JSONDataMap;
            var rec  = new Record(init);

            Assert.AreEqual(0, rec.ServerErrors.Count());

            Assert.AreEqual(true, rec.OK);
            Assert.AreEqual("eng", rec.ISOLang);
            Assert.AreEqual(new Guid("39a833dd-b48f-46c1-83a6-96603cc962a6"), rec.ID);
            Assert.AreEqual("Edit", rec.FormMode);
            Assert.AreEqual("1kk_qzXPLyScAa2A5y5GLTo9IlCqjuP", rec.CSRFToken);

            var roundtrip = rec.Roundtrip;

            Assert.IsNotNull(roundtrip);
            Assert.AreEqual(roundtrip.Count, 1);
            Assert.AreEqual("0:1:5", roundtrip["GDID"]);

            Assert.AreEqual(3, rec.Schema.FieldCount);

            var fdef = rec.Schema["Mnemonic"];

            Assert.IsNotNull(fdef);
            Assert.AreEqual("Mnemonic", fdef.Name);
            Assert.AreEqual(typeof(string), fdef.Type);
            Assert.AreEqual(false, fdef.AnyTargetKey);
            Assert.IsNotNull(fdef.Attrs);
            Assert.AreEqual(1, fdef.Attrs.Count());
            var attr = fdef.Attrs.First();

            Assert.AreEqual("Mnemonic", attr.Description);
            Assert.AreEqual(true, attr.Required);
            Assert.AreEqual(true, attr.Visible);
            Assert.AreEqual(null, attr.Min);
            Assert.AreEqual(null, attr.Max);
            Assert.AreEqual(1, attr.MinLength);
            Assert.AreEqual(25, attr.MaxLength);
            Assert.AreEqual(null, attr.Default);
            Assert.AreEqual(0, attr.ParseValueList().Count);
            Assert.AreEqual(StoreFlag.OnlyLoad, attr.StoreFlag);
            Assert.AreEqual(@"''{Name=Mnemonic Type=string Description=Mnemonic Required=True MinSize=1 Size=25 Placeholder=Mnemonic Stored=False}", attr.MetadataContent);
            Assert.AreEqual("Dns", rec["Mnemonic"]);

            fdef = rec.Schema["Vertical_ID"];
            Assert.IsNotNull(fdef);
            Assert.AreEqual("Vertical_ID", fdef.Name);
            Assert.AreEqual(typeof(string), fdef.Type);
            Assert.AreEqual(true, fdef.AnyTargetKey);
            Assert.IsNotNull(fdef.Attrs);
            Assert.AreEqual(1, fdef.Attrs.Count());
            attr = fdef.Attrs.First();
            Assert.AreEqual("Vertical", attr.Description);
            Assert.AreEqual(false, attr.Required);
            Assert.AreEqual(false, attr.Visible);
            Assert.AreEqual(null, attr.Min);
            Assert.AreEqual(null, attr.Max);
            Assert.AreEqual(0, attr.MinLength);
            Assert.AreEqual(15, attr.MaxLength);
            Assert.AreEqual(CharCase.Upper, attr.CharCase);
            Assert.AreEqual("HAB", attr.Default);
            Assert.AreEqual(FieldAttribute.ParseValueListString("HAB:HAB.rs Health and Beauty,READRS:READ.rs Book Business,SLRS:SL.RS General Business", true), attr.ParseValueList(true));
            Assert.AreEqual("''{Name=Vertical_ID Type=string Description=Vertical Required=False Visible=False Size=15 DefaultValue=HAB Key=True Case=Upper LookupDict=\"{\\\"HAB\\\":\\\"HAB.rs Health and Beauty\\\",\\\"READRS\\\":\\\"READ.rs Book Business\\\",\\\"SLRS\\\":\\\"SL.RS General Business\\\"}\"}", attr.MetadataContent);
            Assert.AreEqual("HAB", rec["Vertical_ID"]);

            fdef = rec.Schema["Table_ID"];
            Assert.IsNotNull(fdef);
            Assert.AreEqual("Table_ID", fdef.Name);
            Assert.AreEqual(typeof(long), fdef.Type);
            Assert.AreEqual(true, fdef.AnyTargetKey);
            Assert.IsNotNull(fdef.Attrs);
            Assert.AreEqual(1, fdef.Attrs.Count());
            attr = fdef.Attrs.First();
            Assert.AreEqual("Table", attr.Description);
            Assert.AreEqual(true, attr.Required);
            Assert.AreEqual(false, attr.Visible);
            Assert.AreEqual(1, attr.Min);
            Assert.AreEqual(123, attr.Max);
            Assert.AreEqual(15, attr.Default);
            Assert.AreEqual(DataKind.Number, attr.Kind);
            Assert.AreEqual(StoreFlag.LoadAndStore, attr.StoreFlag);
            Assert.AreEqual(0, attr.ParseValueList(true).Count);
            Assert.AreEqual("''{Name=Table_ID Type=int Key=True Description=Table Required=True Visible=False MinValue=1 MaxValue=123 DefaultValue=15 Kind=Number Stored=True}", attr.MetadataContent);
            Assert.AreEqual(2, rec["Table_ID"]);
        }
Exemplo n.º 6
0
        private static JSONDataMap fieldDefToJSON(string schema, Schema.FieldDef fdef, string target)
        {
            var result = new JSONDataMap();

            result["Name"] = fdef.Name;
            result["Type"] = mapType(fdef.NonNullableType);
            var key = fdef.AnyTargetKey;

            if (key)
            {
                result["Key"] = key;
            }


            if (fdef.NonNullableType.IsEnum)
            { //Generate default lookupdict for enum
                var names  = Enum.GetNames(fdef.NonNullableType);
                var values = new JSONDataMap(true);
                foreach (var name in names)
                {
                    values[name] = name;
                }

                result["LookupDict"] = values;
            }

            var attr = fdef[target];

            if (attr != null)
            {
                if (attr.Description != null)
                {
                    result["Description"] = localizeString(schema, "Description", attr.Description);
                }
                var str = attr.StoreFlag == StoreFlag.OnlyStore || attr.StoreFlag == StoreFlag.LoadAndStore;
                if (!str)
                {
                    result["Stored"] = str;
                }
                if (attr.Required)
                {
                    result["Required"] = attr.Required;
                }
                if (!attr.Visible)
                {
                    result["Visible"] = attr.Visible;
                }
                if (attr.Min != null)
                {
                    result["MinValue"] = attr.Min;
                }
                if (attr.Max != null)
                {
                    result["MaxValue"] = attr.Max;
                }
                if (attr.MinLength > 0)
                {
                    result["MinSize"] = attr.MinLength;
                }
                if (attr.MaxLength > 0)
                {
                    result["Size"] = attr.MaxLength;
                }
                if (attr.Default != null)
                {
                    result["DefaultValue"] = attr.Default;
                }
                if (attr.ValueList.IsNotNullOrWhiteSpace())
                {
                    var vl = localizeString(schema, "LookupDict", attr.ValueList);
                    result["LookupDict"] = FieldAttribute.ParseValueListString(vl);
                }
                if (attr.Kind != DataKind.Text)
                {
                    result["Kind"] = mapKind(attr.Kind);
                }
            }

            if (attr.Metadata != null)
            {
                foreach (var fn in METADATA_FIELDS)
                {
                    var mv = attr.Metadata.AttrByName(fn).Value;
                    if (mv.IsNullOrWhiteSpace())
                    {
                        continue;
                    }

                    if (fn == "Description" || fn == "Placeholder" || fn == "LookupDict" || fn == "Hint")
                    {
                        mv = localizeString(schema, fn, mv);
                    }

                    result[fn] = mv;
                }
            }


            return(result);
        }
Exemplo n.º 7
0
 public void ParseValList_1_withalt()
 {
     var sut = FieldAttribute.ParseValueListString("01|1|a|A: one, 2|02: two");
 }
Exemplo n.º 8
0
 public void ParseValList_9()
 {
     var sut = FieldAttribute.ParseValueListString("1|01|001:one;2|02|01:two;3:three");
 }
Exemplo n.º 9
0
 public void ParseValList_8()
 {
     var sut = FieldAttribute.ParseValueListString("1:one;2:two;1:again");
 }
Exemplo n.º 10
0
        protected virtual JsonDataMap FieldDefToJSON(Doc doc,
                                                     string schema,
                                                     Schema.FieldDef fdef,
                                                     string target,
                                                     Atom isoLang,
                                                     ModelFieldValueListLookupFunc valueListLookup)
        {
            var result = new JsonDataMap();

            result["Name"] = fdef.Name;
            result["Type"] = MapCLRTypeToJS(fdef.NonNullableType);
            var key = fdef.AnyTargetKey;

            if (key)
            {
                result["Key"] = key;
            }


            if (fdef.NonNullableType.IsEnum)
            { //Generate default lookupdict for enum
                var names  = Enum.GetNames(fdef.NonNullableType);
                var values = new JsonDataMap(true);
                foreach (var name in names)
                {
                    values[name] = name;
                }

                result["LookupDict"] = values;
            }

            var attr = fdef[target];

            if (attr != null)
            {
                if (attr.Description != null)
                {
                    result["Description"] = OnLocalizeString(schema, "Description", attr.Description, isoLang);
                }
                var str = attr.StoreFlag == StoreFlag.OnlyStore || attr.StoreFlag == StoreFlag.LoadAndStore;
                if (!str)
                {
                    result["Stored"] = false;
                }
                if (attr.Required)
                {
                    result["Required"] = attr.Required;
                }
                if (!attr.Visible)
                {
                    result["Visible"] = attr.Visible;
                }
                if (attr.Min != null)
                {
                    result["MinValue"] = attr.Min;
                }
                if (attr.Max != null)
                {
                    result["MaxValue"] = attr.Max;
                }
                if (attr.MinLength > 0)
                {
                    result["MinSize"] = attr.MinLength;
                }
                if (attr.MaxLength > 0)
                {
                    result["Size"] = attr.MaxLength;
                }
                if (attr.Default != null)
                {
                    result["DefaultValue"] = attr.Default;
                }
                if (attr.ValueList.IsNotNullOrWhiteSpace())
                {
                    var vl = OnLocalizeString(schema, "LookupDict", attr.ValueList, isoLang);
                    result["LookupDict"] = FieldAttribute.ParseValueListString(vl);
                }
                else
                {
                    var valueList = valueListLookup != null?valueListLookup(this, doc, fdef, target, isoLang)
                                        : doc.GetDynamicFieldValueList(fdef, target, isoLang);

                    if (valueList == null && attr.HasValueList)
                    {
                        valueList = attr.ParseValueList();
                    }

                    if (valueList != null)
                    {
                        result["LookupDict"] = valueList;
                    }
                }

                if (attr.Kind != DataKind.Text)
                {
                    result["Kind"] = MapCLRKindToJS(attr.Kind);
                }

                if (attr.CharCase != CharCase.AsIs)
                {
                    result["Case"] = MapCLRCharCaseToJS(attr.CharCase);
                }

                if (attr.Metadata != null)
                {
                    var sect = attr.Metadata[METADATA_CONFIG_FIELD];
                    if (sect.Exists)
                    {
                        result[METADATA_CONFIG_FIELD] = sect.ToConfigurationJSONDataMap();
                    }

                    foreach (var mAt in attr.Metadata.Attributes)
                    {
                        var fn = mAt.Name;
                        if (!METADATA_FIELDS.Contains(fn))
                        {
                            continue;
                        }

                        var mv = mAt.Value;
                        if (mv.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        if (fn == "Description" || fn == "Placeholder" || fn == "LookupDict" || fn == "Hint")
                        {
                            mv = OnLocalizeString(schema, fn, mv, isoLang);
                        }

                        result[fn] = mv;
                    }
                }
            }


            return(result);
        }
Exemplo n.º 11
0
        public void Record_ValidInitJSON()
        {
            var json =
                @"{
  'OK': true,
  'ID': '39a833dd-b48f-46c1-83a6-96603cc962a6',
  'ISOLang': 'eng',
  '__FormMode': 'Edit',
  '__CSRFToken': '1kk_qzXPLyScAa2A5y5GLTo9IlCqjuP',
  '__Roundtrip': '{\'GDID\':\'0:1:5\'}',
  'fields': [
      {
        'def': {
          'Name': 'Mnemonic',
          'Type': 'string',
          'Description': 'Mnemonic',
          'Required': true,
          'MinSize': 1,
          'Size': 25,
          'Placeholder': 'Mnemonic',
          'Stored': false
        },
        'val': 'Dns'
      },
      {
        'def': {
          'Name': 'Vertical_ID',
          'Type': 'string',
          'Description': 'Vertical',
          'Required': false,
          'Visible': false,
          'Size': 15,
          'DefaultValue': 'QWE',
          'Key': true,
          'Case': 'Upper',
          'LookupDict': {
            'QWE': 'QWE Qwerty Business',
            'XYZ': 'XYZ Flight Systems',
            'ABCD': 'ABCD Moon Parity'
          }
        },
        'val': 'QWE'
      },
      {
        'def': {
          'Name': 'Table_ID',
          'Type': 'int',
          'Key': true,
          'Description': 'Table',
          'Required': true,
          'Visible': false,
          'MinValue': 1,
          'MaxValue': 123,
          'DefaultValue': 15,
          'Kind': 'Number',
          'Stored': true
        },
        'val': 2
      }
    ]
}";

            var init = JsonReader.DeserializeDataObject(json) as JsonDataMap;
            var rec  = new Record(init);

            Aver.AreEqual(0, rec.ServerErrors.Count());

            Aver.AreEqual(true, rec.OK);
            Aver.AreObjectsEqual("eng", rec.ISOLang);
            Aver.AreEqual(new Guid("39a833dd-b48f-46c1-83a6-96603cc962a6"), rec.ID);
            Aver.AreObjectsEqual("Edit", rec.FormMode);
            Aver.AreObjectsEqual("1kk_qzXPLyScAa2A5y5GLTo9IlCqjuP", rec.CSRFToken);

            var roundtrip = rec.Roundtrip;

            Aver.IsNotNull(roundtrip);
            Aver.AreEqual(roundtrip.Count, 1);
            Aver.AreObjectsEqual("0:1:5", roundtrip["GDID"]);

            Aver.AreEqual(3, rec.Schema.FieldCount);

            var fdef = rec.Schema["Mnemonic"];

            Aver.IsNotNull(fdef);
            Aver.AreObjectsEqual("Mnemonic", fdef.Name);
            Aver.AreObjectsEqual(typeof(string), fdef.Type);
            Aver.AreEqual(false, fdef.AnyTargetKey);
            Aver.IsNotNull(fdef.Attrs);
            Aver.AreEqual(1, fdef.Attrs.Count());
            var attr = fdef.Attrs.First();

            Aver.AreObjectsEqual("Mnemonic", attr.Description);
            Aver.AreEqual(true, attr.Required);
            Aver.AreEqual(true, attr.Visible);
            Aver.IsNull(attr.Min);
            Aver.IsNull(attr.Max);
            Aver.AreEqual(1, attr.MinLength);
            Aver.AreEqual(25, attr.MaxLength);
            Aver.IsNull(attr.Default);
            Aver.AreEqual(0, attr.ParseValueList().Count);
            Aver.IsTrue(StoreFlag.OnlyLoad == attr.StoreFlag);
            Aver.AreEqual(@"''{Name=Mnemonic Type=string Description=Mnemonic Required=True MinSize=1 Size=25 Placeholder=Mnemonic Stored=False}", attr.MetadataContent);
            Aver.AreObjectsEqual("Dns", rec["Mnemonic"]);

            fdef = rec.Schema["Vertical_ID"];
            Aver.IsNotNull(fdef);
            Aver.AreObjectsEqual("Vertical_ID", fdef.Name);
            Aver.AreObjectsEqual(typeof(string), fdef.Type);
            Aver.AreEqual(true, fdef.AnyTargetKey);
            Aver.IsNotNull(fdef.Attrs);
            Aver.AreEqual(1, fdef.Attrs.Count());
            attr = fdef.Attrs.First();
            Aver.AreObjectsEqual("Vertical", attr.Description);
            Aver.AreEqual(false, attr.Required);
            Aver.AreEqual(false, attr.Visible);
            Aver.IsNull(attr.Min);
            Aver.IsNull(attr.Max);
            Aver.AreEqual(0, attr.MinLength);
            Aver.AreEqual(15, attr.MaxLength);
            Aver.IsTrue(CharCase.Upper == attr.CharCase);
            Aver.AreObjectsEqual("QWE", attr.Default);

            var map1 = FieldAttribute.ParseValueListString("QWE:QWE Qwerty Business,XYZ:XYZ Flight Systems,ABCD:ABCD Moon Parity", true);
            var map2 = attr.ParseValueList(true);

            Aver.IsTrue(map1.SequenceEqual(map2));

            Aver.AreObjectsEqual("''{Name=Vertical_ID Type=string Description=Vertical Required=False Visible=False Size=15 DefaultValue=QWE Key=True Case=Upper LookupDict=\"{\\\"QWE\\\":\\\"QWE Qwerty Business\\\",\\\"XYZ\\\":\\\"XYZ Flight Systems\\\",\\\"ABCD\\\":\\\"ABCD Moon Parity\\\"}\"}", attr.MetadataContent);
            Aver.AreObjectsEqual("QWE", rec["Vertical_ID"]);

            fdef = rec.Schema["Table_ID"];
            Aver.IsNotNull(fdef);
            Aver.AreObjectsEqual("Table_ID", fdef.Name);
            Aver.AreObjectsEqual(typeof(long), fdef.Type);
            Aver.AreEqual(true, fdef.AnyTargetKey);
            Aver.IsNotNull(fdef.Attrs);
            Aver.AreEqual(1, fdef.Attrs.Count());
            attr = fdef.Attrs.First();
            Aver.AreObjectsEqual("Table", attr.Description);
            Aver.AreEqual(true, attr.Required);
            Aver.AreEqual(false, attr.Visible);
            Aver.AreObjectsEqual(1, attr.Min);
            Aver.AreObjectsEqual(123, attr.Max);
            Aver.AreObjectsEqual(15, attr.Default);
            Aver.IsTrue(DataKind.Number == attr.Kind);
            Aver.IsTrue(StoreFlag.LoadAndStore == attr.StoreFlag);
            Aver.AreEqual(0, attr.ParseValueList(true).Count);
            Aver.AreObjectsEqual("''{Name=Table_ID Type=int Key=True Description=Table Required=True Visible=False MinValue=1 MaxValue=123 DefaultValue=15 Kind=Number Stored=True}", attr.MetadataContent);
            Aver.AreObjectsEqual((long)2, rec["Table_ID"]);
        }