1
- using System . Linq ;
1
+ using System ;
2
+ using System . Linq ;
2
3
using Microsoft . SqlServer . Types ;
3
4
using NUnit . Framework ;
4
5
using ServiceStack . DataAnnotations ;
@@ -12,81 +13,104 @@ public class SpatialTests : SqlServerConvertersOrmLiteTestBase
12
13
[ Test ]
13
14
public void Can_insert_and_retrieve_SqlGeography ( )
14
15
{
15
- using ( var db = OpenDbConnection ( ) )
16
- {
17
- db . DropAndCreateTable < GeoTest > ( ) ;
16
+ Db . DropAndCreateTable < GeoTest > ( ) ;
18
17
19
- // Statue of Liberty
20
- var geo = SqlGeography . Point ( 40.6898329 , - 74.0452177 , 4326 ) ;
18
+ // Statue of Liberty
19
+ var geo = SqlGeography . Point ( 40.6898329 , - 74.0452177 , 4326 ) ;
21
20
22
- db . Insert ( new GeoTest { Id = 1 , Location = geo , NullLocation = SqlGeography . Null } ) ;
21
+ Db . Insert ( new GeoTest { Id = 1 , Location = geo , NullLocation = SqlGeography . Null } ) ;
23
22
24
- var result = db . SingleById < GeoTest > ( 1 ) ;
23
+ var result = Db . SingleById < GeoTest > ( 1 ) ;
25
24
26
- Assert . AreEqual ( geo . Lat , result . Location . Lat ) ;
27
- Assert . AreEqual ( geo . Long , result . Location . Long ) ;
28
- Assert . AreEqual ( geo . STSrid , result . Location . STSrid ) ;
25
+ Assert . AreEqual ( geo . Lat , result . Location . Lat ) ;
26
+ Assert . AreEqual ( geo . Long , result . Location . Long ) ;
27
+ Assert . AreEqual ( geo . STSrid , result . Location . STSrid ) ;
29
28
30
- // Converter always resolves to null even when Null property inserted into database
31
- Assert . AreEqual ( null , result . NullLocation ) ;
29
+ // Converter always resolves to null even when Null property inserted into database
30
+ Assert . AreEqual ( null , result . NullLocation ) ;
32
31
33
- result . PrintDump ( ) ;
34
- }
32
+ result . PrintDump ( ) ;
35
33
}
36
34
37
35
[ Test ]
38
36
public void Can_insert_and_retrieve_SqlGeometry ( )
39
37
{
40
- using ( var db = OpenDbConnection ( ) )
41
- {
42
- db . DropAndCreateTable < GeoTest > ( ) ;
38
+ Db . DropAndCreateTable < GeoTest > ( ) ;
43
39
44
- // A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
45
- var wkt = new System . Data . SqlTypes . SqlChars ( "LINESTRING(0 0, 4 4)" . ToCharArray ( ) ) ;
46
- var shape = SqlGeometry . STLineFromText ( wkt , 0 ) ;
40
+ // A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
41
+ var wkt = new System . Data . SqlTypes . SqlChars ( "LINESTRING(0 0, 4 4)" . ToCharArray ( ) ) ;
42
+ var shape = SqlGeometry . STLineFromText ( wkt , 0 ) ;
47
43
48
- db . Insert ( new GeoTest { Id = 1 , Shape = shape } ) ;
44
+ Db . Insert ( new GeoTest { Id = 1 , Shape = shape } ) ;
49
45
50
- var result = db . SingleById < GeoTest > ( 1 ) . Shape ;
46
+ var result = Db . SingleById < GeoTest > ( 1 ) . Shape ;
51
47
52
- var lengths = db . Column < double > ( "select Shape.STLength() AS Length from GeoTest" ) ;
48
+ var lengths = Db . Column < double > ( "select Shape.STLength() AS Length from GeoTest" ) ;
53
49
54
- Assert . AreEqual ( ( double ) result . STLength ( ) , lengths . First ( ) ) ;
50
+ Assert . AreEqual ( ( double ) result . STLength ( ) , lengths . First ( ) ) ;
55
51
56
- Assert . AreEqual ( shape . STStartPoint ( ) . STX , result . STStartPoint ( ) . STX ) ;
57
- Assert . AreEqual ( shape . STStartPoint ( ) . STY , result . STStartPoint ( ) . STY ) ;
52
+ Assert . AreEqual ( shape . STStartPoint ( ) . STX , result . STStartPoint ( ) . STX ) ;
53
+ Assert . AreEqual ( shape . STStartPoint ( ) . STY , result . STStartPoint ( ) . STY ) ;
58
54
59
- Assert . AreEqual ( shape . STEndPoint ( ) . STX , result . STEndPoint ( ) . STX ) ;
60
- Assert . AreEqual ( shape . STEndPoint ( ) . STY , result . STEndPoint ( ) . STY ) ;
55
+ Assert . AreEqual ( shape . STEndPoint ( ) . STX , result . STEndPoint ( ) . STX ) ;
56
+ Assert . AreEqual ( shape . STEndPoint ( ) . STY , result . STEndPoint ( ) . STY ) ;
61
57
62
- Assert . AreEqual ( 2 , ( int ) result . STNumPoints ( ) ) ;
58
+ Assert . AreEqual ( 2 , ( int ) result . STNumPoints ( ) ) ;
63
59
64
- result . PrintDump ( ) ;
65
- }
60
+ result . PrintDump ( ) ;
66
61
}
67
62
68
63
[ Test ]
69
64
public void Can_insert_SqlGeography_and_SqlGeometry ( )
70
65
{
71
- using ( var db = OpenDbConnection ( ) )
72
- {
73
- db . DropAndCreateTable < GeoTest > ( ) ;
66
+ Db . DropAndCreateTable < GeoTest > ( ) ;
67
+
68
+ // Statue of Liberty
69
+ var geo = SqlGeography . Point ( 40.6898329 , - 74.0452177 , 4326 ) ;
70
+
71
+ // A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
72
+ var wkt = new System . Data . SqlTypes . SqlChars ( "LINESTRING(0 0, 4 4)" . ToCharArray ( ) ) ;
73
+ var shape = SqlGeometry . STLineFromText ( wkt , 0 ) ;
74
+
75
+ Db . Insert ( new GeoTest { Id = 1 , Location = geo , Shape = shape } ) ;
76
+
77
+ shape = Db . SingleById < GeoTest > ( 1 ) . Shape ;
78
+
79
+ Assert . That ( shape , Is . Not . Null ) ;
74
80
75
- // Statue of Liberty
76
- var geo = SqlGeography . Point ( 40.6898329 , - 74.0452177 , 4326 ) ;
81
+ new { shape . STEndPoint ( ) . STX , shape . STEndPoint ( ) . STY } . PrintDump ( ) ;
82
+ }
77
83
78
- // A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
79
- var wkt = new System . Data . SqlTypes . SqlChars ( "LINESTRING(0 0, 4 4)" . ToCharArray ( ) ) ;
80
- var shape = SqlGeometry . STLineFromText ( wkt , 0 ) ;
84
+ [ Test ]
85
+ public void Can_insert_and_update_SqlGeography ( )
86
+ {
87
+ Db . DropAndCreateTable < ModelWithSqlGeography > ( ) ;
81
88
82
- db . Insert ( new GeoTest { Id = 1 , Location = geo , Shape = shape } ) ;
89
+ var wkt = "POINT(38.028495788574205 55.895460650576936)" ;
90
+ var geo = SqlGeography . STGeomFromText ( new System . Data . SqlTypes . SqlChars ( wkt ) , 4326 ) ;
83
91
84
- shape = db . SingleById < GeoTest > ( 1 ) . Shape ;
92
+ var obj = new ModelWithSqlGeography { Name = "Test" , Created = DateTime . UtcNow , Geo = geo } ;
85
93
86
- Assert . That ( shape , Is . Not . Null ) ;
94
+ var id = ( int ) Db . Insert ( obj , selectIdentity : true ) ;
95
+ obj . ID = id ;
87
96
88
- new { shape . STEndPoint ( ) . STX , shape . STEndPoint ( ) . STY } . PrintDump ( ) ;
97
+ try
98
+ {
99
+ // Update of POCO with SqlGeography proprety should work
100
+ obj . Name = "Test - modified" ;
101
+ obj . Edited = DateTime . UtcNow ;
102
+ Db . Update ( obj ) ;
103
+ }
104
+ catch ( Exception ex )
105
+ {
106
+ Assert . Fail ( ex . ToString ( ) ) ;
89
107
}
108
+ finally
109
+ {
110
+ // GetLastSql shouldn't return null after exception
111
+ var lastSql = Db . GetLastSql ( ) ;
112
+ Assert . IsNotNull ( lastSql ) ;
113
+ }
90
114
}
91
115
}
92
116
@@ -100,4 +124,15 @@ public class GeoTest
100
124
101
125
public SqlGeometry Shape { get ; set ; }
102
126
}
127
+
128
+ public class ModelWithSqlGeography
129
+ {
130
+ [ AutoIncrement ]
131
+ public int ID { get ; set ; }
132
+ [ StringLength ( 255 ) ]
133
+ public string Name { get ; set ; }
134
+ public DateTime Created { get ; set ; }
135
+ public DateTime ? Edited { get ; set ; }
136
+ public SqlGeography Geo { get ; set ; }
137
+ }
103
138
}
0 commit comments