1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Data ;
4
+ using System . Linq ;
2
5
using NUnit . Framework ;
3
- using ServiceStack . OrmLite . SqlServer ;
6
+ using ServiceStack . Common ;
7
+ using ServiceStack . Text ;
4
8
5
9
namespace ServiceStack . OrmLite . Tests
6
10
{
7
- [ TestFixture ]
8
- public class SqlServerProviderTests
9
- {
10
- [ TestFixtureSetUp ]
11
- public void TestFixtureSetUp ( )
12
- {
13
- OrmLiteConfig . DialectProvider = SqlServerOrmLiteDialectProvider . Instance ;
14
- }
15
-
16
- public class OrderDataSubset
17
- {
18
- public DateTime CreatedDate { get ; set ; }
19
- }
20
-
21
- [ Ignore ( "Integration test" ) ] [ Test ]
22
- public void Can_do_SqlServer_DateTime_fields ( )
23
- {
24
- var connString = "Data Source=chi-prod-analytics-db;Initial Catalog=2010-04_MonthlySnapshot;User Id=admin;Password=xxx;" ;
25
-
26
- using ( var db = connString . OpenDbConnection ( ) )
27
- {
28
- var order = db . First < OrderDataSubset > ( "SELECT TOP 1 CreatedDate FROM OrderData" ) ;
29
- Console . WriteLine ( order ) ;
30
- }
31
- }
32
- }
11
+ public class DummyTable
12
+ {
13
+ public int Id { get ; set ; }
14
+ public string Name { get ; set ; }
15
+ }
16
+
17
+ [ TestFixture ]
18
+ public class SqlServerProviderTests
19
+ {
20
+ private IDbConnection db ;
21
+
22
+ [ TestFixtureSetUp ]
23
+ public void TestFixtureSetUp ( )
24
+ {
25
+ db = Config . OpenDbConnection ( ) ;
26
+ }
27
+
28
+ [ TestFixtureTearDown ]
29
+ public void TearDown ( )
30
+ {
31
+ db . Dispose ( ) ;
32
+ }
33
+
34
+ [ Test ]
35
+ public void Can_SqlList_StoredProc_returning_Table ( )
36
+ {
37
+ var sql = @"CREATE PROCEDURE dbo.DummyTable
38
+ @Times integer
39
+ AS
40
+ BEGIN
41
+ SET NOCOUNT ON;
42
+
43
+ CREATE TABLE #Temp
44
+ (
45
+ Id integer NOT NULL,
46
+ Name nvarchar(50) COLLATE DATABASE_DEFAULT NOT NULL
47
+ );
48
+
49
+ declare @i int
50
+ set @i=1
51
+ WHILE @i < @Times
52
+ BEGIN
53
+ INSERT INTO #Temp (Id, Name) VALUES (@i, CAST(@i as nvarchar))
54
+ SET @i = @i + 1
55
+ END
56
+
57
+ SELECT * FROM #Temp;
58
+
59
+ DROP TABLE #Temp;
60
+ END;" ;
61
+ db . ExecuteSql ( "IF OBJECT_ID('DummyTable') IS NOT NULL DROP PROC DummyTable" ) ;
62
+ db . ExecuteSql ( sql ) ;
63
+
64
+ var expected = 0 ;
65
+ 10 . Times ( i => expected += i ) ;
66
+
67
+ var results = db . SqlList < DummyTable > ( "EXEC DummyTable @Times" , new { Times = 10 } ) ;
68
+ results . PrintDump ( ) ;
69
+ Assert . That ( results . Sum ( x => x . Id ) , Is . EqualTo ( expected ) ) ;
70
+
71
+ results = db . SqlList < DummyTable > ( "EXEC DummyTable 10" ) ;
72
+ Assert . That ( results . Sum ( x => x . Id ) , Is . EqualTo ( expected ) ) ;
73
+
74
+ results = db . SqlList < DummyTable > ( "EXEC DummyTable @Times" , new Dictionary < string , object > { { "Times" , 10 } } ) ;
75
+ Assert . That ( results . Sum ( x => x . Id ) , Is . EqualTo ( expected ) ) ;
76
+ }
77
+
78
+ [ Test ]
79
+ public void Can_SqlList_StoredProc_returning_Column ( )
80
+ {
81
+ var sql = @"CREATE PROCEDURE dbo.DummyColumn
82
+ @Times integer
83
+ AS
84
+ BEGIN
85
+ SET NOCOUNT ON;
86
+
87
+ CREATE TABLE #Temp
88
+ (
89
+ Id integer NOT NULL,
90
+ );
91
+
92
+ declare @i int
93
+ set @i=1
94
+ WHILE @i < @Times
95
+ BEGIN
96
+ INSERT INTO #Temp (Id) VALUES (@i)
97
+ SET @i = @i + 1
98
+ END
99
+
100
+ SELECT * FROM #Temp;
101
+
102
+ DROP TABLE #Temp;
103
+ END;" ;
104
+ db . ExecuteSql ( "IF OBJECT_ID('DummyColumn') IS NOT NULL DROP PROC DummyColumn" ) ;
105
+ db . ExecuteSql ( sql ) ;
106
+
107
+ var expected = 0 ;
108
+ 10 . Times ( i => expected += i ) ;
109
+
110
+ var results = db . SqlList < int > ( "EXEC DummyColumn @Times" , new { Times = 10 } ) ;
111
+ results . PrintDump ( ) ;
112
+ Assert . That ( results . Sum ( ) , Is . EqualTo ( expected ) ) ;
113
+
114
+ results = db . SqlList < int > ( "EXEC DummyColumn 10" ) ;
115
+ Assert . That ( results . Sum ( ) , Is . EqualTo ( expected ) ) ;
116
+
117
+ results = db . SqlList < int > ( "EXEC DummyTable @Times" , new Dictionary < string , object > { { "Times" , 10 } } ) ;
118
+ Assert . That ( results . Sum ( ) , Is . EqualTo ( expected ) ) ;
119
+ }
120
+
121
+ [ Test ]
122
+ public void Can_SqlList_StoredProc_returning_Scalar ( )
123
+ {
124
+ var sql = @"CREATE PROCEDURE dbo.DummyScalar
125
+ @Times integer
126
+ AS
127
+ BEGIN
128
+ SET NOCOUNT ON;
129
+
130
+ SELECT @Times AS Id
131
+ END;" ;
132
+ db . ExecuteSql ( "IF OBJECT_ID('DummyScalar') IS NOT NULL DROP PROC DummyScalar" ) ;
133
+ db . ExecuteSql ( sql ) ;
134
+
135
+ const int expected = 10 ;
136
+
137
+ var result = db . SqlScalar < int > ( "EXEC DummyScalar @Times" , new { Times = 10 } ) ;
138
+ result . PrintDump ( ) ;
139
+ Assert . That ( result , Is . EqualTo ( expected ) ) ;
140
+
141
+ result = db . SqlScalar < int > ( "EXEC DummyScalar 10" ) ;
142
+ Assert . That ( result , Is . EqualTo ( expected ) ) ;
143
+
144
+ result = db . SqlScalar < int > ( "EXEC DummyScalar @Times" , new Dictionary < string , object > { { "Times" , 10 } } ) ;
145
+ Assert . That ( result , Is . EqualTo ( expected ) ) ;
146
+ }
147
+
148
+ }
33
149
}
0 commit comments