Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b2e34c0

Browse files
committed
Add example of PostgreSQL JSON DataType test
1 parent cf17ebc commit b2e34c0

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using ServiceStack.DataAnnotations;
6+
using ServiceStack.OrmLite.Tests;
7+
using ServiceStack.Text;
8+
9+
namespace ServiceStack.OrmLite.PostgreSQL.Tests.Issues
10+
{
11+
[TestFixture]
12+
public class JsonDataTypeTests : OrmLiteTestBase
13+
{
14+
[Test]
15+
public void Can_save_and_restore_JSON_property()
16+
{
17+
OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
18+
19+
var item = new LicenseCheckTemp();
20+
item.Body = new CheckHistory();
21+
item.Body.List.Add(new ItemHistory { AddedOn = DateTime.MaxValue, Note = "Test" });
22+
23+
using (var db = OpenDbConnection())
24+
{
25+
db.DropAndCreateTable<LicenseCheckTemp>();
26+
db.GetLastSql().Print();
27+
db.Save(item);
28+
}
29+
30+
using (var db = OpenDbConnection())
31+
{
32+
var items = db.Select<LicenseCheckTemp>();
33+
items.PrintDump();
34+
35+
foreach (var licenseCheck in items.OrderBy(x => x.Id))
36+
{
37+
if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
38+
{
39+
foreach (var itemHistory in licenseCheck.Body.List)
40+
{
41+
Console.WriteLine($"{itemHistory.AddedOn} : Note {itemHistory.Note}");
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
public class LicenseCheckTemp
50+
{
51+
[AutoIncrement]
52+
public int Id { get; set; }
53+
54+
[CustomField("json")]
55+
public CheckHistory Body { get; set; }
56+
}
57+
58+
public class CheckHistory
59+
{
60+
public List<ItemHistory> List { get; set; } = new List<ItemHistory>();
61+
}
62+
63+
public class ItemHistory
64+
{
65+
public string Note { get; set; }
66+
67+
public DateTime AddedOn { get; set; }
68+
69+
}
70+
71+
}

src/ServiceStack.OrmLite.PostgreSQL.Tests/ServiceStack.OrmLite.PostgreSQL.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<ErrorReport>prompt</ErrorReport>
2626
<WarningLevel>4</WarningLevel>
2727
<Prefer32Bit>false</Prefer32Bit>
28+
<PlatformTarget>x86</PlatformTarget>
2829
</PropertyGroup>
2930
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3031
<DebugType>pdbonly</DebugType>
@@ -93,6 +94,7 @@
9394
<Compile Include="Expressions\TestType.cs" />
9495
<Compile Include="Expressions\UnaryExpressionsTest.cs" />
9596
<Compile Include="ForeignKeyAttributeTests.cs" />
97+
<Compile Include="Issues\JsonDataTypeTests.cs" />
9698
<Compile Include="OrmLiteBasicPersistenceProviderTests.cs" />
9799
<Compile Include="OrmLiteCreateTableWithNamigStrategyTests.cs" />
98100
<Compile Include="OrmLiteDropTableWithNamingStrategyTests.cs" />

0 commit comments

Comments
 (0)