Skip to content

Commit cc166ce

Browse files
authored
Merge pull request #88 from boolean-uk/62-api-create-cohortendpoints
changed some of the cohort-endpoints and added cohort-tests
2 parents bc14303 + eccafb4 commit cc166ce

File tree

8 files changed

+328
-18
lines changed

8 files changed

+328
-18
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using exercise.wwwapi.DTOs;
2+
using exercise.wwwapi.DTOs.Cohorts;
3+
using exercise.wwwapi.Endpoints;
4+
using exercise.wwwapi.Models;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Threading.Tasks;
11+
12+
namespace api.tests.CohortEndpointTests
13+
{
14+
public class CreateCohortTests
15+
{
16+
private HttpClient _client;
17+
18+
[SetUp]
19+
public void Setup()
20+
{
21+
_client = TestUtils.CreateClient();
22+
}
23+
24+
[TearDown]
25+
public void TearDown()
26+
{
27+
_client.Dispose();
28+
}
29+
30+
[Test]
31+
// Checks that creating a new cohort with valid values returns Created
32+
public async Task CreateCohortTest()
33+
{
34+
var newCohort = new CreateCohortRequestDTO
35+
{
36+
CohortNumber = 2,
37+
CohortName = "New Cohort",
38+
StartDate = DateTime.UtcNow,
39+
EndDate = DateTime.UtcNow.AddMonths(3)
40+
};
41+
var content = new StringContent(
42+
JsonSerializer.Serialize(newCohort),
43+
System.Text.Encoding.UTF8,
44+
"application/json"
45+
);
46+
var response = await _client.PostAsync("/cohorts", content);
47+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.Created));
48+
}
49+
50+
[Test]
51+
// Checks that creating a new cohort with invalid values returns BadRequest
52+
public async Task CreateCohortValidationFailsTest()
53+
{
54+
var newCohort = new CreateCohortRequestDTO
55+
{
56+
CohortNumber = -2,
57+
CohortName = "New Cohort",
58+
StartDate = DateTime.MinValue,
59+
EndDate = DateTime.MinValue
60+
};
61+
var content = new StringContent(
62+
JsonSerializer.Serialize(newCohort),
63+
System.Text.Encoding.UTF8,
64+
"application/json"
65+
);
66+
var response = await _client.PostAsync("/cohorts", content);
67+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest));
68+
}
69+
70+
// Checks that creating a new cohort with missing required fields returns BadRequest
71+
[Test]
72+
public async Task CreateCohortMissingFieldsTest()
73+
{
74+
var newCohort = new
75+
{
76+
CohortNumber = 3,
77+
// CohortName is missing
78+
StartDate = DateTime.UtcNow,
79+
EndDate = DateTime.UtcNow.AddMonths(3)
80+
};
81+
var content = new StringContent(
82+
JsonSerializer.Serialize(newCohort),
83+
System.Text.Encoding.UTF8,
84+
"application/json"
85+
);
86+
var response = await _client.PostAsync("/cohorts", content);
87+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest));
88+
}
89+
}
90+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using exercise.wwwapi.Endpoints;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace api.tests.CohortEndpointTests
9+
{
10+
public class DeleteCohortTests
11+
{
12+
private HttpClient _client;
13+
14+
[SetUp]
15+
public void Setup()
16+
{
17+
_client = TestUtils.CreateClient();
18+
}
19+
20+
[TearDown]
21+
public void TearDown()
22+
{
23+
_client.Dispose();
24+
}
25+
26+
[Test]
27+
// Checks that deleting an existing cohort returns OK
28+
public async Task DeleteCohortTest()
29+
{
30+
var response = await _client.DeleteAsync("/cohorts/1");
31+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
32+
}
33+
34+
[Test]
35+
// Checks that deleting a non-existent cohort returns NotFound
36+
public async Task DeleteCohortNotFoundTest()
37+
{
38+
var response = await _client.DeleteAsync("/cohorts/99999");
39+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.NotFound));
40+
}
41+
42+
[Test]
43+
// Checks that deleting a cohort with an invalid id returns BadRequest
44+
public async Task DeleteCohortInvalidIdTest()
45+
{
46+
var response = await _client.DeleteAsync("/cohorts/invalid");
47+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest));
48+
}
49+
}
50+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using exercise.wwwapi.Endpoints;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace api.tests.CohortEndpointTests
9+
{
10+
public class GetCohortTests
11+
{
12+
private HttpClient _client;
13+
14+
[SetUp]
15+
public void Setup()
16+
{
17+
_client = TestUtils.CreateClient();
18+
}
19+
20+
[TearDown]
21+
public void TearDown()
22+
{
23+
_client.Dispose();
24+
}
25+
26+
[Test]
27+
// Checks that getting all cohorts returns OK
28+
public async Task GetAllCohortsTest()
29+
{
30+
var getCohortsResponse = await _client.GetAsync($"cohorts");
31+
Assert.That(getCohortsResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
32+
}
33+
34+
[Test]
35+
// Checks that getting a specific cohort by ID returns OK
36+
public async Task GetCohortByIdTest()
37+
{
38+
var getCohortResponse = await _client.GetAsync($"cohorts/1");
39+
Assert.That(getCohortResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
40+
}
41+
42+
[Test]
43+
// Checks that getting a cohort with an invalid id returns BadRequest
44+
public async Task GetCohortByIdBadRequestTest()
45+
{
46+
var response = await _client.GetAsync("cohorts/notanumber");
47+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest));
48+
}
49+
50+
[Test]
51+
// Checks that getting a non-existent cohort returns NotFound
52+
public async Task GetCohortByIdNotFoundTest()
53+
{
54+
var getCohortResponse = await _client.GetAsync($"cohorts/999999999");
55+
Assert.That(getCohortResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.NotFound));
56+
}
57+
}
58+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using exercise.wwwapi.Endpoints;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace api.tests.CohortEndpointTests
9+
{
10+
public class UpdateCohortTests
11+
{
12+
private HttpClient _client;
13+
14+
[SetUp]
15+
public void Setup()
16+
{
17+
_client = TestUtils.CreateClient();
18+
}
19+
20+
[TearDown]
21+
public void TearDown()
22+
{
23+
_client.Dispose();
24+
}
25+
26+
[Test]
27+
// Checks that updating an existing cohort returns OK status
28+
public async Task UpdateCohortTest()
29+
{
30+
var updatedCohort = new
31+
{
32+
CohortNumber = 1,
33+
CohortName = "Updated Cohort Name",
34+
StartDate = DateTime.UtcNow,
35+
EndDate = DateTime.UtcNow.AddMonths(4)
36+
};
37+
var content = new StringContent(
38+
System.Text.Json.JsonSerializer.Serialize(updatedCohort),
39+
System.Text.Encoding.UTF8,
40+
"application/json"
41+
);
42+
var response = await _client.PatchAsync("/cohorts/1", content);
43+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
44+
}
45+
46+
[Test]
47+
// Checks that updating a non-existent cohort returns NotFound status
48+
public async Task UpdateCohortNotFoundTest()
49+
{
50+
var updatedCohort = new
51+
{
52+
CohortNumber = 99999,
53+
CohortName = "Non-existent Cohort",
54+
StartDate = DateTime.UtcNow,
55+
EndDate = DateTime.UtcNow.AddMonths(4)
56+
};
57+
var content = new StringContent(
58+
System.Text.Json.JsonSerializer.Serialize(updatedCohort),
59+
System.Text.Encoding.UTF8,
60+
"application/json"
61+
);
62+
var response = await _client.PatchAsync("/cohorts/99999", content);
63+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.NotFound));
64+
}
65+
66+
[Test]
67+
// Checks that updating a cohort with invalid data returns BadRequest status
68+
public async Task UpdateCohortValidationFailsTest()
69+
{
70+
var updatedCohort = new
71+
{
72+
CohortNumber = 0,
73+
CohortName = "",
74+
StartDate = DateTime.MinValue,
75+
EndDate = DateTime.MinValue
76+
};
77+
var content = new StringContent(
78+
System.Text.Json.JsonSerializer.Serialize(updatedCohort),
79+
System.Text.Encoding.UTF8,
80+
"application/json"
81+
);
82+
var response = await _client.PatchAsync("/cohorts/1", content);
83+
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest));
84+
}
85+
}
86+
}

exercise.wwwapi/DTOs/Cohorts/CohortDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CohortDTO
1111
{
1212
public int Id { get; set; }
1313
public int CohortNumber { get; set; }
14-
public string CohortName { get; set; }
14+
public string CohortName { get; set; }
1515
public DateTime StartDate { get; set; }
1616
public DateTime EndDate { get; set; }
1717
public List<CourseDTO> Courses { get; set; }

exercise.wwwapi/DTOs/Cohorts/CohortPostDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public class CohortPostDTO
55
{
6-
public string CohortName { get; set; }
6+
public required string CohortName { get; set; }
77

88
public DateTime StartDate { get; set; }
99
public DateTime EndDate { get; set; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace exercise.wwwapi.DTOs.Cohorts
2+
{
3+
public class CreateCohortRequestDTO
4+
{
5+
public int CohortNumber { get; set; }
6+
public required string CohortName { get; set; }
7+
public DateTime StartDate { get; set; }
8+
public DateTime EndDate { get; set; }
9+
}
10+
}

0 commit comments

Comments
 (0)