public async Task UpsertDocument_UpdateDocument_IsUpdatedCorrectly() { // Arrange var db = new DocumentDb(location, key, database); var documentId = Guid.NewGuid().ToString(); var person = new PersonTest { Id = Guid.Parse(documentId), PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personEnvelope = new DocumentBase <PersonTest> { VM = person }; await db.UpsertDocument(CollectionId, documentId, personEnvelope); personEnvelope = await db.ReadDocument <PersonTest>(CollectionId, documentId); personEnvelope.VM.Name = "Fred Flintstone"; personEnvelope.VM.Age = 88; await db.UpsertDocument(CollectionId, documentId, personEnvelope); // Assert var doc = await db.ReadDocument <PersonTest>(CollectionId, documentId); doc.VM.Age.Should().Be(88); doc.VM.Name.Should().Be("Fred Flintstone"); doc.VM.PersonId.Should().Be(person.PersonId); }
private async Task <DocumentDb> SetupMultiplePersons() { var db = new DocumentDb(location, key, database); var personA = new PersonTest { PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87, Id = new Guid("C9FCD0EB-E5F3-439D-BA4D-093E692046C0") }; var personAEnvelope = new DocumentBase <PersonTest> { VM = personA, ETag = Guid.NewGuid().ToString() }; var personB = new PersonTest { PersonId = Guid.NewGuid(), Name = "Wilma Flintstone", Age = 88, Id = Guid.NewGuid() }; var personBEnvelope = new DocumentBase <PersonTest> { VM = personB, ETag = Guid.NewGuid().ToString() }; var personC = new PersonTest { PersonId = Guid.NewGuid(), Name = "Wilma Flintstone", Age = 66, Id = Guid.Empty }; var personCEnvelope = new DocumentBase <PersonTest> { VM = personC, ETag = Guid.NewGuid().ToString() }; var personD = new PersonTest { PersonId = Guid.NewGuid(), Name = "Wilma Flintstone", Age = 66, Id = Guid.NewGuid() }; var personDEnvelope = new DocumentBase <PersonTest> { VM = personD, ETag = Guid.NewGuid().ToString() }; await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personAEnvelope); await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personBEnvelope); await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personCEnvelope); await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personDEnvelope); return(db); }
public async Task ReadDocument_DocumentExists_ReturnDocumentIsCorrect() { // Arrange var db = new DocumentDb(location, key, database); var documentId = Guid.NewGuid().ToString(); var person = new PersonTest { PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personEnvelope = new DocumentBase <PersonTest> { VM = person }; await db.UpsertDocument(CollectionId, documentId, personEnvelope); // Act var doc = await db.ReadDocument <PersonTest>(CollectionId, documentId); // Assert doc.VM.Age.Should().Be(person.Age); doc.VM.Name.Should().Be(person.Name); doc.VM.PersonId.Should().Be(person.PersonId); doc.ETag.Should().NotBe(string.Empty); }
public async Task UpsertDocument_NewDocument_IsStoredCorrectly() { // Arrange var db = new DocumentDb(location, key, database); var id = Guid.NewGuid(); var person = new PersonTest() { PersonId = id, Name = "Barney Rubble", Age = 87 }; var personEnvelope = new DocumentBase <PersonTest> { VM = person, ETag = Guid.NewGuid().ToString() }; // Act await db.UpsertDocument(CollectionId, id.ToString(), personEnvelope); // Assert var doc = await db.ReadDocument <PersonTest>(CollectionId, id.ToString()); doc.VM.Age.Should().Be(87); doc.VM.Name.Should().Be("Barney Rubble"); doc.VM.PersonId.Should().Be(person.PersonId); }
public async Task UpsertDocument_TwoNewDocumentsSameIdDifferentEtag_OnlyOneIsStored() { // Arrange var db = new DocumentDb(location, key, database); var id = Guid.NewGuid(); var personA = new PersonTest() { Id = id, PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personB = new PersonTest() { Id = id, PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personAEnvelope = new DocumentBase <PersonTest> { VM = personA }; var personBEnvelope = new DocumentBase <PersonTest> { VM = personB }; // Act var taskForPersonA = db.UpsertDocument(CollectionId, id.ToString(), personAEnvelope); var taskForPersonB = db.UpsertDocument(CollectionId, id.ToString(), personBEnvelope); // Assert //We don't care about exception now we just waiting for tasks to finish await Record.ExceptionAsync(() => Task.WhenAll(taskForPersonA, taskForPersonB)); var persons = await db.Query <PersonTest>(CollectionId, new Dictionary <string, object>() { { "Id", personA.Id } }); persons.Count().Should().Be(1); }
public async Task UpsertDocument_UpdateDocumentConncurrently_ThrowsException() { // Arrange var db = new DocumentDb(location, key, database); var documentId = Guid.NewGuid().ToString(); var person = new PersonTest { PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87, }; var personEnvelope = new DocumentBase <PersonTest> { VM = person }; await db.UpsertDocument(CollectionId, documentId, personEnvelope); personEnvelope = await db.ReadDocument <PersonTest>(CollectionId, documentId); var newChange = new DocumentBase <PersonTest> { VM = person }; newChange.VM.Name = "Change"; newChange.ETag = personEnvelope.ETag; personEnvelope.VM.Name = "Fred Flintstone"; personEnvelope.VM.Age = 88; await db.UpsertDocument(CollectionId, documentId, personEnvelope); var exception = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, newChange)); // Assert exception.Should().NotBeNull(); exception.Should().BeOfType <ConcurrencyException>(); exception.Message.Should().Contain("One of the specified pre-condition is not met"); }
public async Task UpsertDocument_NullItem_ThrowsException() { // Arrange var db = new DocumentDb(location, key, database); var documentId = Guid.NewGuid().ToString(); DocumentBase <PersonTest> personEnvelope = null; // Act var ex = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, personEnvelope)); // Assert ex.Should().NotBeNull(); ex.Should().BeOfType <ArgumentNullException>(); }
public async Task UpsertDocument_TwoNewDocuments_OneThrowsConcurrencyException() { // Arrange var db = new DocumentDb(location, key, database); var id = Guid.NewGuid(); var personA = new PersonTest() { Id = id, PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personB = new PersonTest() { Id = id, PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personAEnvelope = new DocumentBase <PersonTest> { VM = personA, ETag = id.ToString() }; var personBEnvelope = new DocumentBase <PersonTest> { VM = personB, ETag = id.ToString() }; // Act var taskForPersonA = db.UpsertDocument(CollectionId, id.ToString(), personAEnvelope); var taskForPersonB = db.UpsertDocument(CollectionId, id.ToString(), personBEnvelope); // Assert var exception = await Record.ExceptionAsync(() => Task.WhenAll(taskForPersonA, taskForPersonB)); exception.Should().NotBeNull(); exception.Should().BeOfType <ConcurrencyException>(); }
public async Task UpsertDocument_NullDocumentId_ThrowsException(string documentId) { // Arrange var db = new DocumentDb(location, key, database); var person = new PersonTest { PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personEnvelope = new DocumentBase <PersonTest> { VM = person, ETag = Guid.NewGuid().ToString() }; // Act var ex = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, personEnvelope)); //Assert ex.Should().NotBeNull(); ex.Should().BeOfType <ArgumentNullException>(); }
public async Task IsDocumentInCollection_DocumentDoesExist_ReturnsTrue() { // Arrange var db = new DocumentDb(location, key, database); var documentId = Guid.NewGuid().ToString(); var person = new PersonTest { PersonId = Guid.NewGuid(), Name = "Barney Rubble", Age = 87 }; var personEnvelope = new DocumentBase <PersonTest> { VM = person }; await db.UpsertDocument(CollectionId, documentId, personEnvelope); // Act var exists = db.DocumentExists(CollectionId, documentId); // Assert exists.Should().BeTrue(); }