4
4
namespace ServiceStack . OrmLite . PostgreSQL . Tests
5
5
{
6
6
public class TypeWithByteArrayFieldTests : OrmLiteTestBase
7
- {
7
+ {
8
+ TypeWithByteArrayField getSampleObject ( )
9
+ {
10
+ var testByteArray = new byte [ 256 ] ;
11
+ for ( int i = 0 ; i < 256 ; i ++ ) { testByteArray [ i ] = ( byte ) i ; }
12
+
13
+ return new TypeWithByteArrayField { Id = 1 , Content = testByteArray } ;
14
+ }
15
+
8
16
[ Test ]
9
17
public void CanInsertAndSelectByteArray ( )
10
18
{
11
- var orig = new TypeWithByteArrayField { Id = 1 , Content = new byte [ ] { 0 , 17 , 0 , 17 , 0 , 7 } } ;
19
+ var orig = getSampleObject ( ) ;
12
20
13
21
using ( var db = ConnectionString . OpenDbConnection ( ) )
14
22
{
@@ -21,6 +29,116 @@ public void CanInsertAndSelectByteArray()
21
29
Assert . AreEqual ( orig . Id , target . Id ) ;
22
30
Assert . AreEqual ( orig . Content , target . Content ) ;
23
31
}
32
+ }
33
+
34
+ [ Test ]
35
+ public void CanInsertAndSelectByteArray__manual_insert__manual_select ( )
36
+ {
37
+ var orig = getSampleObject ( ) ;
38
+
39
+ using ( var db = ConnectionString . OpenDbConnection ( ) ) {
40
+ //insert and select manually - ok
41
+ db . CreateTable < TypeWithByteArrayField > ( true ) ;
42
+ _insertManually ( orig , db ) ;
43
+
44
+ _selectAndVerifyManually ( orig , db ) ;
45
+ }
46
+ }
47
+
48
+ [ Test ]
49
+ public void CanInsertAndSelectByteArray__InsertParam_insert__manual_select ( )
50
+ {
51
+ var orig = getSampleObject ( ) ;
52
+
53
+ using ( var db = ConnectionString . OpenDbConnection ( ) ) {
54
+ //insert using InsertParam, and select manually - ok
55
+ db . CreateTable < TypeWithByteArrayField > ( true ) ;
56
+ db . InsertParam ( orig ) ;
57
+
58
+ _selectAndVerifyManually ( orig , db ) ;
59
+ }
60
+ }
61
+
62
+ [ Test ]
63
+ public void CanInsertAndSelectByteArray__InsertParam_insert__GetById_select ( )
64
+ {
65
+ var orig = getSampleObject ( ) ;
66
+
67
+ using ( var db = ConnectionString . OpenDbConnection ( ) ) {
68
+ //InsertParam + GetByID - fails
69
+ db . CreateTable < TypeWithByteArrayField > ( true ) ;
70
+ db . InsertParam ( orig ) ;
71
+
72
+ var target = db . GetById < TypeWithByteArrayField > ( orig . Id ) ;
73
+
74
+ Assert . AreEqual ( orig . Id , target . Id ) ;
75
+ Assert . AreEqual ( orig . Content , target . Content ) ;
76
+ }
77
+ }
78
+
79
+ [ Test ]
80
+ public void CanInsertAndSelectByteArray__Insert_insert__GetById_select ( )
81
+ {
82
+ var orig = getSampleObject ( ) ;
83
+
84
+ using ( var db = ConnectionString . OpenDbConnection ( ) ) {
85
+ //InsertParam + GetByID - fails
86
+ db . CreateTable < TypeWithByteArrayField > ( true ) ;
87
+ db . Insert ( orig ) ;
88
+
89
+ var target = db . GetById < TypeWithByteArrayField > ( orig . Id ) ;
90
+
91
+ Assert . AreEqual ( orig . Id , target . Id ) ;
92
+ Assert . AreEqual ( orig . Content , target . Content ) ;
93
+ }
94
+ }
95
+
96
+ [ Test ]
97
+ public void CanInsertAndSelectByteArray__Insert_insert__manual_select ( )
98
+ {
99
+ var orig = getSampleObject ( ) ;
100
+
101
+ using ( var db = ConnectionString . OpenDbConnection ( ) ) {
102
+ //InsertParam + GetByID - fails
103
+ db . CreateTable < TypeWithByteArrayField > ( true ) ;
104
+ db . Insert ( orig ) ;
105
+
106
+ _selectAndVerifyManually ( orig , db ) ;
107
+ }
108
+ }
109
+
110
+ private static void _selectAndVerifyManually ( TypeWithByteArrayField orig , System . Data . IDbConnection db )
111
+ {
112
+ using ( var cmd = db . CreateCommand ( ) ) {
113
+ cmd . CommandText = @"select ""Content"" from ""TypeWithByteArrayField"" where ""Id"" = 1 --manual select" ;
114
+ using ( var reader = cmd . ExecuteReader ( ) ) {
115
+ reader . Read ( ) ;
116
+ var ba = reader [ "Content" ] as byte [ ] ;
117
+ Assert . AreEqual ( orig . Content . Length , ba . Length ) ;
118
+ Assert . AreEqual ( orig . Content , ba ) ;
119
+ }
120
+ }
121
+ }
122
+
123
+ private static void _insertManually ( TypeWithByteArrayField orig , System . Data . IDbConnection db )
124
+ {
125
+ using ( var cmd = db . CreateCommand ( ) ) {
126
+ cmd . CommandText = @"INSERT INTO ""TypeWithByteArrayField"" (""Id"",""Content"") VALUES (@Id, @Content) --manual parameterized insert" ;
127
+
128
+ var p_id = cmd . CreateParameter ( ) ;
129
+ p_id . ParameterName = "@Id" ;
130
+ p_id . Value = orig . Id ;
131
+
132
+ cmd . Parameters . Add ( p_id ) ;
133
+
134
+ var p_content = cmd . CreateParameter ( ) ;
135
+ p_content . ParameterName = "@Content" ;
136
+ p_content . Value = orig . Content ;
137
+
138
+ cmd . Parameters . Add ( p_content ) ;
139
+
140
+ cmd . ExecuteNonQuery ( ) ;
141
+ }
24
142
}
25
143
}
26
144
0 commit comments