コード例 #1
0
        public void ShouldAddDocumentEntityToTheCacheIfDoesNotPresent()
        {
            unitOfWork.UpdateWithDocument(SimpleEntity.CreateDocument());

            object cachedEntity;

            Assert.True(unitOfWork.TryGetByEntityIdAndType(SimpleEntity.StandardId, typeof(SimpleEntity), out cachedEntity));
            Assert.NotNull(cachedEntity);
            Assert.Equal(42, ((SimpleEntity)cachedEntity).Age);
        }
コード例 #2
0
        public void ShouldThrowIfDatabaseMissing()
        {
            var handler = new MockMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent("{\"error\":\"not_found\",\"reason\":\"no_db_file\"}", Encoding.UTF8, MediaType.Json)
            });

            Assert.Throws <DatabaseMissingException>(
                () => GetDatabaseApi(handler).Synchronously.SaveDocument(SimpleEntity.CreateDocument())
                );
        }
コード例 #3
0
        public void ShouldRetriveCachedEntitesByDocumentId()
        {
            unitOfWork.UpdateWithDocument(SimpleEntity.CreateDocument());

            object cachedEntity;

            Assert.True(unitOfWork.TryGetByDocumentId(SimpleEntity.StandardDocId, out cachedEntity));
            Assert.IsType <SimpleEntity>(cachedEntity);
            Assert.NotNull(cachedEntity);
            Assert.Equal(42, ((SimpleEntity)cachedEntity).Age);
        }
コード例 #4
0
        public void ShouldNotUpdateEntitesUnderUser()
        {
            var entity = SimpleEntity.CreateStandard();

            entity.Age = 43;
            unitOfWork.Attach(entity);

            unitOfWork.UpdateWithDocument(SimpleEntity.CreateDocument());

            object cachedEntity;

            Assert.True(unitOfWork.TryGetByDocumentId(SimpleEntity.StandardDocId, out cachedEntity));
            Assert.IsType <SimpleEntity>(cachedEntity);
            Assert.NotNull(cachedEntity);
            Assert.Equal(43, ((SimpleEntity)cachedEntity).Age);
        }
コード例 #5
0
        public void ShouldNotAllowAnyOperationBeforeChangeSaveOperationCompletes(string operationName)
        {
            // I know this test is ginormus, but it's pretty self-contained and simple to understand

            var saveChangesOperationShouldProceed = new AutoResetEvent(initialState: false);
            var saveChangesOperationStarted       = new AutoResetEvent(initialState: false);
            var executedOperations = new List <string>();

            IDictionary <string, DocumentInfo> returnInfo = new Dictionary <string, DocumentInfo> {
                { SimpleEntity.StandardDocId, new DocumentInfo(SimpleEntity.StandardDocId, "2-cc2c5ab22cfa4a0faad27a0cb9ca7968") }
            };

            var dbApiMock = new Mock <IDatabaseApi>();

            dbApiMock
            .Setup(couchApi => couchApi.BulkUpdate(It.IsAny <Action <IBulkUpdateBatch> >()))
            .Returns <Action <IBulkUpdateBatch> >(
                updater => Task.Factory.StartNew(
                    () => {
                saveChangesOperationStarted.Set();
                saveChangesOperationShouldProceed.WaitOne();
                updater(Mock.Of <IBulkUpdateBatch>());
                return(returnInfo);
            }
                    )
                );
            dbApiMock
            .Setup(api => api.Query(It.IsAny <ViewQuery>()))
            .Returns <ViewQuery>(
                _ => {
                lock (executedOperations)
                    executedOperations.Add("Query");
                return(ViewQueryResult.Empty.ToTask());
            });
            dbApiMock
            .Setup(api => api.QueryLucene(It.IsAny <LuceneQuery>()))
            .Returns <LuceneQuery>(
                _ => {
                lock (executedOperations)
                    executedOperations.Add("QueryLucene");
                return
                (LuceneQueryResult.Empty.ToTask());
            });
            dbApiMock
            .Setup(api => api.RequestDocument(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AdditionalDocumentProperty>()))
            .Returns <string, string, AdditionalDocumentProperty>(
                (_, __, ___) => {
                lock (executedOperations)
                    executedOperations.Add("RequestDocument");
                return(SimpleEntity.CreateDocument().ToTask());
            });
            dbApiMock
            .Setup(api => api.RequestLastestDocumentRevision(It.IsAny <string>()))
            .Returns <string>(
                _ => {
                lock (executedOperations)
                    executedOperations.Add("RequestLastestDocumentRevision");
                return(SimpleEntity.StandardDocId.ToTask());
            });
            dbApiMock
            .Setup(api => api.DeleteDocument(It.IsAny <string>(), It.IsAny <string>()))
            .Returns <string, string>(
                (_, __) => {
                lock (executedOperations)
                    executedOperations.Add("DeleteDocument");
                return(SimpleEntity.StandardDocumentInfo.ToTask());
            });
            dbApiMock
            .Setup(api => api.SaveDocument(It.IsAny <Document>()))
            .Returns <Document>(
                _ => {
                lock (executedOperations)
                    executedOperations.Add("SaveDocument");
                return(SimpleEntity.StandardDocumentInfo.ToTask());
            });

            ISession sesion = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object));

            sesion.Save(SimpleEntity.CreateStandardWithoutRevision());
            var saveChangesOperation = sesion.StartSavingChanges();

            // Wating for StartSavingChanges to delegate execution to CouchAPI resetting session-wide wait handle
            saveChangesOperationStarted.WaitOne();

            var startOperationTask = Task.Factory.StartNew(
                () => {
                Task operation = null;
                switch (operationName)
                {
                case "load":
                    operation = sesion.Load <SimpleEntity>("doc1a");
                    break;

                case "query":
                    operation =
                        sesion.Query <SimpleEntity>(
                            new ViewQuery {
                        DesignDocumentName = "dd1",
                        ViewName           = "byX",
                        Key         = "key1",
                        IncludeDocs = true
                    });
                    break;

                case "fullTextQuery":
                    operation =
                        sesion.QueryLucene <SimpleEntity>(
                            new LuceneQuery
                    {
                        DesignDocumentName = "dd1",
                        IndexName          = "byX",
                        IncludeDocs        = true,
                        Query = "key1:2"
                    });
                    break;
                }
                // ReSharper disable PossibleNullReferenceException
                operation.WaitOrThrowOnTimeout();
                // ReSharper restore PossibleNullReferenceException
            });

            Assert.True(
                executedOperations.Count == 0,
                string.Join(", ", executedOperations) + " operation(s) have executed before save changes operation completes");

            saveChangesOperationShouldProceed.Set();
            saveChangesOperation.WaitOrThrowOnTimeout();

            startOperationTask.WaitOrThrowOnTimeout();
            Assert.Equal(1, executedOperations.Count);
        }