1- // Copyright (C) 2014-2021 Xtensive LLC.
1+ // Copyright (C) 2014-2024 Xtensive LLC.
22// This code is distributed under MIT license terms.
33// See the License.txt file in the project root for more information.
44// Created by: Alena Mikshina
@@ -19,7 +19,7 @@ public class Container : Entity
1919 [ Field , Key ]
2020 public int Id { get ; private set ; }
2121
22- [ Field ( Indexed = true ) ]
22+ [ Field ]
2323 public NpgsqlPoint Point { get ; set ; }
2424
2525 [ Field ]
@@ -36,59 +36,84 @@ public class Container : Entity
3636
3737 [ Field ( Indexed = true ) ]
3838 public NpgsqlCircle Circle { get ; set ; }
39+
40+ public Container ( Session session ) : base ( session )
41+ {
42+ }
43+ }
44+
45+ [ HierarchyRoot ]
46+ public class IndexedPointContainer : Entity
47+ {
48+ [ Field , Key ]
49+ public int Id { get ; private set ; }
50+
51+ [ Field ( Indexed = true ) ]
52+ public NpgsqlPoint Point { get ; set ; }
53+
54+
55+ public IndexedPointContainer ( Session session )
56+ : base ( session )
57+ {
58+ }
3959 }
4060}
4161
4262namespace Xtensive . Orm . Tests . Storage
4363{
4464 public class PostgreSqlSpatialTest : AutoBuildTest
4565 {
66+ private readonly NpgsqlPoint point = new NpgsqlPoint ( 1 , 2 ) ;
67+ private readonly NpgsqlLSeg lSeg = new NpgsqlLSeg ( new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) ) ;
68+ private readonly NpgsqlBox box = new NpgsqlBox ( new NpgsqlPoint ( 1 , 1 ) , new NpgsqlPoint ( - 1 , - 1 ) ) ;
69+ private readonly NpgsqlPath path = new NpgsqlPath ( new [ ] { new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) } ) { Open = true } ;
70+ private readonly NpgsqlPolygon polygon = new NpgsqlPolygon ( new [ ] { new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) , new NpgsqlPoint ( 5 , 6 ) } ) ;
71+ private readonly NpgsqlCircle circle = new NpgsqlCircle ( new NpgsqlPoint ( 1 , 2 ) , 3 ) ;
72+
73+
4674 protected override DomainConfiguration BuildConfiguration ( )
4775 {
4876 var config = base . BuildConfiguration ( ) ;
49- config . Types . Register ( typeof ( Container ) . Assembly , typeof ( Container ) . Namespace ) ;
77+ config . Types . Register ( typeof ( Container ) ) ;
78+ if ( StorageProviderInfo . Instance . CheckProviderVersionIsAtLeast ( StorageProviderVersion . PostgreSql90 ) ) {
79+ config . Types . Register ( typeof ( IndexedPointContainer ) ) ;
80+ }
5081 return config ;
5182 }
5283
53- protected override void CheckRequirements ( )
54- {
55- Require . ProviderIs ( StorageProvider . PostgreSql ) ;
56- }
84+ protected override void CheckRequirements ( ) => Require . ProviderIs ( StorageProvider . PostgreSql ) ;
5785
5886 [ Test ]
5987 public void MainTest ( )
6088 {
61- NpgsqlPoint point = new NpgsqlPoint ( 1 , 2 ) ;
62- NpgsqlLSeg lSeg = new NpgsqlLSeg ( new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) ) ;
63- NpgsqlBox box = new NpgsqlBox ( new NpgsqlPoint ( 1 , 1 ) , new NpgsqlPoint ( - 1 , - 1 ) ) ;
64- NpgsqlPath path = new NpgsqlPath ( new [ ] { new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) } ) { Open = true } ;
65- NpgsqlPolygon polygon = new NpgsqlPolygon ( new [ ] { new NpgsqlPoint ( 1 , 2 ) , new NpgsqlPoint ( 3 , 4 ) , new NpgsqlPoint ( 5 , 6 ) } ) ;
66- NpgsqlCircle circle = new NpgsqlCircle ( new NpgsqlPoint ( 1 , 2 ) , 3 ) ;
67-
68- using ( Domain . OpenSession ( ) ) {
69- using ( var t = Session . Current . OpenTransaction ( ) ) {
70- _ = new Container {
89+ using ( var session = Domain . OpenSession ( ) ) {
90+
91+ var defaultValueContainerId = 0 ;
92+ var customValueContainerId = 0 ;
93+ using ( var tx = session . OpenTransaction ( ) ) {
94+ defaultValueContainerId = new Container ( session ) {
7195 Point = new NpgsqlPoint ( ) ,
7296 LSeg = new NpgsqlLSeg ( ) ,
7397 Box = new NpgsqlBox ( ) ,
7498 Path = new NpgsqlPath ( ) ,
7599 Polygon = new NpgsqlPolygon ( ) ,
76100 Circle = new NpgsqlCircle ( )
77- } ;
101+ } . Id ;
78102
79- _ = new Container {
103+ customValueContainerId = new Container ( session ) {
80104 Point = point ,
81105 LSeg = lSeg ,
82106 Box = box ,
83107 Path = path ,
84108 Polygon = polygon ,
85109 Circle = circle
86- } ;
87- t . Complete ( ) ;
110+ } . Id ;
111+
112+ tx . Complete ( ) ;
88113 }
89114
90- using ( var t = Session . Current . OpenTransaction ( ) ) {
91- var record = Query . All < Container > ( ) . First ( c => c . Id == 1 ) ;
115+ using ( var tx = session . OpenTransaction ( ) ) {
116+ var record = session . Query . All < Container > ( ) . First ( c => c . Id == defaultValueContainerId ) ;
92117
93118 Console . WriteLine ( "The record without the initial parameters:" ) ;
94119 OutputRecord ( record ) ;
@@ -100,7 +125,7 @@ public void MainTest()
100125 Assert . IsTrue ( record . Polygon . Equals ( new NpgsqlPolygon ( new [ ] { new NpgsqlPoint ( ) } ) ) ) ;
101126 Assert . IsTrue ( record . Circle . Equals ( new NpgsqlCircle ( ) ) ) ;
102127
103- record = Query . All < Container > ( ) . First ( c => c . Id == 2 ) ;
128+ record = session . Query . All < Container > ( ) . First ( c => c . Id == customValueContainerId ) ;
104129
105130 Console . WriteLine ( "The record with the initial parameters:" ) ;
106131 OutputRecord ( record ) ;
@@ -111,7 +136,41 @@ record = Query.All<Container>().First(c => c.Id==2);
111136 Assert . IsTrue ( record . Polygon . Equals ( polygon ) ) ;
112137 Assert . IsTrue ( record . Circle . Equals ( circle ) ) ;
113138
114- t . Complete ( ) ;
139+ tx . Complete ( ) ;
140+ }
141+ }
142+ }
143+
144+ [ Test ]
145+ public void AddtionalTest ( )
146+ {
147+ Require . ProviderVersionAtLeast ( StorageProviderVersion . PostgreSql90 ) ;
148+
149+ using ( var session = Domain . OpenSession ( ) ) {
150+
151+ var defaultValueContainerId = 0 ;
152+ var customValueContainerId = 0 ;
153+ using ( var tx = session . OpenTransaction ( ) ) {
154+ defaultValueContainerId = new IndexedPointContainer ( session ) { Point = new NpgsqlPoint ( ) } . Id ;
155+ customValueContainerId = new IndexedPointContainer ( session ) { Point = point } . Id ;
156+ tx . Complete ( ) ;
157+ }
158+
159+ using ( var tx = session . OpenTransaction ( ) ) {
160+ var record = session . Query . All < IndexedPointContainer > ( ) . First ( c => c . Id == defaultValueContainerId ) ;
161+
162+ Console . WriteLine ( "The record without the initial parameters:" ) ;
163+ OutputRecord ( record ) ;
164+
165+ Assert . IsTrue ( record . Point . Equals ( new NpgsqlPoint ( ) ) ) ;
166+
167+ record = session . Query . All < IndexedPointContainer > ( ) . First ( c => c . Id == customValueContainerId ) ;
168+
169+ Console . WriteLine ( "The record with the initial parameters:" ) ;
170+ OutputRecord ( record ) ;
171+ Assert . IsTrue ( record . Point . Equals ( point ) ) ;
172+
173+ tx . Complete ( ) ;
115174 }
116175 }
117176 }
@@ -134,5 +193,10 @@ public void OutputRecord(Container record)
134193
135194 Console . WriteLine ( "Circle - <({0},{1}),{2}>" , record . Circle . Center . X , record . Circle . Center . Y , record . Circle . Radius ) ;
136195 }
196+
197+ public void OutputRecord ( IndexedPointContainer record )
198+ {
199+ Console . WriteLine ( "Point - ({0},{1})" , record . Point . X , record . Point . Y ) ;
200+ }
137201 }
138202}
0 commit comments