PageRenderTime 71ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 15ms app.codeStats 0ms

/test/SlowTests/Issues/RavenDB-13695.cs

http://github.com/ravendb/ravendb
C# | 460 lines | 385 code | 69 blank | 6 comment | 7 complexity | 22694dd54a40a86e393ee594188683aa MD5 | raw file
  1using System.Collections.Generic;
  2using System.Linq;
  3using FastTests;
  4using Raven.Client.Documents.Indexes;
  5using Xunit;
  6using Xunit.Abstractions;
  7
  8
  9namespace SlowTests.Issues
 10{
 11    public class RavenDB_13695 : RavenTestBase
 12    {
 13        public RavenDB_13695(ITestOutputHelper output) : base(output)
 14        {
 15        }
 16
 17        private class NewsDocument
 18        {
 19            public List<string> AuthorNames { get; set; }
 20        }
 21
 22        private class DocumentInfo
 23        {
 24        }
 25
 26        private class NewsDocumentIndex : AbstractIndexCreationTask<NewsDocument, NewsDocumentIndex.Info>
 27        {
 28            public static string StaticIndexName = "ETIS/NewsDocumentIndex";
 29
 30            public override string IndexName
 31            {
 32                get { return StaticIndexName; }
 33            }
 34
 35            public class Info : DocumentInfo
 36            {
 37                public string AuthorNamesStr { get; set; }
 38
 39            }
 40
 41            public NewsDocumentIndex()
 42            {
 43                Map = docs => from d in docs
 44                              select new Info
 45                              {
 46                                  AuthorNamesStr = d.AuthorNames != null ? string.Join(", ", d.AuthorNames.OrderBy(x => x)) : string.Empty,
 47                              };
 48
 49
 50                AdditionalSources = new Dictionary<string, string>()
 51                {
 52                    ["DateTimeExtension4"] = @"
 53
 54using System;
 55using System.Collections.Generic;
 56using System.Linq;
 57
 58namespace ETIS
 59{
 60    public static class DateTimeExtension4
 61    {
 62        public static IEnumerable<int> GetYearsRepresented(List<DateTime?> startTimes, IEnumerable<DateTime?> endTimes)
 63        {
 64            if (startTimes == null || endTimes == null)
 65                return null;
 66
 67            var years = new List<int>();
 68            var tuples = startTimes.Zip(endTimes, (x, y) => new Tuple<DateTime?, DateTime?>(x, y)).ToList();
 69
 70            foreach (var pair in tuples)
 71            {
 72                if (!pair.Item1.HasValue)
 73                    return null;
 74                if (!pair.Item2.HasValue)
 75                    continue;
 76                var startYear = pair.Item1.Value.Year;
 77                var endYear = pair.Item2.Value.Year;
 78                if (endYear - startYear < 0)
 79                    continue;
 80                years.AddRange(Enumerable.Range(startYear, endYear - startYear + 1));
 81            }
 82
 83            return years;
 84        }
 85    }
 86}
 87
 88"
 89                };
 90            }
 91        }
 92
 93        [Fact]
 94        public void CanCompileAdditionalSource()
 95        {
 96            using (var store = GetDocumentStore())
 97            {
 98                using (var session = store.OpenSession())
 99                {
100                    session.Store(new NewsDocument
101                    {
102                        AuthorNames = new List<string>
103                        {
104                            "garcia", "weir"
105                        }
106                    });
107
108                    session.Store(new NewsDocument());
109
110                    session.SaveChanges();
111
112                }
113
114                // should not throw
115                new NewsDocumentIndex().Execute(store);
116
117                WaitForIndexing(store);
118                using (var session = store.OpenSession())
119                {
120                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
121                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
122                        .ToList();
123
124                    Assert.Single(q);
125                }
126
127            }
128        }
129
130        [Fact]
131        public void CanCompileAdditionalSource_List()
132        {
133            using (var store = GetDocumentStore())
134            {
135                using (var session = store.OpenSession())
136                {
137                    session.Store(new NewsDocument
138                    {
139                        AuthorNames = new List<string>
140                        {
141                            "garcia", "weir"
142                        }
143                    });
144
145                    session.Store(new NewsDocument());
146
147                    session.SaveChanges();
148
149                }
150
151                // should not throw
152                var index = new NewsDocumentIndex
153                {
154                    AdditionalSources = new Dictionary<string, string>
155                    {
156                        ["DateTimeExtension"] = @"
157
158using System;
159using System.Collections.Generic;
160using System.Linq;
161
162namespace ETIS
163{
164    public static class DateTimeExtension
165    {
166        public static bool Foo(List<DateTime?> startTimes)
167        {
168            if (startTimes == null)
169                return false;
170
171            return startTimes.Count(x => x.Value.Year > 2000) < 10;
172        }
173    }
174}
175
176"
177                    }
178                };
179
180                index.Execute(store);
181
182                WaitForIndexing(store);
183
184                using (var session = store.OpenSession())
185                {
186                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
187                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
188                        .ToList();
189
190                    Assert.Single(q);
191                }
192
193            }
194        }
195
196        [Fact]
197        public void CanCompileAdditionalSource_IList()
198        {
199            using (var store = GetDocumentStore())
200            {
201                using (var session = store.OpenSession())
202                {
203                    session.Store(new NewsDocument
204                    {
205                        AuthorNames = new List<string>
206                        {
207                            "garcia", "weir"
208                        }
209                    });
210
211                    session.Store(new NewsDocument());
212
213                    session.SaveChanges();
214
215                }
216
217                // should not throw
218                var index = new NewsDocumentIndex
219                {
220                    AdditionalSources = new Dictionary<string, string>
221                    {
222                        ["DateTimeExtension"] = @"
223
224using System;
225using System.Collections.Generic;
226using System.Linq;
227
228namespace ETIS
229{
230    public static class DateTimeExtension
231    {
232        public static bool Foo(IList<DateTime?> startTimes)
233        {
234            if (startTimes == null)
235                return false;
236
237            return startTimes.Count(x => x.Value.Year > 2000) < 10;
238        }
239    }
240}
241
242"
243                    }
244                };
245
246                index.Execute(store);
247
248                WaitForIndexing(store);
249
250                using (var session = store.OpenSession())
251                {
252                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
253                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
254                        .ToList();
255
256                    Assert.Single(q);
257                }
258
259            }
260        }
261
262        [Fact]
263        public void CanCompileAdditionalSource_ICollection()
264        {
265            using (var store = GetDocumentStore())
266            {
267                using (var session = store.OpenSession())
268                {
269                    session.Store(new NewsDocument
270                    {
271                        AuthorNames = new List<string>
272                        {
273                            "garcia", "weir"
274                        }
275                    });
276
277                    session.Store(new NewsDocument());
278
279                    session.SaveChanges();
280
281                }
282
283                // should not throw
284                var index = new NewsDocumentIndex
285                {
286                    AdditionalSources = new Dictionary<string, string>
287                    {
288                        ["DateTimeExtension"] = @"
289
290using System;
291using System.Collections.Generic;
292using System.Linq;
293
294namespace ETIS
295{
296    public static class DateTimeExtension
297    {
298        public static bool Foo(ICollection<string> names)
299        {
300            if (names == null)
301                return false;
302
303            return names.Count(x => x != ""ayende"") < 10;
304        }
305    }
306}
307
308"
309                    }
310                };
311
312                index.Execute(store);
313
314                WaitForIndexing(store);
315
316                using (var session = store.OpenSession())
317                {
318                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
319                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
320                        .ToList();
321
322                    Assert.Single(q);
323                }
324
325            }
326        }
327
328        [Fact]
329        public void CanCompileAdditionalSource_Hashset()
330        {
331            using (var store = GetDocumentStore())
332            {
333                using (var session = store.OpenSession())
334                {
335                    session.Store(new NewsDocument
336                    {
337                        AuthorNames = new List<string>
338                        {
339                            "garcia", "weir"
340                        }
341                    });
342
343                    session.Store(new NewsDocument());
344
345                    session.SaveChanges();
346
347                }
348
349                // should not throw
350                var index = new NewsDocumentIndex
351                {
352                    AdditionalSources = new Dictionary<string, string>
353                    {
354                        ["DateTimeExtension"] = @"
355
356using System;
357using System.Collections.Generic;
358using System.Linq;
359
360namespace ETIS
361{
362    public static class DateTimeExtension
363    {
364        public static bool Foo(HashSet<long> numbers)
365        {
366            if (numbers == null)
367                return false;
368
369            return numbers.Count(x => x > 100) < 10;
370        }
371    }
372}
373
374"
375                    }
376                };
377
378                index.Execute(store);
379
380                WaitForIndexing(store);
381
382                using (var session = store.OpenSession())
383                {
384                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
385                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
386                        .ToList();
387
388                    Assert.Single(q);
389                }
390
391            }
392        }
393
394        [Fact]
395        public void CanCompileAdditionalSource_Dictionary()
396        {
397            using (var store = GetDocumentStore())
398            {
399                using (var session = store.OpenSession())
400                {
401                    session.Store(new NewsDocument
402                    {
403                        AuthorNames = new List<string>
404                        {
405                            "garcia", "weir"
406                        }
407                    });
408
409                    session.Store(new NewsDocument());
410
411                    session.SaveChanges();
412
413                }
414
415                // should not throw
416                var index = new NewsDocumentIndex
417                {
418                    AdditionalSources = new Dictionary<string, string>
419                    {
420                        ["DateTimeExtension"] = @"
421
422using System;
423using System.Collections.Generic;
424using System.Linq;
425
426namespace ETIS
427{
428    public static class DateTimeExtension
429    {
430        public static bool Foo(Dictionary<string, long> dictionary)
431        {
432            if (dictionary == null)
433                return false;
434
435            return dictionary.Count(x => x.Value > 100) < 10;
436        }
437    }
438}
439
440"
441                    }
442                };
443
444                index.Execute(store);
445
446                WaitForIndexing(store);
447
448                using (var session = store.OpenSession())
449                {
450                    var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
451                        .Where(doc => doc.AuthorNamesStr == "garcia, weir")
452                        .ToList();
453
454                    Assert.Single(q);
455                }
456
457            }
458        }
459    }
460}