Exemplo n.º 1
0
        public async Task GET_Included_Contains_SideloadedData_ForManyToOne()
        {
            // arrange
            var person   = _personFaker.Generate();
            var todoItem = _todoItemFaker.Generate();

            todoItem.Owner = person;
            _context.TodoItems.RemoveRange(_context.TodoItems);
            _context.TodoItems.Add(todoItem);
            _context.SaveChanges();

            var builder = new WebHostBuilder().UseStartup <Startup>();

            var httpMethod = new HttpMethod("GET");
            var route      = $"/api/v1/todo-items?include=owner";

            var server  = new TestServer(builder);
            var client  = server.CreateClient();
            var request = new HttpRequestMessage(httpMethod, route);

            // act
            var response = await client.SendAsync(request);

            // assert
            var json = await response.Content.ReadAsStringAsync();

            var documents = JsonConvert.DeserializeObject <Documents>(json);
            // we only care about counting the todo-items that have owners
            var expectedCount = documents.Data.Count(d => d.Relationships["owner"].SingleData != null);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.NotEmpty(documents.Included);
            Assert.Equal(expectedCount, documents.Included.Count);
        }
        public async Task Can_Update_ToMany_Relationship_ThroughLink()
        {
            // arrange
            var person = _personFaker.Generate();

            _context.People.Add(person);

            var todoItem = _todoItemFaker.Generate();

            _context.TodoItems.Add(todoItem);

            _context.SaveChanges();

            var builder = new WebHostBuilder()
                          .UseStartup <Startup>();

            var server = new TestServer(builder);
            var client = server.CreateClient();

            var content = new
            {
                data = new List <object>
                {
                    new {
                        type = "todo-items",
                        id   = $"{todoItem.Id}"
                    }
                }
            };

            var httpMethod = new HttpMethod("PATCH");
            var route      = $"/api/v1/people/{person.Id}/relationships/todo-items";
            var request    = new HttpRequestMessage(httpMethod, route);

            request.Content = new StringContent(JsonConvert.SerializeObject(content));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

            // Act
            var response = await client.SendAsync(request);

            _context = _fixture.GetService <AppDbContext>();
            var personsTodoItems = _context.People.Include(p => p.TodoItems).Single(p => p.Id == person.Id).TodoItems;

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.NotEmpty(personsTodoItems);
        }
        private Ticket[] GetTicketsSeed()
        {
            var owners = new List <string>();

            for (var i = 0; i < 20; i++)
            {
                var faker2 = new Faker("en");
                var owner  = faker2.Person.FullName;//.Generate();
                owners.Add(owner);
            }

            var faker = new Bogus.Faker <Ticket>()
                        .RuleFor(t => t.TicketType, (f, u) => f.PickRandom("new", "open", "refused", "closed"))
                        .RuleFor(t => t.Title, (f, u) => f.Lorem.Sentence())
                        .RuleFor(t => t.Details, (f, u) => f.Lorem.Paragraph())
                        .RuleFor(t => t.IsHtml, (f, u) => false)
                        .RuleFor(t => t.TagList, (f, u) => string.Join(",", f.Commerce.Categories(3)))
                        .RuleFor(t => t.CreatedDate, (f, u) => f.Date.Recent(100))
                        .RuleFor(t => t.Owner, (f, u) => f.PickRandom(owners))
                        .RuleFor(t => t.AssignedTo, (f, u) => f.Person.FullName)
                        .RuleFor(t => t.TicketStatus, (f, u) => f.PickRandom(1, 2, 3))
                        .RuleFor(t => t.LastUpdateBy, (f, u) => f.Person.FullName)
                        .RuleFor(t => t.LastUpdateDate, (f, u) => f.Date.Soon(5))
                        .RuleFor(t => t.Priority, (f, u) => f.PickRandom("low", "medium", "high", "critical"))
                        .RuleFor(t => t.AffectedCustomer, (f, u) => f.PickRandom(true, false))
                        .RuleFor(t => t.Version, (f, u) => f.PickRandom("1.0.0", "1.1.0", "2.0.0"))
                        .RuleFor(t => t.ProjectId, (f, u) => f.Random.Number(100))
                        .RuleFor(t => t.DueDate, (f, u) => f.Date.Soon(5))
                        .RuleFor(t => t.EstimatedDuration, (f, u) => f.Random.Number(20))
                        .RuleFor(t => t.ActualDuration, (f, u) => f.Random.Number(20))
                        .RuleFor(t => t.TargetDate, (f, u) => f.Date.Soon(5))
                        .RuleFor(t => t.ResolutionDate, (f, u) => f.Date.Soon(5))
                        .RuleFor(t => t.Type, (f, u) => f.PickRandom(1, 2, 3))
                        .RuleFor(t => t.ParentId, () => 0)
                        .RuleFor(t => t.PreferredLanguage, (f, u) => f.PickRandom("fr", "en", "es"))
            ;

            var fakeModels = new List <Ticket>();

            for (var i = 0; i < 500; i++)
            {
                var t = faker.Generate();
                t.Id = i + 1;
                fakeModels.Add(t);
            }

            return(fakeModels.ToArray());
        }
        private Task[] GetTaskSeed()
        {
            var faker = new Bogus.Faker <Task>()
                        .RuleFor(t => t.Name, (f, u) => f.Lorem.Sentence())
                        .RuleFor(t => t.OrderNumber, (f, u) => f.Random.Number(10000000))
                        .RuleFor(t => t.AssignedLogId, (f, u) => f.Random.Number(10000000))
                        .RuleFor(t => t.IsAssigned, (f, u) => f.PickRandom(0, 1))
                        .RuleFor(t => t.OTSTaskTypeId, (f, u) => f.PickRandom(1, 2, 3, 4))
                        .RuleFor(t => t.Comment, (f) => f.Lorem.Paragraph())
                        .RuleFor(t => t.LogComplete, f => f.Date.Soon(5))
                        .RuleFor(t => t.IsLogComplete, f => f.PickRandom(true, false))
                        .RuleFor(t => t.ActualComplete, f => f.PickRandom(true, false))
                        .RuleFor(t => t.PlannedStartDate, f => f.Date.Soon(5))
                        .RuleFor(t => t.PlannedCompleteStartDate, f => f.Date.Soon(5))
                        .RuleFor(t => t.IsComplete, f => f.PickRandom(true, false))
                        .RuleFor(t => t.Unplanned, f => f.PickRandom(true, false))
                        .RuleFor(t => t.UnplannedCodeId, f => f.Random.Number(10000000))
                        .RuleFor(t => t.UnplannedComments, f => f.Lorem.Paragraph())
                        .RuleFor(t => t.IsImport, f => f.PickRandom(true, false))
                        .RuleFor(t => t.IsArchived, f => f.PickRandom(true, false))
                        .RuleFor(t => t.OTSProjectId, f => f.Random.Number(10000000))
                        .RuleFor(t => t.BarrierTypeId, f => f.Random.Number(10000000))
                        .RuleFor(t => t.IsBarrier, f => f.PickRandom(true, false))
                        .RuleFor(t => t.IsActive, f => f.PickRandom(true, false))
                        .RuleFor(t => t.IsDeleted, f => f.PickRandom(true, false))
                        .RuleFor(t => t.CreatedBy, f => f.Person.FullName)
                        .RuleFor(t => t.CreatedDate, f => f.Date.Soon(5))
                        .RuleFor(t => t.LastModifiedDate, f => f.Date.Soon(5))
                        .RuleFor(t => t.LastModifiedBy, f => f.Person.FullName)
            ;

            var fakeModels = new List <Task>();

            for (var i = 0; i < 500; i++)
            {
                var t = faker.Generate();
                t.Id = i + 1;
                fakeModels.Add(t);
            }

            return(fakeModels.ToArray());
        }
        public async Task Can_Update_ToMany_Relationship_By_Patching_Resource()
        {
            // arrange
            var todoCollection = new TodoItemCollection();

            todoCollection.TodoItems = new List <TodoItem>();
            var person   = _personFaker.Generate();
            var todoItem = _todoItemFaker.Generate();

            todoCollection.Owner = person;
            todoCollection.TodoItems.Add(todoItem);
            _context.TodoItemCollections.Add(todoCollection);
            _context.SaveChanges();

            var newTodoItem1 = _todoItemFaker.Generate();
            var newTodoItem2 = _todoItemFaker.Generate();

            _context.AddRange(new TodoItem[] { newTodoItem1, newTodoItem2 });
            _context.SaveChanges();

            var builder = new WebHostBuilder()
                          .UseStartup <Startup>();

            var server = new TestServer(builder);
            var client = server.CreateClient();

            var content = new
            {
                data = new
                {
                    type          = "todo-collections",
                    id            = todoCollection.Id,
                    relationships = new Dictionary <string, object>
                    {
                        { "todo-items", new
                          {
                              data = new object[]
                              {
                                  new { type = "todo-items", id = $"{newTodoItem1.Id}" },
                                  new { type = "todo-items", id = $"{newTodoItem2.Id}" }
                              }
                          } },
                    }
                }
            };

            var httpMethod = new HttpMethod("PATCH");
            var route      = $"/api/v1/todo-collections/{todoCollection.Id}";
            var request    = new HttpRequestMessage(httpMethod, route);

            string serializedContent = JsonConvert.SerializeObject(content);

            request.Content = new StringContent(serializedContent);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

            // Act
            var response = await client.SendAsync(request);

            _context = _fixture.GetService <AppDbContext>();
            var updatedTodoItems = _context.TodoItemCollections.AsNoTracking()
                                   .Where(tic => tic.Id == todoCollection.Id)
                                   .Include(tdc => tdc.TodoItems).SingleOrDefault().TodoItems;


            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            /// we are expecting two, not three, because the request does
            /// a "complete replace".
            Assert.Equal(2, updatedTodoItems.Count);
        }