public void Clone_gets_column_clones_from_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var primaryKey = new PrimaryKey("PK", new[] { column0, column1 }, isClustered: false);
            
            var cloneContext = new CloneContext();
            var columnClone0 = column0.Clone(cloneContext);
            var columnClone1 = column1.Clone(cloneContext);
            var clone = primaryKey.Clone(cloneContext);

            Assert.NotSame(primaryKey, clone);
            Assert.Equal(2, clone.Columns.Count);
            Assert.Same(columnClone0, clone.Columns[0]);
            Assert.Same(columnClone1, clone.Columns[1]);
        }
        public void Create_and_initialize_primary_key()
        {
            var table = new Table("dbo.MyTable");
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            table.AddColumn(column0);
            table.AddColumn(column1);
            var primaryKey = new PrimaryKey(
                "MyPrimaryKey", new[] { column0, column1, }, isClustered: true);

            Assert.Equal("MyPrimaryKey", primaryKey.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(2, primaryKey.Columns.Count);
            Assert.Same(column0, primaryKey.Columns[0]);
            Assert.Same(column1, primaryKey.Columns[1]);
            Assert.Same(table, primaryKey.Table);
            Assert.True(primaryKey.IsClustered);
        }
        public void Clone_replicates_instance_and_adds_column_clones_to_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var primaryKey = new PrimaryKey("PK", new[] { column0, column1 }, isClustered: false);

            var cloneContext = new CloneContext();
            var clone = primaryKey.Clone(cloneContext);

            Assert.NotSame(primaryKey, clone);
            Assert.Equal("PK", clone.Name);
            Assert.Equal(2, clone.Columns.Count);
            Assert.NotSame(column0, clone.Columns[0]);
            Assert.NotSame(column1, clone.Columns[1]);
            Assert.Equal("Foo", clone.Columns[0].Name);
            Assert.Equal("Bar", clone.Columns[1].Name);
            Assert.False(clone.IsClustered);

            Assert.Same(clone.Columns[0], cloneContext.GetOrAdd(column0, () => null));
            Assert.Same(clone.Columns[1], cloneContext.GetOrAdd(column1, () => null));
        }
Exemple #4
0
        public void Clone_replicates_instance_and_uses_cache()
        {
            var column00 = new Column("C00", typeof(int));
            var column01 = new Column("C01", typeof(string));
            var column10 = new Column("C10", typeof(int));
            var column11 = new Column("C11", typeof(string));
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var table1 = new Table("dbo.T1", new[] { column10, column11 });
            var primaryKey0 = new PrimaryKey("PK0", new[] { column00 });
            var primaryKey1 = new PrimaryKey("PK1", new[] { column10 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column01 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column11 }, new[] { column00 });
            var index0 = new Index("IX0", new[] { column00 });
            var index1 = new Index("IX1", new[] { column10 });

            table0.PrimaryKey = primaryKey0;
            table1.PrimaryKey = primaryKey1;
            table0.AddForeignKey(foreignKey0);
            table1.AddForeignKey(foreignKey1);
            table0.AddIndex(index0);
            table1.AddIndex(index1);

            var cloneContext = new CloneContext();
            var clone0 = table0.Clone(cloneContext);

            Assert.NotSame(table0, clone0);
            Assert.Equal("dbo.T0", clone0.Name);

            Assert.Equal(2, clone0.Columns.Count);
            Assert.NotSame(column00, clone0.Columns[0]);
            Assert.NotSame(column01, clone0.Columns[1]);
            Assert.Equal("C00", clone0.Columns[0].Name);
            Assert.Equal("C01", clone0.Columns[1].Name);
            Assert.Equal(typeof(int), clone0.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone0.Columns[1].ClrType);

            Assert.NotNull(clone0.PrimaryKey);
            Assert.NotSame(primaryKey0, clone0.PrimaryKey);
            Assert.Equal("PK0", clone0.PrimaryKey.Name);
            Assert.Equal(1, clone0.PrimaryKey.Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone0.ForeignKeys.Count);
            Assert.NotSame(foreignKey0, clone0.ForeignKeys[0]);
            Assert.Equal("FK0", clone0.ForeignKeys[0].Name);
            Assert.Equal(1, clone0.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone0.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone0.Columns[1], clone0.ForeignKeys[0].Columns[0]);
            Assert.Same(cloneContext.GetOrAdd(column10, () => null), clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone0.Indexes.Count);
            Assert.NotSame(index0, clone0.Indexes[0]);
            Assert.Equal("IX0", clone0.Indexes[0].Name);
            Assert.Equal(1, clone0.Indexes[0].Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.Indexes[0].Columns[0]);

            Assert.Equal(3, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.NotNull(cloneContext.GetOrAdd(column10, () => null));

            var clone1 = table1.Clone(cloneContext);

            Assert.NotSame(table1, clone1);
            Assert.Equal("dbo.T1", clone1.Name);

            Assert.Equal(2, clone1.Columns.Count);
            Assert.NotSame(column10, clone1.Columns[0]);
            Assert.NotSame(column11, clone1.Columns[1]);
            Assert.Equal("C10", clone1.Columns[0].Name);
            Assert.Equal("C11", clone1.Columns[1].Name);
            Assert.Equal(typeof(int), clone1.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone1.Columns[1].ClrType);

            Assert.NotNull(clone1.PrimaryKey);
            Assert.NotSame(primaryKey1, clone1.PrimaryKey);
            Assert.Equal("PK1", clone1.PrimaryKey.Name);
            Assert.Equal(1, clone1.PrimaryKey.Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone1.ForeignKeys.Count);
            Assert.NotSame(foreignKey1, clone1.ForeignKeys[0]);
            Assert.Equal("FK1", clone1.ForeignKeys[0].Name);
            Assert.Equal(1, clone1.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone1.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone1.Columns[1], clone1.ForeignKeys[0].Columns[0]);
            Assert.Same(clone0.Columns[0], clone1.ForeignKeys[0].ReferencedColumns[0]);
            Assert.Same(clone1.Columns[0], clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone1.Indexes.Count);
            Assert.NotSame(index1, clone1.Indexes[0]);
            Assert.Equal("IX1", clone1.Indexes[0].Name);
            Assert.Equal(1, clone1.Indexes[0].Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.Indexes[0].Columns[0]);

            Assert.Equal(4, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.Same(clone1.Columns[0], cloneContext.GetOrAdd(column10, () => null));
            Assert.Same(clone1.Columns[1], cloneContext.GetOrAdd(column11, () => null));
        }
Exemple #5
0
        public void Get_set_primary_key()
        {
            var columns = new[] { new Column("Foo", "int") };
            var table = new Table("dbo.MyTable", columns);
            var primaryKey = new PrimaryKey("PK", columns);

            Assert.Null(table.PrimaryKey);

            table.PrimaryKey = primaryKey;

            Assert.Same(primaryKey, table.PrimaryKey);

            table.PrimaryKey = null;

            Assert.Null(table.PrimaryKey);
        }