Skip to content

Commit d0406ac

Browse files
committed
Adding tests for students
1 parent 81d04c4 commit d0406ac

File tree

9 files changed

+346
-13
lines changed

9 files changed

+346
-13
lines changed

ContosoUniversity.IntegrationTests/Features/Instructors/CreateEditTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ public async Task Should_merge_course_instructors()
126126
Department = englishDept,
127127
Title = "English 101",
128128
Credits = 4,
129-
Id = 123
129+
Id = NextCourseNumber()
130130
};
131131
var english201 = new Course
132132
{
133133
Department = englishDept,
134134
Title = "English 201",
135135
Credits = 4,
136-
Id = 456
136+
Id = NextCourseNumber()
137137
};
138138
await InsertAsync(englishDept, english101, english201);
139139

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
using static ContosoUniversity.IntegrationTests.SliceFixture;
5+
using ContosoUniversity.Features.Students;
6+
using ContosoUniversity.Models;
7+
using Shouldly;
8+
9+
namespace ContosoUniversity.IntegrationTests.Features.Students
10+
{
11+
public class CreateTests
12+
{
13+
[Fact]
14+
public async Task Should_create_student()
15+
{
16+
var cmd = new Create.Command
17+
{
18+
FirstMidName = "Joe",
19+
LastName = "Schmoe",
20+
EnrollmentDate = DateTime.Today
21+
};
22+
23+
var studentId = await SendAsync(cmd);
24+
25+
var student = await FindAsync<Student>(studentId);
26+
27+
student.ShouldNotBeNull();
28+
student.FirstMidName.ShouldBe(cmd.FirstMidName);
29+
student.LastName.ShouldBe(cmd.LastName);
30+
student.EnrollmentDate.ShouldBe(cmd.EnrollmentDate.GetValueOrDefault());
31+
}
32+
}
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
using static ContosoUniversity.IntegrationTests.SliceFixture;
5+
using ContosoUniversity.Features.Students;
6+
using ContosoUniversity.Models;
7+
using Shouldly;
8+
9+
namespace ContosoUniversity.IntegrationTests.Features.Students
10+
{
11+
public class DeleteTests
12+
{
13+
[Fact]
14+
public async Task Should_get_delete_details()
15+
{
16+
var cmd = new Create.Command
17+
{
18+
FirstMidName = "Joe",
19+
LastName = "Schmoe",
20+
EnrollmentDate = DateTime.Today
21+
};
22+
23+
var studentId = await SendAsync(cmd);
24+
25+
var query = new Delete.Query
26+
{
27+
Id = studentId
28+
};
29+
30+
var result = await SendAsync(query);
31+
32+
result.FirstMidName.ShouldBe(cmd.FirstMidName);
33+
result.LastName.ShouldBe(cmd.LastName);
34+
result.EnrollmentDate.ShouldBe(cmd.EnrollmentDate.GetValueOrDefault());
35+
}
36+
37+
[Fact]
38+
public async Task Should_delete_student()
39+
{
40+
var createCommand = new Create.Command
41+
{
42+
FirstMidName = "Joe",
43+
LastName = "Schmoe",
44+
EnrollmentDate = DateTime.Today
45+
};
46+
47+
var studentId = await SendAsync(createCommand);
48+
49+
var deleteCommand = new Delete.Command
50+
{
51+
ID = studentId
52+
};
53+
54+
await SendAsync(deleteCommand);
55+
56+
var student = await FindAsync<Student>(studentId);
57+
58+
student.ShouldBeNull();
59+
}
60+
}
61+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using ContosoUniversity.Features.Students;
5+
using ContosoUniversity.Models;
6+
using Shouldly;
7+
using Xunit;
8+
using static ContosoUniversity.IntegrationTests.SliceFixture;
9+
10+
namespace ContosoUniversity.IntegrationTests.Features.Students
11+
{
12+
public class DetailsTests
13+
{
14+
[Fact]
15+
public async Task Should_get_details()
16+
{
17+
var adminId = await SendAsync(new ContosoUniversity.Features.Instructors.CreateEdit.Command
18+
{
19+
FirstMidName = "George",
20+
LastName = "Costanza",
21+
HireDate = DateTime.Today,
22+
});
23+
24+
var englishDept = new Department
25+
{
26+
InstructorID = adminId,
27+
Budget = 123m,
28+
Name = "English 101",
29+
StartDate = DateTime.Today,
30+
};
31+
await InsertAsync(englishDept);
32+
var deptId = englishDept.Id;
33+
34+
var course1 = new Course
35+
{
36+
DepartmentID = deptId,
37+
Credits = 10,
38+
Id = NextCourseNumber(),
39+
Title = "Course 1"
40+
};
41+
var course2 = new Course
42+
{
43+
DepartmentID = deptId,
44+
Credits = 10,
45+
Id = NextCourseNumber(),
46+
Title = "Course 2"
47+
};
48+
await InsertAsync(course1, course2);
49+
50+
var command = new Create.Command
51+
{
52+
FirstMidName = "Joe",
53+
LastName = "Schmoe",
54+
EnrollmentDate = new DateTime(2013, 1, 1)
55+
};
56+
var studentId = await SendAsync(command);
57+
58+
var enrollment1 = new Enrollment
59+
{
60+
CourseID = course1.Id,
61+
Grade = Grade.A,
62+
StudentID = studentId,
63+
};
64+
var enrollment2 = new Enrollment
65+
{
66+
CourseID = course2.Id,
67+
Grade = Grade.F,
68+
StudentID = studentId,
69+
};
70+
await InsertAsync(enrollment1, enrollment2);
71+
72+
var details = await SendAsync(new Details.Query {Id = studentId});
73+
74+
details.ShouldNotBeNull();
75+
details.FirstMidName.ShouldBe(command.FirstMidName);
76+
details.LastName.ShouldBe(command.LastName);
77+
details.EnrollmentDate.ShouldBe(command.EnrollmentDate.GetValueOrDefault());
78+
details.Enrollments.Count.ShouldBe(2);
79+
}
80+
}
81+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
using static ContosoUniversity.IntegrationTests.SliceFixture;
5+
using ContosoUniversity.Features.Students;
6+
using ContosoUniversity.Models;
7+
using Shouldly;
8+
9+
namespace ContosoUniversity.IntegrationTests.Features.Students
10+
{
11+
public class EditTests
12+
{
13+
[Fact]
14+
public async Task Should_get_edit_details()
15+
{
16+
var cmd = new Create.Command
17+
{
18+
FirstMidName = "Joe",
19+
LastName = "Schmoe",
20+
EnrollmentDate = DateTime.Today
21+
};
22+
23+
var studentId = await SendAsync(cmd);
24+
25+
var query = new Edit.Query
26+
{
27+
Id = studentId
28+
};
29+
30+
var result = await SendAsync(query);
31+
32+
result.FirstMidName.ShouldBe(cmd.FirstMidName);
33+
result.LastName.ShouldBe(cmd.LastName);
34+
result.EnrollmentDate.ShouldBe(cmd.EnrollmentDate);
35+
}
36+
37+
[Fact]
38+
public async Task Should_edit_student()
39+
{
40+
var createCommand = new Create.Command
41+
{
42+
FirstMidName = "Joe",
43+
LastName = "Schmoe",
44+
EnrollmentDate = DateTime.Today
45+
};
46+
47+
var studentId = await SendAsync(createCommand);
48+
49+
var editCommand = new Edit.Command
50+
{
51+
ID = studentId,
52+
FirstMidName = "Mary",
53+
LastName = "Smith",
54+
EnrollmentDate = DateTime.Today.AddYears(-1)
55+
};
56+
57+
await SendAsync(editCommand);
58+
59+
var student = await FindAsync<Student>(studentId);
60+
61+
student.ShouldNotBeNull();
62+
student.FirstMidName.ShouldBe(editCommand.FirstMidName);
63+
student.LastName.ShouldBe(editCommand.LastName);
64+
student.EnrollmentDate.ShouldBe(editCommand.EnrollmentDate.GetValueOrDefault());
65+
}
66+
}
67+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using ContosoUniversity.Features.Students;
5+
using ContosoUniversity.Models;
6+
using Shouldly;
7+
using Xunit;
8+
using static ContosoUniversity.IntegrationTests.SliceFixture;
9+
10+
namespace ContosoUniversity.IntegrationTests.Features.Students
11+
{
12+
public class IndexTests
13+
{
14+
[Fact]
15+
public async Task Should_return_all_items_for_default_search()
16+
{
17+
var suffix = DateTime.Now.Ticks.ToString();
18+
var lastName = "Schmoe" + suffix;
19+
var student1 = new Student
20+
{
21+
EnrollmentDate = DateTime.Today,
22+
FirstMidName = "Joe",
23+
LastName = lastName
24+
};
25+
var student2 = new Student
26+
{
27+
EnrollmentDate = DateTime.Today,
28+
FirstMidName = "Jane",
29+
LastName = lastName
30+
};
31+
await InsertAsync(student1, student2);
32+
33+
var query = new Index.Query{CurrentFilter = lastName };
34+
35+
var result = await SendAsync(query);
36+
37+
result.Results.Count.ShouldBeGreaterThanOrEqualTo(2);
38+
result.Results.Select(r => r.ID).ShouldContain(student1.Id);
39+
result.Results.Select(r => r.ID).ShouldContain(student2.Id);
40+
}
41+
42+
[Fact]
43+
public async Task Should_sort_based_on_name()
44+
{
45+
var suffix = DateTime.Now.Ticks.ToString();
46+
var lastName = "Schmoe" + suffix;
47+
var student1 = new Student
48+
{
49+
EnrollmentDate = DateTime.Today,
50+
FirstMidName = "Joe",
51+
LastName = lastName + "zzz"
52+
};
53+
var student2 = new Student
54+
{
55+
EnrollmentDate = DateTime.Today,
56+
FirstMidName = "Jane",
57+
LastName = lastName + "aaa"
58+
};
59+
await InsertAsync(student1, student2);
60+
61+
var query = new Index.Query{CurrentFilter = lastName, SortOrder = "name_desc" };
62+
63+
var result = await SendAsync(query);
64+
65+
result.Results.Count.ShouldBe(2);
66+
result.Results[0].ID.ShouldBe(student1.Id);
67+
result.Results[1].ID.ShouldBe(student2.Id);
68+
}
69+
}
70+
}

ContosoUniversity/Features/Courses/Create.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using AutoMapper;
1+
using System.Threading.Tasks;
2+
using AutoMapper;
23
using ContosoUniversity.Data;
34
using ContosoUniversity.Models;
45
using MediatR;
@@ -7,7 +8,7 @@ namespace ContosoUniversity.Features.Courses
78
{
89
public class Create
910
{
10-
public class Command : IRequest
11+
public class Command : IRequest<int>
1112
{
1213
[IgnoreMap]
1314
public int Number { get; set; }
@@ -16,18 +17,22 @@ public class Command : IRequest
1617
public Department Department { get; set; }
1718
}
1819

19-
public class Handler : IRequestHandler<Command>
20+
public class Handler : IAsyncRequestHandler<Command, int>
2021
{
2122
private readonly SchoolContext _db;
2223

2324
public Handler(SchoolContext db) => _db = db;
2425

25-
public void Handle(Command message)
26+
public async Task<int> Handle(Command message)
2627
{
2728
var course = Mapper.Map<Command, Course>(message);
2829
course.Id = message.Number;
2930

3031
_db.Courses.Add(course);
32+
33+
await _db.SaveChangesAsync();
34+
35+
return course.Id;
3136
}
3237
}
3338
}

0 commit comments

Comments
 (0)