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