1
1
using System ;
2
+ using System . Data ;
2
3
using NUnit . Framework ;
3
4
using ServiceStack . DataAnnotations ;
4
5
using ServiceStack . Text ;
@@ -12,6 +13,8 @@ public class DefaultValues
12
13
[ Default ( 1 ) ]
13
14
public int DefaultInt { get ; set ; }
14
15
16
+ public int DefaultIntNoDefault { get ; set ; }
17
+
15
18
[ Default ( 1 ) ]
16
19
public int ? NDefaultInt { get ; set ; }
17
20
@@ -39,28 +42,62 @@ public void Can_create_table_with_DefaultValues()
39
42
{
40
43
using ( var db = OpenDbConnection ( ) )
41
44
{
42
- db . DropAndCreateTable < DefaultValues > ( ) ;
45
+ var row = CreateAndInitialize ( db ) ;
46
+
47
+ var expectedDate = Dialect != Dialect . MySql && Dialect != Dialect . Firebird
48
+ ? DateTime . UtcNow . Date
49
+ : DateTime . Now . Date ; //MySql CURRENT_TIMESTAMP == LOCAL_TIME
50
+
51
+ Assert . That ( row . CreatedDateUtc , Is . GreaterThan ( expectedDate ) ) ;
52
+ Assert . That ( row . NCreatedDateUtc , Is . GreaterThan ( expectedDate ) ) ;
53
+ }
54
+ }
55
+
56
+ private DefaultValues CreateAndInitialize ( IDbConnection db )
57
+ {
58
+ db . DropAndCreateTable < DefaultValues > ( ) ;
43
59
44
- db . GetLastSql ( ) . Print ( ) ;
60
+ db . GetLastSql ( ) . Print ( ) ;
45
61
46
- db . Insert ( new DefaultValues { Id = 1 } ) ;
62
+ db . Insert ( new DefaultValues { Id = 1 } ) ;
63
+
64
+
65
+ var row = db . SingleById < DefaultValues > ( 1 ) ;
66
+
67
+ row . PrintDump ( ) ;
68
+ Assert . That ( row . DefaultInt , Is . EqualTo ( 1 ) ) ;
69
+ Assert . That ( row . DefaultIntNoDefault , Is . EqualTo ( 0 ) ) ;
70
+ Assert . That ( row . NDefaultInt , Is . EqualTo ( 1 ) ) ;
71
+ Assert . That ( row . DefaultDouble , Is . EqualTo ( 1.1 ) . Within ( .1d ) ) ;
72
+ Assert . That ( row . NDefaultDouble , Is . EqualTo ( 1.1 ) . Within ( .1d ) ) ;
73
+ Assert . That ( row . DefaultString , Is . EqualTo ( "String" ) ) ;
74
+
75
+ return row ;
76
+ }
77
+
78
+ [ Test ]
79
+ public void Can_use_ToUpdateStatement_to_generate_inline_SQL ( )
80
+ {
81
+ using ( var db = OpenDbConnection ( ) )
82
+ {
83
+ CreateAndInitialize ( db ) ;
47
84
48
85
var row = db . SingleById < DefaultValues > ( 1 ) ;
86
+ row . DefaultIntNoDefault = 42 ;
87
+
88
+ var sql = db . ToUpdateStatement ( row ) ;
89
+ sql . Print ( ) ;
90
+ db . ExecuteSql ( sql ) ;
91
+
92
+ row = db . SingleById < DefaultValues > ( 1 ) ;
49
93
50
- row . PrintDump ( ) ;
51
94
Assert . That ( row . DefaultInt , Is . EqualTo ( 1 ) ) ;
95
+ Assert . That ( row . DefaultIntNoDefault , Is . EqualTo ( 42 ) ) ;
52
96
Assert . That ( row . NDefaultInt , Is . EqualTo ( 1 ) ) ;
53
97
Assert . That ( row . DefaultDouble , Is . EqualTo ( 1.1 ) . Within ( .1d ) ) ;
54
98
Assert . That ( row . NDefaultDouble , Is . EqualTo ( 1.1 ) . Within ( .1d ) ) ;
55
99
Assert . That ( row . DefaultString , Is . EqualTo ( "String" ) ) ;
56
-
57
- var expectedDate = Dialect != Dialect . MySql && Dialect != Dialect . Firebird
58
- ? DateTime . UtcNow . Date
59
- : DateTime . Now . Date ; //MySql CURRENT_TIMESTAMP == LOCAL_TIME
60
-
61
- Assert . That ( row . CreatedDateUtc , Is . GreaterThan ( expectedDate ) ) ;
62
- Assert . That ( row . NCreatedDateUtc , Is . GreaterThan ( expectedDate ) ) ;
63
100
}
64
101
}
65
102
}
66
- }
103
+ }
0 commit comments