PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ToMigrate/Raven.Tests/Storage/GeneralStorage.cs

https://github.com/georgiosd/ravendb
C# | 287 lines | 223 code | 59 blank | 5 comment | 3 complexity | c990a8d3c37dc65284c9c110556aa7e2 MD5 | raw file
  1//-----------------------------------------------------------------------
  2// <copyright file="GeneralStorage.cs" company="Hibernating Rhinos LTD">
  3//     Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4// </copyright>
  5//-----------------------------------------------------------------------
  6using System;
  7using System.Collections.Generic;
  8using System.Threading;
  9using Raven.Client.Embedded;
 10using Raven.Imports.Newtonsoft.Json.Linq;
 11using Raven.Abstractions;
 12using Raven.Abstractions.Data;
 13using Raven.Json.Linq;
 14using Raven.Database;
 15using Raven.Database.Tasks;
 16using Raven.Tests.Common;
 17
 18using Xunit;
 19using System.Linq;
 20using Raven.Abstractions.Extensions;
 21using Raven.Database.Config;
 22using Raven.Storage.Esent;
 23using Xunit.Extensions;
 24
 25namespace Raven.Tests.Storage
 26{
 27    public class GeneralStorage : RavenTest
 28    {
 29        private readonly EmbeddableDocumentStore store;
 30        private readonly DocumentDatabase db;
 31
 32        public GeneralStorage()
 33        {
 34            store = NewDocumentStore();
 35            db = store.SystemDatabase;
 36        }
 37
 38        public override void Dispose()
 39        {
 40            store.Dispose();
 41            base.Dispose();
 42        }
 43
 44        [Fact]
 45        public void Can_query_by_id_prefix()
 46        {
 47            db.Documents.Put("abc", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 48            db.Documents.Put("Raven/Databases/Hello", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 49            db.Documents.Put("Raven/Databases/Northwind", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 50            db.Documents.Put("Raven/Databases/Sys", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 51            db.Documents.Put("Raven/Databases/Db", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 52            db.Documents.Put("Raven/Database", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 53
 54            int nextPageStart = 0;
 55            var dbs = db.Documents.GetDocumentsWithIdStartingWith("Raven/Databases/", null, null, 0, 10, CancellationToken.None, ref nextPageStart);
 56
 57            Assert.Equal(4, dbs.Length);
 58        }
 59
 60        [Fact]
 61        public void WhenPutAnIdWithASpace_IdWillBeAGuid()
 62        {
 63            db.Documents.Put(" ", null, new RavenJObject { { "a", "b" } }, new RavenJObject(), null);
 64
 65            var doc = db.Documents.GetDocumentsAsJson(0, 10, null, CancellationToken.None)
 66                .OfType<RavenJObject>()
 67                .Single();
 68            var id = doc["@metadata"].Value<string>("@id");
 69            Assert.False(string.IsNullOrWhiteSpace(id));
 70            Assert.DoesNotThrow(() => new Guid(id));
 71        }
 72
 73        [Fact]
 74        public void CanGetDocumentCounts()
 75        {
 76            db.TransactionalStorage.Batch(actions =>
 77            {
 78                Assert.Equal(0, actions.Documents.GetDocumentsCount());
 79
 80                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
 81            });
 82
 83            db.TransactionalStorage.Batch(actions =>
 84            {
 85                Assert.Equal(1, actions.Documents.GetDocumentsCount());
 86
 87                RavenJObject metadata;
 88                Etag tag;
 89                actions.Documents.DeleteDocument("a", null, out metadata, out tag);
 90            });
 91
 92
 93            db.TransactionalStorage.Batch(actions => Assert.Equal(0, actions.Documents.GetDocumentsCount()));
 94        }
 95
 96        [Fact]
 97        public void CanGetDocumentAfterEmptyEtag()
 98        {
 99            db.TransactionalStorage.Batch(actions => actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject()));
100
101            db.TransactionalStorage.Batch(actions =>
102            {
103                var documents = actions.Documents.GetDocumentsAfter(Etag.Empty, 5, CancellationToken.None).ToArray();
104                Assert.Equal(1, documents.Length);
105            });
106        }
107
108        [Fact]
109        public void CanGetDocumentAfterAnEtag()
110        {
111            db.TransactionalStorage.Batch(actions =>
112            {
113                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
114                actions.Documents.AddDocument("b", null, new RavenJObject(), new RavenJObject());
115                actions.Documents.AddDocument("c", null, new RavenJObject(), new RavenJObject());
116            });
117
118            db.TransactionalStorage.Batch(actions =>
119            {
120                var doc = actions.Documents.DocumentByKey("a");
121                var documents = actions.Documents.GetDocumentsAfter(doc.Etag, 5, CancellationToken.None).Select(x => x.Key).ToArray();
122                Assert.Equal(2, documents.Length);
123                Assert.Equal("b", documents[0]);
124                Assert.Equal("c", documents[1]);
125            });
126        }
127
128        [Fact]
129        public void CanGetDocumentAfterAnEtagAfterDocumentUpdateWouldReturnThatDocument()
130        {
131            db.TransactionalStorage.Batch(actions =>
132            {
133                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
134                actions.Documents.AddDocument("b", null, new RavenJObject(), new RavenJObject());
135                actions.Documents.AddDocument("c", null, new RavenJObject(), new RavenJObject());
136            });
137
138            Etag etag = null;
139            db.TransactionalStorage.Batch(actions =>
140            {
141                var doc = actions.Documents.DocumentByKey("a");
142                etag = doc.Etag;
143                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
144            });
145
146            db.TransactionalStorage.Batch(actions =>
147            {
148                var documents = actions.Documents.GetDocumentsAfter(etag, 5, CancellationToken.None).Select(x => x.Key).ToArray();
149                Assert.Equal(3, documents.Length);
150                Assert.Equal("b", documents[0]);
151                Assert.Equal("c", documents[1]);
152                Assert.Equal("a", documents[2]);
153            });
154        }
155
156        [Fact]
157        public void UpdatingDocumentWillKeepSameCount()
158        {
159            db.TransactionalStorage.Batch(actions =>
160            {
161                Assert.Equal(0, actions.Documents.GetDocumentsCount());
162
163                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
164
165            });
166
167            db.TransactionalStorage.Batch(actions =>
168            {
169                Assert.Equal(1, actions.Documents.GetDocumentsCount());
170
171                actions.Documents.AddDocument("a", null, new RavenJObject(), new RavenJObject());
172            });
173
174
175            db.TransactionalStorage.Batch(actions => Assert.Equal(1, actions.Documents.GetDocumentsCount()));
176        }
177
178
179
180        [Fact]
181        public void CanEnqueueAndPeek()
182        {
183            db.TransactionalStorage.Batch(actions => actions.Queue.EnqueueToQueue("ayende", new byte[] { 1, 2 }));
184
185            db.TransactionalStorage.Batch(actions => Assert.Equal(new byte[] { 1, 2 }, actions.Queue.PeekFromQueue("ayende").First().Item1));
186        }
187
188        [Fact]
189        public void PoisonMessagesWillBeDeleted()
190        {
191            db.TransactionalStorage.Batch(actions => actions.Queue.EnqueueToQueue("ayende", new byte[] { 1, 2 }));
192
193            db.TransactionalStorage.Batch(actions =>
194            {
195                for (int i = 0; i < 6; i++)
196                {
197                    actions.Queue.PeekFromQueue("ayende").First();
198                }
199                Assert.Equal(null, actions.Queue.PeekFromQueue("ayende").FirstOrDefault());
200            });
201        }
202
203        [Fact]
204        public void CanDeleteQueuedData()
205        {
206            db.TransactionalStorage.Batch(actions => actions.Queue.EnqueueToQueue("ayende", new byte[] { 1, 2 }));
207
208            db.TransactionalStorage.Batch(actions =>
209            {
210                actions.Queue.DeleteFromQueue("ayende", actions.Queue.PeekFromQueue("ayende").First().Item2);
211                Assert.Equal(null, actions.Queue.PeekFromQueue("ayende").FirstOrDefault());
212            });
213        }
214
215        [Fact]
216        public void CanGetNewIdentityValues()
217        {
218            db.TransactionalStorage.Batch(actions =>
219            {
220                var nextIdentityValue = actions.General.GetNextIdentityValue("users");
221
222                Assert.Equal(1, nextIdentityValue);
223
224                nextIdentityValue = actions.General.GetNextIdentityValue("users");
225
226                Assert.Equal(2, nextIdentityValue);
227
228            });
229
230            db.TransactionalStorage.Batch(actions =>
231            {
232                var nextIdentityValue = actions.General.GetNextIdentityValue("users");
233
234                Assert.Equal(3, nextIdentityValue);
235
236                nextIdentityValue = actions.General.GetNextIdentityValue("users");
237
238                Assert.Equal(4, nextIdentityValue);
239
240            });
241        }
242
243        [Fact]
244        public void CanGetNewIdentityValuesWhenUsingTwoDifferentItems()
245        {
246            db.TransactionalStorage.Batch(actions =>
247            {
248                var nextIdentityValue = actions.General.GetNextIdentityValue("users");
249
250                Assert.Equal(1, nextIdentityValue);
251
252                nextIdentityValue = actions.General.GetNextIdentityValue("blogs");
253
254                Assert.Equal(1, nextIdentityValue);
255
256            });
257
258            db.TransactionalStorage.Batch(actions =>
259            {
260                var nextIdentityValue = actions.General.GetNextIdentityValue("blogs");
261
262                Assert.Equal(2, nextIdentityValue);
263
264                nextIdentityValue = actions.General.GetNextIdentityValue("users");
265
266                Assert.Equal(2, nextIdentityValue);
267
268            });
269        }
270
271        [Fact]
272        public void check_alerts_document()
273        {
274            db.TransactionalStorage.Batch(actions =>
275            {
276                Assert.DoesNotThrow(() =>
277                {
278                    var doc = actions.Documents.DocumentByKey(Constants.RavenAlerts);
279                    if (doc == null)
280                        return;
281
282                    throw new InvalidOperationException("Alerts document data: " + doc.DataAsJson);
283                });
284            });
285        }
286    }
287}