Skip to content

Commit e3ca525

Browse files
committed
Add tests for custom type serialization, minor fixes
1 parent 04a088e commit e3ca525

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

SQLiteSharp.Tests/ReadMeTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ public void Test1() {
3333
List<ShopItem> Bananas = Connection.Table<ShopItem>().Where(ShopItem => ShopItem.ItemName == "Banana").ToList();
3434
Assert.Single(Bananas);
3535
}
36+
[Fact]
37+
public void Test2() {
38+
// Open a database connection
39+
using SQLiteConnection Connection = new(":memory:");
40+
41+
// Register custom type
42+
Connection.Orm.RegisterType<Sweet>(
43+
SqliteType.Text,
44+
serialize: (Sweet sweet) => System.Text.Json.JsonSerializer.Serialize(sweet),
45+
deserialize: (SqliteValue value, Type clrType) => System.Text.Json.JsonSerializer.Deserialize(value.AsText, clrType)
46+
);
47+
48+
// Create a table for a class
49+
Connection.CreateTable<SweetWrapper>();
50+
51+
// Insert items into the table
52+
Connection.Insert(new SweetWrapper() {
53+
Sweet = new Sweet("orange"),
54+
});
55+
56+
// Find one item in the table matching a predicate
57+
SweetWrapper? Sweet = Connection.Table<SweetWrapper>().FirstOrDefault();
58+
Assert.NotNull(Sweet);
59+
}
3660
}
3761

3862
public class ShopItem {
@@ -46,4 +70,12 @@ public class ShopItem {
4670

4771
[Ignore]
4872
public int SomethingToIgnore { get; set; }
73+
}
74+
75+
public class SweetWrapper {
76+
public Sweet? Sweet { get; set; }
77+
}
78+
79+
public class Sweet(string Flavour) {
80+
public string? Flavour { get; set; } = Flavour;
4981
}

SQLiteSharp/Orm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Orm() {
1919
public void RegisterType(Type type, SqliteType sqliteType, Func<object, SqliteValue> serialize, Func<SqliteValue, Type, object?> deserialize) {
2020
TypeSerializers[type] = new TypeSerializer(type, sqliteType, serialize, deserialize);
2121
}
22-
public void RegisterType<T>(SqliteType sqliteType, Func<T, SqliteValue> serialize, Func<SqliteValue, Type, object> deserialize) {
22+
public void RegisterType<T>(SqliteType sqliteType, Func<T, SqliteValue> serialize, Func<SqliteValue, Type, object?> deserialize) {
2323
RegisterType(typeof(T), sqliteType, (object clr) => serialize((T)clr), (SqliteValue sqlite, Type clrType) => deserialize(sqlite, clrType));
2424
}
2525
public bool UnregisterType(Type type) {

SQLiteSharp/SQLiteConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public CreateTableResult CreateTable(Type type, string? virtualModuleName = null
127127

128128
// Ensure table has at least one column
129129
if (map.Columns.Length == 0) {
130-
throw new Exception($"Cannot create a table without columns (add properties to '{type.Name}')");
130+
throw new Exception($"Cannot create a table without columns (add properties to '{type}')");
131131
}
132132

133133
// Check if the table already exists

0 commit comments

Comments
 (0)