1+ using System ;
2+ using System . Data ;
3+ using NUnit . Framework ;
4+ using ServiceStack . DataAnnotations ;
5+ using ServiceStack . OrmLite . Tests ;
6+ using System . Collections . Generic ;
7+ using ServiceStack . Text ;
8+
9+ namespace ServiceStack . OrmLite . PostgreSQL . Tests . Issues
10+ {
11+ [ Alias ( "color" ) ]
12+ public class ColorModel
13+ {
14+ public string Color { get ; set ; }
15+ public string Value { get ; set ; }
16+ }
17+
18+ public class ColorJsonModel
19+ {
20+ public int Id { get ; set ; }
21+ public string ColorJson { get ; set ; }
22+ }
23+
24+ public class OrmLiteModelArrayTests : OrmLiteTestBase
25+ {
26+ public OrmLiteModelArrayTests ( ) : base ( Dialect . PostgreSql ) { }
27+
28+ [ Test ]
29+ public void test_model_with_array_to_json ( )
30+ {
31+ using ( var db = OpenDbConnection ( ) )
32+ {
33+ db . DropAndCreateTable < ColorModel > ( ) ;
34+
35+ db . Insert ( new ColorModel { Color = "red" , Value = "#f00" } ) ;
36+ db . Insert ( new ColorModel { Color = "green" , Value = "#0f0" } ) ;
37+ db . Insert ( new ColorModel { Color = "blue" , Value = "#00f" } ) ;
38+ db . Insert ( new ColorModel { Color = "cyan" , Value = "#0ff" } ) ;
39+ db . Insert ( new ColorModel { Color = "magenta" , Value = "#f0f" } ) ;
40+ db . Insert ( new ColorModel { Color = "yellow" , Value = "#ff0" } ) ;
41+ db . Insert ( new ColorModel { Color = "black" , Value = "#000" } ) ;
42+
43+ const string sql = @"SELECT 1::integer AS id
44+ , json_agg(color.*) AS color_json
45+ FROM color;" ;
46+
47+ var results = db . Select < ColorJsonModel > ( sql ) ;
48+
49+ //results.PrintDump();
50+
51+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
52+
53+ foreach ( var result in results )
54+ {
55+ Assert . That ( result . Id , Is . EqualTo ( 1 ) ) ;
56+ Assert . That ( result . ColorJson , Is . Not . Null ) ;
57+ }
58+
59+ }
60+ }
61+
62+ [ Test ]
63+ public void test_model_with_array_and_json ( )
64+ {
65+ //OrmLiteConfig.DeoptimizeReader = true;
66+
67+ using ( var db = OpenDbConnection ( ) )
68+ {
69+ db . DropAndCreateTable < ColorModel > ( ) ;
70+
71+ db . Insert ( new ColorModel { Color = "red" , Value = "#f00" } ) ;
72+ db . Insert ( new ColorModel { Color = "green" , Value = "#0f0" } ) ;
73+ db . Insert ( new ColorModel { Color = "blue" , Value = "#00f" } ) ;
74+ db . Insert ( new ColorModel { Color = "cyan" , Value = "#0ff" } ) ;
75+ db . Insert ( new ColorModel { Color = "magenta" , Value = "#f0f" } ) ;
76+ db . Insert ( new ColorModel { Color = "yellow" , Value = "#ff0" } ) ;
77+ db . Insert ( new ColorModel { Color = "black" , Value = "#000" } ) ;
78+
79+ // SQL contains array and json aggs.
80+ // We usually have ARRAY fields defined in the db, but when
81+ // retrieved we json-ize them. In otherwords the array exists in the tables/views.
82+ // We use SELECT.* which would contain the ARRAY field.
83+ // Array fields are not used in any of our models and should not cause the other
84+ // fields in the model to not be populated.
85+ const string sql = @"SELECT 1::integer AS id
86+ , json_agg(color.*) AS color_json
87+ , array_agg(color.*) AS color_array
88+ FROM color;" ;
89+
90+ var results = db . Select < ColorJsonModel > ( sql ) ;
91+
92+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
93+
94+ foreach ( var result in results )
95+ {
96+ result . ColorJson . Print ( ) ;
97+ Assert . That ( result . Id , Is . EqualTo ( 1 ) ) ;
98+ Assert . That ( result . ColorJson , Is . Not . Null ) ;
99+ }
100+ }
101+ }
102+ }
103+
104+ }
0 commit comments