This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathTypeWithByteArrayFieldTests.cs
More file actions
151 lines (119 loc) · 5 KB
/
Copy pathTypeWithByteArrayFieldTests.cs
File metadata and controls
151 lines (119 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using NUnit.Framework;
using ServiceStack.OrmLite.Tests;
namespace ServiceStack.OrmLite.PostgreSQL.Tests
{
public class TypeWithByteArrayFieldTests : OrmLiteTestBase
{
public TypeWithByteArrayFieldTests() : base(Dialect.PostgreSql) { }
TypeWithByteArrayField getSampleObject()
{
var testByteArray = new byte[256];
for(int i = 0; i < 256; i++) { testByteArray[i] = (byte)i; }
return new TypeWithByteArrayField { Id = 1, Content = testByteArray };
}
[Test]
public void CanInsertAndSelectByteArray()
{
var orig = getSampleObject();
using (var db = OpenDbConnection())
{
db.CreateTable<TypeWithByteArrayField>(true);
db.Save(orig);
var target = db.SingleById<TypeWithByteArrayField>(orig.Id);
Assert.AreEqual(orig.Id, target.Id);
Assert.AreEqual(orig.Content, target.Content);
}
}
[Test]
public void CanInsertAndSelectByteArray__manual_insert__manual_select()
{
var orig = getSampleObject();
using(var db = OpenDbConnection()) {
//insert and select manually - ok
db.CreateTable<TypeWithByteArrayField>(true);
_insertManually(orig, db);
_selectAndVerifyManually(orig, db);
}
}
[Test]
public void CanInsertAndSelectByteArray__InsertParam_insert__manual_select()
{
var orig = getSampleObject();
using(var db = OpenDbConnection()) {
//insert using InsertParam, and select manually - ok
db.CreateTable<TypeWithByteArrayField>(true);
db.Insert(orig);
_selectAndVerifyManually(orig, db);
}
}
[Test]
public void CanInsertAndSelectByteArray__InsertParam_insert__GetById_select()
{
var orig = getSampleObject();
using(var db = OpenDbConnection()) {
//InsertParam + GetByID - fails
db.CreateTable<TypeWithByteArrayField>(true);
db.Insert(orig);
var target = db.SingleById<TypeWithByteArrayField>(orig.Id);
Assert.AreEqual(orig.Id, target.Id);
Assert.AreEqual(orig.Content, target.Content);
}
}
[Test]
public void CanInsertAndSelectByteArray__Insert_insert__GetById_select()
{
var orig = getSampleObject();
using(var db = OpenDbConnection()) {
//InsertParam + GetByID - fails
db.CreateTable<TypeWithByteArrayField>(true);
db.Insert(orig);
var target = db.SingleById<TypeWithByteArrayField>(orig.Id);
Assert.AreEqual(orig.Id, target.Id);
Assert.AreEqual(orig.Content, target.Content);
}
}
[Test]
public void CanInsertAndSelectByteArray__Insert_insert__manual_select()
{
var orig = getSampleObject();
using(var db = OpenDbConnection()) {
//InsertParam + GetByID - fails
db.CreateTable<TypeWithByteArrayField>(true);
db.Insert(orig);
_selectAndVerifyManually(orig, db);
}
}
private static void _selectAndVerifyManually(TypeWithByteArrayField orig, System.Data.IDbConnection db)
{
using(var cmd = db.CreateCommand()) {
cmd.CommandText = @"select ""content"" from ""type_with_byte_array_field"" where ""id"" = 1 --manual select";
using(var reader = cmd.ExecuteReader()) {
reader.Read();
var ba = reader["content"] as byte[];
Assert.AreEqual(orig.Content.Length, ba.Length);
Assert.AreEqual(orig.Content, ba);
}
}
}
private static void _insertManually(TypeWithByteArrayField orig, System.Data.IDbConnection db)
{
using(var cmd = db.CreateCommand()) {
cmd.CommandText = @"INSERT INTO ""type_with_byte_array_field"" (""id"",""content"") VALUES (@Id, @Content) --manual parameterized insert";
var p_id = cmd.CreateParameter();
p_id.ParameterName = "@Id";
p_id.Value = orig.Id;
cmd.Parameters.Add(p_id);
var p_content = cmd.CreateParameter();
p_content.ParameterName = "@Content";
p_content.Value = orig.Content;
cmd.Parameters.Add(p_content);
cmd.ExecuteNonQuery();
}
}
}
class TypeWithByteArrayField
{
public int Id { get; set; }
public byte[] Content { get; set; }
}
}