Beispiel #1
0
        /// <summary>
        /// Execute transformer function and if needed update fields in use
        /// </summary>
        /// <param name="cluster">Cluster to transform</param>
        /// <returns></returns>
        internal IEnumerable <KeyValCluster> TransformAndSetFields(KeyValCluster cluster)
        {
            var clustersToReturn = Transform(cluster);

            if (!_config.AllowTransformToAlterFields)
            {
                return(clustersToReturn);
            }

            //Here, we need to merge fields into FieldsInUse
            //clustersToReturn.ForEach.. would come in handy if it existed on IEnumerable :-(
            foreach (var clstr in clustersToReturn)
            {
                foreach (var rec in clstr.Records)
                {
                    //TODO: Verify performance of this locking mechanism (consider changing its granularity)
                    //      also, consider other thread synchronization methods (ConcurrentBag? instead of List)
                    lock (_locker)
                    {
                        MergeFields(rec.Keys);
                    }
                }
            }

            return(clustersToReturn);
        }
Beispiel #2
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);
        }
        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);
        }
 /// <summary>
 /// Helper function to determine if cluster contains at least one record with NAME=Susan
 /// </summary>
 /// <param name="cluster"></param>
 /// <returns>true if cluster contains such record; false otherwise</returns>
 private bool RejectSusan(KeyValCluster cluster)
 {
     foreach (var rec in cluster.Records)
     {
         //verify, if the record contains NAME=Susan
         if ((string)rec["NAME"] == "Susan")
         {
             return(false);
         }
     }
     //none of the records contains NAME=Susan
     return(true);
 }
        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");
        }
        public void ObtainEmptyRecord_ThenCreateX12Segment_CorrectData()
        {
            //arrange
            var clstr = new KeyValCluster(new List <KeyValRecord>(), 0, 0, 1, null, null, _typeDefs, _config, null);

            //act
            var seg = clstr.ObtainEmptyRecord().CreateEmptyX12Segment("GS", 8);

            //assert
            clstr.Count.Should().Be(0); //still no records in cluster
            seg.Should().BeOfType <KeyValRecord>();
            seg.RecNo.Should().Be(0);
            seg.SourceNo.Should().Be(1);
            seg.Count.Should().Be(9); //8 + 1
            seg.Keys[0].Should().Be("Segment");
            seg[0].Should().Be("GS");
        }
Beispiel #8
0
        /// <summary>
        /// Helper function to add extra record (to the cluster) for every record with NAME=J** (i.e. name starting with J)
        /// Extra records will be copies of those records with NAME=J**, except for NAME prefixed by "Dup_of_" and NUM item removed
        /// </summary>
        /// <param name="cluster"></param>
        /// <returns>A copy of the original cluster plus possible extra records</returns>
        private KeyValCluster DupRecsForJ(ICluster cluster)
        {
            KeyValCluster clstrToReturn = (KeyValCluster)cluster.GetClone(); //need to clone in order to manipulate

            foreach (var rec in cluster.Records)
            {
                //verify, if the record contains NAME=J**
                if (rec.ContainsKey("NAME") && ((string)rec["NAME"]).StartsWith("J"))
                {
                    dynamic newRec = rec.GetClone();
                    newRec.NAME = "Dup_of_" + (string)rec["NAME"];
                    newRec.RemoveItem("NUM");
                    clstrToReturn.AddRecord(newRec);
                }
            }

            return(clstrToReturn);
        }
        public void ObtainEmptyRecord_ThenCreateX12SegmentWithContents_CorrectData()
        {
            //arrange
            var clstr = new KeyValCluster(new List <KeyValRecord>(), 0, 0, 1, null, null, _typeDefs, _config, null);
            var eRec  = clstr.ObtainEmptyRecord();

            //act
            var seg = eRec.CreateFilledX12Segment("NM1*1P*1*GARDENER*JAMES****46*8189991234");

            //note that if undefined in config (or passed as a 2nd parm), CreateX12Segment uses * as field delimiter

            //assert
            clstr.Count.Should().Be(0); //still no records in cluster
            seg.Should().BeOfType <KeyValRecord>();
            seg.RecNo.Should().Be(0);
            seg.SourceNo.Should().Be(1);
            seg.Count.Should().Be(10);
            seg.Keys[0].Should().Be("Segment");
            seg[0].Should().Be("NM1");
            seg.Keys[1].Should().Be("Elem001");
            seg[1].Should().Be("1P");
            seg.Keys[6].Should().Be("Elem006");
            seg[6].Should().Be(string.Empty);
            seg.Keys[9].Should().Be("Elem009");
            seg[9].Should().Be("8189991234");

            //act2
            seg = eRec.CreateFilledX12Segment("HL^2^1^21^1", '^'); // here, explicit field delimiter

            //assert2
            clstr.Count.Should().Be(0); //still no records in cluster
            seg.Should().BeOfType <KeyValRecord>();
            seg.RecNo.Should().Be(0);
            seg.SourceNo.Should().Be(1);
            seg.Count.Should().Be(5);
            seg.Keys[0].Should().Be("Segment");
            seg[0].Should().Be("HL");
            seg.Keys[1].Should().Be("Elem001");
            seg[1].Should().Be("2");
            seg.Keys[3].Should().Be("Elem003");
            seg[3].Should().Be("21");
            seg.Keys[4].Should().Be("Elem004");
            seg[4].Should().Be("1");
        }
        public void ObtainEmptyRecord_Simple_CorrectData()
        {
            //arrange
            var clstr = new KeyValCluster(new List <KeyValRecord>(), 6, 15, 4, null, null, _typeDefs, _config, null);

            //act
            var rec = clstr.ObtainEmptyRecord();

            //assert
            clstr.Count.Should().Be(0); //still no records in cluster
            rec.Should().BeOfType <KeyValRecord>();
            rec.RecNo.Should().Be(15);
            rec.SourceNo.Should().Be(4);
            rec.Count.Should().Be(0); //no items in record

            //act2
            rec.AddItem("K", "Val");

            //assert2
            rec.Count.Should().Be(1);
            rec["K"].Should().Be("Val");
        }