Skip to content

Commit 3262fd1

Browse files
authored
Improve mocked ITagClient.GetAsync(TagQuery query) (#1074)
Improve mocked ITagClient.GetAsync(TagQuery query) - Add more cases to NGitLab.Tests' SearchTags.SearchTags, but now all within a single test pass (to set up things once) - Duplicate the said test cases on the mock side - Adapt mock code so that those tests pass
1 parent a41210f commit 3262fd1

3 files changed

Lines changed: 98 additions & 19 deletions

File tree

NGitLab.Mock.Tests/TagTests.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task GetTagAsync()
1717
.WithUser("user1", isDefault: true)
1818
.WithProject("test-project", id: 1, addDefaultUserAsMaintainer: true, configure: project => project
1919
.WithCommit("Initial commit")
20-
.WithCommit("Changes with tag", tags: new[] { "1.0.0" }))
20+
.WithCommit("Changes with tag", tags: ["1.0.0"]))
2121
.BuildServer();
2222

2323
var client = server.CreateClient();
@@ -32,7 +32,7 @@ public async Task GetTagAsync()
3232
}
3333

3434
[Theory]
35-
public void GetTaskAsync_CanSortByName([Values] bool useDefault)
35+
public void GetTagsAsync_CanSortByName([Values] bool useDefault)
3636
{
3737
// Arrange
3838
using var server = new GitLabConfig()
@@ -61,7 +61,7 @@ public void GetTaskAsync_CanSortByName([Values] bool useDefault)
6161
}
6262

6363
[Test]
64-
public void GetTagAsync_CanSortByVersion()
64+
public void GetTagsAsync_CanSortByVersion()
6565
{
6666
// Arrange
6767
using var server = new GitLabConfig()
@@ -88,4 +88,49 @@ public void GetTagAsync_CanSortByVersion()
8888
// Assert
8989
Assert.That(tags.AsEnumerable().Select(t => t.Name), Is.EqualTo(["not-semver", "0.0.1", "0.0.2", "0.0.10"]));
9090
}
91+
92+
[Test]
93+
public async Task SearchTags()
94+
{
95+
// Arrange
96+
using var server = new GitLabConfig()
97+
.WithUser("user1", isDefault: true)
98+
.WithProject("test-project", id: 1, addDefaultUserAsMaintainer: true, configure: project => project
99+
.WithCommit("First Tag", tags: ["v0.5"])
100+
.WithCommit("Second Tag", tags: ["v0.6"]))
101+
.BuildServer();
102+
103+
var client = server.CreateClient();
104+
var tagClient = client.GetRepository(1).Tags;
105+
106+
(string, int)[] testCases =
107+
[
108+
// You can use "^term" and "term$" to find tags that begin and end with "term". No other regular expressions are supported.
109+
// The search expression is case-insensitive.
110+
// https://docs.gitlab.com/api/tags/#list-all-project-repository-tags
111+
("^v0.5", 1),
112+
("^v0", 2),
113+
("^v", 2),
114+
("^V", 2),
115+
("^v1", 0),
116+
("0.5", 1),
117+
("0", 2),
118+
("6", 1),
119+
("V", 2),
120+
("0.5$", 1),
121+
(".5$", 1),
122+
("\\.5$", 0),
123+
(".[0-9]$", 0),
124+
("0\\.", 0),
125+
];
126+
127+
foreach (var (searchExpression, expectedCount) in testCases)
128+
{
129+
// Act
130+
var tags = tagClient.GetAsync(new TagQuery { Search = searchExpression });
131+
132+
// Assert
133+
Assert.That(tags.Count(), Is.EqualTo(expectedCount), $"Expected search expression '{searchExpression}' to return {expectedCount} results.");
134+
}
135+
}
91136
}

NGitLab.Mock/Clients/TagClient.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,42 @@ public Tag ToTagClient(LibGit2Sharp.Tag tag)
8686
using (Context.BeginOperationScope())
8787
{
8888
IEnumerable<LibGit2Sharp.Tag> result = GetProject(_projectId, ProjectPermission.View).Repository.GetTags();
89-
if (query != null)
89+
if (query is not null)
9090
{
91-
result = ApplyQuery(result, query.OrderBy, query.Sort);
91+
result = ApplyQuery(result, query);
9292
}
9393

9494
return GitLabCollectionResponse.Create(result.Select(ToTagClient).ToArray());
9595
}
9696

