Falcon.SugarApi/Falcon.SugarApi.Test/IEnumerableExtendTest.cs

90 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Falcon.SugarApi.Test
{
/// <summary>
/// 枚举类型扩展方法
/// </summary>
[TestClass]
public class IEnumerableExtendTest
{
[TestMethod("ToDataTable测试")]
public void ToDataTableTest() {
var sw = new Stopwatch();
var list = new List<dynamic> {
new{a=1,b="b1" },
new{a=2,b="b2",c=1 },
};
sw.Start();
var r = list.ToDataTable();
sw.Stop();
Console.WriteLine($"ToDataTable转换共用时{sw.ElapsedMilliseconds}毫秒");
//测试表
Assert.IsNotNull(r);
Assert.AreEqual(3, r.Columns.Count);
Assert.AreEqual(2, r.Rows.Count);
//测试列
var col1 = r.Columns[0];
Assert.IsTrue(col1.ColumnName == "a");
Assert.IsTrue(col1.DataType == typeof(int));
var col2 = r.Columns[1];
Assert.IsTrue(col2.ColumnName == "b");
Assert.IsTrue(col2.DataType == typeof(string));
var col3 = r.Columns[2];
Assert.IsTrue(col3.ColumnName == "c");
Assert.IsTrue(col3.DataType == typeof(int));
//测试行
var row = r.Rows[0];
Assert.IsTrue(row[0].ToString() == "1");
Assert.IsTrue(row[1].ToString() == "b1");
Assert.IsTrue(row[2].ToString() == "");
row = r.Rows[1];
Assert.IsTrue((int)row[0] == 2);
Assert.IsTrue((string)row[1] == "b2");
Assert.IsTrue((int)row[2] == 1);
Console.WriteLine($"ToDataTable测试完成");
var models = new List<ToDataTableTestModel> {
new ToDataTableTestModel{ id=1, },
new ToDataTableTestModel{id=2,Name="name2" },
};
sw.Reset();
r = models.ToDataTable();
sw.Stop();
Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒");
//测试表
Assert.IsNotNull(r);
Assert.AreEqual(2, r.Columns.Count);
Assert.AreEqual(2, r.Rows.Count);
//测试列
col1 = r.Columns[0];
Assert.IsTrue(col1.ColumnName == "id");
Assert.IsTrue(col1.DataType == typeof(int));
col2 = r.Columns[1];
Assert.IsTrue(col2.ColumnName == "Name");
Assert.IsTrue(col2.DataType == typeof(string));
//测试行
row = r.Rows[0];
Assert.IsTrue(row[0].ToString() == "1");
Assert.IsTrue(row[1].ToString() == "");
row = r.Rows[1];
Assert.IsTrue((int)row[0] == 2);
Assert.IsTrue(row[1].ToString() == "name2");
Console.WriteLine($"ToDataTable<>测试完成");
}
}
internal class ToDataTableTestModel
{
public int id { get; set; }
public string Name { get; set; }
}
}