コード例 #1
0
        public void CanWriteAndReadXmlForResultSetSchema()
        {
            var rss = new ResultSetSchema();

            rss.Columns.Add(new Column {
                ClrType = typeof(int), DbType = "int", Name = "colc"
            });

            using (var w = new TestXmlWriter())
            {
                new ResultSetSchemaSerializer().Serialize(w.Writer, rss);

                using (var r = new TestXmlReader(w.Xml))
                {
                    var rss2 = new ResultSetSchemaSerializer().Deserialize(r.Reader);

                    Assert.IsNotNull(rss2);
                    Assert.IsNotNull(rss2.Columns);
                    Assert.AreEqual(1, rss2.Columns.Count);
                    Assert.AreEqual("colc", rss2.Columns[0].Name);
                    Assert.AreEqual("int", rss2.Columns[0].DbType);
                    Assert.AreSame(typeof(int), rss2.Columns[0].ClrType);
                }
            }
        }
コード例 #2
0
        public void CanReadRowWith2Columns()
        {
            var xml = "<Row><cola>11</cola><colb>value2</colb></Row>";

            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int)
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string)
            });

            using (var r = TestXmlReader.Create(xml))
            {
                var row = new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                Assert.IsNotNull(row);
                Assert.AreEqual(2, row.Count);
                Assert.AreEqual(11, row["cola"]);
                Assert.AreSame(typeof(int), row["cola"].GetType());
                Assert.AreEqual("value2", row["colb"]);
                Assert.AreSame(typeof(string), row["colb"].GetType());
            }
        }
コード例 #3
0
        public void CanEquateResultSetSchema()
        {
            var s1 = new ResultSetSchema();
            var s2 = new ResultSetSchema();

            Assert.IsTrue(s1.EqualsSchema(s2));

            s1.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(string), DbType = ""
            });
            Assert.IsFalse(s1.EqualsSchema(s2));
            s2.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(string), DbType = ""
            });
            Assert.IsTrue(s1.EqualsSchema(s2));

            s1.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = ""
            });
            Assert.IsFalse(s1.EqualsSchema(s2));
            s2.Columns.Add(new Column {
                Name = "colabc", ClrType = typeof(string), DbType = ""
            });
            Assert.IsFalse(s1.EqualsSchema(s2));
        }
コード例 #4
0
        public void ReadXmlAddsNullValuesForNotDefinedRowValuesInXml()
        {
            var xml = "<Row><colb>value2</colb></Row>";

            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int)
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string)
            });

            using (var r = TestXmlReader.Create(xml))
            {
                var row = new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                Assert.IsNotNull(row);
                Assert.AreEqual(2, row.Count);
                Assert.AreEqual(DBNull.Value, row["cola"]);
                Assert.AreSame(DBNull.Value.GetType(), row["cola"].GetType());
                Assert.AreEqual("value2", row["colb"]);
                Assert.AreSame(typeof(string), row["colb"].GetType());
            }
        }
コード例 #5
0
        public void CanWriteAndReadRow()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colc", ClrType = typeof(DateTime), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cold", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cole", ClrType = typeof(int), DbType = ""
            });

            var row = new ResultSetRow();

            row["cola"] = 12;
            row["colb"] = "hallo";
            row["colc"] = new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123);
            row["cold"] = null;
            row["cole"] = DBNull.Value;

            using (var writer = new TestXmlWriter())
            {
                new ResultSetRowSerializer().Serialize(writer.Writer, row, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                using (var r = new TestXmlReader(writer.Xml))
                {
                    var row2 = new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                        Schema = schema
                    });

                    Assert.IsNotNull(row2);
                    Assert.AreEqual(5, row2.Count);

                    Assert.AreEqual(12, row2["cola"]);
                    Assert.AreSame(typeof(int), row2["cola"].GetType());
                    Assert.AreEqual("hallo", row2["colb"]);
                    Assert.AreSame(typeof(string), row2["colb"].GetType());
                    Assert.AreEqual(new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123), row2["colc"]);
                    Assert.AreSame(typeof(DateTime), row2["colc"].GetType());
                    Assert.AreEqual(DBNull.Value, row2["cold"]);
                    Assert.AreSame(DBNull.Value.GetType(), row2["cold"].GetType());
                    Assert.AreEqual(DBNull.Value, row2["cole"]);
                    Assert.AreSame(DBNull.Value.GetType(), row2["cole"].GetType());
                }
            }
        }
