File: End2EndQueryTests.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (416 lines) | stat: -rw-r--r-- 16,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Spatial;
using System.Diagnostics;
using NorthwindEFModel;
using Xunit;
using System.Linq;
using SampleEntityFrameworkProvider;
using System.Data.Common;

namespace ProviderTests
{
    public class End2EndQueryTests : TestBase
    {
        private class ExpectedResult
        {
            private readonly string name;
            private readonly string location;

            public ExpectedResult(string name, string location)
            {
                this.name = name;
                this.location = location;
            }

            public string Name
            {
                get { return name; }
            }

            public string Location
            {
                get { return location; }
            }
        }

        private readonly List<ExpectedResult> expectedResults =
            new List<ExpectedResult>()
                {
                    new ExpectedResult("Alfreds Futterkiste", "POINT (13.32737 52.420563)"),
                    new ExpectedResult("Ana Trujillo Emparedados y helados",
                                       "POINT (-99.1327229817708 19.4333312988281)"),
                    new ExpectedResult("Antonio Moreno Taquería", "POINT (-99.2789713541667 19.3417358398438)"),
                    new ExpectedResult("Around the Horn", "POINT (-0.143545896959206 51.513600908938)")
                };

        [Fact]
        public void Verify_querying_database_with_DbCommand_works()
        {
            var factory = DbProviderFactories.GetFactory(SampleProviderName);

            using (var connection = factory.CreateConnection())
            {
                Debug.Assert(connection != null, "connection != null");
                connection.ConnectionString = NorthwindDirectConnectionString;
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText =
                        "SELECT CompanyName, Location FROM Customers WHERE CustomerID LIKE @CustomerID";

                    var parameter = command.CreateParameter();
                    parameter.ParameterName = "@CustomerID";
                    parameter.Value = "A%";
                    command.Parameters.Add(parameter);

                    using (var transaction = connection.BeginTransaction())
                    {
                        command.Transaction = transaction;

                        using (var reader = command.ExecuteReader())
                        {
                            foreach (var expectedResult in expectedResults)
                            {
                                reader.Read();
                                Assert.Equal(expectedResult.Name, reader["CompanyName"]);
                                // location is SqlGeography
                                dynamic location = reader["Location"];
                                Assert.Equal(expectedResult.Location, new string(location.STAsText().Value));
                            }

                            Assert.False(reader.Read());
                        }
                    }
                }
            }
        }

        [Fact]
        public void Verify_querying_database_with_EntityClient_works()
        {
            const string commandText =
                "SELECT C.CompanyName, C.Location FROM NorthwindEntities.Customers AS C WHERE C.CustomerID LIKE @CustomerID";
            using (var connection = new EntityConnection(NorthwindEntitiesConnectionString))
            {
                connection.Open();

                using (var command = new EntityCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("CustomerID", "A%");
                    using (var reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        foreach (var expectedResult in expectedResults)
                        {
                            reader.Read();
                            Assert.Equal(expectedResult.Name, reader["CompanyName"]);
                            Assert.Equal(expectedResult.Location, ((DbGeography)reader["Location"]).AsText());
                        }

                        Assert.False(reader.Read());
                    }
                }
            }

            Console.WriteLine();

        }

        [Fact]
        public void Verify_querying_database_with_ObjectQuery_works()
        {
            const string commandText =
                "SELECT VALUE C FROM NorthwindEntities.Customers AS C WHERE C.CustomerID LIKE @CustomerID";
            using (var context = new ObjectContext(NorthwindEntitiesConnectionString))
            {
                var query = context.CreateQuery<Customer>(commandText, new ObjectParameter("CustomerID", "A%"));

                var caseIdx = 0;

                foreach (var customer in query)
                {
                    Assert.Equal(expectedResults[caseIdx].Name, customer.CompanyName);
                    Assert.Equal(expectedResults[caseIdx].Location, customer.Location.AsText());
                    caseIdx++;
                }

                Assert.Equal(4, caseIdx);
            }
        }

