public async Task Can_Filter_On_Guid_Properties() { // arrange var context = _fixture.GetService <AppDbContext>(); var todoItem = _todoItemFaker.Generate(); context.TodoItems.Add(todoItem); await context.SaveChangesAsync(); var builder = new WebHostBuilder() .UseStartup <Startup>(); var httpMethod = new HttpMethod("GET"); var route = $"/api/v1/todo-items?filter[guid-property]={todoItem.GuidProperty}"; var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(httpMethod, route); // act var response = await client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); var deserializedBody = _fixture .GetService <IJsonApiDeSerializer>() .DeserializeList <TodoItem>(body); var todoItemResponse = deserializedBody.Single(); // assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(todoItem.Id, todoItemResponse.Id); Assert.Equal(todoItem.GuidProperty, todoItemResponse.GuidProperty); }
public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection() { // arrange var context = _fixture.GetService <AppDbContext>(); context.TodoItems.RemoveRange(context.TodoItems); await context.SaveChangesAsync(); var builder = new WebHostBuilder() .UseStartup <Startup>(); var httpMethod = new HttpMethod("GET"); var route = "/api/v1/todo-items"; var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(httpMethod, route); var expectedBody = JsonConvert.SerializeObject(new { data = new List <object>() }); // act var response = await client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); var deserializedBody = _fixture.GetService <IJsonApiDeSerializer>().DeserializeList <TodoItem>(body); // assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString()); Assert.Empty(deserializedBody); Assert.Equal(expectedBody, body); context.Dispose(); }
public async Task Request_UnsetRelationship_Returns_Null_DataObject() { // arrange var context = _fixture.GetService <AppDbContext>(); var todoItem = _todoItemFaker.Generate(); context.TodoItems.Add(todoItem); await context.SaveChangesAsync(); var builder = new WebHostBuilder() .UseStartup <Startup>(); var httpMethod = new HttpMethod("GET"); var route = $"/api/v1/todo-items/{todoItem.Id}/owner"; var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(httpMethod, route); var expectedBody = "{\"data\":null}"; // act var response = await client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); // assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString()); Assert.Equal(expectedBody, body); context.Dispose(); }
public async Task Can_Get_CamelCasedModels() { // Arrange var model = _faker.Generate(); _context.CamelCasedModels.Add(model); _context.SaveChanges(); var httpMethod = new HttpMethod("GET"); var route = "/camelCasedModels"; var builder = new WebHostBuilder() .UseStartup <Startup>(); var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(httpMethod, route); // Act var response = await client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); var deserializedBody = _fixture.GetService <IJsonApiDeSerializer>() .DeserializeList <CamelCasedModel>(body); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotEmpty(deserializedBody); Assert.True(deserializedBody.Count > 0); }
public async Task CustomRouteControllers_Uses_Dasherized_Item_Route() { // Arrange var context = _fixture.GetService <AppDbContext>(); var todoItem = _todoItemFaker.Generate(); var person = _personFaker.Generate(); todoItem.Owner = person; context.TodoItems.Add(todoItem); await context.SaveChangesAsync(); var builder = new WebHostBuilder() .UseStartup <Startup>(); var httpMethod = new HttpMethod("GET"); var route = $"/custom/route/todo-items/{todoItem.Id}"; var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(httpMethod, route); // act var response = await client.SendAsync(request); // assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public CamelCasedModelsControllerTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _jsonApiContext = fixture.GetService <IJsonApiContext>(); _faker = new Faker <CamelCasedModel>() .RuleFor(m => m.CompoundAttr, f => f.Lorem.Sentence()); }
public TodoItemControllerTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _jsonApiContext = fixture.GetService <IJsonApiContext>(); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()) .RuleFor(t => t.Ordinal, f => f.Random.Number()); }
public TodoItemControllerIntegrationTests(DocsFixture <TodoItem, Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()); }
public UserControllerIntegrationTests(DocsFixture <User, Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _userFaker = new Faker <User>() .RuleFor(u => u.FirstName, f => f.Name.FirstName()) .RuleFor(u => u.LastName, f => f.Name.LastName()); }
public Relationships(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()) .RuleFor(t => t.Ordinal, f => f.Random.Number()) .RuleFor(t => t.CreatedDate, f => f.Date.Past()); }
public async Task Can_Create_Guid_Identifiable_Entity() { // arrange var builder = new WebHostBuilder() .UseStartup <Startup>(); var httpMethod = new HttpMethod("POST"); var server = new TestServer(builder); var client = server.CreateClient(); var context = _fixture.GetService <AppDbContext>(); var owner = new JsonApiDotNetCoreExample.Models.Person(); context.People.Add(owner); await context.SaveChangesAsync(); var route = "/api/v1/todo-collections"; var request = new HttpRequestMessage(httpMethod, route); var content = new { data = new { type = "todo-collections", relationships = new { owner = new { data = new { type = "people", id = owner.Id.ToString() } } } } }; request.Content = new StringContent(JsonConvert.SerializeObject(content)); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json"); // act var response = await client.SendAsync(request); // assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); }
public FetchingDataTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _jsonApiContext = fixture.GetService <IJsonApiContext>(); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()) .RuleFor(t => t.Ordinal, f => f.Random.Number()) .RuleFor(t => t.CreatedDate, f => f.Date.Past()); _personFaker = new Faker <Person>() .RuleFor(p => p.FirstName, f => f.Name.FirstName()) .RuleFor(p => p.LastName, f => f.Name.LastName()); }
public UpdatingRelationshipsTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _personFaker = new Faker <Person>() .RuleFor(t => t.FirstName, f => f.Name.FirstName()) .RuleFor(t => t.LastName, f => f.Name.LastName()); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()) .RuleFor(t => t.Ordinal, f => f.Random.Number()); }
public PagingTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); _personFaker = new Faker <Person>() .RuleFor(p => p.FirstName, f => f.Name.FirstName()) .RuleFor(p => p.LastName, f => f.Name.LastName()); _todoItemFaker = new Faker <TodoItem>() .RuleFor(t => t.Description, f => f.Lorem.Sentence()) .RuleFor(t => t.Ordinal, f => f.Random.Number()); _todoItemCollectionFaker = new Faker <TodoItemCollection>() .RuleFor(t => t.Name, f => f.Company.CatchPhrase()); }
public async Task Total_Record_Count_Included() { // arrange var builder = new WebHostBuilder() .UseStartup <AuthorizedStartup>(); var server = new TestServer(builder); var client = server.CreateClient(); var context = (AppDbContext)server.Host.Services.GetService(typeof(AppDbContext)); var jsonApiContext = (IJsonApiContext)server.Host.Services.GetService(typeof(IJsonApiContext)); var person = new Person(); context.People.Add(person); var ownedTodoItem = new TodoItem(); var unOwnedTodoItem = new TodoItem(); ownedTodoItem.Owner = person; context.TodoItems.Add(ownedTodoItem); context.TodoItems.Add(unOwnedTodoItem); context.SaveChanges(); var authService = (IAuthorizationService)server.Host.Services.GetService(typeof(IAuthorizationService)); authService.CurrentUserId = person.Id; var httpMethod = new HttpMethod("GET"); var route = $"/api/v1/todo-items?include=owner"; var request = new HttpRequestMessage(httpMethod, route); // act var response = await client.SendAsync(request); var responseBody = await response.Content.ReadAsStringAsync(); var deserializedBody = _fixture.GetService <IJsonApiDeSerializer>().DeserializeList <TodoItem>(responseBody); // assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); foreach (var item in deserializedBody) { Assert.Equal(person.Id, item.OwnerId); } }
public async Task Can_Get_TodoItems() { // Arrange const int expectedEntitiesPerPage = 5; var person = new Person(); var todoItem = _todoItemFaker.Generate(); todoItem.Owner = person; _context.TodoItems.Add(todoItem); _context.SaveChanges(); var httpMethod = new HttpMethod("GET"); var route = "/api/v1/todo-items"; var description = new RequestProperties("Get TodoItems"); // Act var response = await _fixture.MakeRequest <TodoItem>(description, httpMethod, route); var body = await response.Content.ReadAsStringAsync(); var deserializedBody = _fixture.GetService <IJsonApiDeSerializer>().DeserializeList <TodoItem>(body); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotEmpty(deserializedBody); Assert.True(deserializedBody.Count <= expectedEntitiesPerPage); }
public Meta(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _context = fixture.GetService <AppDbContext>(); }
public SparseFieldSetTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _dbContext = fixture.GetService <AppDbContext>(); }
public FetchingDataTests(DocsFixture <Startup, JsonDocWriter> fixture) { _fixture = fixture; _jsonApiContext = fixture.GetService <IJsonApiContext>(); }