public void Ctor_ByNameDataColumns()
        {
            var       ds       = new DataSet();
            DataTable dtChild  = DataProvider.CreateChildDataTable();
            DataTable dtParent = DataProvider.CreateParentDataTable();

            ds.Tables.Add(dtParent);
            ds.Tables.Add(dtChild);

            DataRelation dRel;

            dRel = new DataRelation("MyRelation", dtParent.Columns[0], dtChild.Columns[0]);
            ds.Relations.Add(dRel);

            // DataRelation - parent Constraints
            Assert.Equal(1, dtParent.Constraints.Count);

            // DataRelation - child Constraints
            Assert.Equal(1, dtChild.Constraints.Count);

            // DataRelation - child relations
            Assert.Equal(dRel, dtParent.ChildRelations[0]);

            // DataRelation - parent relations
            Assert.Equal(dRel, dtChild.ParentRelations[0]);

            // DataRelation - name
            Assert.Equal("MyRelation", dRel.RelationName);

            // DataRelation - parent UniqueConstraint
            Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType());

            // DataRelation - Child ForeignKeyConstraint
            Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType());

            ds.Relations.Clear();
            // Remove DataRelation - Parent Constraints
            Assert.Equal(1, dtParent.Constraints.Count);

            // Remove DataRelation - Child Constraints
            Assert.Equal(1, dtChild.Constraints.Count);

            // Remove DataRelation - child relations
            Assert.Equal(0, dtParent.ChildRelations.Count);

            // Remove DataRelation - parent relations
            Assert.Equal(0, dtChild.ParentRelations.Count);

            //add relation which will create invalid constraint
            dtChild.Constraints.Clear();
            dtParent.Constraints.Clear();
            //add duplicated row
            dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
            dRel = new DataRelation("MyRelation", dtParent.Columns[0], dtChild.Columns[0]);

            // Add relation which will create invalid constraint
            AssertExtensions.Throws <ArgumentException>(null, () =>
            {
                ds.Relations.Add(dRel);
            });
        }
        public void Ctor_ByNameDataColumnsArraysCreateConstraints()
        {
            DataRelation dRel;
            DataTable    dtChild  = DataProvider.CreateChildDataTable();
            DataTable    dtParent = DataProvider.CreateParentDataTable();

            var ds = new DataSet();

            ds.Tables.Add(dtParent);
            ds.Tables.Add(dtChild);

            //parameter createConstraints = true

            bool createConstraints = true;

            for (int i = 0; i <= 1; i++)
            {
                if (i == 0)
                {
                    createConstraints = false;
                }
                else
                {
                    createConstraints = true;
                }

                ds.Relations.Clear();
                dtParent.Constraints.Clear();
                dtChild.Constraints.Clear();

                //add duplicated row
                dtParent.Rows.Add(dtParent.Rows[0].ItemArray);
                dRel = new DataRelation("MyRelation", new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0] }, createConstraints);
                // Add relation which will create invalid constraint
                if (createConstraints == true)
                {
                    AssertExtensions.Throws <ArgumentException>(null, () => ds.Relations.Add(dRel));
                }
                else
                {
                    ds.Relations.Add(dRel);
                }

                ds.Relations.Clear();
                dtParent.Constraints.Clear();
                dtChild.Constraints.Clear();
                dtParent.Rows.Remove(dtParent.Rows[dtParent.Rows.Count - 1]);

                dRel = new DataRelation("MyRelation", new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0] }, createConstraints);
                ds.Relations.Add(dRel);

                // DataRelation - parent Constraints,createConstraints=
                Assert.Equal(i, dtParent.Constraints.Count);

                // DataRelation - child Constraints,createConstraints=
                Assert.Equal(i, dtChild.Constraints.Count);

                // DataRelation - child relations,createConstraints=
                Assert.Equal(dRel, dtParent.ChildRelations[0]);

                // DataRelation - parent relations,createConstraints=
                Assert.Equal(dRel, dtChild.ParentRelations[0]);

                // DataRelation - name
                Assert.Equal("MyRelation", dRel.RelationName);
            }
        }
        public void ctor_ParentColChildCol()
        {
            DataTable dtParent = DataProvider.CreateParentDataTable();
            DataTable dtChild  = DataProvider.CreateChildDataTable();
            var       ds       = new DataSet();

            ds.Tables.Add(dtChild);
            ds.Tables.Add(dtParent);

            ForeignKeyConstraint fc = null;

            // Ctor ArgumentException
            AssertExtensions.Throws <ArgumentException>(null, () =>
            {
                fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0], dtChild.Columns[1] });
            });

            fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0], dtParent.Columns[1] }, new DataColumn[] { dtChild.Columns[0], dtChild.Columns[2] });

            // Add constraint to table - ArgumentException
            AssertExtensions.Throws <ArgumentException>(null, () =>
            {
                dtChild.Constraints.Add(fc);
            });

            // Child Table Constraints Count - two columnns
            Assert.Equal(0, dtChild.Constraints.Count);

            // Parent Table Constraints Count - two columnns
            Assert.Equal(1, dtParent.Constraints.Count);

            // DataSet relations Count
            Assert.Equal(0, ds.Relations.Count);

            dtParent.Constraints.Clear();
            dtChild.Constraints.Clear();

            fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0] });
            // Ctor
            Assert.Equal(false, fc == null);

            // Child Table Constraints Count
            Assert.Equal(0, dtChild.Constraints.Count);

            // Parent Table Constraints Count
            Assert.Equal(0, dtParent.Constraints.Count);

            // DataSet relations Count
            Assert.Equal(0, ds.Relations.Count);

            dtChild.Constraints.Add(fc);

            // Child Table Constraints Count, Add
            Assert.Equal(1, dtChild.Constraints.Count);

            // Parent Table Constraints Count, Add
            Assert.Equal(1, dtParent.Constraints.Count);

            // DataSet relations Count, Add
            Assert.Equal(0, ds.Relations.Count);

            // Parent Table Constraints type
            Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType());

            // Parent Table Constraints type
            Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType());

            // Parent Table Primary key
            Assert.Equal(0, dtParent.PrimaryKey.Length);

            dtChild.Constraints.Clear();
            dtParent.Constraints.Clear();
            ds.Relations.Add(new DataRelation("myRelation", dtParent.Columns[0], dtChild.Columns[0]));

            // Relation - Child Table Constraints Count
            Assert.Equal(1, dtChild.Constraints.Count);

            // Relation - Parent Table Constraints Count
            Assert.Equal(1, dtParent.Constraints.Count);

            // Relation - Parent Table Constraints type
            Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType());

            // Relation - Parent Table Constraints type
            Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType());

            // Relation - Parent Table Primary key
            Assert.Equal(0, dtParent.PrimaryKey.Length);
        }
        public void deleteRule()
        {
            var       ds       = new DataSet();
            DataTable dtParent = DataProvider.CreateParentDataTable();
            DataTable dtChild  = DataProvider.CreateChildDataTable();

            ds.Tables.Add(dtParent);
            ds.Tables.Add(dtChild);
            dtParent.PrimaryKey   = new DataColumn[] { dtParent.Columns[0] };
            ds.EnforceConstraints = true;

            ForeignKeyConstraint fc = null;

            fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]);

            //checking default
            // Default
            Assert.Equal(Rule.Cascade, fc.DeleteRule);

            //checking set/get
            foreach (Rule rule in Enum.GetValues(typeof(Rule)))
            {
                // Set/Get - rule
                fc.DeleteRule = rule;
                Assert.Equal(rule, fc.DeleteRule);
            }

            dtChild.Constraints.Add(fc);

            //checking delete rule

            // Rule = None, Delete Exception
            fc.DeleteRule = Rule.None;
            //Exception = "Cannot delete this row because constraints are enforced on relation Constraint1, and deleting this row will strand child rows."
            Assert.Throws <InvalidConstraintException>(() =>
            {
                dtParent.Rows.Find(1).Delete();
            });

            // Rule = None, Delete succeed
            fc.DeleteRule = Rule.None;
            foreach (DataRow dr in dtChild.Select("ParentId = 1"))
            {
                dr.Delete();
            }
            dtParent.Rows.Find(1).Delete();
            Assert.Equal(0, dtParent.Select("ParentId=1").Length);

            // Rule = Cascade
            fc.DeleteRule = Rule.Cascade;
            dtParent.Rows.Find(2).Delete();
            Assert.Equal(0, dtChild.Select("ParentId=2").Length);

            // Rule = SetNull
            DataSet ds1 = new DataSet();

            ds1.Tables.Add(DataProvider.CreateParentDataTable());
            ds1.Tables.Add(DataProvider.CreateChildDataTable());

            ForeignKeyConstraint fc1 = new ForeignKeyConstraint(ds1.Tables[0].Columns[0], ds1.Tables[1].Columns[1]);

            fc1.DeleteRule = Rule.SetNull;
            ds1.Tables[1].Constraints.Add(fc1);

            Assert.Equal(0, ds1.Tables[1].Select("ChildId is null").Length);

            ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] };
            ds1.Tables[0].Rows.Find(3).Delete();

            ds1.Tables[0].AcceptChanges();
            ds1.Tables[1].AcceptChanges();

            DataRow[] arr = ds1.Tables[1].Select("ChildId is null");

            /*foreach (DataRow dr in arr)
             *                          {
             *                                  Assert.Equal(null, dr["ChildId"]);
             *                          }*/

            Assert.Equal(4, arr.Length);

            // Rule = SetDefault
            //fc.DeleteRule = Rule.SetDefault;
            ds1 = new DataSet();
            ds1.Tables.Add(DataProvider.CreateParentDataTable());
            ds1.Tables.Add(DataProvider.CreateChildDataTable());

            fc1            = new ForeignKeyConstraint(ds1.Tables[0].Columns[0], ds1.Tables[1].Columns[1]);
            fc1.DeleteRule = Rule.SetDefault;
            ds1.Tables[1].Constraints.Add(fc1);
            ds1.Tables[1].Columns[1].DefaultValue = "777";

            //Add new row  --> in order to apply the forigen key rules
            DataRow dr2 = ds1.Tables[0].NewRow();

            dr2["ParentId"] = 777;
            ds1.Tables[0].Rows.Add(dr2);

            ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] };
            ds1.Tables[0].Rows.Find(3).Delete();
            Assert.Equal(4, ds1.Tables[1].Select("ChildId=777").Length);
        }
        private void RemoveAt_I()
        {
            DataTable dt = DataProvider.CreateUniqueConstraint();

            dt.Constraints.RemoveAt(2);
        }
        public void Generate()
        {
            DataTable tbl = DataProvider.CreateParentDataTable();

            // ----------- check with columnn type int -----------------------
            tbl.Columns[0].AllowDBNull = false;

            //add new row with null value
            // NoNullAllowedException - Add Row
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows.Add(new object[] { null, "value", "value", new DateTime(0), 0.5, true });
            });

            //add new row with DBNull value
            // NoNullAllowedException - Add Row
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows.Add(new object[] { DBNull.Value, "value", "value", new DateTime(0), 0.5, true });
            });

            // NoNullAllowedException - ItemArray
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows[0].ItemArray = new object[] { DBNull.Value, "value", "value", new DateTime(0), 0.5, true };
            });

            // NoNullAllowedException - Add Row - LoadDataRow
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.LoadDataRow(new object[] { DBNull.Value, "value", "value", new DateTime(0), 0.5, true }, true);
            });

            // NoNullAllowedException - EndEdit
            tbl.Rows[0].BeginEdit();
            tbl.Rows[0][0] = DBNull.Value;
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows[0].EndEdit();
            });

            // ----------- add new column -----------------------
            tbl.Columns[0].AllowDBNull = true;
            tbl.Columns.Add(new DataColumn("bolCol", typeof(bool)));

            // add new column
            Assert.Throws <DataException>(() =>
            {
                tbl.Columns[tbl.Columns.Count - 1].AllowDBNull = false;
            });

            //clear table data in order to add the new column
            tbl.Rows.Clear();
            tbl.Columns[tbl.Columns.Count - 1].AllowDBNull = false;
            tbl.Rows.Add(new object[] { 99, "value", "value", new DateTime(0), 0.5, true, false }); //missing last value - will be null

            //add new row with null value
            // NoNullAllowedException - Add Row
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows.Add(new object[] { 99, "value", "value", new DateTime(0), 0.5, true }); //missing last value - will be null
            });

            //add new row with DBNull value
            // NoNullAllowedException - Add Row
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows.Add(new object[] { 1, "value", "value", new DateTime(0), 0.5, true, DBNull.Value });
            });

            // NoNullAllowedException - ItemArray
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows[0].ItemArray = new object[] { 77, "value", "value", new DateTime(0), 0.5, true, DBNull.Value };
            });

            // NoNullAllowedException - Add Row - LoadDataRow
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.LoadDataRow(new object[] { 66, "value", "value", new DateTime(0), 0.5, true }, true);
            });

            // NoNullAllowedException - EndEdit
            tbl.Rows[0].BeginEdit();
            tbl.Rows[0][tbl.Columns.Count - 1] = DBNull.Value;
            Assert.Throws <NoNullAllowedException>(() =>
            {
                tbl.Rows[0].EndEdit();
            });
        }
        private void Item2()
        {
            DataTable dt = DataProvider.CreateUniqueConstraint();

            dt.Constraints[1].ConstraintName = "error";
        }
        public void CanRemove_ChildForeign()
        {
            DataSet ds = DataProvider.CreateForeignConstraint();

            Assert.True(ds.Tables["child"].Constraints.CanRemove(ds.Tables["child"].Constraints[0]));
        }
        public void CanRemove_ParentForeign()
        {
            DataSet ds = DataProvider.CreateForeignConstraint();

            Assert.False(ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]));
        }
        public void IsSynchronized()
        {
            DataTable dt = DataProvider.CreateUniqueConstraint();

            Assert.False(dt.Constraints.IsSynchronized);
        }
        public void IsReadOnly()
        {
            DataTable dt = DataProvider.CreateUniqueConstraint();

            Assert.False(dt.Constraints.IsReadOnly);
        }