コード例 #6
0
        public void CanValidateWithoutNoRecursiveCheck()
        {
            var schema = new ResultSetSchema();
            var rows   = new ResultSetRowCollection();

            rows.Add(new ResultSetRow());
            rows[0]["Name"] = "will";

            Assert.IsNull(rows.Validate(schema));
        }
コード例 #7
0
        public void ValiationFailsWithEmptyRowName()
        {
            var schema = new ResultSetSchema();

            var row = new ResultSetRow();

            row[" "] = "smith"; // defines use of string.isnulloremptywhitespace

            var ex = row.Validate(schema);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Row contains a value without column name (null/empty)", ex.Message);
        }
コード例 #8
0
        public void ValidationOfSchemaWith1ValidAnd1InvalidRowsSucceedsWithoutRecusriveCheck()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            schema.Columns.Add(new Column {
                ClrType = typeof(int), DbType = "int"
            });                                                                       // invalid

            Assert.IsNull(schema.Validate(false));
            Assert.IsNull(schema.Validate());
        }
コード例 #9
0
        public void CanValidateSchemaWith2ValidRows()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            schema.Columns.Add(new Column {
                Name = "Length", ClrType = typeof(int), DbType = "int"
            });

            Assert.IsNull(schema.Validate(false));
            Assert.IsNull(schema.Validate(true));
        }
コード例 #10
0
        public void ValidationSucceedsWithRecursiveCheckAndValidRows()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            var rows = new ResultSetRowCollection();

            rows.Add(new ResultSetRow());
            rows[0]["Name"] = "will";

            Assert.IsNull(rows.Validate(schema, true));
        }
コード例 #11
0
        public void ValidationFailsWithRecursiveCheckAndInvalidRow()
        {
            var schema = new ResultSetSchema();
            var rows   = new ResultSetRowCollection();

            rows.Add(new ResultSetRow());
            rows[0]["Name"] = "will";

            var ex = rows.Validate(schema, true);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Found invalid row(s)", ex.Message);
            Assert.IsNotNull(ex.InnerException);
        }
コード例 #12
0
        public void CanReadXmlFor2Rows()
        {
            using (var r = new TestXmlReader("<Rows><Row><Name>will</Name></Row><Row><Name>tess</Name></Row></Rows>"))
            {
                var schema = new ResultSetSchema();
                schema.Columns.Add(new Column {
                    ClrType = typeof(string), Name = "Name", DbType = "varchar"
                });

                var rows = new ResultSetRowCollectionSerializer().Deserialize(r.Reader, new ResultSetRowCollectionSerializerContext {
                    Schema = schema
                });

                Assert.IsNotNull(rows);
                Assert.AreEqual(2, rows.Count);
            }
        }
コード例 #13
0
        public void ValidationFailsWithRowNameThatIsNotInSchema()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Age", ClrType = typeof(int), DbType = "varchar"
            });
            var row = new ResultSetRow();

            row["Name"] = "smith";
            row["Age"]  = 12;

            var ex = row.Validate(schema);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Column 'Name' is not defined in schema", ex.Message);
        }
コード例 #14
0
        public void CanValidate()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            schema.Columns.Add(new Column {
                Name = "Age", ClrType = typeof(int), DbType = "int"
            });

            var row = new ResultSetRow();

            row["Name"] = "smith";
            row["Age"]  = 81;

            Assert.IsNull(row.Validate(schema));
        }
