-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement grades filter * Apply grades filter to the endpoint
- Loading branch information
1 parent
d3d6a33
commit 82da81b
Showing
6 changed files
with
202 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using EUniversity.Core.Models.University.Grades; | ||
using EUniversity.Infrastructure.Filters; | ||
|
||
namespace EUniversity.Tests.Filters; | ||
|
||
public class GradesFilterTests | ||
{ | ||
private readonly Grade[] TestGrades = | ||
{ | ||
new Grade() {Id = 1, Name = "1", Score = 500}, | ||
new Grade() {Id = 3, Name = "3", Score = 700}, | ||
new Grade() {Id = 2, Name = "2", Score = 600}, | ||
new Grade() {Id = 4, Name = "10", Score = 800}, | ||
}; | ||
|
||
[Test] | ||
public void EmptyFilter_ReturnsEntireQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(); | ||
GradesFilter filter = new(properties); | ||
|
||
// Act | ||
var result = filter.Apply(TestGrades.AsQueryable()); | ||
|
||
// Assert | ||
Assert.That(result, Is.EquivalentTo(TestGrades)); | ||
} | ||
|
||
[Test] | ||
public void NameSpecified_ReturnsFilteredQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(Name: "1"); | ||
GradesFilter filter = new(properties); | ||
int[] expectedIds = { 1, 4 }; | ||
|
||
// Act | ||
var actualIds = filter.Apply(TestGrades.AsQueryable()) | ||
.Select(g => g.Id); | ||
|
||
// Assert | ||
Assert.That(actualIds, Is.EquivalentTo(expectedIds)); | ||
} | ||
|
||
[Test] | ||
public void SortByName_ReturnsOrderedByNameQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(SortingMode: GradesSortingMode.Name); | ||
GradesFilter filter = new(properties); | ||
|
||
// Act | ||
var result = filter.Apply(TestGrades.AsQueryable()); | ||
|
||
// Assert | ||
Assert.That(result, Is.EquivalentTo(TestGrades)); | ||
Assert.That(result, Is.Ordered.Ascending.By("Name")); | ||
} | ||
|
||
[Test] | ||
public void SortByNameDescending_ReturnsOrderedByNameQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(SortingMode: GradesSortingMode.NameDescending); | ||
GradesFilter filter = new(properties); | ||
|
||
// Act | ||
var result = filter.Apply(TestGrades.AsQueryable()); | ||
|
||
// Assert | ||
Assert.That(result, Is.EquivalentTo(TestGrades)); | ||
Assert.That(result, Is.Ordered.Descending.By("Name")); | ||
} | ||
|
||
[Test] | ||
public void SortByScore_ReturnsOrderedByScoreQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(SortingMode: GradesSortingMode.Score); | ||
GradesFilter filter = new(properties); | ||
|
||
// Act | ||
var result = filter.Apply(TestGrades.AsQueryable()); | ||
|
||
// Assert | ||
Assert.That(result, Is.EquivalentTo(TestGrades)); | ||
Assert.That(result, Is.Ordered.Ascending.By("Score")); | ||
} | ||
|
||
[Test] | ||
public void SortByScoreDescending_ReturnsOrderedByScoreQuery() | ||
{ | ||
// Arrange | ||
GradesFilterProperties properties = new(SortingMode: GradesSortingMode.ScoreDescending); | ||
GradesFilter filter = new(properties); | ||
|
||
// Act | ||
var result = filter.Apply(TestGrades.AsQueryable()); | ||
|
||
// Assert | ||
Assert.That(result, Is.EquivalentTo(TestGrades)); | ||
Assert.That(result, Is.Ordered.Descending.By("Score")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Bogus.DataSets; | ||
using EUniversity.Core.Filters; | ||
using EUniversity.Core.Models; | ||
using EUniversity.Core.Models.University.Grades; | ||
|
||
namespace EUniversity.Infrastructure.Filters; | ||
|
||
/// <summary> | ||
/// Represents a sorting mode for <see cref="GradesFilter" />. | ||
/// </summary> | ||
public enum GradesSortingMode | ||
{ | ||
/// <summary> | ||
/// Default sorting mode, grades will not be sorted. | ||
/// </summary> | ||
Default = 0, | ||
/// <summary> | ||
/// Sort grades by name alphabetically from a to z. | ||
/// </summary> | ||
Name = 1, | ||
/// <summary> | ||
/// Sort grades by name alphabetically from z to a. | ||
/// </summary> | ||
NameDescending = 2, | ||
/// <summary> | ||
/// Sort grades by its score. | ||
/// </summary> | ||
Score = 3, | ||
/// <summary> | ||
/// Sort grades by its score descending. | ||
/// </summary> | ||
ScoreDescending = 4 | ||
} | ||
|
||
/// <summary> | ||
/// Filter for grades. | ||
/// </summary> | ||
public class GradesFilter : IFilter<Grade> | ||
{ | ||
/// <summary> | ||
/// Properties used for filtering grades. | ||
/// </summary> | ||
public GradesFilterProperties Properties { get; set; } | ||
|
||
public GradesFilter(GradesFilterProperties properties) | ||
{ | ||
Properties = properties; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual IQueryable<Grade> Apply(IQueryable<Grade> query) | ||
{ | ||
// Filter by name | ||
query = query.Where(g => g.Name.Contains(Properties.Name)); | ||
|
||
// Apply sort | ||
switch (Properties.SortingMode) | ||
{ | ||
case GradesSortingMode.Name: | ||
query = query.OrderBy(g => g.Name); | ||
break; | ||
case GradesSortingMode.NameDescending: | ||
query = query.OrderByDescending(g => g.Name); | ||
break; | ||
case GradesSortingMode.Score: | ||
query = query.OrderBy(g => g.Score); | ||
break; | ||
case GradesSortingMode.ScoreDescending: | ||
query = query.OrderByDescending(g => g.Score); | ||
break; | ||
} | ||
|
||
return query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace EUniversity.Infrastructure.Filters; | ||
|
||
/// <summary> | ||
/// Represents properties for filtering grades. | ||
/// </summary> | ||
/// <param name="Name">Optional name to filter by.</param> | ||
/// <param name="SortingMode">Sorting mode for grades.</param> | ||
public record GradesFilterProperties(string Name = "", GradesSortingMode SortingMode = GradesSortingMode.ScoreDescending); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters