public async Task GetItemAsync_Basic() { await InitializeAsync(true); // Arrange var id = GetRandomId(); var expected = MovieServer.GetMovieById(id) !; // Act var response = await table !.GetItemAsync(id); // Assert Assert.NotNull(response); var movie = client.Serializer.Deserialize <ClientMovie>(response); Assert.Equal <IMovie>(expected, movie); }
public async Task ReplacementValidationTests(string propName, object propValue) { string id = GetRandomId(); var expected = MovieServer.GetMovieById(id) !; var entity = expected.ToDictionary(); entity[propName] = propValue; var response = await MovieServer.SendRequest(HttpMethod.Put, $"tables/movies/{id}", entity); var stored = MovieServer.GetMovieById(id) !; await AssertResponseWithLoggingAsync(HttpStatusCode.BadRequest, response); Assert.Equal <IMovie>(expected, stored); Assert.Equal <ITableData>(expected, stored); }
public async Task UndeleteItemAsync_SoftDelete() { // Arrange var id = GetRandomId(); var item = new ClientMovie { Id = id }; // Act await soft.UndeleteItemAsync(item).ConfigureAwait(false); var stored = MovieServer.GetMovieById(id) !; // Assert Assert.False(stored.Deleted); Assert.Equal <IMovie>(stored, item); AssertEx.SystemPropertiesMatch(stored, item); }
public async Task DeleteItemAsync_Basic() { await InitializeAsync(); // Arrange var id = GetRandomId(); var item = ClientMovie.From(MovieServer.GetMovieById(id)); // Act await table !.DeleteItemAsync(item); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); await table !.PushItemsAsync(); Assert.Equal(MovieCount - 1, MovieServer.GetMovieCount()); Assert.Null(MovieServer.GetMovieById(id)); }
public async Task AuthenticatedPatchTests( [CombinatorialValues(0, 1, 2, 3, 7, 14, 25)] int index, [CombinatorialValues(null, "failed", "success")] string userId, [CombinatorialValues("movies_rated", "movies_legal")] string table) { var id = Utils.GetMovieId(index); var original = MovieServer.GetMovieById(id) !; var expected = original.Clone(); expected.Title = "TEST MOVIE TITLE"; // Upper Cased because of the PreCommitHook expected.Rating = "PG-13"; var replacement = MovieServer.GetMovieById(id) !; replacement.Title = "Test Movie Title"; replacement.Rating = "PG-13"; Dictionary <string, string> headers = new(); Utils.AddAuthHeaders(headers, userId); var response = await MovieServer.SendRequest(HttpMethod.Put, $"tables/{table}/{id}", replacement, headers); var stored = MovieServer.GetMovieById(id); if (userId != "success") { var statusCode = table.Contains("legal") ? HttpStatusCode.UnavailableForLegalReasons : HttpStatusCode.Unauthorized; await AssertResponseWithLoggingAsync(statusCode, response); Assert.Equal <IMovie>(original, stored !); Assert.Equal <ITableData>(original, stored !); } else { await AssertResponseWithLoggingAsync(HttpStatusCode.OK, response); var result = response.DeserializeContent <ClientMovie>(); AssertEx.SystemPropertiesSet(stored, StartTime); AssertEx.SystemPropertiesChanged(expected, stored); AssertEx.SystemPropertiesMatch(stored, result); Assert.Equal <IMovie>(expected, result !); AssertEx.ResponseHasConditionalHeaders(stored, response); } }
public async Task DeleteItemAsync_GoneWhenDeleted() { // Arrange var id = GetRandomId(); var item = ClientMovie.From(MovieServer.GetMovieById(id)); await MovieServer.SoftDeleteMoviesAsync(x => x.Id == id).ConfigureAwait(false); // Act var exception = await Assert.ThrowsAsync <DatasyncInvalidOperationException>(() => soft.DeleteItemAsync(item)).ConfigureAwait(false); // Assert Assert.NotNull(exception.Request); Assert.NotNull(exception.Response); Assert.Equal(HttpStatusCode.Gone, exception.Response?.StatusCode); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); var entity = MovieServer.GetMovieById(id); Assert.True(entity.Deleted); }
public async Task CreateOverwriteSystemProperties(bool useUpdatedAt, bool useVersion) { var movieToAdd = GetSampleMovie <ClientMovie>(); movieToAdd.Id = Guid.NewGuid().ToString("N"); if (useUpdatedAt) { movieToAdd.UpdatedAt = DateTimeOffset.Parse("2018-12-31T01:01:01.000Z"); } if (useVersion) { movieToAdd.Version = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); } var response = await MovieServer.SendRequest <ClientMovie>(HttpMethod.Post, "tables/movies", movieToAdd); await AssertResponseWithLoggingAsync(HttpStatusCode.Created, response); var result = response.DeserializeContent <ClientMovie>(); Assert.NotNull(result); Assert.Equal(movieToAdd.Id, result !.Id); Assert.Equal(TestData.Movies.Count + 1, MovieServer.GetMovieCount()); var entity = MovieServer.GetMovieById(result.Id); Assert.NotNull(entity); AssertEx.SystemPropertiesSet(entity, StartTime); AssertEx.SystemPropertiesMatch(entity, result); if (useUpdatedAt) { Assert.NotEqual(movieToAdd.UpdatedAt, result.UpdatedAt); } if (useVersion) { Assert.NotEqual(movieToAdd.Version, result.Version); } Assert.Equal <IMovie>(movieToAdd, result); Assert.Equal <IMovie>(movieToAdd, entity !); AssertEx.ResponseHasConditionalHeaders(entity, response); AssertEx.HasHeader(response, "Location", $"https://localhost/tables/movies/{movieToAdd.Id}"); }
public async Task InsertItemAsync_Basic(bool hasId) { await InitializeAsync(true); var movieToAdd = GetSampleMovie <ClientMovie>(); if (hasId) { movieToAdd.Id = MyIdGenerator(table?.TableName); } var jsonDocument = CreateJsonDocument(movieToAdd); // Act var response = await table !.InsertItemAsync(jsonDocument); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); // Ensure that the ID is present and the same (if specified) var insertedId = response.Value <string>("id"); Assert.NotNull(insertedId); if (hasId) { Assert.Equal(movieToAdd.Id, insertedId); } await table !.PushItemsAsync(); Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); var stored = await table !.GetItemAsync(insertedId); // Ensure that there is a version var insertedVersion = stored.Value <string>("version"); Assert.NotNull(insertedVersion); // This is the entity that was actually inserted. var entity = MovieServer.GetMovieById(insertedId); Assert.Equal <IMovie>(movieToAdd, entity); AssertSystemPropertiesMatch(entity, stored); }
public async Task InsertItemAsync_OverwriteSystemProperties(bool useUpdatedAt, bool useVersion) { await InitializeAsync(true); var movieToAdd = GetSampleMovie <ClientMovie>(); movieToAdd.Id = MyIdGenerator(table?.TableName); if (useUpdatedAt) { movieToAdd.UpdatedAt = DateTimeOffset.Parse("2018-12-31T01:01:01.000Z"); } if (useVersion) { movieToAdd.Version = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); } var jsonDocument = CreateJsonDocument(movieToAdd); // Act await table !.InsertItemAsync(jsonDocument); await table !.PushItemsAsync(); var response = await table !.GetItemAsync(movieToAdd.Id); // Assert Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); var insertedId = response.Value <string>("id"); Assert.Equal(movieToAdd.Id, insertedId); var entity = MovieServer.GetMovieById(insertedId); Assert.Equal <IMovie>(movieToAdd, entity); AssertSystemPropertiesMatch(entity, response); if (useUpdatedAt) { Assert.NotEqual("2018-12-31T01:01:01.000Z", response.Value <string>("updatedAt")); } if (useVersion) { Assert.NotEqual(movieToAdd.Version, response.Value <string>("version")); } }
public async Task BasicReplaceTests([CombinatorialValues("movies", "movies_pagesize")] string table) { string id = GetRandomId(); var original = MovieServer.GetMovieById(id) !; var expected = original.Clone(); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; var response = await MovieServer.SendRequest(HttpMethod.Put, $"tables/{table}/{id}", expected); var stored = MovieServer.GetMovieById(id) !; await AssertResponseWithLoggingAsync(HttpStatusCode.OK, response); Assert.Equal <IMovie>(expected, stored); AssertEx.SystemPropertiesSet(stored, StartTime); AssertEx.SystemPropertiesChanged(original, stored); AssertEx.ResponseHasConditionalHeaders(stored, response); }
public async Task ReplaceSoftDeleted_ReturnsGone([CombinatorialValues("soft", "soft_logged")] string table) { var id = GetRandomId(); await MovieServer.SoftDeleteMoviesAsync(x => x.Id == id); var original = MovieServer.GetMovieById(id) !; var expected = MovieServer.GetMovieById(id) !; expected.Title = "Replacement Title"; expected.Rating = "PG-13"; var response = await MovieServer.SendRequest(HttpMethod.Put, $"tables/{table}/{id}", expected); var stored = MovieServer.GetMovieById(id); await AssertResponseWithLoggingAsync(HttpStatusCode.Gone, response); Assert.Equal <IMovie>(original, stored !); Assert.Equal <ITableData>(original, stored !); }
public async Task ReplaceItemAsync_ConditionalFailure() { await InitializeAsync(); // Arrange var id = GetRandomId(); var original = MovieServer.GetMovieById(id) !; var expected = ClientMovie.From(original); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; expected.Version = "dGVzdA=="; // Act await table !.ReplaceItemAsync(expected); var exception = await Assert.ThrowsAsync <PushFailedException>(() => table !.PushItemsAsync()); // Assert Assert.Single(exception.PushResult.Errors); }
public async Task UndeleteItemAsync_WhenDeleted() { // Arrange var id = GetRandomId(); var jsonDocument = CreateJsonDocument(new IdOnly { Id = id }); await MovieServer.SoftDeleteMoviesAsync(x => x.Id == id).ConfigureAwait(false); var original = MovieServer.GetMovieById(id); // Act var response = await soft.UndeleteItemAsync(jsonDocument).ConfigureAwait(false); var stored = MovieServer.GetMovieById(id) !; // Assert Assert.False(stored.Deleted); AssertJsonDocumentMatches(stored, response); }
public async Task ReplaceItemAsync_SoftNotDeleted() { // Arrange var id = GetRandomId(); var original = MovieServer.GetMovieById(id) !; var expected = ClientMovie.From(original); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; var json = CreateJsonDocument(expected); // Act var response = await soft.ReplaceItemAsync(json).ConfigureAwait(false); var stored = MovieServer.GetMovieById(id).Clone(); // Assert Assert.Equal <IMovie>(expected, stored); AssertEx.SystemPropertiesChanged(original, stored); AssertJsonDocumentMatches(stored, response); }
public async Task ReplaceItemAsync_Basic() { // Arrange var id = GetRandomId(); var original = MovieServer.GetMovieById(id) !; var expected = ClientMovie.From(original); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; var response = expected.Clone(); // Act await table.ReplaceItemAsync(response).ConfigureAwait(false); var stored = MovieServer.GetMovieById(id); // Assert Assert.Equal <IMovie>(expected, stored); AssertEx.SystemPropertiesChanged(original, stored); AssertEx.SystemPropertiesMatch(stored, response); }
public async Task ConditionalReadTests(string headerName, int offset, HttpStatusCode expectedStatusCode) { var id = GetRandomId(); var expected = MovieServer.GetMovieById(id) !; Dictionary <string, string> headers = new() { { headerName, expected.UpdatedAt.AddHours(offset).ToString("R") } }; var response = await MovieServer.SendRequest(HttpMethod.Get, $"tables/movies/{id}", headers); await AssertResponseWithLoggingAsync(expectedStatusCode, response); if (expectedStatusCode == HttpStatusCode.OK || expectedStatusCode == HttpStatusCode.PreconditionFailed) { var actual = response.DeserializeContent <ClientMovie>(); Assert.Equal <IMovie>(expected, actual !); AssertEx.SystemPropertiesMatch(expected, actual); AssertEx.ResponseHasConditionalHeaders(expected, response); } }
public async Task DeleteItemAsync_Basic() { await InitializeAsync(true); // Arrange var idToDelete = GetRandomId(); // Act var jsonDocument = await table !.GetItemAsync(idToDelete); Assert.NotNull(jsonDocument); await table !.DeleteItemAsync(jsonDocument); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); Assert.NotNull(MovieServer.GetMovieById(idToDelete)); await table !.PushItemsAsync(); Assert.Equal(MovieCount - 1, MovieServer.GetMovieCount()); Assert.Null(MovieServer.GetMovieById(idToDelete)); }
public async Task ToAsyncEnumerable_RetrievesItems() { // Arrange int count = 0; var result = table.ToAsyncEnumerable(); var enumerator = result.GetAsyncEnumerator(); while (await enumerator.MoveNextAsync().ConfigureAwait(false)) { count++; var item = enumerator.Current; Assert.NotNull(item); Assert.NotNull(item.Id); var expected = MovieServer.GetMovieById(item.Id); Assert.Equal <IMovie>(expected, item); } Assert.Equal(MovieCount, count); }
public async Task ConditionalPatchTests(string headerName, int offset, HttpStatusCode expectedStatusCode) { string id = GetRandomId(); var entity = MovieServer.GetMovieById(id) !; Dictionary <string, string> headers = new() { { headerName, entity.UpdatedAt.AddHours(offset).ToString("R") } }; var expected = MovieServer.GetMovieById(id) !; var replacement = expected.Clone(); replacement.Title = "Test Movie Title"; replacement.Rating = "PG-13"; var response = await MovieServer.SendRequest(HttpMethod.Put, $"tables/movies/{id}", replacement, headers); var stored = MovieServer.GetMovieById(id); await AssertResponseWithLoggingAsync(expectedStatusCode, response); var actual = response.DeserializeContent <ClientMovie>(); switch (expectedStatusCode) { case HttpStatusCode.OK: AssertEx.SystemPropertiesSet(stored, StartTime); AssertEx.SystemPropertiesChanged(expected, stored); AssertEx.SystemPropertiesMatch(stored, actual); Assert.Equal <IMovie>(replacement, actual !); AssertEx.ResponseHasConditionalHeaders(stored, response); break; case HttpStatusCode.PreconditionFailed: Assert.Equal <IMovie>(expected, actual !); AssertEx.SystemPropertiesMatch(expected, actual); AssertEx.ResponseHasConditionalHeaders(expected, response); break; } }
public async Task ReplaceItemAsync_SoftDeleted_ReturnsGone() { // Arrange var id = GetRandomId(); await MovieServer.SoftDeleteMoviesAsync(x => x.Id == id).ConfigureAwait(false); var original = MovieServer.GetMovieById(id) !; var expected = ClientMovie.From(original); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; // Act var exception = await Assert.ThrowsAsync <DatasyncInvalidOperationException>(() => soft.ReplaceItemAsync(expected)).ConfigureAwait(false); var stored = MovieServer.GetMovieById(id).Clone(); // Assert Assert.Equal(HttpStatusCode.Gone, exception.Response?.StatusCode); Assert.Equal <IMovie>(original, stored); Assert.Equal <ITableData>(original, stored); }
public async Task CreateItemAsync_OverwriteSystemProperties(bool useUpdatedAt, bool useVersion) { await InitializeAsync(); var movieToAdd = GetSampleMovie <ClientMovie>(); movieToAdd.Id = Guid.NewGuid().ToString("N"); if (useUpdatedAt) { movieToAdd.UpdatedAt = DateTimeOffset.Parse("2018-12-31T01:01:01.000Z"); } if (useVersion) { movieToAdd.Version = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); } // Act await table !.InsertItemAsync(movieToAdd); await table !.PushItemsAsync(); var result = await table !.GetItemAsync(movieToAdd.Id); // Assert Assert.Equal(movieToAdd.Id, result.Id); Assert.Equal <IMovie>(movieToAdd, result); Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); var entity = MovieServer.GetMovieById(result.Id) !; if (useUpdatedAt) { Assert.NotEqual(movieToAdd.UpdatedAt, result.UpdatedAt); } if (useVersion) { Assert.NotEqual(movieToAdd.Version, result.Version); } Assert.Equal <IMovie>(movieToAdd, entity); }
public async Task InsertItemAsync_OverwriteSystemProperties(bool useUpdatedAt, bool useVersion) { var movieToAdd = GetSampleMovie <ClientMovie>(); movieToAdd.Id = Guid.NewGuid().ToString("N"); if (useUpdatedAt) { movieToAdd.UpdatedAt = DateTimeOffset.Parse("2018-12-31T01:01:01.000Z"); } if (useVersion) { movieToAdd.Version = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); } var jsonDocument = CreateJsonDocument(movieToAdd); // Act var response = await table.InsertItemAsync(jsonDocument).ConfigureAwait(false); // Assert Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); var insertedId = response.Value <string>("id"); Assert.Equal(movieToAdd.Id, insertedId); var entity = MovieServer.GetMovieById(insertedId); Assert.Equal <IMovie>(movieToAdd, entity); AssertJsonDocumentMatches(entity, response); if (useUpdatedAt) { Assert.NotEqual("2018-12-31T01:01:01.000Z", response.Value <string>("updatedAt")); } if (useVersion) { Assert.NotEqual(movieToAdd.Version, response.Value <string>("version")); } }
public async Task DeleteItemAsync_GoneWhenDeleted() { // Arrange var idToDelete = GetRandomId(); var jsonDocument = CreateJsonDocument(new IdOnly { Id = idToDelete }); await MovieServer.SoftDeleteMoviesAsync(x => x.Id == idToDelete).ConfigureAwait(false); var original = MovieServer.GetMovieById(idToDelete); // Act var exception = await Assert.ThrowsAsync <DatasyncInvalidOperationException>(() => soft.DeleteItemAsync(jsonDocument)).ConfigureAwait(false); // Assert Assert.NotNull(exception.Request); Assert.NotNull(exception.Response); Assert.Equal(HttpStatusCode.Gone, exception.Response?.StatusCode); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); var entity = MovieServer.GetMovieById(idToDelete); Assert.True(entity.Deleted); }
public async Task InsertItemAsync_Basic(bool hasId) { var movieToAdd = GetSampleMovie <ClientMovie>(); if (hasId) { movieToAdd.Id = Guid.NewGuid().ToString("N"); } var jsonDocument = CreateJsonDocument(movieToAdd); // Act var response = await table.InsertItemAsync(jsonDocument).ConfigureAwait(false); // Assert Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); // Ensure that the ID is present and the same (if specified) var insertedId = response.Value <string>("id"); Assert.NotNull(insertedId); if (hasId) { Assert.Equal(movieToAdd.Id, insertedId); } // Ensure that there is a version var insertedVersion = response.Value <string>("version"); Assert.NotNull(insertedVersion); // This is the entity that was actually inserted. var entity = MovieServer.GetMovieById(insertedId); Assert.Equal <IMovie>(movieToAdd, entity); AssertJsonDocumentMatches(entity, response); }
public async Task ReplaceItemAsync_Basic() { await InitializeAsync(); // Arrange var id = GetRandomId(); var original = MovieServer.GetMovieById(id) !; var expected = ClientMovie.From(original); expected.Title = "Replacement Title"; expected.Rating = "PG-13"; // Act await table !.ReplaceItemAsync(expected); await table !.PushItemsAsync(); var response = await table !.GetItemAsync(id); var stored = MovieServer.GetMovieById(id); // Assert Assert.Equal <IMovie>(expected, stored); AssertEx.SystemPropertiesChanged(original, stored); AssertEx.SystemPropertiesMatch(stored, response); }
public async Task CreateItemAsync_Basic() { await InitializeAsync(); var movieToAdd = GetSampleMovie <ClientMovie>(); movieToAdd.Id = Guid.NewGuid().ToString("N"); // Act await table !.InsertItemAsync(movieToAdd); await table !.PushItemsAsync(); var result = await table !.GetItemAsync(movieToAdd.Id); // Assert Assert.True(Guid.TryParse(result.Id, out _)); Assert.Equal(movieToAdd.Id, result.Id); Assert.Equal <IMovie>(movieToAdd, result); Assert.Equal(MovieCount + 1, MovieServer.GetMovieCount()); var entity = MovieServer.GetMovieById(result.Id) !; Assert.Equal <IMovie>(movieToAdd, entity); AssertEx.SystemPropertiesMatch(entity, result); }
public async Task DeleteItemAsync_SoftDelete() { await InitializeAsync(true); // Arrange var idToDelete = GetRandomId(); // Act var jsonDocument = await soft !.GetItemAsync(idToDelete); Assert.NotNull(jsonDocument); await soft !.DeleteItemAsync(jsonDocument); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); Assert.NotNull(MovieServer.GetMovieById(idToDelete)); await soft !.PushItemsAsync(); Assert.Equal(MovieCount, MovieServer.GetMovieCount()); var item = MovieServer.GetMovieById(idToDelete); Assert.True(item.Deleted); }