forked from softlion/SQLite.Net-PCL2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultAttributeTest.cs
More file actions
203 lines (163 loc) · 5.78 KB
/
DefaultAttributeTest.cs
File metadata and controls
203 lines (163 loc) · 5.78 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
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
202
203
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
namespace SQLite.Net2.Tests
{
[TestFixture]
public class DefaultAttributeTest : BaseTest
{
private class WithDefaultValue
{
public const string CustomAttributeDefaultValue = "12345";
public const int IntVal = 666;
public static decimal DecimalVal = 666.666m;
public static string StringVal = "Working String";
public static DateTime DateTimegVal = new DateTime(2014, 2, 13);
public WithDefaultValue()
{
TestInt = IntVal;
TestDateTime = DateTimegVal;
TestDecimal = DecimalVal;
TestString = StringVal;
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Default]
public int TestInt { get; set; }
[Default]
public decimal TestDecimal { get; set; }
[Default]
public DateTime TestDateTime { get; set; }
[Default]
public string TestString { get; set; }
[Default(value: IntVal, usePropertyValue: false)]
public int DefaultValueInAttributeTestInt { get; set; }
public class Default666Attribute : DefaultAttribute
{
public Default666Attribute() :base(usePropertyValue:false, value:IntVal)
{
}
}
[Default666]
public int TestIntWithSubtypeOfDefault { get; set; }
}
private class TestDefaultValueAttribute : Attribute
{
public string DefaultValue { get; private set; }
public TestDefaultValueAttribute(string defaultValue)
{
DefaultValue = defaultValue;
}
}
public class TestColumnInformationProvider : IColumnInformationProvider
{
public string GetColumnName(Type containedType, MemberInfo p, int tupleElementIndex)
{
var colAttr = p.GetCustomAttributes<ColumnAttribute>(true).FirstOrDefault();
return colAttr == null ? p.Name : colAttr.Name;
}
public Type GetMemberType(MemberInfo m)
{
return m switch
{
PropertyInfo p => p.PropertyType,
FieldInfo f => f.FieldType,
_ => throw new NotSupportedException($"{m.GetType()} is not supported.")
};
}
public object GetValue(MemberInfo m, object obj)
{
return m switch
{
PropertyInfo p => p.GetValue(obj),
FieldInfo f => f.GetValue(obj),
_ => throw new NotSupportedException($"{m.GetType()} is not supported.")
};
}
public bool IsIgnored(MemberInfo p)
{
return false;
}
public IEnumerable<IndexedAttribute> GetIndices(MemberInfo p)
{
return p.GetCustomAttributes<IndexedAttribute>();
}
public bool IsPK(MemberInfo m)
{
return m.GetCustomAttributes<PrimaryKeyAttribute>().Any();
}
public string Collation(MemberInfo m)
{
return string.Empty;
}
public bool IsAutoInc(Type containedType, MemberInfo m, int tupleElementIndex)
{
return false;
}
public int? MaxStringLength(MemberInfo p)
{
return null;
}
public object GetDefaultValue(MemberInfo p)
{
var defaultValueAttributes = p.GetCustomAttributes<TestDefaultValueAttribute> ();
if (!defaultValueAttributes.Any())
{
return null;
}
return defaultValueAttributes.First().DefaultValue;
}
public bool IsMarkedNotNull(MemberInfo p)
{
return false;
}
}
public abstract class TestObjBase<T>
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public T Data { get; set; }
}
public class TestObjIntWithDefaultValue : TestObjBase<int>
{
[TestDefaultValue("12345")]
public string SomeValue { get; set; }
}
public class TestDbWithCustomAttributes : SQLiteConnection
{
public TestDbWithCustomAttributes(String path)
: base(path)
{
ColumnInformationProvider = new TestColumnInformationProvider();
CreateTable<TestObjIntWithDefaultValue>();
}
}
[Test]
public void TestColumnValues()
{
using (TestDb db = new TestDb())
{
db.CreateTable<WithDefaultValue>();
string failed = string.Empty;
foreach (var col in db.GetMapping<WithDefaultValue>().Columns)
{
if (col.PropertyName == "TestInt" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
failed += " , TestInt does not equal " + WithDefaultValue.IntVal;
if (col.PropertyName == "TestDecimal" && !col.DefaultValue.Equals(WithDefaultValue.DecimalVal))
failed += "TestDecimal does not equal " + WithDefaultValue.DecimalVal;
if (col.PropertyName == "TestDateTime" && !col.DefaultValue.Equals(WithDefaultValue.DateTimegVal))
failed += "TestDateTime does not equal " + WithDefaultValue.DateTimegVal;
if (col.PropertyName == "TestString" && !col.DefaultValue.Equals(WithDefaultValue.StringVal))
failed += "TestString does not equal " + WithDefaultValue.StringVal;
if (col.PropertyName == "DefaultValueInAttributeTestInt" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
failed += " , DefaultValueInAttributeTestInt does not equal " + WithDefaultValue.IntVal;
if (col.PropertyName == "TestIntWithSubtypeOfDefault" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
failed += " , TestIntWithSubtypeOfDefault does not equal " + WithDefaultValue.IntVal;
}
Assert.True(string.IsNullOrWhiteSpace(failed), failed);
}
}
}
}