public void TableValueParameters_WhenConstructedUsingDecimalsWithoutPrecisionAttribute_ReturnsPrecisionOfOriginalValue()
        {
            // ARRANGE
            const int expectedFirstFieldPrecision = 3;
            const int expectedSecondFieldPrecision = 4;
            const int expectedThirdFieldPrecision = 5;
            var itemList = new List<DecimalParameterTableType>
            {
                new DecimalParameterTableType { Value1 = 30.1M, Value2 = 30.02M, Value3 = 30.003M }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstFieldOfFirstRecord = firstRecord.GetSqlDecimal(0);
            var secondFieldOfFirstRecord = firstRecord.GetSqlDecimal(1);
            var thirdFieldOfFirstRecord = firstRecord.GetSqlDecimal(2);

            // ASSERT
            Assert.AreEqual(expectedFirstFieldPrecision, firstFieldOfFirstRecord.Precision);
            Assert.AreEqual(expectedSecondFieldPrecision, secondFieldOfFirstRecord.Precision);
            Assert.AreEqual(expectedThirdFieldPrecision, thirdFieldOfFirstRecord.Precision);
        }
        public void TableValueParameters_WhenConstructedUsingDecimalsWithPrecisionAttribute_ReturnsPrecisionOfAttributeForSqlMetatData()
        {
            // ARRANGE
            const int expectedFirstFieldPrecision = 7;
            const int expectedSecondFieldPrecision = 8;
            const int expectedThirdFieldPrecision = 9;
            var itemList = new List<DecimalParameterWithAttributeTableType>
            {
                new DecimalParameterWithAttributeTableType { Value1 = 30.11111111M, Value2 = 30.022222M, Value3 = 30.003333M }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(0);
            var secondFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(1);
            var thirdFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(2);

            // ASSERT
            Assert.AreEqual(expectedFirstFieldPrecision, firstFieldOfFirstRecordMetaData.Precision);
            Assert.AreEqual(expectedSecondFieldPrecision, secondFieldOfFirstRecordMetaData.Precision);
            Assert.AreEqual(expectedThirdFieldPrecision, thirdFieldOfFirstRecordMetaData.Precision);
        }
        public void TableValueParameters_WhenConstructedWithValidList_ReturnsCorrectFieldCount()
        {
            // ARRANGE
            const int expectedFieldCount = 3;
            var itemList = new List<SimpleParameterTableType>
            {
                new SimpleParameterTableType { Name = "Company 1", IsActive = true, Id = 1 },
                new SimpleParameterTableType { Name = "Company 2", IsActive = false, Id = 2 },
                new SimpleParameterTableType { Name = "Company 3", IsActive = true, Id = 3 },
                new SimpleParameterTableType { Name = "Company 4", IsActive = true, Id = 4 }
            };

            // ACT
            var actualSqlDataRecords = new TableValuedParameterBuilder(itemList)
                .BuildParameters()
                .TableValueParameters;

            // ASSERT
            Assert.AreEqual(expectedFieldCount, actualSqlDataRecords.First().FieldCount);
            Assert.AreEqual(expectedFieldCount, actualSqlDataRecords.Last().FieldCount);
        }
        public void TableValueParameters_WhenConstructedUsingStringsWithSizeAttribute_ReturnsSizeOfAttributeForSqlMetatDataMaxLength()
        {
            // ARRANGE
            const int expectedFirstFieldMaxLength = 5;
            const int expectedSecondFieldSizeMaxLength = 6;
            const int expectedThirdFieldSizeMaxLength = 7;
            var itemList = new List<StringParameterWithAttributeTableType>
            {
                new StringParameterWithAttributeTableType { Value1 = "123456", Value2 = "123456", Value3 = "123456" }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(0);
            var secondFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(1);
            var thirdFieldOfFirstRecordMetaData = firstRecord.GetSqlMetaData(2);

            // ASSERT
            Assert.AreEqual(expectedFirstFieldMaxLength, firstFieldOfFirstRecordMetaData.MaxLength);
            Assert.AreEqual(expectedSecondFieldSizeMaxLength, secondFieldOfFirstRecordMetaData.MaxLength);
            Assert.AreEqual(expectedThirdFieldSizeMaxLength, thirdFieldOfFirstRecordMetaData.MaxLength);
        }
        public void TableValueParameters_WhenConstructedUsingStringsWithoutSizeAttribute_ReturnsSizeOfOriginalValue()
        {
            // ARRANGE
            const int expectedFirstFieldSize = 3;
            const int expectedSecondFieldSize = 4;
            const int expectedThirdFieldSize = 5;
            var itemList = new List<StringParameterTableType>
            {
                new StringParameterTableType { Value1 = "123", Value2 = "1234", Value3 = "12345" }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstFieldOfFirstRecord = firstRecord.GetSqlString(0);
            var secondFieldOfFirstRecord = firstRecord.GetSqlString(1);
            var thirdFieldOfFirstRecord = firstRecord.GetSqlString(2);

            // ASSERT
            Assert.AreEqual(expectedFirstFieldSize, firstFieldOfFirstRecord.Value.Length);
            Assert.AreEqual(expectedSecondFieldSize, secondFieldOfFirstRecord.Value.Length);
            Assert.AreEqual(expectedThirdFieldSize, thirdFieldOfFirstRecord.Value.Length);
        }
        public void TableValueParameters_WhenConstructedUsingListWithoutDbTypeAttribute_ReturnsFieldsWithOriginalDataType()
        {
            // ARRANGE
            const SqlDbType expectedField1SqlDbType = SqlDbType.Int;
            const SqlDbType expectedField2SqlDbType = SqlDbType.Bit;
            const SqlDbType expectedField3SqlDbType = SqlDbType.NVarChar;
            var itemList = new List<SimpleParameterTableType>
            {
                new SimpleParameterTableType { Name = "Company 1", IsActive = true, Id = 1 }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstRecordFirstMetaData = firstRecord.GetSqlMetaData(0);
            var firstRecordSecondMetaData = firstRecord.GetSqlMetaData(1);
            var firstRecordThirdMetaData = firstRecord.GetSqlMetaData(2);

            // ASSERT
            Assert.AreEqual(expectedField1SqlDbType, firstRecordFirstMetaData.SqlDbType);
            Assert.AreEqual(expectedField2SqlDbType, firstRecordSecondMetaData.SqlDbType);
            Assert.AreEqual(expectedField3SqlDbType, firstRecordThirdMetaData.SqlDbType);
        }
        public void TableValueParameters_WhenConstructedUsingListWithNameAttributes_ReturnsMetaDataFieldsWithAttributeNames()
        {
            // ARRANGE
            const string expectedField1Name = "RecordId";
            const string expectedField2Name = "Active";
            const string expectedField3Name = "Fullname";
            var itemList = new List<SimpleParameterWithNameAttributeTableType>
            {
                new SimpleParameterWithNameAttributeTableType { Name = "Company 1", IsActive = true, Id = 1 }
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList).BuildParameters().TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstRecordFirstMetaData = firstRecord.GetSqlMetaData(0);
            var firstRecordSecondMetaData = firstRecord.GetSqlMetaData(1);
            var firstRecordThirdMetaData = firstRecord.GetSqlMetaData(2);

            // ASSERT
            Assert.AreEqual(expectedField1Name, firstRecordFirstMetaData.Name);
            Assert.AreEqual(expectedField2Name, firstRecordSecondMetaData.Name);
            Assert.AreEqual(expectedField3Name, firstRecordThirdMetaData.Name);
        }
        public void TableValueParameters_WhenConstructedUsingListWithDbTypeAttributeAndIncorrectType_ThrowsException()
        {
            // ARRANGE
            const SqlDbType expectedField1SqlDbType = SqlDbType.BigInt;
            var itemList = new List<SimpleParameterWithDbTypeAttributeTableType>
            {
                new SimpleParameterWithDbTypeAttributeTableType { Id = 1}
            };

            // ACT
            IEnumerable<SqlDataRecord> actualSqlDataRecords = new TableValuedParameterBuilder(itemList)
                .BuildParameters()
                .TableValueParameters;
            SqlDataRecord firstRecord = actualSqlDataRecords.First();
            var firstRecordFirstMetaData = firstRecord.GetSqlMetaData(0);

            // ASSERT
            Assert.AreEqual(expectedField1SqlDbType, firstRecordFirstMetaData.SqlDbType);
        }