Пример #1
0
        public void ConstructRecordWithDupKey_WithinThresholdIgnoreDups_WarningLogged()
        {
            //This is really an integration test

            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("Fld1", "71941", _typeDefs),
                                      KeyValItem.CreateItem("Fld2", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("Fld1", 243, _typeDefs) }; //this field has the same key as the first field
            int recNo = 25;

            //act
            var logger = (MockLogger)_config.Logger;
            var rec    = new KeyValRecord(items, recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //assert
            //   Item1=severity, Item2=message (evaluated by logger), Item3=entire log entry (passed through by logger)
            logger.Results.Count.Should().Be(3); //incl. Log Title Box and "Configuration initialized." entry
            var result = logger.Results[2];

            result.Item1.Should().Be(LogEntrySeverity.Warning);
            result.Item2.Contains("Duplicate key in field #3 of record #25:").Should().BeTrue(); //text from KeyValRecord class
            result.Item2.Contains("Item containing value of '243' has been ignored.").Should().BeTrue();
            var entry = result.Item3;

            result.Item2.Should().Be(entry.MessageOnDemand()); //this confirms that timestamp in a message is from a time of LogInfo method
            entry.Severity.Should().Be(LogEntrySeverity.Warning);
            entry.MessageOnDemand.Should().BeOfType(typeof(Func <string>));
            entry.Exception.Should().BeNull();
            var msg = result.Item3.MessageOnDemand(); //evaluate message on demand one more time

            msg.Contains("Duplicate key in field #3 of record #25:").Should().BeTrue();
            msg.Contains("Item containing value of '243' has been ignored.").Should().BeTrue();
            msg.Should().Be(entry.MessageOnDemand()); //this confirms again that timestamp in a message is from a time of LogInfo method
        }
Пример #2
0
        public void Initialize()
        {
            _config = new OrchestratorConfig();
            _config.AllowTransformToAlterFields = true;
            _config.PropertyBinEntities         = PropertyBinAttachedTo.Clusters;

            // simple type definitions, everything string, except for fields starting with I_ (int)
            Func <string, ItemType> fldTypeFunc = key => key.StartsWith("I_") ? ItemType.Int : ItemType.String;
            var initFldTypes = new ConcurrentDictionary <string, ItemType>();
            Func <string, string> fldFormatFunc = key => string.Empty;
            var initFldFormats = new ConcurrentDictionary <string, string>();

            _typeDefs = new TypeDefinitions(fldTypeFunc, initFldTypes, fldFormatFunc, initFldFormats);

            var items1 = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                       KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                       KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            var items2 = new IItem[] { KeyValItem.CreateItem("I_#", 15, _typeDefs),
                                       KeyValItem.CreateItem("Fld1", "data1", _typeDefs) };

            var recs = new KeyValRecord[] { new KeyValRecord(items1, 16, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem),
                                            new KeyValRecord(items2, 17, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem) };

            _cluster = new KeyValCluster(recs, 9, 16, 1, null, new Dictionary <string, object>(), _typeDefs, _config, null); //clstr# 9 starting at rec# 16; AllowTransformToAlterFields is true
        }
Пример #3
0
        public void CreateX12Segment_NoElements_CorrectSegmentCreated()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            var rec = new KeyValRecord(items, 12, 3, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var x12Seg = rec.CreateEmptyX12Segment("LX", 0); //not really practical to have a segment with no elements

            //assert
            x12Seg.Should().NotBeSameAs(rec);
            x12Seg.Count.Should().Be(1); // 1 + 4
            x12Seg.RecNo.Should().Be(12);
            x12Seg.SourceNo.Should().Be(3);
            x12Seg.Keys.Count.Should().Be(1);
            x12Seg.Items.Count.Should().Be(1);

            x12Seg[0].Should().Be("LX");
            x12Seg["Segment"].Should().Be("LX");
            x12Seg.Keys[0].Should().Be("Segment");
            x12Seg[1].Should().BeNull();
            x12Seg["Elem001"].Should().BeNull();
            //x12Seg.Keys[5].Should().BeNull(); - this throws ArgumentOutOfRangeException
        }
Пример #4
0
        public void GetEmptyClone_DisallowAlterFields_SameRecNoButEmptyData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            _config.AllowTransformToAlterFields = false; //it was set to true in test Initialize method
            var rec = new KeyValRecord(items, 16, 7, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var clone  = rec.GetEmptyClone(); //note that this clone is unmaintainable (i.e. it will stay empty forever)
            var cloneR = (IRecord)clone;
            var cloneU = (IUntypedRecord)clone;

            //assert
            clone.Should().NotBeSameAs(rec);
            clone.Count.Should().Be(0);
            clone.RecNo.Should().Be(16);
            clone.SourceNo.Should().Be(7);
            clone.Keys.Count.Should().Be(0);
            clone.Items.Count.Should().Be(0);
            clone[0].Should().BeNull();
            clone["any"].Should().BeNull();
            cloneR[0].Should().BeNull();
            cloneR["any"].Should().BeNull();
            cloneU[0].Should().BeNull();
            cloneU["any"].Should().BeNull();
        }
Пример #5
0
        public void AssignValue_TypedValueSetToStringItem_ConvertedToString()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("blah", "origData", _typeDefs),
                                      KeyValItem.CreateItem("I_int", 33, _typeDefs) };
            var            rec  = new KeyValRecord(items, 16, 2, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var            recR = (IRecord)rec;
            IUntypedRecord recU = rec;
            dynamic        recD = rec;

            //act
            rec["blah"] = true;

            //assert
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(2);
            rec.Keys[0].Should().Be("blah");
            rec[0].Should().Be("True");

            //act2
            recR["blah"] = new DateTime(2050, 12, 1);

            //assert2
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(2);
            rec.Keys[0].Should().Be("blah");
            rec[0].Should().Be("12/1/2050 12:00:00 AM");
        }
