Exemple #1
0
        public void TestInvalidArgumentsBuildConnection()
        {
            var attribute = new SqlAttribute("");

            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.BuildConnection(attribute.ConnectionStringSetting, config.Object));

            attribute = new SqlAttribute("");
            attribute.ConnectionStringSetting = "ConnectionStringSetting";
            Assert.Throws <ArgumentNullException>(() => SqlBindingUtilities.BuildConnection(attribute.ConnectionStringSetting, null));
        }
Exemple #2
0
        public void TestInvalidCommandType()
        {
            // Specify an invalid type
            var attribute = new SqlAttribute("");

            attribute.CommandType = System.Data.CommandType.TableDirect;
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.BuildCommand(attribute, null));


            // Don't specify a type at all
            attribute = new SqlAttribute("");
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.BuildCommand(attribute, null));
        }
Exemple #3
0
        public void TestValidCommandType()
        {
            var query     = "select * from Products";
            var attribute = new SqlAttribute(query);

            attribute.CommandType = System.Data.CommandType.Text;
            var command = SqlBindingUtilities.BuildCommand(attribute, null);

            Assert.Equal(System.Data.CommandType.Text, command.CommandType);
            Assert.Equal(query, command.CommandText);

            var procedure = "StoredProceudre";

            attribute             = new SqlAttribute(procedure);
            attribute.CommandType = System.Data.CommandType.StoredProcedure;
            command = SqlBindingUtilities.BuildCommand(attribute, null);
            Assert.Equal(System.Data.CommandType.StoredProcedure, command.CommandType);
            Assert.Equal(procedure, command.CommandText);
        }
Exemple #4
0
        public void TestMalformedParametersString()
        {
            var command = new SqlCommand();
            // Second param name doesn't start with "@"
            string parameters = "@param1=param1,param2=param2";

            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));

            // Second param not separated by "=", or contains extra "="
            parameters = "@param1=param1,@param2==param2";
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));
            parameters = "@param1=param1,@param2;param2";
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));
            parameters = "@param1=param1,@param2=param2=";
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));

            // Params list not separated by "," correctly
            parameters = "@param1=param1;@param2=param2";
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));
            parameters = "@param1=param1,@par,am2=param2";
            Assert.Throws <ArgumentException>(() => SqlBindingUtilities.ParseParameters(parameters, command));
        }
Exemple #5
0
 public void TestNullCommand()
 {
     Assert.Throws <ArgumentNullException>(() => SqlBindingUtilities.ParseParameters("", null));
 }
Exemple #6
0
        public void TestWellformedParametersString()
        {
            var    command    = new SqlCommand();
            string parameters = "@param1=param1,@param2=param2";

            SqlBindingUtilities.ParseParameters(parameters, command);

            // Apparently SqlParameter doesn't implement an Equals method, so have to do this manually
            Assert.Equal(2, command.Parameters.Count);
            foreach (SqlParameter param in command.Parameters)
            {
                Assert.True(param.ParameterName.Equals("@param1") || param.ParameterName.Equals("@param2"));
                if (param.ParameterName.Equals("@param1"))
                {
                    Assert.True(param.Value.Equals("param1"));
                }
                else
                {
                    Assert.True(param.Value.Equals("param2"));
                }
            }

            // Confirm we throw away empty entries at the beginning/end and ignore multiple commas in between
            // parameter pairs
            command    = new SqlCommand();
            parameters = ",,@param1=param1,,@param2=param2,,,";
            SqlBindingUtilities.ParseParameters(parameters, command);

            Assert.Equal(2, command.Parameters.Count);
            foreach (SqlParameter param in command.Parameters)
            {
                Assert.True(param.ParameterName.Equals("@param1") || param.ParameterName.Equals("@param2"));
                if (param.ParameterName.Equals("@param1"))
                {
                    Assert.True(param.Value.Equals("param1"));
                }
                else
                {
                    Assert.True(param.Value.Equals("param2"));
                }
            }

            // Confirm we interpret "null" as being a NULL parameter value, and that we interpret
            // a string like "@param1=,@param2=param2" as @param1 having an empty string as its value
            command    = new SqlCommand();
            parameters = "@param1=,@param2=null";
            SqlBindingUtilities.ParseParameters(parameters, command);

            Assert.Equal(2, command.Parameters.Count);
            foreach (SqlParameter param in command.Parameters)
            {
                Assert.True(param.ParameterName.Equals("@param1") || param.ParameterName.Equals("@param2"));
                if (param.ParameterName.Equals("@param1"))
                {
                    Assert.True(param.Value.Equals(string.Empty));
                }
                else
                {
                    Assert.True(param.Value.Equals(DBNull.Value));
                }
            }

            // Confirm nothing is done when parameters are not specified
            command    = new SqlCommand();
            parameters = null;
            SqlBindingUtilities.ParseParameters(parameters, command);
            Assert.Equal(0, command.Parameters.Count);
        }