コード例 #15
0
        public void ValidationFailsWithInvalidSchema()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = null, ClrType = typeof(string), DbType = "varchar"
            });

            var row = new ResultSetRow();

            row["Name"] = "smith";

            var ex = row.Validate(schema);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Invalid schema", ex.Message);
            Assert.IsNotNull(ex.InnerException);
        }
コード例 #16
0
        public void CanReadRowWith4ColumnsAnd1NullValue()
        {
            var xml = "<Row><cola>12</cola><colb>hallo</colb><colc>2011-09-14T18:58:44.123</colc><cold>13.66666</cold></Row>";

            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int)
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string)
            });
            schema.Columns.Add(new Column {
                Name = "colc", ClrType = typeof(DateTime)
            });
            schema.Columns.Add(new Column {
                Name = "cold", ClrType = typeof(decimal)
            });
            schema.Columns.Add(new Column {
                Name = "cole", ClrType = typeof(float)
            });

            using (var r = TestXmlReader.Create(xml))
            {
                var row = new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                Assert.IsNotNull(row);
                Assert.AreEqual(5, row.Count);
                Assert.AreEqual(12, row["cola"]);
                Assert.AreSame(typeof(int), row["cola"].GetType());
                Assert.AreEqual("hallo", row["colb"]);
                Assert.AreSame(typeof(string), row["colb"].GetType());
                Assert.AreEqual(new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123), row["colc"]);
                Assert.AreSame(typeof(DateTime), row["colc"].GetType());
                Assert.AreEqual(13.66666m, row["cold"]);
                Assert.AreSame(typeof(decimal), row["cold"].GetType());
                Assert.AreEqual(DBNull.Value, row["cole"]);
                Assert.AreSame(DBNull.Value.GetType(), row["cole"].GetType());
            }
        }
コード例 #17
0
        public void GetColumnThrowsExceptionIfColumnIsNotFound()
        {
            var rs = new ResultSetSchema();

            var cola = new Column {
                Name = "cola"
            };
            var colb = new Column {
                Name = "colb"
            };
            var colc = new Column {
                Name = "colc"
            };

            rs.Columns.Add(cola);
            rs.Columns.Add(colb);
            rs.Columns.Add(colc);

            Assert.ThrowsException <InvalidOperationException>(() => { rs.GetColumn("nonexisting"); });
        }
コード例 #18
0
        public void ReadXmlThrowsIfColumnNameNotInSchema()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "ColA", ClrType = typeof(string), DbType = ""
            });

            var xml = "<Row><ColA>test</ColA><colb>hey</colb></Row>";

            using (var r = new TestXmlReader(xml))
            {
                Assert.ThrowsException <InvalidOperationException>(() =>
                {
                    new ResultSetRowSerializer().Deserialize(r.Reader, new ResultSetRowSerializerContext {
                        Schema = schema
                    });
                }, "Cannot find column 'colb' in schema");
            }
        }
コード例 #19
0
        public void ValidationOfSchemaWith1ValidAnd1InvalidRowsFailsWithRecusriveCheck()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            schema.Columns.Add(new Column {
                ClrType = typeof(int), DbType = "int"
            });                                                                       // invalid

            Assert.IsNull(schema.Validate(false));
            Assert.IsNull(schema.Validate());

            var ex = schema.Validate(true);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Schema validation failed", ex.Message);
            Assert.IsNotNull(ex.InnerException);
        }
コード例 #20
0
        public void CanGetColumn()
        {
            var rs = new ResultSetSchema();

            var cola = new Column {
                Name = "cola"
            };
            var colb = new Column {
                Name = "colb"
            };
            var colc = new Column {
                Name = "colc"
            };

            rs.Columns.Add(cola);
            rs.Columns.Add(colb);
            rs.Columns.Add(colc);

            Assert.AreSame(cola, rs.GetColumn("cola"));
            Assert.AreSame(colb, rs.GetColumn("colb"));
            Assert.AreSame(colc, rs.GetColumn("colc"));
        }
