Esempio n. 1
0
        public void TestInsert()
        {
            var students = StudentHelper.CreateSample(10).ToArray();
            using (var sqlConnection = DatabaseHelper.OpenSqlConnection(DatabaseName))
            {
                var sqlUpserter = new SqlUpserter<Student>(StudentMapper, TableName, students);
                sqlUpserter.Execute(sqlConnection);

                var selectText = "SELECT " + string.Join(",", StudentMapper.Columns.Select(column => column.Name)) +
                                 " FROM " + TableName;
                var reader = new SqlCommand(selectText, sqlConnection).ExecuteReader();
                var row = 0;
                while (reader.Read())
                {
                    var student = students[row];
                    for (var column = 0; column < reader.FieldCount; column++)
                    {
                        var expected = StudentMapper.GetValueAt(column, student);
                        var actual = reader.GetValue(column);
                        Assert.AreEqual(expected, actual);
                    }
                    row++;
                }
            }
        }
        public void TestPerformance(int count)
        {
            var students = StudentHelper.CreateSample(count).ToArray();
            var sqlTableMapper = StudentHelper.CreateSqlTableMapper();

            using (var sqlConnection = DatabaseHelper.OpenSqlConnection(DatabaseName))
            {
                var sw = new Stopwatch();
                sw.Start();
                var sqlUpserter = new SqlUpserter<Student>(sqlTableMapper, TableName, students);
                sqlUpserter.Execute(sqlConnection);

                sw.Stop();
                var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestPerformance.txt");
                File.AppendAllText(path, string.Format("#{0}: {1}ms\r\n", count, sw.ElapsedMilliseconds));
            }
        }
Esempio n. 3
0
        public void TestUpdate()
        {
            var students = StudentHelper.CreateSample(10).ToArray();
            using (var sqlConnection = DatabaseHelper.OpenSqlConnection(DatabaseName))
            {
                var sqlUpserter1 = new SqlUpserter<Student>(StudentMapper, TableName, students);
                sqlUpserter1.Execute(sqlConnection);

                var expectedChanges = 0;
                foreach (var student in students)
                {
                    if (student.Id%2 == 0)
                    {
                        student.FirstName = student.FirstName + "_Changed";
                        expectedChanges++;
                    }
                }

                var sqlUpserter2 = new SqlUpserter<Student>(StudentMapper, TableName, students);
                sqlUpserter2.Execute(sqlConnection);

                var selectText = "SELECT " + string.Join(",", StudentMapper.Columns.Select(column => column.Name)) +
                                 " FROM " + TableName;
                var reader = new SqlCommand(selectText, sqlConnection).ExecuteReader();
                var row = 0;
                var currentChanges = 0;
                while (reader.Read())
                {
                    var student = students[row];
                    for (var column = 0; column < reader.FieldCount; column++)
                    {
                        var expected = StudentMapper.GetValueAt(column, student);
                        var actual = reader.GetValue(column);
                        if (actual.ToString().EndsWith("_Changed"))
                        {
                            currentChanges++;
                        }
                        Assert.AreEqual(expected, actual);
                    }
                    row++;
                }
                Assert.AreEqual(expectedChanges, currentChanges);
            }
        }