public void SchemaNameTest() { var schema = new TableSchema("SchemaNameTest"); Assert.AreEqual(schema.Name, "SchemaNameTest"); var table = new Table("SchemaNameTest 2"); Assert.AreEqual(table.Schema.Name, "SchemaNameTest 2"); }
public void ColumnsTest() { int columnCount = 0, schemaChangeCount = 0; var schema = new TableSchema("ColumnAddedTest"); schema.ColumnAdded += (s, e) => { Assert.AreEqual(e.Column, schema.Columns.Last()); columnCount++; Assert.AreEqual(columnCount, schema.Columns.Count); }; schema.ColumnRemoved += (s, e) => { Assert.IsFalse(schema.Columns.Contains(e.Column)); columnCount--; Assert.AreEqual(columnCount, schema.Columns.Count); }; schema.SchemaChanged += delegate { schemaChangeCount++; }; schema.Columns.AddValueColumn("Col1", typeof(int), 4); schema.Columns.AddValueColumn("Col2", typeof(string), "Hello!"); schema.Columns.AddValueColumn("Col3", typeof(DateTime?), (DateTime?)DateTime.Now); schema.Columns.RemoveColumn("Col2"); schema.Columns.RemoveColumn(schema.Columns.Last()); schema.Columns.AddValueColumn("Col2", typeof(TimeSpan), TimeSpan.Zero); Assert.AreEqual(2, schema.Columns.Count); Assert.AreEqual(6, schemaChangeCount); }
public void ForeignKeyDeletionTest() { var numbersSchema = new TableSchema("Numbers"); var powersSchema = new TableSchema("Powers"); numbersSchema.Columns.AddValueColumn("Number", typeof(int), 0); numbersSchema.Columns.AddValueColumn("Date", typeof(DateTime?), null); powersSchema.Columns.AddForeignKey("Number", numbersSchema, "Powers"); powersSchema.Columns.AddValueColumn("Exponent", typeof(int), 0); powersSchema.Columns.AddValueColumn("Value", typeof(int), 0); var context = new DataContext(); Table numbersTable, powersTable; context.Tables.AddTable(numbersTable = new Table(numbersSchema)); context.Tables.AddTable(powersTable = new Table(powersSchema)); var n1 = numbersTable.Rows.AddFromValues(2); var p1 = powersTable.Rows.AddFromValues(n1, 2, 4); var p2 = powersTable.Rows.AddFromValues(n1, 4, 16); CollectionAssert.AreEqual(new[] { p1, p2 }, n1.ChildRows("Powers").ToList()); n1.RemoveRow(); p1.RemoveRow(); Assert.AreEqual(n1, p1["Number"]); Assert.AreEqual(n1, p1["Number"]); Assert.AreEqual(n1, p2["Number"]); }
public void ColumnEventsTest() { var schema = new TableSchema("ColumnEventsTest"); schema.Columns.AddValueColumn("Col1", typeof(int), 5); schema.Columns.AddValueColumn("Col2", typeof(int), 5); var cc = schema.Columns.AddCalculatedColumn<int>("Calculated", row => 4); schema.Columns.AddValueColumn("Col4", typeof(int), 5); var mapping = new SchemaMapping(schema); var cc2 = schema.Columns.AddCalculatedColumn<long>("Other Calculated", row => row.Field<int>("Col1") * (long)row["Col2"]); Assert.AreEqual(3, mapping.Columns.Count); var lastCol = schema.Columns.AddValueColumn("Col6", typeof(int), 7); Assert.AreEqual(4, mapping.Columns.Count); Assert.AreEqual(lastCol, mapping.Columns[3].Column); schema.Columns.RemoveColumn(cc2); schema.Columns.RemoveColumn("Col2"); Assert.AreEqual(3, mapping.Columns.Count); Assert.AreEqual(lastCol, mapping.Columns[2].Column); mapping.Columns.RemoveMapping(lastCol); Assert.AreEqual(2, mapping.Columns.Count); Assert.AreEqual(schema.Columns["Col4"], mapping.Columns[1].Column); Assert.AreEqual("Col4", mapping.Columns[1].SqlName); }
public void NoCalculatedColumnsTest() { var schema = new TableSchema("NoCalculatedColumnsTest"); var cc = schema.Columns.AddCalculatedColumn<int>("Calculated", row => 4); var mapping = new SchemaMapping(schema); Assert.AreEqual(0, mapping.Columns.Count); mapping.Columns.AddMapping(cc); }
internal static new void FromXml(TableSchema newSchema, XElement xml, IEnumerable<TableSchema> referencedSchemas) { if (referencedSchemas == null) throw new ArgumentNullException("referencedSchemas", "referencedSchemas is required when schema XML has foreign key columns"); var foreignSchemaName = xml.Attribute("ForeignSchemaName").Value; var foreignSchema = referencedSchemas.FirstOrDefault(ts => ts.Name == foreignSchemaName); if (foreignSchema == null) throw new ArgumentException("Referenced schema '" + foreignSchemaName + "' not found", "referencedSchemas"); var column = newSchema.Columns.AddForeignKey(xml.Attribute("Name").Value, foreignSchema, xml.Attribute("RelationName").Value); column.AllowNulls = Boolean.Parse(xml.Attribute("AllowNulls").Value); column.Unique = Boolean.Parse(xml.Attribute("Unique").Value); }
public void ColumnsTest() { var schema = new TableSchema("ColumnsTest"); schema.Columns.AddValueColumn("Col1", typeof(int), 5); schema.Columns.AddValueColumn("Col2", typeof(int), 5); var cc = schema.Columns.AddCalculatedColumn<int>("Calculated", row => 4); schema.Columns.AddValueColumn("Col4", typeof(int), 5); var mapping = new SchemaMapping(schema); Assert.AreEqual(3, mapping.Columns.Count); Assert.IsNull(mapping.Columns[cc]); Assert.AreEqual(schema.Columns.Last(), mapping.Columns[2].Column); Assert.AreEqual(schema.Columns[1], mapping.Columns["Col2"].Column); }
public void RelationTest() { var childSchema = new TableSchema("RelationTest Child"); var parentSchema = new TableSchema("RelationTest Parent"); var foreignColumn = childSchema.Columns.AddForeignKey("Parent", parentSchema, "Children"); Assert.AreEqual(parentSchema, foreignColumn.ForeignSchema); Assert.AreEqual(1, parentSchema.ChildRelations.Count); Assert.AreEqual(foreignColumn.ChildRelation, parentSchema.ChildRelations[0]); Assert.AreEqual(foreignColumn, parentSchema.ChildRelations[0].ChildColumn); Assert.AreEqual(childSchema, parentSchema.ChildRelations[0].ChildSchema); Assert.AreEqual(parentSchema, parentSchema.ChildRelations[0].ParentSchema); childSchema.Columns.RemoveColumn(foreignColumn); Assert.AreEqual(0, childSchema.Columns.Count); Assert.AreEqual(0, parentSchema.Columns.Count); Assert.AreEqual(0, childSchema.ChildRelations.Count); Assert.AreEqual(0, parentSchema.ChildRelations.Count); }
static void AssertSchemasEqual(TableSchema expected, TableSchema actual) { Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.Columns.Count, actual.Columns.Count); Assert.AreEqual(expected.ChildRelations.Count, actual.ChildRelations.Count); if (expected.PrimaryKey == null) Assert.IsNull(actual.PrimaryKey); else Assert.AreEqual(expected.PrimaryKey.Name, actual.PrimaryKey.Name); for (int i = 0; i < expected.Columns.Count; i++) { Column e = expected.Columns[i], a = actual.Columns[i]; Assert.AreEqual(e.GetType(), a.GetType()); Assert.AreEqual(e.DataType, a.DataType); Assert.AreEqual(e.DefaultValue, a.DefaultValue); Assert.AreEqual(e.Name, a.Name); if (e is ValueColumn) { ValueColumn te = (ValueColumn)e, ta = (ValueColumn)a; Assert.AreEqual(te.Unique, ta.Unique); Assert.AreEqual(te.AllowNulls, ta.AllowNulls); } if (e is ForeignKeyColumn) { //Already checked ValueColumn properties ForeignKeyColumn te = (ForeignKeyColumn)e, ta = (ForeignKeyColumn)a; Assert.AreEqual(te.ChildRelation.Name, ta.ChildRelation.Name); AssertSchemasEqual(te.ForeignSchema, ta.ForeignSchema); } //if (e is CalculatedColumn) { // CalculatedColumn te = (CalculatedColumn)e, ta = (CalculatedColumn)a; //} } Assert.AreEqual(expected.ToXml().ToString(), actual.ToXml().ToString()); }
public void XmlSimpleTest() { var schema = new TableSchema("XmlSimpleTest"); schema.Columns.AddValueColumn("Col1", typeof(DateTimeOffset?), null).Unique = true; schema.Columns.AddValueColumn("Col2", typeof(int?), 57); schema.Columns.AddValueColumn("Col3", typeof(int), null); schema.Columns.AddValueColumn("Col4", typeof(string), null); schema.Columns.AddValueColumn("Col5", typeof(string), "ABC").AllowNulls = false; schema.Columns.AddValueColumn("Col6", typeof(double), Math.PI).Unique = true; schema.Columns.AddValueColumn("Col7", typeof(DateTime), DateTime.Now).Unique = true; schema.PrimaryKey = schema.Columns.AddValueColumn("Col8", typeof(byte?), (byte)7); var newSchema = TableSchema.FromXml(schema.ToXml()); AssertSchemasEqual(schema, newSchema); }
public void XmlColumnsTest() { var schema = new TableSchema("XmlColumnsTest"); var col1 = schema.Columns.AddValueColumn("Col1", typeof(int), null); var col2 = schema.Columns.AddValueColumn("Col2", typeof(int), null); var col3 = schema.Columns.AddValueColumn("Col3", typeof(int), null); var mapping = new SchemaMapping(schema); TestMapping(mapping); mapping.Columns.RemoveMapping(col2); TestMapping(mapping); mapping.Columns[col3].SqlName = "Sql3"; TestMapping(mapping); mapping.Columns.AddMapping(col2, "SSSSSSS"); TestMapping(mapping); }
///<summary>Reads a TableSchema from an XML element.</summary> ///<param name="xml">An XML element created by <see cref="TableSchema.ToXml"/>.</param> ///<param name="referencedSchemas">A set of schemas that may be referenced by the foreign keys in the schema being created.</param> public static TableSchema FromXml(XElement xml, IEnumerable<TableSchema> referencedSchemas) { if (xml == null) throw new ArgumentNullException("xml"); var retVal = new TableSchema(xml.Attribute("Name").Value); foreach (var elem in xml.Elements()) { ColumnParsers[elem.Name.LocalName](retVal, elem, referencedSchemas); } var primaryKey = xml.Attribute("PrimaryKey"); if (primaryKey != null) retVal.PrimaryKey = retVal.Columns[primaryKey.Value]; return retVal; }
public void PrimaryKeyTest() { var schema = new TableSchema("PrimaryKeyTest"); schema.Columns.AddValueColumn("Col1", typeof(int), 5); schema.PrimaryKey = schema.Columns.AddValueColumn("Col2", typeof(int), 5); var cc = schema.Columns.AddCalculatedColumn<int>("Calculated", row => 4); schema.Columns.AddValueColumn("Col4", typeof(int), 5); var mapping = new SchemaMapping(schema); Assert.AreEqual(3, mapping.Columns.Count); Assert.AreEqual(schema.PrimaryKey, schema.Columns[1]); Assert.AreEqual(mapping.PrimaryKey, mapping.Columns[1]); Assert.AreEqual(mapping.PrimaryKey.Column, schema.PrimaryKey); }
public void XmlPropertiesTest() { var schema = new TableSchema("XmlPropertiesTest"); var mapping = new SchemaMapping(schema); var newMapping = SchemaMapping.FromXml(mapping.ToXml(), schema); TestMapping(mapping); mapping.SqlSchemaName = "ABC"; TestMapping(mapping); mapping.SqlName = "DEF"; TestMapping(mapping); }
public void XmlEmptyTest() { var schema = new TableSchema("XmlEmptyTest"); Assert.AreEqual(new XElement("TableSchema", new XAttribute("Name", "XmlEmptyTest")).ToString(), schema.ToXml().ToString()); var newSchema = TableSchema.FromXml(schema.ToXml()); AssertSchemasEqual(schema, newSchema); }
internal static void FromXml(TableSchema newSchema, XElement xml, IEnumerable<TableSchema> referencedSchemas) { var column = newSchema.Columns.AddValueColumn(xml.Attribute("Name").Value, Type.GetType(xml.Attribute("Type").Value), null); var def = xml.Element("DefaultValue"); if (def.Element("Null") == null) //If it's not <Null /> column.DefaultValue = column.CoerceValue(def.Value, CultureInfo.InvariantCulture); column.AllowNulls = Boolean.Parse(xml.Attribute("AllowNulls").Value); column.Unique = Boolean.Parse(xml.Attribute("Unique").Value); }
public void XmlRelationsTest() { var parentSchema = new TableSchema("XmlRelationsTest - Parent"); parentSchema.Columns.AddValueColumn("ABC", typeof(string), "DEF"); var childSchema = new TableSchema("XmlRelationsTest - Child 1"); childSchema.Columns.AddValueColumn("Col1", typeof(uint), 35u); childSchema.Columns.AddForeignKey("Parent1", parentSchema, "Child1"); childSchema.Columns.AddForeignKey("Parent2", parentSchema, "Child2"); var pLogsSchema = new TableSchema("XmlRelationsTest - Parent Logs"); pLogsSchema.Columns.AddForeignKey("Row", parentSchema, "History"); pLogsSchema.Columns.AddValueColumn("Timestamp", typeof(DateTime), null); pLogsSchema.Columns.AddValueColumn("ABC", typeof(string), "DEF"); var cLogsSchema = new TableSchema("XmlRelationsTest - Child Logs"); cLogsSchema.Columns.AddForeignKey("Row", childSchema, "History"); cLogsSchema.Columns.AddValueColumn("Timestamp", typeof(DateTime), null); cLogsSchema.Columns.AddValueColumn("Col1", typeof(uint), 35u); var newSchemas = TableSchema.FromXml(parentSchema.ToXml(), childSchema.ToXml(), pLogsSchema.ToXml(), cLogsSchema.ToXml()); Func<TableSchema, TableSchema> NewSchema = oldSchema => newSchemas.Single(ts => ts.Name == oldSchema.Name); AssertSchemasEqual(parentSchema, NewSchema(parentSchema)); AssertSchemasEqual(childSchema, NewSchema(childSchema)); AssertSchemasEqual(pLogsSchema, NewSchema(pLogsSchema)); AssertSchemasEqual(cLogsSchema, NewSchema(cLogsSchema)); }
public void SqlNameTest() { var schema = new TableSchema("SqlNameTest"); var mapping = new SchemaMapping(schema); Assert.AreEqual(mapping.SqlName, schema.Name); Assert.IsNull(mapping.SqlSchemaName); }