コード例 #21
0
        public void ValidationFailsIfRowValueDiffersFromClrTypeDefinedInSchema()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Name", ClrType = typeof(string), DbType = "varchar"
            });
            schema.Columns.Add(new Column {
                Name = "Age", ClrType = typeof(int), DbType = "int"
            });

            var row = new ResultSetRow();

            row["Name"] = "smith";
            row["Age"]  = "test";

            var ex = row.Validate(schema);

            Assert.IsNotNull(ex);
            Assert.AreSame(typeof(InvalidOperationException), ex.GetType());
            Assert.AreEqual("Value for 'Age' is of type 'System.String' while schema defines type 'System.Int32'", ex.Message);
        }
コード例 #22
0
        public static bool EqualsSchema(this ResultSetSchema schema1, ResultSetSchema schema2)
        {
            if (schema2 == null)
            {
                throw new ArgumentNullException("schema2");
            }

            if (schema1.Columns.Count != schema2.Columns.Count)
            {
                return(false);
            }

            for (var t = 0; t < schema1.Columns.Count; t++)
            {
                if (!schema1.Columns[t].EqualsColumn(schema2.Columns[t]))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #23
0
        public void CanwriteXmlFor2Rows()
        {
            using (var w = new TestXmlWriter())
            {
                var rows = new ResultSetRowCollection();
                rows.Add(new ResultSetRow());
                rows.Add(new ResultSetRow());
                rows[0]["Name"] = "john";
                rows[1]["Name"] = "smith";

                var schema = new ResultSetSchema();
                schema.Columns.Add(new Column {
                    ClrType = typeof(string), DbType = "varchar", Name = "Name"
                });

                new ResultSetRowCollectionSerializer().Serialize(w.Writer, rows, new ResultSetRowCollectionSerializerContext {
                    Schema = schema
                });

                Assert.AreEqual("<Rows><Row><Name>john</Name></Row><Row><Name>smith</Name></Row></Rows>", w.Xml);
            }
        }
コード例 #24
0
        public void CanWriteRowWithVariousTypes()
        {
            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "colc", ClrType = typeof(DateTime), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cold", ClrType = typeof(int), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "cole", ClrType = typeof(int), DbType = ""
            });

            var row = new ResultSetRow();

            row["cola"] = 12;
            row["colb"] = "hallo";
            row["colc"] = new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123);
            row["cold"] = null;
            row["cole"] = DBNull.Value;

            using (var writer = new TestXmlWriter())
            {
                new ResultSetRowSerializer().Serialize(writer.Writer, row, new ResultSetRowSerializerContext {
                    Schema = schema
                });

                var expectedXml = "<Row><cola>12</cola><colb>hallo</colb><colc>2011-09-14T18:58:44.123</colc></Row>";
                Assert.AreEqual(expectedXml, writer.Xml);
            }
        }
コード例 #25
0
        public void CanWriteXmlForResultSetSchema()
        {
            var expectedXml = new StringBuilder();

            expectedXml.Append("<Schema>");
            expectedXml.Append("<Columns>");
            expectedXml.Append("<Column name=\"colc\" dbType=\"int\" clrType=\"System.Int32\" />");
            expectedXml.Append("</Columns>");
            expectedXml.Append("</Schema>");

            var rss = new ResultSetSchema();

            rss.Columns.Add(new Column {
                ClrType = typeof(int), DbType = "int", Name = "colc"
            });

            using (var w = new TestXmlWriter())
            {
                new ResultSetSchemaSerializer().Serialize(w.Writer, rss);

                Assert.AreEqual(expectedXml.ToString(), w.Xml);
            }
        }
