Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectRecord" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The table definition.</param>
        /// <param name="randomData">if set to <see langword="true" /> fills columns with random data; otherwise fills them with their default values.</param>
        /// <param name="nullProbability">The probability of a column's value being set to SQL null (0.0 for no nulls) -
        /// this is only applicable is <paramref name="randomData" /> is set to <see langword="true" /> [Defaults to 0.1 = 10%].</param>
        /// <param name="columnGenerators">The column generators is an array of functions that generate a value for each column, if the function is
        /// <see langword="null" /> for a particular index then a random value is generated, if it is not null then the function is used.  The function takes
        /// the current row number as it's only parameter and must return an object of the correct type for the column.</param>
        /// <param name="rowNumber">The optional row number to pass to the generator.</param>
        /// <exception cref="System.ArgumentException">Thrown if the number of column generators exceeds the number of columns in the record set definition.</exception>
        public ObjectRecord(
            [NotNull] RecordSetDefinition recordSetDefinition,
            bool randomData = false,
            double nullProbability = 0.1,
            [CanBeNull] Func<int, object>[] columnGenerators = null,
            int rowNumber = 1)
        {
            if (recordSetDefinition == null) throw new ArgumentNullException("recordSetDefinition");

            _recordSetDefinition = recordSetDefinition;
            int columnCount = recordSetDefinition.FieldCount;
            _columnValues = new object[columnCount];

            if ((columnGenerators != null) &&
                (columnGenerators.Length > recordSetDefinition.FieldCount))
                throw new ArgumentException(
                    "The number of column generators must not exceed the number of columns in the record set definition.",
                    "columnGenerators");

            for (int c = 0; c < columnCount; c++)
                // Check if we have a generator
                if ((columnGenerators != null) &&
                    (columnGenerators.Length > c) &&
                    (columnGenerators[c] != null))
                    // Use generator to get value
                    this[c] = columnGenerators[c](rowNumber);
                else if (randomData)
                    // Generate random value.
                    this[c] = recordSetDefinition[c].GetRandomValue(nullProbability);
                else
                // Just set to default value (no need to revalidate so set directly).
                    _columnValues[c] = recordSetDefinition[c].DefaultValue;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectRecord" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The table definition.</param>
        /// <param name="randomData">if set to <see langword="true" /> fills columns with random data; otherwise fills them with their default values.</param>
        /// <param name="nullProbability">The probability of a column's value being set to SQL null (0.0 for no nulls) -
        /// this is only applicable is <paramref name="randomData" /> is set to <see langword="true" /> [Defaults to 0.1 = 10%].</param>
        /// <param name="columnGenerators">The column generators is an array of functions that generate a value for each column, if the function is
        /// <see langword="null" /> for a particular index then a random value is generated, if it is not null then the function is used.  The function takes
        /// the current row number as it's only parameter and must return an object of the correct type for the column.</param>
        /// <param name="rowNumber">The optional row number to pass to the generator.</param>
        /// <exception cref="System.ArgumentException">Thrown if the number of column generators exceeds the number of columns in the record set definition.</exception>
        public ObjectRecord(
            [NotNull] RecordSetDefinition recordSetDefinition,
            bool randomData        = false,
            double nullProbability = 0.1,
            [CanBeNull] Func <int, object>[] columnGenerators = null,
            int rowNumber = 1)
        {
            if (recordSetDefinition == null)
            {
                throw new ArgumentNullException("recordSetDefinition");
            }

            _recordSetDefinition = recordSetDefinition;
            int columnCount = recordSetDefinition.FieldCount;

            _columnValues = new object[columnCount];

            if ((columnGenerators != null) &&
                (columnGenerators.Length > recordSetDefinition.FieldCount))
            {
                throw new ArgumentException(
                          "The number of column generators must not exceed the number of columns in the record set definition.",
                          "columnGenerators");
            }

            for (int c = 0; c < columnCount; c++)
            {
                // Check if we have a generator
                if ((columnGenerators != null) &&
                    (columnGenerators.Length > c) &&
                    (columnGenerators[c] != null))
                {
                    // Use generator to get value
                    this[c] = columnGenerators[c](rowNumber);
                }
                else if (randomData)
                {
                    // Generate random value.
                    this[c] = recordSetDefinition[c].GetRandomValue(nullProbability);
                }
                else
                {
                    // Just set to default value (no need to revalidate so set directly).
                    _columnValues[c] = recordSetDefinition[c].DefaultValue;
                }
            }
        }
Example #3
0
        private static IEnumerable <IObjectRecord> GenerateRecords(
            [NotNull] RecordSetDefinition recordSetDefinition,
            int minRows,
            int maxRows,
            double nullProbability,
            [CanBeNull] Func <int, object>[] columnGenerators = null)
        {
            if (recordSetDefinition == null)
            {
                throw new ArgumentNullException("recordSetDefinition");
            }
            if (minRows < 0)
            {
                throw new ArgumentOutOfRangeException("minRows");
            }
            if (maxRows < 0)
            {
                throw new ArgumentOutOfRangeException("maxRows");
            }
            if (minRows > maxRows)
            {
                throw new ArgumentOutOfRangeException("maxRows");
            }

            // Calculate number of rows.
            int rows = minRows == maxRows
                ? minRows
                : Tester.RandomGenerator.Next(minRows, maxRows);

            if (rows < 1)
            {
                return(Enumerable.Empty <IObjectRecord>());
            }

            // Create random records
            List <IObjectRecord> records = new List <IObjectRecord>();

            for (int r = 0; r < rows; r++)
            {
                records.Add(new ObjectRecord(recordSetDefinition, true, nullProbability, columnGenerators, r + 1));
            }

            return(records);
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectSet" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The record set definition.</param>
        /// <param name="records">The records.</param>
        /// <remarks></remarks>
        public ObjectSet([NotNull] RecordSetDefinition recordSetDefinition, IEnumerable <IObjectRecord> records = null)
        {
            if (recordSetDefinition == null)
            {
                throw new ArgumentNullException("recordSetDefinition");
            }

            _definition = recordSetDefinition;

            if (records == null)
            {
                return;
            }

            foreach (IObjectRecord record in records)
            {
                Add(record);
            }
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectRecord" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The table definition.</param>
        /// <param name="columnValues">The column values.</param>
        /// <remarks>
        /// If the number of column values supplied is less than the number of columns then the remaining columns are set to
        /// their equivalent default value.
        /// </remarks>
        public ObjectRecord([NotNull] RecordSetDefinition recordSetDefinition, [NotNull] params object[] columnValues)
        {
            if (recordSetDefinition == null) throw new ArgumentNullException("recordSetDefinition");
            if (columnValues == null) throw new ArgumentNullException("columnValues");

            int length = columnValues.Length;
            int columns = recordSetDefinition.FieldCount;
            if (length > columns)
                throw new ArgumentException(
                    string.Format(
                        "The number of values specified '{0}' cannot exceed the number of expected columns '{1}'.",
                        length,
                        columns),
                    "columnValues");

            _recordSetDefinition = recordSetDefinition;
            _columnValues = new object[recordSetDefinition.FieldCount];

            // Import values or set to null.
            for (int i = 0; i < columns; i++)
                SetValue(i, i < length ? columnValues[i] : _recordSetDefinition[i].DefaultValue);
        }
Example #6
0
        public void RecordExample()
        {
            // To create a record that implement IDataRecord we start with a record set definition.
            RecordSetDefinition recordSetDefinition = new RecordSetDefinition(
                new ColumnDefinition("ID", SqlDbType.Int),
                new ColumnDefinition("Name", SqlDbType.Char, 50),
                new ColumnDefinition("Description", SqlDbType.NVarChar),
                // This column is not nullable so defaults to true
                new ColumnDefinition("Active", SqlDbType.Bit, isNullable: false, defaultValue: true)
                );

            // Now we can create a record
            IObjectRecord dataRecord = new ObjectRecord(recordSetDefinition, 1, "Test", "This is my test record");

            // Or we can create one with random values
            IObjectRecord randomRecord = new ObjectRecord(recordSetDefinition, true);

            // To create a record that throws an exception we first create a SqlException
            // We can't do this directly, but we can use our prototypes to construct one.

            // SqlExceptions are made from a collection of SqlErrors - which can make like this :
            SqlErrorCollection errorCollection = new SqlErrorCollectionPrototype
            {
                new SqlErrorPrototype(
                    1000,
                    80,
                    17,
                    "MyFakeServer",
                    "Connection Timeout.",
                    "spMySproc",
                    54)
            };

            SqlException sqlException = new SqlExceptionPrototype(errorCollection, "9.0.0.0", Guid.NewGuid());
            IObjectRecord exceptionRecord = new ExceptionRecord(sqlException);

            // We can stick these records into a recordset
            // Note the records must have the same RecordSetDefinition (unless it's an exception record)
            // The final record will through an exception when reached!
            ObjectSet recordSet = new ObjectSet(recordSetDefinition)
            {
                dataRecord,
                randomRecord,
                //exceptionRecord
            };

            // We can add recordsets to an ObjectReader
            ObjectReader reader = new ObjectReader
            {
                recordSet
            };

            // We can also add random record sets - this one has the same definition as the first.
            reader.Add(new RandomSet(recordSetDefinition));

            // We can also fix certain rows values using the column generators arry, a null indicates
            // that the column should us a random value, otherwise a lambda can be supplied - in this case
            // it sets the row to the row number (1 - indexed).
            reader.Add(
                new RandomSet(
                    recordSetDefinition,
                    columnGenerators: new Func<int, object>[] { null, row => "Row #" + row }));

            // Whereas this one has a random set of columns (with random types).
            reader.Add(new RandomSet(10));

            // Now that we have a reader we can use it like a normal reader - it even simulates disposal.
            using (IDataReader dataReader = reader)
            {
                int recordset = 1;
                do
                {
                    Trace.Write("Recordset #" + recordset);
                    int rows = 0;
                    while (dataReader.Read())
                        rows++;
                    Trace.WriteLine(" - " + rows + " rows.");
                    recordset++;
                } while (dataReader.NextResult());
            }
        }
Example #7
0
        public void TestCustomException()
        {
            // To create a record that implement IDataRecord we start with a record set definition.
            RecordSetDefinition recordSetDefinition = new RecordSetDefinition(
                new ColumnDefinition("ID", SqlDbType.Int),
                new ColumnDefinition("Name", SqlDbType.Char, 50),
                new ColumnDefinition("Description", SqlDbType.NVarChar),
                // This column is not nullable so defaults to true
                new ColumnDefinition("Active", SqlDbType.Bit, isNullable: false, defaultValue: true)
                );

            // Now we can create a record
            IObjectRecord dataRecord = new ObjectRecord(recordSetDefinition, 1, "Test", "This is my test record");

            // To create a record that throws an exception we first create an ExceptionRecord
            IObjectRecord exceptionRecord = new ExceptionRecord(new SqlInvalidSyntaxException());

            // We can stick these records into a recordset
            // Note the records must have the same RecordSetDefinition (unless it's an exception record)
            // The final record will through an exception when reached!
            ObjectSet recordSet = new ObjectSet(recordSetDefinition)
            {
                dataRecord,
                exceptionRecord
            };

            // We can add recordsets to an ObjectReader
            ObjectReader reader = new ObjectReader
            {
                recordSet
            };

            // Now that we have a reader we can use it like a normal reader - it even simulates disposal.
            ReadFromRecordSet(reader);
        }