97-
static IEnumerable<LibGit2Sharp.Tag> ApplyQuery(IEnumerable<LibGit2Sharp.Tag> tags, string orderBy, string direction)
97+
static IEnumerable<LibGit2Sharp.Tag> ApplyQuery(IEnumerable<LibGit2Sharp.Tag> tags, TagQuery query)
9898
{
99+
// First, filter by search term if provided
100+
if (!string.IsNullOrEmpty(query.Search))
101+
{
102+
tags = query.Search switch
103+
{
104+
string search when search.StartsWith("^", StringComparison.Ordinal) =>
105+
tags.Where(t => t.FriendlyName.StartsWith(search[1..], StringComparison.OrdinalIgnoreCase)),
106+
string search when search.EndsWith("$", StringComparison.Ordinal) =>
107+
tags.Where(t => t.FriendlyName.EndsWith(search[..^1], StringComparison.OrdinalIgnoreCase)),
108+
_ => tags.Where(t => t.FriendlyName.Contains(query.Search, StringComparison.OrdinalIgnoreCase)),
109+
};
110+
}
111+
112+
var orderBy = query.OrderBy?.ToLowerInvariant();
99113
tags = orderBy switch
100114
{
101115
"name" => tags.OrderBy(t => t.FriendlyName, StringComparer.Ordinal),
102116
"version" => tags.OrderBy(t => t.FriendlyName, SemanticVersionComparer.Instance),
103117
null => tags,
104118

105119
// LibGitSharp does not really expose tag creation time, so hard to sort using that annotation,
106-
"updated" => throw new NotSupportedException("Sorting by 'updated' is not supported since the info is not available in LibGit2Sharp."),
120+
"updated" => throw new NotSupportedException("Sorting by 'updated' is not supported by mock, since the info is not available in LibGit2Sharp."),
107121
_ => throw new NotSupportedException($"Sorting by '{orderBy}' is not supported."),
108122
};
109123

124+
var direction = query.Sort;
110125
if (string.IsNullOrEmpty(direction))
111126
direction = "desc";
112127

NGitLab.Tests/TagTests.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,8 @@ public async Task Test_can_tag_a_project()
3434
}
3535

3636
[NGitLabRetry]
37-
[TestCase("^v0.5", 1)]
38-
[TestCase("^v0", 2)]
39-
[TestCase("^v1", 0)]
40-
[TestCase("v1", 0)]
41-
[TestCase("0.5$", 1)]
42-
[TestCase("0\\.", 0)]
43-
[TestCase(".5$", 1)]
44-
[TestCase("\\.5$", 0)]
45-
[TestCase(".[0-9]$", 0)]
46-
public async Task SearchTags(string search, int expectedCount)
37+
[Test]
38+
public async Task SearchTags()
4739
{
4840
// Arrange
4941
using var context = await GitLabTestContext.CreateAsync();
@@ -64,8 +56,35 @@ public async Task SearchTags(string search, int expectedCount)
6456
Ref = project.DefaultBranch,
6557
});
6658

67-
var tagFetched = tagClient.GetAsync(new TagQuery { Search = search });
68-
Assert.That(tagFetched.Count(), Is.EqualTo(expectedCount));
59+
(string, int)[] testCases =
60+
[
61+
// You can use "^term" and "term$" to find tags that begin and end with "term". No other regular expressions are supported.
62+
// The search expression is case-insensitive.
63+
// https://docs.gitlab.com/api/tags/#list-all-project-repository-tags
64+
("^v0.5", 1),
65+
("^v0", 2),
66+
("^v", 2),
67+
("^V", 2),
68+
("^v1", 0),
69+
("0.5", 1),
70+
("0", 2),
71+
("6", 1),
72+
("V", 2),
73+
("0.5$", 1),
74+
(".5$", 1),
75+
("\\.5$", 0),
76+
(".[0-9]$", 0),
77+
("0\\.", 0),
78+
];
79+
80+
foreach (var (searchExpression, expectedCount) in testCases)
81+
{
82+
// Act
83+
var tags = tagClient.GetAsync(new TagQuery { Search = searchExpression });
84+
85+
// Assert
86+
Assert.That(tags.Count(), Is.EqualTo(expectedCount), $"Expected search expression '{searchExpression}' to return {expectedCount} results.");
87+
}
6988
}
7089

7190
[NGitLabRetry]

0 commit comments

Comments
 (0)