コード例 #26
0
        public void CanCreateFromReader()
        {
            var r = new TestDataReader();

            r.ResultSets.Add(new ResultSet());
            r.ResultSets[0].Schema.Columns.Add(new Column {
                Name = "cola", ClrType = typeof(DateTime), DbType = "datetime"
            });
            r.ResultSets[0].Schema.Columns.Add(new Column {
                Name = "colb", ClrType = typeof(string), DbType = "varchar"
            });

            var rss = ResultSetSchema.CreateFromReader(r);

            Assert.IsNotNull(rss);
            Assert.IsNotNull(rss.Columns);
            Assert.AreEqual(2, rss.Columns.Count);
            Assert.AreEqual("cola", rss.Columns[0].Name);
            Assert.AreEqual("colb", rss.Columns[1].Name);
            Assert.AreEqual("datetime", rss.Columns[0].DbType);
            Assert.AreEqual("varchar", rss.Columns[1].DbType);
            Assert.AreSame(typeof(DateTime), rss.Columns[0].ClrType);
            Assert.AreEqual(typeof(string), rss.Columns[1].ClrType);
        }
コード例 #27
0
        public void SerializationAndDeserializationOfValuesIsLocalCultureIndependant()
        {
            // execute write and read using defferent cultures and check that datetime and decimal values are serialized and deserialized ok
            // use threads to avoid changing the current culture for th executing thread.

            var schema = new ResultSetSchema();

            schema.Columns.Add(new Column {
                Name = "Cola", ClrType = typeof(DateTime), DbType = ""
            });
            schema.Columns.Add(new Column {
                Name = "Colb", ClrType = typeof(decimal), DbType = ""
            });

            var row = new ResultSetRow();

            row["Cola"] = new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123);
            row["Colb"] = 18.66m;

            var writeContext = new ThreadContext
            {
                Row    = row,
                Schema = schema
            };

            var writeThread = new Thread(WriteXml);

            writeThread.CurrentCulture = new CultureInfo("nl-NL");
            writeThread.Start(writeContext);
            if (!writeThread.Join(2000))
            {
                Assert.Fail("WriteXml did not finish within time");
            }
            if (writeContext.Exception != null)
            {
                throw writeContext.Exception;
            }


            var readContext = new ThreadContext
            {
                Xml    = writeContext.Xml,
                Schema = schema
            };
            var readThread = new Thread(ReadXml);

            readThread.CurrentCulture = new CultureInfo("en-US");
            readThread.Start(readContext);
            if (!readThread.Join(2000))
            {
                Assert.Fail("ReadXml did not finish within time");
            }
            if (readContext.Exception != null)
            {
                throw readContext.Exception;
            }

            var _row = readContext.Row;

            Assert.IsNotNull(_row);
            Assert.AreEqual(2, _row.Count);
            Assert.AreEqual(new DateTime(2011, 9, 14, 18, 58, 44).AddMilliseconds(123), _row["Cola"]);
            Assert.AreSame(typeof(DateTime), _row["Cola"].GetType());
            Assert.AreEqual(18.66m, _row["Colb"]);
            Assert.AreSame(typeof(Decimal), _row["Colb"].GetType());
        }
コード例 #28
0
 public override Exception Validate(ResultSetSchema schema)
 {
     return(Exception);
 }
コード例 #29
0
 private ResultSetRowCollection ReadRows(XmlReader reader, ResultSetSchema schema)
 {
     return(rowCollectionSerializer.Deserialize(reader, new ResultSetRowCollectionSerializerContext {
         Schema = schema
     }));
 }
コード例 #30
0
 public static void AddNonExistingColumnValuesToRowsWithDbNullValueFromSchema(ResultSetRow row, ResultSetSchema schema)
 {
     foreach (var nullColumn in schema.Columns.Where(a => !row.ContainsKey(a.Name)))
     {
         row[nullColumn.Name] = DBNull.Value;
     }
 }