        [Fact]
        public void Verify_parametrized_Linq_query_works()
        {
            using (var context = new NorthwindEntities())
            {
                var query = from c in context.Customers
                            where c.CustomerID == "ALFKI"
                            select c;

                var customer = query.Single();

                Assert.Equal(expectedResults[0].Name, customer.CompanyName);
                Assert.Equal(expectedResults[0].Location, customer.Location.AsText());
            }
        }

        [Fact]
        public void Verify_query_with_provider_store_function_works()
        {
            var expected =
                new string[]
                {
                    "Aroun... - Company",
                    "B's B... - Company",
                    "Conso... - Company",
                    "Easte... - Company",
                    "North... - Company",
                    "Seven... - Company"
                };

            using (var context = new NorthwindEntities())
            {
                var query =
                    from c in context.Customers
                    where c.Address.City == "London"
                    select SampleSqlFunctions.Stuff(c.CompanyName, 6, c.CompanyName.Length - 5, "... - Company");

                var caseIdx = 0;
                foreach (var result in query)
                {
                    Assert.Equal(expected[caseIdx], result);
                    caseIdx++;
                }

                Assert.Equal(6, caseIdx);
            }
        }

        [Fact]
        public void Verify_query_containing_StartsWith_works()
        {
            var expected =
                new string[]
                {
                    "La corne d'abondance",
                    "La maison d'Asie",
                    "Laughing Bacchus Wine Cellars",
                    "Lazy K Kountry Store"
                };

            using (var context = new NorthwindEntities())
            {
                var query = from c in context.Customers
                            where c.CompanyName.StartsWith("La")
                            select c;

                var caseIdx = 0;
                foreach (var customer in query)
                {
                    Assert.Equal(expected[caseIdx], customer.CompanyName);
                    caseIdx++;
                }

                Assert.Equal(4, caseIdx);
            }
        }

        [Fact]
        public void Verify_DbGeometry_can_be_materialized()
        {
            using (var context = new NorthwindEntities())
            {
                var order = context.Orders.OrderBy(o => o.OrderID).First();

                Assert.Equal(10248, order.OrderID);
                Assert.Equal("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", order.ContainerSize.AsText());
            }
        }

        [Fact]
        public void Verify_DbGeography_instance_method_translated_correctly()
        {
            var seattleLocation = SpatialServices.Instance.GeographyFromText("POINT(-122.333056 47.609722)");

            var expectedResults = new string[]
                                      {
                                          "BOTTM",
                                          "LAUGB",
                                          "LONEP",
                                          "THEBI",
                                          "TRAIH",
                                          "WHITC",
                                      };

            using(var context = new NorthwindEntities())
            {
                var query = from c in context.Customers
                            where c.Location.Distance(seattleLocation) < 250000 // 250 km
                            select c;

                var caseIdx = 0;
                foreach(var customer in query)
                {
                    Assert.Equal(expectedResults[caseIdx++], customer.CustomerID);
                }
            }
        }

        [Fact]
        public void Verify_DbGeography_instance_property_translated_correctly()
        {
            using (var context = new NorthwindEntities())
            {
                var query = from c in context.Customers
                            where c.Location.Latitude < 0
                            select c;

                Assert.Equal(9, query.Count());
            }
        }

        [Fact]
        public void Verify_static_store_DbGeography_method_translated_correctly()
        {
            var seattleLocation = SpatialServices.Instance.GeographyFromText("POINT(-122.333056 47.609722)");

            var expectedResults = new string[]
                                      {
                                          "BOTTM",
                                          "LAUGB",
                                          "LONEP",
                                          "THEBI",
                                          "TRAIH",
                                          "WHITC",
                                      };

            using (var context = new NorthwindEntities())
            {
                var query = from c in context.Customers
                            where c.Location.Distance(SampleSqlFunctions.Pointgeography(47.609722, -122.333056, 4326)) < 250000 // 250 km
                            select c;

                var caseIdx = 0;
                foreach (var customer in query)
                {
                    Assert.Equal(expectedResults[caseIdx++], customer.CustomerID);
                }
            }
        }