Пример #6
0
        public void GetEmptyClone_SimpleValues_SameRecNoButEmptyData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            var rec = new KeyValRecord(items, 12, 11, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var     clone  = rec.GetEmptyClone();
            var     cloneR = (IRecord)clone; //this is no different, both clone and cloneR are of type IRecord
            var     cloneU = (IUntypedRecord)clone;
            dynamic cloneD = clone;          //n/a as there are no dynamic properties in this case

            //assert
            clone.Should().NotBeSameAs(rec);
            clone.Count.Should().Be(0);
            clone.RecNo.Should().Be(12);
            clone.SourceNo.Should().Be(11);
            clone.Keys.Count.Should().Be(0);
            clone.Items.Count.Should().Be(0);
            clone[0].Should().BeNull();
            clone["any"].Should().BeNull();
            cloneR[0].Should().BeNull();
            cloneR["any"].Should().BeNull();
            cloneU[0].Should().BeNull();
            cloneU["any"].Should().BeNull();
        }
Пример #7
0
        public void GetItemClone_NewTypedValue_CorrectData()
        {
            //arrange
            var item  = KeyValItem.CreateItem("I_fld", 156, _typeDefs);
            var items = new IItem[] { item };
            int recNo = 14;
            var rec   = new KeyValRecord(items, recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var clone = rec.GetItemClone(item, 3);

            //assert
            clone.Should().NotBeSameAs(item);
            clone.Should().BeOfType(typeof(KeyValItem <int>));
            clone.ItemDef.Type.Should().Be(ItemType.Int);
            clone.Key.Should().Be("I_fld");
            clone.Value.Should().Be(3);

            //act2
            clone = rec.GetItemClone(item, "4");

            //assert2
            clone.Should().NotBeSameAs(item);
            clone.Should().BeOfType(typeof(KeyValItem <int>));
            clone.ItemDef.Type.Should().Be(ItemType.Int);
            clone.Key.Should().Be("I_fld");
            clone.Value.Should().Be(4);
        }
Пример #8
0
        public void GetEmptyClone_DisallowAlterFields_EmptyData()
        {
            //arrange
            var items1 = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                       KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                       KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            var items2 = new IItem[] { KeyValItem.CreateItem("I_#", 15, _typeDefs),
                                       KeyValItem.CreateItem("Fld1", "data1", _typeDefs) };

            var recs = new KeyValRecord[] { new KeyValRecord(items1, 16, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem),
                                            new KeyValRecord(items2, 17, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem) };

            _config.AllowTransformToAlterFields = false;                                           //it was set to true in test Initialize method
            var cluster = new KeyValCluster(recs, 9, 16, 1, null, null, _typeDefs, _config, null); //clstr# 9 starting at rec# 16; AllowTransformToAlterFields is false

            //act
            var clone = cluster.GetEmptyClone();

            //assert
            clone.Count.Should().Be(0);
            clone.ClstrNo.Should().Be(9);
            clone.StartRecNo.Should().Be(16);

            clone.Records.Count.Should().Be(0);

            clone.GetRecord(0).Should().BeNull();
        }
        public void RoClstrCtor_SimpleValuesThrowOnMisuse_UnsupportedThrows()
        {
            //arrange
            var items1 = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                       KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                       KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            var items2 = new IItem[] { KeyValItem.CreateItem("I_#", 15, _typeDefs),
                                       KeyValItem.CreateItem("Fld1", "data1", _typeDefs) };

            var recs = new KeyValRecord[] { new KeyValRecord(items1, 16, 7, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem),
                                            new KeyValRecord(items2, 17, 8, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem) };

            //act
            var clstr = new KeyValCluster(recs, 9, 16, 78, null, null, _typeDefs, _config, null); //clstr# 9 starting at rec# 16; AllowTransformToAlterFields is true
                                                                                                  // note that startSourceNo 78 does not match the 1st record (7) - not a real scenario, but a good text
            var roClstr = new ReadOnlyCluster(clstr, true);                                       //this ctor causes objReadOnlyCluster to throw NotSupportedexception on misuse

            //assert
            roClstr.Count.Should().Be(2);
            roClstr.ClstrNo.Should().Be(9);
            roClstr.StartRecNo.Should().Be(16);
            roClstr.StartSourceNo.Should().Be(78);

            roClstr.Records.Count.Should().Be(2);

            Action a = () => { roClstr[0] = null; };

            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyCluster object.");
            roClstr[0].Should().NotBeNull();
            a = () => { roClstr.RemoveRecord(0); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyCluster object.");
            roClstr.Count.Should().Be(2);
            roClstr.Records.Count.Should().Be(2);
            a = () => { roClstr.AddRecord(null); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyCluster object.");
            roClstr.Count.Should().Be(2);
            roClstr.Records.Count.Should().Be(2);
            a = () => { roClstr.GetClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyCluster object.");
            a = () => { roClstr.GetEmptyClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyCluster object.");

            var roRec = roClstr[0];

            roRec.Should().BeOfType <ReadOnlyRecord>();
            a = () => { roRec.RemoveItem("IDCD_ID"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Count.Should().Be(3);

            roRec = roClstr[1];
            roRec.Should().BeOfType <ReadOnlyRecord>();
            a = () => { roRec.AddItem("a", "b"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Count.Should().Be(2);
        }
Пример #10
0
        public void RoClstrCtor_SimpleValuesIgnoreMisuse_CorrectData()
        {
            //arrange
            var items1 = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                       KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                       KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            var items2 = new IItem[] { KeyValItem.CreateItem("I_#", 15, _typeDefs),
                                       KeyValItem.CreateItem("Fld1", "data1", _typeDefs) };

            var recs = new KeyValRecord[] { new KeyValRecord(items1, 16, 7, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem),
                                            new KeyValRecord(items2, 17, 8, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem) };

            //act
            var clstr = new KeyValCluster(recs, 9, 16, 78, null, null, _typeDefs, _config, null); //clstr# 9 starting at rec# 16; AllowTransformToAlterFields is true
                                                                                                  // note that startSourceNo 78 does not match the 1st record (7) - not a real scenario, but a good text
            var roClstr = new ReadOnlyCluster(clstr);                                             // this ctor causes objReadOnlyCluster to ignore misuse

            //assert
            roClstr.Count.Should().Be(2);
            roClstr.ClstrNo.Should().Be(9);
            roClstr.StartRecNo.Should().Be(16);
            roClstr.StartSourceNo.Should().Be(78);

            roClstr.Records.Count.Should().Be(2);

            var roRec = roClstr[0];

            roRec.Should().BeOfType <ReadOnlyRecord>();
            roRec.Count.Should().Be(3);
            roRec.RecNo.Should().Be(16);
            roRec.SourceNo.Should().Be(7);
            roRec.Items[0].Key.Should().Be("IDCD_ID");
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec[1].Should().Be("blahblah");
            roRec["blah"].Should().Be("blahblah");
            roRec[2].Should().Be(243);
            roRec["I_num"].Should().Be(243);
            roClstr.GetRecord(0).Should().Be(roRec);

            roRec = roClstr[1];
            roRec.Should().BeOfType <ReadOnlyRecord>();
            roRec.Count.Should().Be(2);
            roRec.RecNo.Should().Be(17);
            roRec.SourceNo.Should().Be(8);
            roRec.Keys[0].Should().Be("I_#");
            roRec[0].Should().Be(15);
            roRec["I_#"].Should().Be(15);
            roRec.Keys[1].Should().Be("Fld1");
            roRec["Fld1"].Should().Be("data1");
            roClstr.GetRecord(1).Should().Be(roRec);
        }
Пример #11
0
        public void Clone_SimpleValues_SameData()
        {
            //arrange
            var items1 = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                       KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                       KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            var items2 = new IItem[] { KeyValItem.CreateItem("I_#", 15, _typeDefs),
                                       KeyValItem.CreateItem("Fld1", "data1", _typeDefs) };

            var recs = new KeyValRecord[] { new KeyValRecord(items1, 16, 7, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem),
                                            new KeyValRecord(items2, 17, 8, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem) };

            //act
            var clstr = new KeyValCluster(recs, 9, 16, 78, null, null, _typeDefs, _config, null); //clstr# 9 starting at rec# 16; AllowTransformToAlterFields is true
                                                                                                  // note that startSourceNo 78 does not match the 1st record (7) - not a real scenario, but a good text
            var clone = clstr.GetClone();

            //assert
            clone.Should().NotBeSameAs(clstr);
            clone.Count.Should().Be(2);
            clone.ClstrNo.Should().Be(9);
            clone.StartRecNo.Should().Be(16);
            clone.StartSourceNo.Should().Be(78);

            var rec = clone[0];

            rec.Count.Should().Be(3);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(7);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.Items[0].Key.Should().Be("IDCD_ID");
            rec.GetItem(0).Key.Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);

            rec = clone[1];
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(17);
            rec.SourceNo.Should().Be(8);
            rec.Keys[0].Should().Be("I_#");
            rec[0].Should().Be(15);
            rec.GetItem("I_#").Key.Should().Be("I_#");
            rec.Keys[1].Should().Be("Fld1");
            rec["Fld1"].Should().Be("data1");
        }
Пример #12
0
        public void GetClone_SimpleValues_SameData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            var rec = new KeyValRecord(items, 15, 4, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var clone = rec.GetClone();

            //assert
            clone.Should().NotBeSameAs(rec);
            clone.Count.Should().Be(3);
            clone.RecNo.Should().Be(15);
            clone.SourceNo.Should().Be(4);
            clone.Keys[0].Should().Be("IDCD_ID");
            clone.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            clone[1].Should().Be("blahblah");
            clone["blah"].Should().Be("blahblah");
            clone[2].Should().Be(243);
            clone["I_num"].Should().Be(243);

            var keys = clone.Keys.ToList();

            keys.Count.Should().Be(3);
            keys[0].Should().Be("IDCD_ID");
            keys[1].Should().Be("blah");
            keys[2].Should().Be("I_num");

            var itms = clone.Items.ToList();

            itms.Count.Should().Be(3);
            itms[0].Key.Should().Be("IDCD_ID");
            itms[0].Value.Should().BeOfType(typeof(string));
            itms[0].Value.Should().Be("71941");
            itms[0].ItemDef.Type.Should().Be(ItemType.String);
            itms[1].Key.Should().Be("blah");
            itms[1].Value.Should().BeOfType(typeof(string));
            itms[1].Value.Should().Be("blahblah");
            itms[1].ItemDef.Type.Should().Be(ItemType.String);
            itms[2].Key.Should().Be("I_num");
            itms[2].Value.Should().BeOfType(typeof(int));
            itms[2].Value.Should().Be(243);
            itms[2].ItemDef.Type.Should().Be(ItemType.Int);
        }
Пример #13
0
        public void ConstructRecordWithDupKey_OutsideOfThreshold_NothingLogged()
        {
            //arrange
            var config = new OrchestratorConfig(new MockLogger(LogEntrySeverity.Error)); //Warning (sent as a result of dup) is outside of this threshold

            var items = new IItem[] { KeyValItem.CreateItem("Fld1", "71941", _typeDefs),
                                      KeyValItem.CreateItem("Fld2", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("Fld1", 243, _typeDefs) }; //this field has the same key as the first field
            int recNo = 25;

            //act
            var logger = (MockLogger)config.Logger;
            var rec    = new KeyValRecord(items, recNo, 1, 0, null, null, null, _typeDefs, config, null, ActionOnDuplicateKey.IgnoreItem);

            //assert
            logger.Results.Count.Should().Be(1); //Only Log Title
            logger.Results[0].Item1.Should().Be(LogEntrySeverity.None);
        }
Пример #14
0
        public void GetItemClone_NewStringValue_CorrectData()
        {
            //arrange
            IItem item  = KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs);
            var   items = new IItem[] { item };
            int   recNo = 14;
            var   rec   = new KeyValRecord(items, recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var clone = rec.GetItemClone(item, "newValue");

            //assert
            clone.Should().NotBeSameAs(item);
            clone.Should().BeOfType(typeof(KeyValItem <string>));
            clone.ItemDef.Type.Should().Be(ItemType.String);
            clone.Key.Should().Be("IDCD_ID");
            clone.Value.Should().Be("newValue");
        }
Пример #15
0
 /// <summary>
 /// Helper method to create a new item and also update a list of field names (FieldsInUse).
 /// This overload is intended for use when value is alredy of the intended type (e.g. in case of JSON).
 /// </summary>
 /// <param name="key"></param>
 /// <param name="val"></param>
 /// <param name="typeDefinitons"></param>
 /// <returns>The item just created or null if item excluded (this happens in case key was new and new fields disallowed).</returns>
 protected IItem CreateItemAndMarkField(string key, object val, TypeDefinitions typeDefinitons)
 {
     //side-effect, i.e. update FieldsInUse
     //update only if key not yet in the list
     if (_newFieldsAllowed)
     {
         IncludeField(key);
         return(KeyValItem.CreateItem(key, val, typeDefinitons));
     }
     else //adding new fields to FieldsInUse is disallowed
     {
         if (FieldsInUse.Contains(key))
         {
             return(KeyValItem.CreateItem(key, val, typeDefinitons));
         }
         return(null); //item excluded
     }
 }
Пример #16
0
        public void GetItemClone_NoChange_SameData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            //act
            var rec = new KeyValRecord(items, 16, 3, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            var clone = rec.GetItemClone(items[0], items[0].Value); //not a practical example to create exact clone of an item(!)

            //assert
            clone.Should().NotBeSameAs(items[0]); //note that this assertion may become invalid (KeyValItem is immutable)
            clone.Should().BeOfType(typeof(KeyValItem <string>));
            clone.ItemDef.Type.Should().Be(ItemType.String);
            clone.Key.Should().Be("IDCD_ID");
            clone.Value.Should().Be("71941");
        }
Пример #17
0
        public void Initialize()
        {
            _config = new OrchestratorConfig();
            _config.AllowTransformToAlterFields = true;
            _config.PropertyBinEntities         = PropertyBinAttachedTo.Records;

            // simple type definitions, everything string, except for fields starting with I_ (int) & D_ (DateTime
            Func <string, ItemType> fldTypeFunc = key => key.StartsWith("I_") ? ItemType.Int : key.StartsWith("D_") ? ItemType.DateTime : ItemType.String;
            var initFldTypes = new ConcurrentDictionary <string, ItemType>();
            Func <string, string> fldFormatFunc = key => string.Empty;
            var initFldFormats = new ConcurrentDictionary <string, string>();

            _typeDefs = new TypeDefinitions(fldTypeFunc, initFldTypes, fldFormatFunc, initFldFormats);
            _items    = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("D_date", new DateTime(2012, 12, 28), _typeDefs),
                                      KeyValItem.CreateItem("I_num", 184, _typeDefs) };
            _recNo = 26;
        }
Пример #18
0
        public void CreateX12Segment_4Elements_CorrectSegmentCreated()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            var rec = new KeyValRecord(items, 12, 3, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);

            //act
            var x12Seg = rec.CreateEmptyX12Segment("NM1", 4);

            x12Seg[2] = null;
            x12Seg[4] = "blah";

            //assert
            x12Seg.Should().NotBeSameAs(rec);
            x12Seg.Count.Should().Be(5); // 1 + 4
            x12Seg.RecNo.Should().Be(12);
            x12Seg.SourceNo.Should().Be(3);
            x12Seg.Keys.Count.Should().Be(5);
            x12Seg.Items.Count.Should().Be(5);

            x12Seg[0].Should().Be("NM1");
            x12Seg["Segment"].Should().Be("NM1");
            x12Seg.Keys[0].Should().Be("Segment");
            x12Seg[1].Should().Be(string.Empty);
            x12Seg["Elem001"].Should().Be(string.Empty);
            x12Seg.Keys[1].Should().Be("Elem001");
            x12Seg[2].Should().BeNull();
            x12Seg["Elem002"].Should().BeNull();
            x12Seg.Keys[2].Should().Be("Elem002");
            x12Seg[3].Should().Be(string.Empty);
            x12Seg["Elem003"].Should().Be(string.Empty);
            x12Seg.Keys[3].Should().Be("Elem003");
            x12Seg[4].Should().Be("blah");
            x12Seg["Elem004"].Should().Be("blah");
            x12Seg.Keys[4].Should().Be("Elem004");
            x12Seg[5].Should().BeNull();
            x12Seg["Elem005"].Should().BeNull();
            //x12Seg.Keys[5].Should().BeNull(); - this throws ArgumentOutOfRangeException
        }
Пример #19
0
        public void AddOrReplaceItem_NewKeyButCannotAlterFields_ReturnNull()
        {
            //arrange
            _config.AllowTransformToAlterFields = false; //it was set to true in test Initialize method
            IRecord rec       = new KeyValRecord(_items, _recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var     itemToAdd = KeyValItem.CreateItem("I_newNum", 518, _typeDefs);

            //act
            var result = rec.AddOrReplaceItem(itemToAdd);

            //assert
            result.Should().NotHaveValue();

            rec.Count.Should().Be(4);
            rec.RecNo.Should().Be(26);

            rec[0].Should().BeOfType(typeof(string));
            rec[0].Should().Be("71941");
            rec[3].Should().BeOfType(typeof(int));
            rec[3].Should().Be(184);
        }
Пример #20
0
        public void AddOrReplaceItem_ExistingKey_ReplaceAndReturnsTrue()
        {
            //arrange
            IUntypedRecord rec           = new KeyValRecord(_items, _recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var            itemToReplace = KeyValItem.CreateItem("I_num", 518, _typeDefs);

            //act
            var result = rec.AddOrReplaceItem(itemToReplace);

            //assert
            result.Should().BeTrue();

            rec.Count.Should().Be(4);
            rec.RecNo.Should().Be(26);

            rec[0].Should().BeOfType(typeof(string));
            rec[0].Should().Be("71941");
            rec[3].Should().BeOfType(typeof(string));
            rec[3].Should().Be("518");
            rec.Keys[3].Should().Be("I_num");
        }
Пример #21
0
        public void AssignValue_BadValueSetToTypedField_DefaultData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("blah", "origData", _typeDefs),
                                      KeyValItem.CreateItem("I_int", 33, _typeDefs) };
            var     rec  = new KeyValRecord(items, 16, 2, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var     recR = (IRecord)rec;
            dynamic recD = rec;

            //act
            rec["I_int"] = true; //type mismatch

            //assert
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(2);
            rec.Keys[1].Should().Be("I_int");
            rec[1].Should().Be(0); //default(int)

            //act2
            rec["I_int"] = 34; //fix back to a non-default value

            //assert2
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(2);
            rec.Keys[1].Should().Be("I_int");
            rec[1].Should().Be(34);

            //act3
            rec["I_int"] = "35nonNum"; //bad string value

            //assert3
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(16);
            rec.SourceNo.Should().Be(2);
            rec.Keys[1].Should().Be("I_int");
            rec[1].Should().Be(0); //default(int)
        }
Пример #22
0
        public void CreateItem_StringNullValue_CorrectData()
        {
            //arrange
            // simple type definitions, everything string
            Func <string, ItemType> fldTypeFunc = key => ItemType.String;;
            var initFldTypes = new ConcurrentDictionary <string, ItemType>();
            Func <string, string> fldFormatFunc = key => string.Empty;
            var initFldFormats = new ConcurrentDictionary <string, string>();
            var typeDefs       = new TypeDefinitions(fldTypeFunc, initFldTypes, fldFormatFunc, initFldFormats);

            //act
            var item = KeyValItem.CreateItem("IDCD_ID", null, typeDefs);

            //item is of IItem type

            //assert
            item.Should().BeOfType(typeof(KeyValItem <string>));
            item.ItemDef.Type.Should().Be(ItemType.String);
            item.Key.Should().Be("IDCD_ID");
            item.Value.Should().BeNull();
            item.StringValue.Should().BeNull();
        }
Пример #23
0
        public void AddOrReplaceItem_NewKey_AddAndReturnFalse()
        {
            //arrange
            IUntypedRecord rec       = new KeyValRecord(_items, _recNo, 1, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var            itemToAdd = KeyValItem.CreateItem("I_newNum", 518, _typeDefs);

            //act
            var result = rec.AddOrReplaceItem(itemToAdd);

            //assert
            result.Should().BeFalse();

            rec.Count.Should().Be(5);
            rec.RecNo.Should().Be(26);

            rec[0].Should().BeOfType(typeof(string));
            rec[0].Should().Be("71941");
            rec[3].Should().BeOfType(typeof(string));
            rec[3].Should().Be("184");
            rec[4].Should().BeOfType(typeof(string));
            rec[4].Should().Be("518");
            rec.Keys[4].Should().Be("I_newNum");
        }
Пример #24
0
        public void ReplaceItem_ExistingKey_ChangeAccepted()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("blah", "origData", _typeDefs),
                                      KeyValItem.CreateItem("I_int", 33, _typeDefs) };
            var            rec  = new KeyValRecord(items, 20, 10, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var            recR = (IRecord)rec;
            IUntypedRecord recU = rec;
            dynamic        recD = rec;

            //act
            rec.ReplaceItem(KeyValItem.CreateItem("blah", "66", _typeDefs));
            rec.ReplaceItem(KeyValItem.CreateItem("I_int", "badData", _typeDefs));

            //assert
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(20);
            rec.SourceNo.Should().Be(10);
            rec.Keys[0].Should().Be("blah");
            rec[0].Should().Be("66");
            rec.Keys[1].Should().Be("I_int");
            rec[1].Should().Be(0); //default(int) as a result of "badData"
        }
Пример #25
0
        public void ReplaceItem_NonExistingKey_ChangeRejected()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("blah", "origData", _typeDefs),
                                      KeyValItem.CreateItem("I_int", 33, _typeDefs) };
            var            rec  = new KeyValRecord(items, 22, 11, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var            recR = (IRecord)rec;
            IUntypedRecord recU = rec;
            dynamic        recD = rec;

            //act
            rec.ReplaceItem(KeyValItem.CreateItem("I_blah", "66", _typeDefs));
            rec.ReplaceItem(KeyValItem.CreateItem("I_newInt", "badData", _typeDefs));

            //assert
            rec.Count.Should().Be(2);
            rec.RecNo.Should().Be(22);
            rec.SourceNo.Should().Be(11);
            rec.Keys[0].Should().Be("blah");
            rec[0].Should().Be("origData");
            rec.Keys[1].Should().Be("I_int");
            rec[1].Should().Be(33);
        }
Пример #26
0
        public void RoRecCtor_SimpleValuesIgnoreMisuse_CorrectData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            int recNo    = 76;
            int sourceNo = 3;

            //act
            var rec   = new KeyValRecord(items, recNo, sourceNo, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var roRec = new ReadOnlyRecord(rec); //this ctor ignores misuse

            //assert
            roRec.Count.Should().Be(3);
            roRec.RecNo.Should().Be(recNo);
            roRec.SourceNo.Should().Be(sourceNo);
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            roRec[1].Should().Be("blahblah");
            roRec["blah"].Should().Be("blahblah");
            roRec[2].Should().Be(243);
            roRec["I_num"].Should().Be(243);

            roRec.GetItemClone(items[0], "someNewValue").Should().BeOfType(typeof(KeyValItem <string>));

            // indexers from IRecord & IUntypedRecord interfaces:
            IRecord roRecR = roRec;
            var     roRecU = (IUntypedRecord)roRec;

            roRecR[0].Should().Be("71941");
            roRecU[0].Should().Be("71941");
            roRecR["IDCD_ID"].Should().Be("71941");
            roRecU["IDCD_ID"].Should().Be("71941");
            roRecR[2].Should().Be(243);
            roRecU[2].Should().Be("243");
            roRecR["I_num"].Should().Be(243);
            roRecU["I_num"].Should().Be("243");

            roRecU.GetItemClone(items[0], "someNewValue").Should().BeOfType(typeof(KeyValItem <string>));

            // dynamic properties:
            dynamic roRecD = roRec;

            Assert.AreEqual("71941", roRecD.IDCD_ID); //FluentAssertions don't work with dynamic properties, e.g.'string' does not contain a definition for 'Should'
            Assert.AreEqual("blahblah", roRecD.blah);
            Assert.AreEqual(243, roRecD.I_num);
            Assert.IsNull(roRecD.BadKey);

            Assert.IsInstanceOfType(roRecD.GetItemClone(items[0], "someNewValue"), typeof(KeyValItem <string>));

            var keys = roRec.Keys.ToList();

            keys.Count.Should().Be(3);
            keys[0].Should().Be("IDCD_ID");
            keys[1].Should().Be("blah");
            keys[2].Should().Be("I_num");

            var itms = roRec.Items.ToList();

            itms.Count.Should().Be(3);
            itms[0].Key.Should().Be("IDCD_ID");
            itms[0].Value.Should().BeOfType(typeof(string));
            itms[0].Value.Should().Be("71941");
            itms[0].ItemDef.Type.Should().Be(ItemType.String);
            itms[1].Key.Should().Be("blah");
            itms[1].Value.Should().BeOfType(typeof(string));
            itms[1].Value.Should().Be("blahblah");
            itms[1].ItemDef.Type.Should().Be(ItemType.String);
            itms[2].Key.Should().Be("I_num");
            itms[2].Value.Should().BeOfType(typeof(int));
            itms[2].Value.Should().Be(243);
            itms[2].ItemDef.Type.Should().Be(ItemType.Int);
        }
Пример #27
0
        public void RoRecCtor_SimpleValuesThrowOnMisuse_UnsupportedThrows()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            int recNo    = 76;
            int sourceNo = 3;

            //act
            var rec   = new KeyValRecord(items, recNo, sourceNo, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var roRec = new ReadOnlyRecord(rec, true); //this ctor causes misuse to throw NotSupportedException

            //assert
            roRec.Count.Should().Be(3);
            roRec.RecNo.Should().Be(recNo);
            roRec.SourceNo.Should().Be(sourceNo);

            //Not supported calls (misuse) throw NotSupportedException:
            Action a = () => { roRec.RemoveItem("IDCD_ID"); };

            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec.RemoveItem("NonExistingKey"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            a = () => { roRec.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            a = () => { roRec.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.Count.Should().Be(3);

            a = () => { roRec["IDCD_ID"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec["NonExistingKey"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec[0] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec[20] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.Count.Should().Be(3);
            a = () => { roRec.AddItem("IDCD_ID", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec.AddItem("NonExistingKey", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRec.Count.Should().Be(3);
            a = () => { roRec.GetClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRec.GetEmptyClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");

            // ReadOnlyRecord as IRecord:
            IRecord roRecR = roRec;

            a = () => { roRecR.RemoveItem("IDCD_ID"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR.RemoveItem("NonExistingKey"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            a = () => { roRecR.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            a = () => { roRecR.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.Count.Should().Be(3);

            a = () => { roRecR["IDCD_ID"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR["NonExistingKey"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR[0] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR[20] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.Count.Should().Be(3);

            a = () => { roRecR.AddItem("IDCD_ID", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR.AddItem("NonExistingKey", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecR.Count.Should().Be(3);
            a = () => { roRecR.GetClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecR.GetEmptyClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");


            // ReadOnlyRecord as IUntypedRecord:
            var roRecU = (IUntypedRecord)roRec;

            a = () => { roRecU.RemoveItem("IDCD_ID"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU.RemoveItem("NonExistingKey"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            a = () => { roRecU.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            a = () => { roRecU.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.Count.Should().Be(3);

            a = () => { roRecU["IDCD_ID"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU["NonExistingKey"] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU[0] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU[20] = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.Count.Should().Be(3);
            a = () => { roRecU.AddItem("IDCD_ID", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU.AddItem("NonExistingKey", "1000"); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            roRecU.Count.Should().Be(3);
            a = () => { roRecU.GetClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            a = () => { roRecU.GetEmptyClone(); };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");

            // dynamic properties:
            dynamic roRecD = roRec;

            a = () => { roRecD.IDCD_ID = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            Assert.AreEqual("71941", roRecD.IDCD_ID); //FluentAssertions don't work with dynamic properties, e.g.'string' does not contain a definition for 'Should'
            a = () => { roRecD.blah = "somethingNew"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            Assert.AreEqual("blahblah", roRecD.blah);
            a = () => { roRecD.I_num = 5; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            //Assert.AreEqual(243, roRecD.I_num);
            a = () => { roRecD.NonExisting = "something"; };
            a.Should().Throw <NotSupportedException>().WithMessage("Unsupported operation invoked on ReadOnlyRecord object.");
            Assert.IsNull(roRecD.NonExisting);
            Assert.AreEqual(3, roRecD.Count);
        }
Пример #28
0
        public void AddItem_SimpleValues_CorrectData()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            var            rec  = new KeyValRecord(items, 18, 12, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            IRecord        recR = rec;
            IUntypedRecord recU = rec;
            dynamic        recD = rec;

            //act
            var result = rec.AddItem("I_alsoNum", 120);

            //assert
            result.Should().BeOfType <KeyValItem <int> >();
            result.Key.Should().Be("I_alsoNum");
            result.Value.Should().Be(120);
            rec.Count.Should().Be(4);
            rec.RecNo.Should().Be(18);
            rec.SourceNo.Should().Be(12);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);
            rec[3].Should().Be(120);
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            Assert.AreEqual(120, recD.I_alsoNum);


            //act2
            rec.AddItem("foo", "bar");

            //assert2
            rec.Count.Should().Be(5);
            rec.RecNo.Should().Be(18);
            rec.SourceNo.Should().Be(12);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);
            rec[3].Should().Be(120);
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            Assert.AreEqual(120, recD.I_alsoNum);
            rec[4].Should().Be("bar");
            recR[4].Should().Be("bar");
            recU["foo"].Should().Be("bar");
            recR[4].Should().Be("bar");
            recU["foo"].Should().Be("bar");
            Assert.AreEqual("bar", recD.foo);

            //act3
            recD.I_NUMBER = 1356;

            //assert3
            rec.Count.Should().Be(6);
            rec.RecNo.Should().Be(18);
            rec.SourceNo.Should().Be(12);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);
            rec[3].Should().Be(120);
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            recR[3].Should().Be(120);
            recU["I_alsoNum"].Should().Be("120");
            Assert.AreEqual(120, recD.I_alsoNum);
            rec[4].Should().Be("bar");
            recR[4].Should().Be("bar");
            recU["foo"].Should().Be("bar");
            recR[4].Should().Be("bar");
            recU["foo"].Should().Be("bar");
            Assert.AreEqual("bar", recD.foo);
            rec[5].Should().Be(1356);
            recR[5].Should().Be(1356);
            recU["I_NUMBER"].Should().Be("1356");
            recR[5].Should().Be(1356);
            recU["I_NUMBER"].Should().Be("1356");
            Assert.AreEqual(1356, recD.I_NUMBER);
        }
Пример #29
0
        public void RoRecCtor_SimpleValuesIgnoreMisuse_UnsupportedGetsIgnored()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };
            int recNo    = 76;
            int sourceNo = 3;

            //act
            var rec   = new KeyValRecord(items, recNo, sourceNo, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            var roRec = new ReadOnlyRecord(rec); //this ctor ignores misuse

            //assert
            roRec.Count.Should().Be(3);
            roRec.RecNo.Should().Be(recNo);
            roRec.SourceNo.Should().Be(sourceNo);

            //Not supported calls (misuse) - take default action (no action), do not throw:
            roRec.RemoveItem("IDCD_ID").Should().Be(null);
            roRec.RemoveItem("NonExistingKey").Should().Be(null);
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().BeFalse();
            roRec.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().BeFalse();
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().Be(null);
            roRec.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().Be(null);
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.Count.Should().Be(3);

            roRec["IDCD_ID"]        = "somethingNew"; //this is simply ignored
            roRec["NonExistingKey"] = "somethingNew"; //this is simply ignored
            roRec[0]  = "somethingNew";               //this is simply ignored
            roRec[20] = "somethingNew";               //index out of range, still ignored
            roRec.Keys[0].Should().Be("IDCD_ID");
            roRec[0].Should().Be("71941");
            roRec.Count.Should().Be(3);
            roRec.AddItem("IDCD_ID", "1000").Should().BeNull();
            roRec.AddItem("NonExistingKey", "1000").Should().BeNull();
            roRec.Count.Should().Be(3);
            roRec.GetClone().Should().BeNull();
            roRec.GetEmptyClone().Should().BeNull();

            // ReadOnlyRecord as IRecord:
            IRecord roRecR = roRec;

            roRecR.RemoveItem("IDCD_ID").Should().Be(null);
            roRecR.RemoveItem("NonExistingKey").Should().Be(null);
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().BeFalse();
            roRecR.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().BeFalse();
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().Be(null);
            roRecR.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().Be(null);
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.Count.Should().Be(3);

            roRecR["IDCD_ID"]        = "somethingNew"; //this is simply ignored
            roRecR["NonExistingKey"] = "somethingNew"; //this is simply ignored
            roRecR[0]  = "somethingNew";               //this is simply ignored
            roRecR[20] = "somethingNew";               //index out of range, still ignored
            roRecR.Keys[0].Should().Be("IDCD_ID");
            roRecR[0].Should().Be("71941");
            roRecR.Count.Should().Be(3);
            roRecR.AddItem("IDCD_ID", "1000").Should().BeNull();
            roRecR.AddItem("NonExistingKey", "1000").Should().BeNull();
            roRecR.Count.Should().Be(3);
            roRecR.GetClone().Should().BeNull();
            roRecR.GetEmptyClone().Should().BeNull();


            // ReadOnlyRecord as IUntypedRecord:
            var roRecU = (IUntypedRecord)roRec;

            roRecU.RemoveItem("IDCD_ID").Should().Be(null);
            roRecU.RemoveItem("NonExistingKey").Should().Be(null);
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.ReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().BeFalse();
            roRecU.ReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().BeFalse();
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.AddOrReplaceItem(KeyValItem.CreateItem("IDCD_ID", "1000", _typeDefs)).Should().Be(null);
            roRecU.AddOrReplaceItem(KeyValItem.CreateItem("NonExistingKey", "1000", _typeDefs)).Should().Be(null);
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.Count.Should().Be(3);

            roRecU["IDCD_ID"]        = "somethingNew"; //this is simply ignored
            roRecU["NonExistingKey"] = "somethingNew"; //this is simply ignored
            roRecU[0]  = "somethingNew";               //this is simply ignored
            roRecU[20] = "somethingNew";               //index out of range, still ignored
            roRecU.Keys[0].Should().Be("IDCD_ID");
            roRecU[0].Should().Be("71941");
            roRecU.Count.Should().Be(3);
            roRecU.AddItem("IDCD_ID", "1000").Should().BeNull();
            roRecU.AddItem("NonExistingKey", "1000").Should().BeNull();
            roRecU.Count.Should().Be(3);
            roRecU.GetClone().Should().BeNull();
            roRecU.GetEmptyClone().Should().BeNull();

            // dynamic properties:
            dynamic roRecD = roRec;

            roRecD.IDCD_ID = "somethingNew";          //this is simply ignored
            Assert.AreEqual("71941", roRecD.IDCD_ID); //FluentAssertions don't work with dynamic properties, e.g.'string' does not contain a definition for 'Should'
            roRecD.blah = "somethingNew";
            Assert.AreEqual("blahblah", roRecD.blah);
            roRecD.I_num = 5;
            Assert.AreEqual(243, roRecD.I_num);
            roRecD.NonExisting = "something";
            Assert.IsNull(roRecD.NonExisting);
            Assert.AreEqual(3, roRecD.Count);
        }
Пример #30
0
        public void AddItem_SimpleValuesButCannotAlterFields_NoChange()
        {
            //arrange
            var items = new IItem[] { KeyValItem.CreateItem("IDCD_ID", "71941", _typeDefs),
                                      KeyValItem.CreateItem("blah", "blahblah", _typeDefs),
                                      KeyValItem.CreateItem("I_num", 243, _typeDefs) };

            _config.AllowTransformToAlterFields = false; //it was set to true in test Initialize method
            var            rec  = new KeyValRecord(items, 19, 13, 0, null, null, null, _typeDefs, _config, null, ActionOnDuplicateKey.IgnoreItem);
            IRecord        recR = rec;
            IUntypedRecord recU = rec;
            dynamic        recD = rec;

            //act
            var result = rec.AddItem("I_alsoNum", 120);

            //assert
            result.Should().BeNull();
            rec.Count.Should().Be(3);
            rec.RecNo.Should().Be(19);
            rec.SourceNo.Should().Be(13);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);

            //act2
            result = rec.AddItem("foo", "bar");

            //assert2
            result.Should().BeNull();
            rec.Count.Should().Be(3);
            rec.RecNo.Should().Be(19);
            rec.SourceNo.Should().Be(13);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);


            //act3
            recD.I_NUMBER = 1356; //attempt to set a value of non-existing item is simply ignored(!) - this behavior may be revised

            //assert3
            Assert.IsNull(recD.I_NUMBER);
            rec.Count.Should().Be(3);
            rec.RecNo.Should().Be(19);
            rec.SourceNo.Should().Be(13);
            rec.Keys[0].Should().Be("IDCD_ID");
            rec.GetItem("IDCD_ID").Key.Should().Be("IDCD_ID");
            recR[0].Should().Be("71941");
            recU[0].Should().Be("71941");
            Assert.AreEqual("71941", recD.IDCD_ID);
            rec[1].Should().Be("blahblah");
            rec["blah"].Should().Be("blahblah");
            Assert.AreEqual("blahblah", recD.blah);
            rec[2].Should().Be(243);
            rec["I_num"].Should().Be(243);
            recR["I_num"].Should().Be(243);
            recU["I_num"].Should().Be("243");
            Assert.AreEqual(243, recD.I_num);
        }