Пример #1
0
        public IEnumerable <ObjectType> Transform(IQueryResult source)
        {
            ICollection <ObjectType> ret = new List <ObjectType>();
            IDictionary <int, Action <ObjectType, object> > columnsActions = new Dictionary <int, Action <ObjectType, object> >();
            bool hasOnlyNullActions = true;

            int currentColumnIndex           = 0;
            IEnumerable <string> columnNames = source.ColumnsNames();

            foreach (var item in columnNames)
            {
                Action <ObjectType, object> currentAction = this.ColumnToObject.GetColumnSetter(item);

                if (currentAction != null)
                {
                    hasOnlyNullActions = false;
                }

                columnsActions.Add(currentColumnIndex, currentAction);
                currentColumnIndex++;
            }

            //If there are no actions, we return. This prevents an innecesary loop data.
            if (hasOnlyNullActions)
            {
                return(ret);
            }



            foreach (var item in source.ResultRows())
            {
                ObjectType o = new ObjectType();

                for (int i = 0; i < item.Length; i++)
                {
                    columnsActions.TryGetValue(i, out Action <ObjectType, object> currentAction);

                    if (currentAction == null)
                    {
                        continue;
                    }

                    currentAction.Invoke(o, item[i]);
                }

                ret.Add(o);
            }

            return(ret);
        }
Пример #2
0
        public void TestReturnsValidObject()
        {
            string sql = "SELECT code AS code, description AS description, price AS price FROM items;";
            Mock <IQueryDefinition> mock = new Mock <IQueryDefinition>();

            mock.Setup(_ => _.Connection()).Returns(this.Connection);
            mock.Setup(_ => _.Query()).Returns(sql);
            mock.Setup(_ => _.Parameters()).Returns(new List <Parameter>());

            DataBaseExtractor model  = new DataBaseExtractor("", mock.Object);
            IQueryResult      result = model.Extract();

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.ResultRows());
            Assert.IsNotNull(result.ColumnsNames());
        }
Пример #3
0
        public void TestResturnsExpectedColumnNames()
        {
            string sql = "SELECT code AS code, description AS description, price AS price FROM items;";
            Mock <IQueryDefinition> mock = new Mock <IQueryDefinition>();

            mock.Setup(_ => _.Connection()).Returns(this.Connection);
            mock.Setup(_ => _.Query()).Returns(sql);
            mock.Setup(_ => _.Parameters()).Returns(new List <Parameter>());

            DataBaseExtractor model  = new DataBaseExtractor("", mock.Object);
            IQueryResult      result = model.Extract();

            IEnumerator <string> columnEnumerator = result.ColumnsNames().GetEnumerator();

            Assert.IsTrue(columnEnumerator.MoveNext());
            Assert.AreEqual("code", columnEnumerator.Current);
            Assert.IsTrue(columnEnumerator.MoveNext());
            Assert.AreEqual("description", columnEnumerator.Current);
            Assert.IsTrue(columnEnumerator.MoveNext());
            Assert.AreEqual("price", columnEnumerator.Current);
            Assert.IsFalse(columnEnumerator.MoveNext());
        }