        [Fact]
        public void Verify_DbGeometry_instance_method_translated_correctly()
        {
            var containerSize =
                SpatialServices.Instance.GeometryFromText("POLYGON ((0 0, 9 0, 9 9, 0 9, 0 0))", 0);

            using(var context = new NorthwindEntities())
            {
                var query = from o in context.Orders
                            where o.ContainerSize.SpatialEquals(containerSize)
                            select o;

                Assert.Equal(73, query.Count());
            }
        }

        [Fact]
        public void Verify_DbGeometry_static_method_translated_correctly()
        {
            using (var context = new NorthwindEntities())
            {
                var query = from o in context.Orders
                            where o.ContainerSize.SpatialEquals(DbGeometry.FromText("POLYGON ((0 0, 9 0, 9 9, 0 9, 0 0))", 0))
                            select o;

                Assert.Equal(73, query.Count());
            }
        }

        [Fact]
        public void Verify_store_DbGeometry_method_works()
        {
            using(var context = new NorthwindEntities())
            {
                var query = from o in context.Orders
                            where SampleSqlFunctions.Astextzm(o.ContainerSize) == "POLYGON ((0 0, 9 0, 9 9, 0 9, 0 0))"
                            select o;

                Assert.Equal(73, query.Count());
            }
        }

        [Fact]
        public void Verify_stored_procedures_with_multiple_resultsets_work()
        {
            using (var context = new NorthwindEntities())
            {
                var query = context.CustomerWithRecentOrders("ALFKI");
                Assert.Equal("ALFKI", query.Single().CustomerID);

                var orders = query
                    .GetNextResult<CustomerWithRecentOrders_OrderInfo>()
                    .ToList();

                var expectedOrderIds = new int[] { 11011, 10952, 10835, 10702, 10692, 10643 };
                var actualResult = expectedOrderIds.Zip(orders, (oid, order) => oid == order.OrderID).ToList();

                Assert.True(expectedOrderIds.Length == actualResult.Count && actualResult.All(r => r));
            }
        }

        [Fact]
        public void Verify_TVFs_returning_scalar_values_work()
        {
            using(var context = new NorthwindEntities())
            {
                var customerLocations = context.fx_CustomerLocationForCountry("Portugal").ToList();

                Assert.Equal(2, customerLocations.Count);
                Assert.Contains("POINT (-9.19968872070313 38.7638671875)", customerLocations.Select(s => s.AsText()));
                Assert.Contains( "POINT (-9.13509541581515 38.7153290459515)", customerLocations.Select(s => s.AsText()));
            }
        }

        [Fact]
        public void Verify_TVFs_returning_entities_work()
        {
            using(var context = new NorthwindEntities())
            {
                var inTransitOrders = context.fx_OrdersForShippingStatus(ShippingStatus.InTransit);

                // because TVFs are composable, we can query over the TVF results on the server instead of in memory.
                var orders = inTransitOrders.Where(o => o.ShipCountry == "Poland").ToList();

                Assert.Equal(3, orders.Count);
                Assert.Contains(10611, orders.Select(o => o.OrderID));
                Assert.Contains(10870, orders.Select(o => o.OrderID));
                Assert.Contains(10998, orders.Select(o => o.OrderID));
            }
        }

        [Fact]
        public void Verify_TVFs_returning_complex_values_work()
        {
            DbGeography londonLocation = DbGeography.FromText("POINT(-0.5 51.50)");
            using(var context = new NorthwindEntities())
            {
                var suppliersNearLondon = context.fx_SuppliersWithinRange(500, londonLocation).ToList();

                Assert.Equal(7, suppliersNearLondon.Count);
                Assert.Contains(1, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(12, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(13, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(18, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(22, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(27, suppliersNearLondon.Select(s => s.SupplierID));
                Assert.Contains(28, suppliersNearLondon.Select(s => s.SupplierID));                
            }
        }
    }
}