This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCqlParameterTest.cs
201 lines (162 loc) · 6.93 KB
/
CqlParameterTest.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// CqlSharp - CqlSharp.Test
// Copyright (c) 2014 Joost Reuzel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Generic;
using CqlSharp.Serialization;
using CqlSharp.Serialization.Marshal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CqlSharp.Test
{
[TestClass]
public class CqlParameterTest
{
[TestMethod]
public void DeriveTypeFromStringValue()
{
var param = new CqlParameter("hello.world", "hello2");
Assert.IsNull(param.Keyspace);
Assert.AreEqual("hello", param.Table);
Assert.AreEqual("world", param.ColumnName);
Assert.AreEqual("hello.world", param.ParameterName);
Assert.AreEqual("hello2", param.Value);
Assert.AreEqual(CqlType.Varchar, param.CqlType);
}
[TestMethod]
public void DeriveTypeFromMapValue()
{
var param = new CqlParameter("say.hello.world.me", new Dictionary<string, int> {{"hi", 1}, {"there", 2}});
Assert.AreEqual("say", param.Keyspace);
Assert.AreEqual("hello", param.Table);
Assert.AreEqual("world.me", param.ColumnName);
Assert.AreEqual("say.hello.world.me", param.ParameterName);
Assert.AreEqual(CqlTypeCode.Map, param.CqlType.CqlTypeCode);
Assert.AreEqual(CqlType.Varchar, ((MapType<string, int>)param.CqlType).KeyType);
Assert.AreEqual(CqlType.Int, ((MapType<string, int>)param.CqlType).ValueType);
}
[TestMethod]
public void SetParametersFromObject()
{
var collection = new CqlParameterCollection();
collection.Add("dummy.test.id", CqlType.Int);
collection.Add("dummy.test.value", CqlType.Text);
collection.Add("dummy.test.ignored", CqlType.Blob);
var a = new A {Id = 1, Ignored = new byte[] {1, 2}, Value = "Hello!"};
collection.Set(a);
Assert.AreEqual(a.Id, collection["dummy.test.id"].Value);
Assert.AreEqual(a.Value, collection["dummy.test.value"].Value);
Assert.IsNull(collection["dummy.test.ignored"].Value);
}
[TestMethod]
public void SetNameOnlyParametersFromObject()
{
var collection = new CqlParameterCollection();
collection.Add("id", CqlType.Int);
collection.Add("value", CqlType.Text);
collection.Add("ignored", CqlType.Blob);
var a = new A {Id = 1, Ignored = new byte[] {1, 2}, Value = "Hello!"};
collection.Set(a);
Assert.AreEqual(a.Id, collection["id"].Value);
Assert.AreEqual(a.Value, collection["value"].Value);
Assert.IsNull(collection["ignored"].Value);
}
[TestMethod]
public void SetNameOnlyParametersFromObjectWithUpperCaseColumnNames()
{
var collection = new CqlParameterCollection();
collection.Add("Id", CqlType.Int);
collection.Add("Value", CqlType.Text);
collection.Add("Ignored", CqlType.Blob);
var a = new AUpperCase {Id = 1, Ignored = new byte[] {1, 2}, Value = "Hello!"};
collection.Set(a);
Assert.AreEqual(a.Id, collection["Id"].Value);
Assert.AreEqual(a.Value, collection["Value"].Value);
Assert.IsNull(collection["Ignored"].Value);
}
[TestMethod]
public void SetParametersFromTwoObjects()
{
var collection = new CqlParameterCollection();
collection.Add("test.id", CqlType.Int);
collection.Add("test.value", CqlType.Text);
collection.Add("test.ignored", CqlType.Blob);
collection.Add("test2.id", CqlType.Int);
collection.Add("test2.value", CqlType.Text);
collection.Add("test2.value2", CqlType.Blob);
collection.Fixate();
var a = new A {Id = 1, Ignored = new byte[] {1, 2}, Value = "Hello!"};
var b = new B {Id = 2, Value2 = new byte[] {3, 4}, Value = "World!"};
collection.Set(a);
collection.Set(b);
Assert.AreEqual(a.Id, collection["test.id"].Value);
Assert.AreEqual(a.Value, collection["test.value"].Value);
Assert.IsNull(collection["test.ignored"].Value);
Assert.AreEqual(b.Id, collection["test2.id"].Value);
Assert.AreEqual(b.Value, collection["test2.value"].Value);
Assert.AreEqual(b.Value2, collection["test2.value2"].Value);
}
[TestMethod]
public void SetParametersFromAnonymousObject()
{
var collection = new CqlParameterCollection
{
{"test.id", CqlType.Int},
{"test.value", CqlType.Text},
{"test.value2", CqlType.Blob},
{"test.map", CqlType.CreateType(CqlTypeCode.Map, CqlType.Text, CqlType.Boolean)}
};
collection.Fixate();
var a = new {Id = 1, value2 = new byte[] {1, 2}, Value = "Hello!"};
collection.Set(a);
Assert.AreEqual(a.Id, collection["test.id"].Value);
Assert.AreEqual(a.Value, collection["test.value"].Value);
Assert.AreEqual(a.value2, collection["test.value2"].Value);
Assert.IsNull(collection[3].Value);
}
#region Nested type: A
[CqlTable("test")]
private class A
{
public int Id { get; set; }
public string Value { get; set; }
[CqlIgnore]
// ReSharper disable UnusedAutoPropertyAccessor.Local
public byte[] Ignored { get; set; }
// ReSharper restore UnusedAutoPropertyAccessor.Local
}
#endregion
#region Nested type: AUpperCase
[CqlTable("Test")]
private class AUpperCase
{
[CqlColumn("Id")]
public int Id { get; set; }
[CqlColumn("Value")]
public string Value { get; set; }
[CqlIgnore]
// ReSharper disable UnusedAutoPropertyAccessor.Local
public byte[] Ignored { get; set; }
// ReSharper restore UnusedAutoPropertyAccessor.Local
}
#endregion
#region Nested type: B
[CqlTable("test2")]
private class B
{
public int Id { get; set; }
public string Value { get; set; }
public byte[] Value2 { get; set; }
}
#endregion
}
}