Skip to content

Commit d3ab500

Browse files
committed
Add Unit Tests for all basic types
1 parent 4837e6d commit d3ab500

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

nuget/Sqlite_net.SourceGenerator/SQLiteFastColumnSetterGenerator.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,20 @@ static void GeneratePropertySetter(StringBuilder sb, PropertyInfo property)
262262
sb.AppendLine($" typedObj.{property.PropertyName} = SQLite3.ColumnString(stmt, index);");
263263
break;
264264

265-
case "int":
265+
case "byte":
266+
case "Byte":
267+
case "System.Byte":
268+
sb.AppendLine ($" typedObj.{property.PropertyName} = (byte)SQLite3.ColumnInt(stmt, index);");
269+
break;
270+
271+
case "short":
272+
case "Int16":
273+
case "System.Int16":
274+
sb.AppendLine ($" typedObj.{property.PropertyName} = (short)SQLite3.ColumnInt(stmt, index);");
275+
break;
276+
277+
278+
case "int":
266279
case "Int32":
267280
case "System.Int32":
268281
sb.AppendLine($" typedObj.{property.PropertyName} = SQLite3.ColumnInt(stmt, index);");

tests/SQLite.Tests/SourceGeneratorTest.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,35 @@ public class InnerTestSetter
6464
public DateTime Date { get; set; }
6565
}
6666

67-
// Shouldn't generate Setter because it is not accessible
67+
public class AllBasicTypesSetter
68+
{
69+
[PrimaryKey]
70+
public int Id { get; set; }
71+
72+
public string String { get; set; }
73+
74+
public byte Byte { get; set; }
75+
76+
public short Short { get; set; }
77+
78+
public int Int { get; set; }
79+
80+
public long Long { get; set; }
81+
82+
public float Float { get; set; }
83+
84+
public double Double { get; set; }
85+
86+
public decimal Decimal { get; set; }
87+
88+
public TimeSpan TimeSpam { get; set; }
89+
90+
public DateTime DateTime { get; set; }
91+
92+
public Guid Guid { get; set; }
93+
}
94+
95+
// Shouldn't generate Setter because it is not accessible
6896
private class PrivateInnerTestSetter
6997
{
7098
[AutoIncrement, PrimaryKey]
@@ -75,6 +103,15 @@ private class PrivateInnerTestSetter
75103
public DateTime Date { get; set; }
76104
}
77105

106+
public class AllBasicTypesTestDb : SQLiteConnection
107+
{
108+
public AllBasicTypesTestDb (String path)
109+
: base (path)
110+
{
111+
CreateTable<AllBasicTypesSetter> ();
112+
}
113+
}
114+
78115
public class InnerTestDb : SQLiteConnection
79116
{
80117
public InnerTestDb (String path)
@@ -288,5 +325,49 @@ public void SetFastColumnSetters_Outer_AndReadData_IsCalled ()
288325
Assert.AreEqual (results.FirstOrDefault ().Data, "10");
289326
Assert.AreEqual (mapperCount, FastColumnSetter.customSetter.Count);
290327
}
328+
329+
[Test]
330+
public void SetFastColumnSetters_AllBasicTypes_Works ()
331+
{
332+
SQLiteInitializer.Init ();
333+
var mapperCount = FastColumnSetter.customSetter.Count;
334+
335+
var n = 20;
336+
var cq = from i in Enumerable.Range (1, n)
337+
select new AllBasicTypesSetter() {
338+
Id = i,
339+
String = Convert.ToString(i),
340+
Byte = (byte)i,
341+
Short = (short)i,
342+
Int = i,
343+
Long = i,
344+
Float = i,
345+
Double = i,
346+
Decimal = i,
347+
DateTime = new DateTime(2000, 1, i),
348+
TimeSpam = new TimeSpan(i, 0, 0),
349+
Guid = new Guid (i, 0, 0, new byte[8]),
350+
};
351+
352+
var db = new AllBasicTypesTestDb(TestPath.GetTempFileName ());
353+
db.InsertAll (cq);
354+
355+
var results = db.Table<AllBasicTypesSetter> ().Where (o => o.Id.Equals (10));
356+
Assert.AreEqual (results.Count (), 1);
357+
var data = results.FirstOrDefault ();
358+
Assert.AreEqual (data.String, "10");
359+
Assert.AreEqual (data.Byte, (byte)10);
360+
Assert.AreEqual (data.Short, (short)10);
361+
Assert.AreEqual (data.Int, (int)10);
362+
Assert.AreEqual (data.Long, (long)10);
363+
Assert.AreEqual (data.Float, (float)10);
364+
Assert.AreEqual (data.Double, (double)10);
365+
Assert.AreEqual (data.Decimal, (decimal)10);
366+
Assert.AreEqual (data.TimeSpam, new TimeSpan(10, 0, 0));
367+
Assert.AreEqual (data.DateTime, new DateTime(2000, 1, 10));
368+
Assert.AreEqual (data.Guid, new Guid (10, 0, 0, new byte[8]));
369+
370+
Assert.AreEqual (mapperCount, FastColumnSetter.customSetter.Count);
371+
}
291372
}
292373
}

0 commit comments

Comments
 (0)