public void ShouldNotFailOnIncorrectDocumentFromCouchDb(string docString) { var dbApiMock = new Mock <IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] { "key1", 0 }.ToJsonFragment(), new { Title = "Object title", Subject = "some" }.ToJsonFragment(), Entity.StandardDocId, new Document(docString) ) }, totalCount: 1, offset: 0).ToTask <IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var queryResult = session.Synchronously.Query <Entity>(new ViewQuery { IncludeDocs = true }); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.Rows.Count()); Assert.Null(queryResult.First()); }
public void ShouldCacheInstanceFromQuery() { var dbApiMock = new Mock <IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery q) => new ViewQueryResult(query: q, rows: new[] { new ViewResultRow( new object[] { "key1", 0 }.ToJsonFragment(), null, Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask <IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); Entity queriedEntity = session.Synchronously.Query <Entity>(new ViewQuery { IncludeDocs = true }).First(); var loadedEntity = session.Synchronously.Load <Entity>(Entity.StandardEntityId); Assert.Same(queriedEntity, loadedEntity); }
public void ShouldQueryCochApiWithSameQueryObject() { ViewQuery sendQuery = null; var dbApiMock = new Mock <IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery query) => { sendQuery = query; return(new ViewQueryResult( query, new[] { new ViewResultRow( Entity.StandardDocId.ToJsonFragment(), new { rev = Entity.StandardRevision }.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask <IViewQueryResult>()); }); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var viewQuery = new ViewQuery { ViewName = "_all_docs", IncludeDocs = true }; session.Synchronously.Query <Entity>(viewQuery); Assert.Same(sendQuery, viewQuery); }
public void ShouldMapViewdataIfTypeIsCompatible() { var dbApiMock = new Mock <IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] { "key1", 0 }.ToJsonFragment(), new { Title = "Object title", Subject = "some" }.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask <IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var queryResult = session.Synchronously.Query <ViewData>(new ViewQuery()); Assert.NotNull(queryResult); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.TotalCount); var row = queryResult.First(); Assert.NotNull(row); Assert.Equal("Object title", row.Title); }
public void ShouldBindDocumentsCorrectly() { Entity entity = Entity.CreateStandard(); var dbApiMock = new Mock<IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.Query(It.IsAny<ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult( query, new[] { new ViewResultRow( Entity.StandardDocId.ToJsonFragment(), new {rev = Entity.StandardRevision}.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0 ).ToTask<IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); IViewQueryResult<Entity> queryResult = session.Synchronously.Query<Entity>(new ViewQuery {ViewName = "_all_docs", IncludeDocs = true}); Entity firstRow = queryResult.First(); Assert.NotNull(firstRow); Assert.Equal(Entity.StandardEntityId, firstRow.Id); Assert.Equal(Entity.StandardRevision, firstRow.Revision); Assert.Equal(entity.Age, firstRow.Age); Assert.Equal(entity.Date, firstRow.Date); Assert.Equal(entity.Name, firstRow.Name); }
public void ShouldThrowOnNullEntity() { ISession session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>()); Assert.Throws<ArgumentNullException>(() => session.Save(null as Entity)); Assert.Throws<ArgumentNullException>(() => session.Save(null as Entity[])); Assert.Throws<ArgumentNullException>(() => session.Save(Entity.CreateStandardWithoutRevision(), null)); }
public void ShouldReturnSameInstanceWhenRequestingTwice() { var session = new CouchSession(Default.Settings, MockCouchApi()); var entityA = session.Synchronously.Load <Entity>("doc1"); var entityB = session.Synchronously.Load <Entity>("doc1"); Assert.Same(entityA, entityB); }
public void ShouldThrowOnNullEntity() { ISession session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>()); Assert.Throws <ArgumentNullException>(() => session.Save(null as Entity)); Assert.Throws <ArgumentNullException>(() => session.Save(null as Entity[])); Assert.Throws <ArgumentNullException>(() => session.Save(Entity.CreateStandardWithoutRevision(), null)); }
public void ShouldThrowQueryExceptionIfNoIncludeDocsOptionAndEntityTypeParameter() { var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>()); Assert.Throws <QueryException>( () => session.Synchronously.Query <Entity>(new ViewQuery { ViewName = "_all_docs" })); }
public void ShouldGenerateIdIfNoneSet() { var savingEntity = new Entity { Name = "Стас Гиркин" }; ISession session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>()); session.Save(savingEntity); Assert.NotNull(savingEntity.Id); Assert.NotEqual(string.Empty, savingEntity.Id); }
public void x() { Documents.Configure <Port>((info, port) => port.Id = info.Id).UniqueConstraint = p => "--" + p.Slug; using (var session = new CouchSession(new Uri("http://127.0.0.1:5984"), "cruiseme")) { var results = session.Design("ports").ViewDocs <Port>("by_slug"); } }
public void ShouldCacheFreashlySavedInstance() { var savingEntity = Entity.CreateStandardWithoutRevision(); ISession session = new CouchSession( Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == new Mock<IDatabaseApi>(MockBehavior.Strict).Object)); session.Save(savingEntity); var loadedInstance = session.Synchronously.Load<Entity>(savingEntity.Id); Assert.Same(savingEntity, loadedInstance); }
public void ShouldGenerateIdIfNoneSet() { var savingEntity = new Entity { Name = "Стас Гиркин" }; ISession session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>()); session.Save(savingEntity); Assert.NotNull(savingEntity.Id); Assert.NotEqual(string.Empty, savingEntity.Id); }
public void ShouldReturnSameInstancePerformingLoadAfterSave() { var entity = new Entity { Id = "doc1", Name = "Стас Гиркин" }; var session = new CouchSession(Default.Settings, MockCouchApi()); session.Save(entity); var loadedEntity = session.Synchronously.Load <Entity>("doc1"); Assert.Same(entity, loadedEntity); }
public void ShouldStopReturnigDeletedEntityFromCache() { var entity = Entity.CreateStandardWithoutRevision(); ISession session = new CouchSession( Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == new Mock <IDatabaseApi>(MockBehavior.Strict).Object)); session.Save(entity); session.Delete(entity); Assert.Null(session.Synchronously.Load <Entity>(entity.Id)); }
public void get_known_db() { var url = "https://397ad533-6a4c-4a30-813f-bbf616b6d2c9.appharbor:6jLDgafReC50mf6tcgRmumJy@397ad533-6a4c-4a30-813f-bbf616b6d2c9.appharbor.cloudant.com"; using (var session = new CouchSession(new Uri(url), "cruiseme")) { var db = session.Db.Get(); Assert.That(db, Is.Not.Null); Assert.That(db.Name, Is.EqualTo(session.DatabaseName)); } }
public void ShouldLoadDataCorrectlyIfNoRevisionPropertyFound() { var couchApi = Mock.Of<ICouchApi>( c => c.Db("testdb") == Mock.Of<IDatabaseApi>( api => api.RequestDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AdditionalDocumentProperty>()) == EntityWithoutRevision.CreateDocumentWithRevision().ToTask() )); var loadedEntity = new CouchSession(Default.Settings, couchApi).Synchronously.Load<EntityWithoutRevision>("doc1"); Assert.NotNull(loadedEntity); Assert.Equal("doc1", loadedEntity.Id); Assert.Equal("Стас Гиркин", loadedEntity.Name); }
public void ShouldCacheFreashlySavedInstance() { var savingEntity = Entity.CreateStandardWithoutRevision(); ISession session = new CouchSession( Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == new Mock <IDatabaseApi>(MockBehavior.Strict).Object)); session.Save(savingEntity); var loadedInstance = session.Synchronously.Load <Entity>(savingEntity.Id); Assert.Same(savingEntity, loadedInstance); }
public void ShouldThrowOnSaveWithRevision() { ISession session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>()); Assert.Throws<ArgumentException>(() => session.Save( new Entity { Id = "doc1", Revision = "42-1a517022a0c2d4814d51abfedf9bfee7", Name = "Стас Гиркин" } ) ); }
public void test_cloudant_connection() { using (var couch = new CouchSession( new Uri( "https://397ad533-6a4c-4a30-813f-bbf616b6d2c9.appharbor:6jLDgafReC50mf6tcgRmumJy@397ad533-6a4c-4a30-813f-bbf616b6d2c9.appharbor.cloudant.com"), "cruiseme")) { var dbs = couch.Db.Get(); Console.WriteLine(dbs); } }
public void ShouldLoadDataCorrectlyIfNoRevisionPropertyFound() { var couchApi = Mock.Of <ICouchApi>( c => c.Db("testdb") == Mock.Of <IDatabaseApi>( api => api.RequestDocument(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AdditionalDocumentProperty>()) == EntityWithoutRevision.CreateDocumentWithRevision().ToTask() )); var loadedEntity = new CouchSession(Default.Settings, couchApi).Synchronously.Load <EntityWithoutRevision>("doc1"); Assert.NotNull(loadedEntity); Assert.Equal("doc1", loadedEntity.Id); Assert.Equal("Стас Гиркин", loadedEntity.Name); }
public void ShouldNotAllowNewChangeSaveOperationBeforePreviousOneCompletes() { var apiShouldEnter = new AutoResetEvent(initialState: false); var bulkUpdateCalled = 0; 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( () => { apiShouldEnter.WaitOne(); Interlocked.Increment(ref bulkUpdateCalled); updater(Mock.Of <IBulkUpdateBatch>()); return(returnInfo); }, TaskCreationOptions.PreferFairness ) ); ISession sesion = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var entity = SimpleEntity.CreateStandardWithoutRevision(); sesion.Save(entity); var firstSaveChangesTask = sesion.StartSavingChanges(); // Enters API method Thread.Sleep(500); // Allowing first task to arrive to apiShouldEnter wating line first var secondSaveChangesTask = sesion.StartSavingChanges(); // Waits for first task to end Assert.Equal(0, bulkUpdateCalled); apiShouldEnter.Set(); firstSaveChangesTask.WaitOrThrowOnTimeout(); Assert.Equal(1, bulkUpdateCalled); entity.Age--; // Modifing entity for second save to be applicable apiShouldEnter.Set(); // Permitting second API method call to proceed secondSaveChangesTask.WaitOrThrowOnTimeout(); Assert.Equal(2, bulkUpdateCalled); Assert.Equal("2-cc2c5ab22cfa4a0faad27a0cb9ca7968", entity.Revision); }
public void ShouldThrowOnSaveWithRevision() { ISession session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>()); Assert.Throws <ArgumentException>(() => session.Save( new Entity { Id = "doc1", Revision = "42-1a517022a0c2d4814d51abfedf9bfee7", Name = "Стас Гиркин" } ) ); }
public void ShouldDelegateChangesSaveToApi() { var couchApi = Mock.Of<ICouchApi>( c => c.Db("testdb") == Mock.Of<IDatabaseApi>( d => d.BulkUpdate(It.IsAny<Action<IBulkUpdateBatch>>()) == new Dictionary<string, DocumentInfo>().ToTask<IDictionary<string, DocumentInfo>>() ) ); ISession sesion = new CouchSession(Default.Settings, couchApi); sesion.Save(SimpleEntity.CreateStandardWithoutRevision()); sesion.SaveChanges(); Mock.Get(couchApi.Db("testdb")).Verify(dbApi => dbApi.BulkUpdate(It.IsAny<Action<IBulkUpdateBatch>>()), Times.Once()); }
public void ShouldDelegateChangesSaveToApi() { var couchApi = Mock.Of <ICouchApi>( c => c.Db("testdb") == Mock.Of <IDatabaseApi>( d => d.BulkUpdate(It.IsAny <Action <IBulkUpdateBatch> >()) == new Dictionary <string, DocumentInfo>().ToTask <IDictionary <string, DocumentInfo> >() ) ); ISession sesion = new CouchSession(Default.Settings, couchApi); sesion.Save(SimpleEntity.CreateStandardWithoutRevision()); sesion.SaveChanges(); Mock.Get(couchApi.Db("testdb")).Verify(dbApi => dbApi.BulkUpdate(It.IsAny <Action <IBulkUpdateBatch> >()), Times.Once()); }
public void ShouldNotThrowArgumentExceptionIfNoRevisionOnLoadedEntity() { string deletedId = null; string deletedRev = null; var bulkUpdateBatchMock = new Mock <IBulkUpdateBatch>(); bulkUpdateBatchMock .Setup(b => b.Delete(It.IsAny <string>(), It.IsAny <string>())) .Callback( (string id, string rev) => { deletedId = id; deletedRev = rev; }); var documentInfo = new DocumentInfo(EntityWithoutRevision.StandardDocId, "2-1a517022a0c2d4814d51abfedf9bfee7"); var result = documentInfo; var dbApiMock = new Mock <IDatabaseApi>(); dbApiMock .Setup(ca => ca.BulkUpdate(It.IsAny <Action <IBulkUpdateBatch> >())) .Returns <Action <IBulkUpdateBatch> >( updateAction => { updateAction(bulkUpdateBatchMock.Object); return(new Dictionary <string, DocumentInfo> { { result.Id, result } } .ToTask <IDictionary <string, DocumentInfo> >()); }); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AdditionalDocumentProperty>())) .Returns(EntityWithoutRevision.CreateDocumentWithRevision().ToTask()); dbApiMock .Setup(ca => ca.Synchronously).Returns(new SynchronousDatabaseApi(dbApiMock.Object)); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); Assert.DoesNotThrow(() => { var entity = session.Synchronously.Load <EntityWithoutRevision>(EntityWithoutRevision.StandardEntityId); session.Delete(entity: entity); session.SaveChanges(); }); Assert.Equal(EntityWithoutRevision.StandardDocId, deletedId); Assert.Equal(EntityWithoutRevision.StandardRevision, deletedRev); }
public void ShouldAssignNewlyUpdatedRevisionsToEntities() { var couchApi = Mock.Of<ICouchApi>( c => c.Db("testdb") == Mock.Of<IDatabaseApi>( d => d.BulkUpdate(It.IsAny<Action<IBulkUpdateBatch>>()) == new Dictionary<string, DocumentInfo> { { SimpleEntity.StandardDocId, new DocumentInfo(SimpleEntity.StandardDocId, "2-cc2c5ab22cfa4a0faad27a0cb9ca7968") } }.ToTask<IDictionary<string, DocumentInfo>>() ) ); ISession sesion = new CouchSession(Default.Settings, couchApi); var entity = SimpleEntity.CreateStandardWithoutRevision(); sesion.Save(entity); sesion.SaveChanges(); Assert.Equal("2-cc2c5ab22cfa4a0faad27a0cb9ca7968", entity.Revision); }
public void ShouldNotThrowArgumentExceptionIfNoRevisionOnLoadedEntity() { string deletedId = null; string deletedRev = null; var bulkUpdateBatchMock = new Mock<IBulkUpdateBatch>(); bulkUpdateBatchMock .Setup(b => b.Delete(It.IsAny<string>(), It.IsAny<string>())) .Callback( (string id, string rev) => { deletedId = id; deletedRev = rev; }); var documentInfo = new DocumentInfo(EntityWithoutRevision.StandardDocId, "2-1a517022a0c2d4814d51abfedf9bfee7"); var result = documentInfo; var dbApiMock = new Mock<IDatabaseApi>(); dbApiMock .Setup(ca => ca.BulkUpdate(It.IsAny<Action<IBulkUpdateBatch>>())) .Returns<Action<IBulkUpdateBatch>>( updateAction => { updateAction(bulkUpdateBatchMock.Object); return new Dictionary<string, DocumentInfo> {{result.Id, result}} .ToTask<IDictionary<string, DocumentInfo>>(); }); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AdditionalDocumentProperty>())) .Returns(EntityWithoutRevision.CreateDocumentWithRevision().ToTask()); dbApiMock .Setup(ca => ca.Synchronously).Returns(new SynchronousDatabaseApi(dbApiMock.Object)); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); Assert.DoesNotThrow(() => { var entity = session.Synchronously.Load<EntityWithoutRevision>(EntityWithoutRevision.StandardEntityId); session.Delete(entity: entity); session.SaveChanges(); }); Assert.Equal(EntityWithoutRevision.StandardDocId, deletedId); Assert.Equal(EntityWithoutRevision.StandardRevision, deletedRev); }
public void ShouldRequestCouchApiUsingDocumentIdRatherEntityId() { string requestedId = null; var dbApiMock = new Mock <IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AdditionalDocumentProperty>())) .Returns( (string id, string revision, AdditionalDocumentProperty additionalProperties) => { requestedId = id; return(Entity.CreateDocWithRevision().ToTask()); }); var session = new CouchSession(Default.Settings, MockCouchApi(dbApiMock)); session.Synchronously.Load <Entity>(Entity.StandardEntityId); Assert.Equal(Entity.StandardDocId, requestedId); }
public void ShouldAssignNewlyUpdatedRevisionsToEntities() { var couchApi = Mock.Of <ICouchApi>( c => c.Db("testdb") == Mock.Of <IDatabaseApi>( d => d.BulkUpdate(It.IsAny <Action <IBulkUpdateBatch> >()) == new Dictionary <string, DocumentInfo> { { SimpleEntity.StandardDocId, new DocumentInfo(SimpleEntity.StandardDocId, "2-cc2c5ab22cfa4a0faad27a0cb9ca7968") } }.ToTask <IDictionary <string, DocumentInfo> >() ) ); ISession sesion = new CouchSession(Default.Settings, couchApi); var entity = SimpleEntity.CreateStandardWithoutRevision(); sesion.Save(entity); sesion.SaveChanges(); Assert.Equal("2-cc2c5ab22cfa4a0faad27a0cb9ca7968", entity.Revision); }
public void ShouldLoadDataThroughtCouchApi() { var dbApiMock = new Mock<IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AdditionalDocumentProperty>())) .Returns( (string _, string __, AdditionalDocumentProperty ___) => Entity.CreateDocWithRevision().ToTask()); dbApiMock .Setup(ca => ca.SaveDocument(It.IsAny<Document>())) .Returns(Entity.StandardDococumentInfo.ToTask()); dbApiMock .Setup(ca => ca.Synchronously).Returns(() => new SynchronousDatabaseApi(dbApiMock.Object)); var session = new CouchSession(Default.Settings, MockCouchApi(dbApiMock.Object)); var loadedEntity = session.Synchronously.Load<Entity>(Entity.StandardEntityId); Assert.NotNull(loadedEntity); Assert.Equal(Entity.StandardEntityId, loadedEntity.Id); Assert.Equal(Entity.StandardRevision, loadedEntity.Revision); Assert.Equal("Стас Гиркин", loadedEntity.Name); }
public void ShouldBindDocumentsCorrectly() { Entity entity = Entity.CreateStandard(); var dbApiMock = new Mock <IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult( query, new[] { new ViewResultRow( Entity.StandardDocId.ToJsonFragment(), new { rev = Entity.StandardRevision }.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0 ).ToTask <IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); IViewQueryResult <Entity> queryResult = session.Synchronously.Query <Entity>(new ViewQuery { ViewName = "_all_docs", IncludeDocs = true }); Entity firstRow = queryResult.First(); Assert.NotNull(firstRow); Assert.Equal(Entity.StandardEntityId, firstRow.Id); Assert.Equal(Entity.StandardRevision, firstRow.Revision); Assert.Equal(entity.Age, firstRow.Age); Assert.Equal(entity.Date, firstRow.Date); Assert.Equal(entity.Name, firstRow.Name); }
public void ShouldLoadDataThroughtCouchApi() { var dbApiMock = new Mock <IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AdditionalDocumentProperty>())) .Returns( (string _, string __, AdditionalDocumentProperty ___) => Entity.CreateDocWithRevision().ToTask()); dbApiMock .Setup(ca => ca.SaveDocument(It.IsAny <Document>())) .Returns(Entity.StandardDococumentInfo.ToTask()); dbApiMock .Setup(ca => ca.Synchronously).Returns(() => new SynchronousDatabaseApi(dbApiMock.Object)); var session = new CouchSession(Default.Settings, MockCouchApi(dbApiMock.Object)); var loadedEntity = session.Synchronously.Load <Entity>(Entity.StandardEntityId); Assert.NotNull(loadedEntity); Assert.Equal(Entity.StandardEntityId, loadedEntity.Id); Assert.Equal(Entity.StandardRevision, loadedEntity.Revision); Assert.Equal("Стас Гиркин", loadedEntity.Name); }
public void ShouldUpdateChangedDocumentsOnFlush() { IDocument lastUpdatedDoc = null; var totalSaveCount = 0; var couchApiMock = new Mock <ICouchApi>(MockBehavior.Loose); couchApiMock .Setup(ca => ca.SaveDocument(It.IsAny <IDocument>())) .Returns( (IDocument doc) => { lastUpdatedDoc = doc; totalSaveCount++; return(new DocumentInfo(entity.Id, "2-1a517022a0c2d4814d51abfedf9bfee7").ToTask()); }); couchApiMock .Setup(ca => ca.Synchronously).Returns(() => new SynchronousCouchApi(couchApiMock.Object)); var session = new CouchSession(Default.Settings, couchApiMock.Object); session.Save(entity); entity.Name = "Artem Tikhomirov"; session.SaveChanges(); Assert.Equal(2, totalSaveCount); Assert.Equal("simpleEntity.doc1", lastUpdatedDoc.Id); Assert.Equal( new { _id = "simpleEntity.doc1", _rev = "2-1a517022a0c2d4814d51abfedf9bfee7", type = "simpleEntity", name = "Artem Tikhomirov", age = 42, date = "1957-04-10T00:00:00" }.ToDocument(), lastUpdatedDoc ); }
public void ShouldUpdateChangedDocumentsOnFlush() { IDocument lastUpdatedDoc = null; var totalSaveCount = 0; var couchApiMock = new Mock<ICouchApi>(MockBehavior.Loose); couchApiMock .Setup(ca => ca.SaveDocument(It.IsAny<IDocument>())) .Returns( (IDocument doc) => { lastUpdatedDoc = doc; totalSaveCount++; return new DocumentInfo(entity.Id, "2-1a517022a0c2d4814d51abfedf9bfee7").ToTask(); }); couchApiMock .Setup(ca => ca.Synchronously).Returns(() => new SynchronousCouchApi(couchApiMock.Object)); var session = new CouchSession(Default.Settings, couchApiMock.Object); session.Save(entity); entity.Name = "Artem Tikhomirov"; session.SaveChanges(); Assert.Equal(2, totalSaveCount); Assert.Equal("simpleEntity.doc1", lastUpdatedDoc.Id); Assert.Equal( new { _id = "simpleEntity.doc1", _rev = "2-1a517022a0c2d4814d51abfedf9bfee7", type = "simpleEntity", name = "Artem Tikhomirov", age = 42, date = "1957-04-10T00:00:00" }.ToDocument(), lastUpdatedDoc ); }
public void ShouldNotFailIfOnNullValueRowsFromCouchDB() { var dbApiMock = new Mock <IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny <ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] { "key1", 0 }.ToJsonFragment(), null, Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask <IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of <ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); IViewQueryResult <ViewData> queryResult = session.Synchronously.Query <ViewData>(new ViewQuery()); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.Count()); Assert.Null(queryResult.First()); }
public void ShouldMapViewdataIfTypeIsCompatible() { var dbApiMock = new Mock<IDatabaseApi>(); dbApiMock .Setup(a => a.Query( It.IsAny<ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] {"key1", 0}.ToJsonFragment(), new {Title = "Object title", Subject = "some"}.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask<IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var queryResult = session.Synchronously.Query<ViewData>(new ViewQuery()); Assert.NotNull(queryResult); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.TotalCount); var row = queryResult.First(); Assert.NotNull(row); Assert.Equal("Object title", row.Title); }
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); }
public void ShouldNotAllowNewChangeSaveOperationBeforePreviousOneCompletes() { var apiShouldEnter = new AutoResetEvent(initialState: false); var bulkUpdateCalled = 0; 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( () => { apiShouldEnter.WaitOne(); Interlocked.Increment(ref bulkUpdateCalled); updater(Mock.Of<IBulkUpdateBatch>()); return returnInfo; }, TaskCreationOptions.PreferFairness ) ); ISession sesion = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var entity = SimpleEntity.CreateStandardWithoutRevision(); sesion.Save(entity); var firstSaveChangesTask = sesion.StartSavingChanges(); // Enters API method Thread.Sleep(500); // Allowing first task to arrive to apiShouldEnter wating line first var secondSaveChangesTask = sesion.StartSavingChanges(); // Waits for first task to end Assert.Equal(0, bulkUpdateCalled); apiShouldEnter.Set(); firstSaveChangesTask.WaitOrThrowOnTimeout(); Assert.Equal(1, bulkUpdateCalled); entity.Age--; // Modifing entity for second save to be applicable apiShouldEnter.Set(); // Permitting second API method call to proceed secondSaveChangesTask.WaitOrThrowOnTimeout(); Assert.Equal(2, bulkUpdateCalled); Assert.Equal("2-cc2c5ab22cfa4a0faad27a0cb9ca7968", entity.Revision); }
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); }
public void ShouldNotFailIfOnNullValueRowsFromCouchDB() { var dbApiMock = new Mock<IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny<ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] {"key1", 0}.ToJsonFragment(), null, Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask<IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); IViewQueryResult<ViewData> queryResult = session.Synchronously.Query<ViewData>(new ViewQuery()); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.Count()); Assert.Null(queryResult.First()); }
public void ShouldReturnSameInstancePerformingLoadAfterSave() { var entity = new Entity { Id = "doc1", Name = "Стас Гиркин" }; var session = new CouchSession(Default.Settings, MockCouchApi()); session.Save(entity); var loadedEntity = session.Synchronously.Load<Entity>("doc1"); Assert.Same(entity, loadedEntity); }
public void ShouldQueryCochApiWithSameQueryObject() { ViewQuery sendQuery = null; var dbApiMock = new Mock<IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.Query(It.IsAny<ViewQuery>())) .Returns( (ViewQuery query) => { sendQuery = query; return new ViewQueryResult( query, new[] { new ViewResultRow( Entity.StandardDocId.ToJsonFragment(), new {rev = Entity.StandardRevision}.ToJsonFragment(), Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask<IViewQueryResult>(); }); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var viewQuery = new ViewQuery {ViewName = "_all_docs", IncludeDocs = true}; session.Synchronously.Query<Entity>(viewQuery); Assert.Same(sendQuery, viewQuery); }
public void ShouldNotFailOnIncorrectDocumentFromCouchDb(string docString) { var dbApiMock = new Mock<IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny<ViewQuery>())) .Returns( (ViewQuery query) => new ViewQueryResult(query: query, rows: new[] { new ViewResultRow( new object[] {"key1", 0}.ToJsonFragment(), new {Title = "Object title", Subject = "some"}.ToJsonFragment(), Entity.StandardDocId, new Document(docString) ) }, totalCount: 1, offset: 0).ToTask<IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); var queryResult = session.Synchronously.Query<Entity>(new ViewQuery {IncludeDocs = true}); Assert.Equal(1, queryResult.Count); Assert.Equal(1, queryResult.Rows.Count()); Assert.Null(queryResult.First()); }
public void ShouldThrowQueryExceptionIfNoIncludeDocsOptionAndEntityTypeParameter() { var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>()); Assert.Throws<QueryException>( () => session.Synchronously.Query<Entity>(new ViewQuery {ViewName = "_all_docs"})); }
public void ShouldRequestCouchApiUsingDocumentIdRatherEntityId() { string requestedId = null; var dbApiMock = new Mock<IDatabaseApi>(MockBehavior.Loose); dbApiMock .Setup(ca => ca.RequestDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AdditionalDocumentProperty>())) .Returns( (string id, string revision, AdditionalDocumentProperty additionalProperties) => { requestedId = id; return Entity.CreateDocWithRevision().ToTask(); }); var session = new CouchSession(Default.Settings, MockCouchApi(dbApiMock)); session.Synchronously.Load<Entity>(Entity.StandardEntityId); Assert.Equal(Entity.StandardDocId, requestedId); }
public void ShouldReturnSameInstanceWhenRequestingTwice() { var session = new CouchSession(Default.Settings, MockCouchApi()); var entityA = session.Synchronously.Load<Entity>("doc1"); var entityB = session.Synchronously.Load<Entity>("doc1"); Assert.Same(entityA, entityB); }
public void ShouldCacheInstanceFromQuery() { var dbApiMock = new Mock<IDatabaseApi>(); dbApiMock .Setup(a => a.Query(It.IsAny<ViewQuery>())) .Returns( (ViewQuery q) => new ViewQueryResult(query: q, rows: new[] { new ViewResultRow( new object[] {"key1", 0}.ToJsonFragment(), null, Entity.StandardDocId, Entity.CreateDocWithRevision() ) }, totalCount: 1, offset: 0).ToTask<IViewQueryResult>() ); var session = new CouchSession(Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == dbApiMock.Object)); Entity queriedEntity = session.Synchronously.Query<Entity>(new ViewQuery {IncludeDocs = true}).First(); var loadedEntity = session.Synchronously.Load<Entity>(Entity.StandardEntityId); Assert.Same(queriedEntity, loadedEntity); }
public void ShouldStopReturnigDeletedEntityFromCache() { var entity = Entity.CreateStandardWithoutRevision(); ISession session = new CouchSession( Default.Settings, Mock.Of<ICouchApi>(c => c.Db("testdb") == new Mock<IDatabaseApi>(MockBehavior.Strict).Object)); session.Save(entity); session.Delete(entity); Assert.Null(session.Synchronously.Load<Entity>(entity.Id)); }