Skip to content

Commit e5ee463

Browse files
authored
Merge pull request #1140 from SamSmithNZ-dotcom/ImprovingCodeCoverage
Added more tests to improve code coverage
2 parents ca94e3b + bc47309 commit e5ee463

27 files changed

+16630
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This website is a place for me to learn, experiment and play with new technologies in a public place. I currently have 8 major projects here, with a few more under the radar prototypes and experiments.
44

55
[![CI/CD](https://github.com/samsmithnz/SamSmithNZ.com/actions/workflows/dotnet.yml/badge.svg)](https://github.com/samsmithnz/SamSmithNZ.com/actions/workflows/dotnet.yml)
6-
[![Coverage Status](https://coveralls.io/repos/github/samsmithnz/SamSmithNZ.com/badge.svg?branch=main)](https://coveralls.io/github/samsmithnz/SamSmithNZ.com?branch=main)
6+
[![Coverage Status](https://coveralls.io/repos/github/SamSmithNZ-dotcom/SamSmithNZ.com/badge.svg?branch=main)](https://coveralls.io/github/samsmithnz/SamSmithNZ.com?branch=main)
77
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=samsmithnz_SamSmithNZ.com&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=samsmithnz_SamSmithNZ.com)
88
[![Current Release](https://img.shields.io/github/release/samsmithnz/SamSmithNZ.com/all.svg)](https://github.com/samsmithnz/SamSmithNZ.com/releases)
99

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using NSubstitute;
3+
using SamSmithNZ.Service.Controllers.FooFighters;
4+
using SamSmithNZ.Service.DataAccess.FooFighters.Interfaces;
5+
using SamSmithNZ.Service.Models.FooFighters;
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
9+
namespace SamSmithNZ.Tests.Controllers.FooFighters
10+
{
11+
[TestClass]
12+
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
13+
public class ShowControllerTests
14+
{
15+
private IShowDataAccess _mockRepo;
16+
private ShowController _controller;
17+
18+
[TestInitialize]
19+
public void Setup()
20+
{
21+
_mockRepo = Substitute.For<IShowDataAccess>();
22+
_controller = new ShowController(_mockRepo);
23+
}
24+
25+
[TestMethod]
26+
public void ShowController_Constructor_InitializesCorrectly()
27+
{
28+
// Arrange & Act
29+
ShowController controller = new ShowController(_mockRepo);
30+
31+
// Assert
32+
Assert.IsNotNull(controller);
33+
}
34+
35+
[TestMethod]
36+
public async Task GetShowsByYear_ValidYear_ReturnsShows()
37+
{
38+
// Arrange
39+
int yearCode = 2023;
40+
List<Show> expectedShows = new List<Show>
41+
{
42+
new Show { ShowCode = 1, ShowDate = new System.DateTime(2023, 6, 1) },
43+
new Show { ShowCode = 2, ShowDate = new System.DateTime(2023, 8, 15) }
44+
};
45+
46+
_mockRepo.GetListByYearAsync(yearCode).Returns(expectedShows);
47+
48+
// Act
49+
List<Show> result = await _controller.GetShowsByYear(yearCode);
50+
51+
// Assert
52+
Assert.IsNotNull(result);
53+
Assert.AreEqual(2, result.Count);
54+
await _mockRepo.Received(1).GetListByYearAsync(yearCode);
55+
}
56+
57+
[TestMethod]
58+
public async Task GetShowsByYear_NoShows_ReturnsEmptyList()
59+
{
60+
// Arrange
61+
int yearCode = 1990;
62+
List<Show> expectedShows = new List<Show>();
63+
64+
_mockRepo.GetListByYearAsync(yearCode).Returns(expectedShows);
65+
66+
// Act
67+
List<Show> result = await _controller.GetShowsByYear(yearCode);
68+
69+
// Assert
70+
Assert.IsNotNull(result);
71+
Assert.AreEqual(0, result.Count);
72+
}
73+
74+
[TestMethod]
75+
public async Task GetShowsBySong_ValidSongCode_ReturnsShows()
76+
{
77+
// Arrange
78+
int songCode = 10;
79+
List<Show> expectedShows = new List<Show>
80+
{
81+
new Show { ShowCode = 100 },
82+
new Show { ShowCode = 101 },
83+
new Show { ShowCode = 102 }
84+
};
85+
86+
_mockRepo.GetListBySongAsync(songCode).Returns(expectedShows);
87+
88+
// Act
89+
List<Show> result = await _controller.GetShowsBySong(songCode);
90+
91+
// Assert
92+
Assert.IsNotNull(result);
93+
Assert.AreEqual(3, result.Count);
94+
await _mockRepo.Received(1).GetListBySongAsync(songCode);
95+
}
96+
97+
[TestMethod]
98+
public async Task GetShowsBySong_NoShows_ReturnsEmptyList()
99+
{
100+
// Arrange
101+
int songCode = 999;
102+
List<Show> expectedShows = new List<Show>();
103+
104+
_mockRepo.GetListBySongAsync(songCode).Returns(expectedShows);
105+
106+
// Act
107+
List<Show> result = await _controller.GetShowsBySong(songCode);
108+
109+
// Assert
110+
Assert.IsNotNull(result);
111+
Assert.AreEqual(0, result.Count);
112+
}
113+
114+
[TestMethod]
115+
public async Task GetListByFFLCode_ReturnsShows()
116+
{
117+
// Arrange
118+
List<Show> expectedShows = new List<Show>
119+
{
120+
new Show { ShowCode = 1, FFLCode = 1001 },
121+
new Show { ShowCode = 2, FFLCode = 1002 }
122+
};
123+
124+
_mockRepo.GetListByFFLCode().Returns(expectedShows);
125+
126+
// Act
127+
List<Show> result = await _controller.GetListByFFLCode();
128+
129+
// Assert
130+
Assert.IsNotNull(result);
131+
Assert.AreEqual(2, result.Count);
132+
await _mockRepo.Received(1).GetListByFFLCode();
133+
}
134+
135+
[TestMethod]
136+
public async Task GetListByFFLCode_NoShows_ReturnsEmptyList()
137+
{
138+
// Arrange
139+
List<Show> expectedShows = new List<Show>();
140+
141+
_mockRepo.GetListByFFLCode().Returns(expectedShows);
142+
143+
// Act
144+
List<Show> result = await _controller.GetListByFFLCode();
145+
146+
// Assert
147+
Assert.IsNotNull(result);
148+
Assert.AreEqual(0, result.Count);
149+
}
150+
151+
[TestMethod]
152+
public async Task GetShow_ValidShowCode_ReturnsShow()
153+
{
154+
// Arrange
155+
int showCode = 42;
156+
Show expectedShow = new Show
157+
{
158+
ShowCode = 42,
159+
ShowDate = new System.DateTime(2022, 5, 20),
160+
FFLCode = 42
161+
};
162+
163+
_mockRepo.GetItem(showCode).Returns(expectedShow);
164+
165+
// Act
166+
Show result = await _controller.GetShow(showCode);
167+
168+
// Assert
169+
Assert.IsNotNull(result);
170+
Assert.AreEqual(42, result.ShowCode);
171+
await _mockRepo.Received(1).GetItem(showCode);
172+
}
173+
174+
[TestMethod]
175+
public async Task GetShow_InvalidShowCode_ReturnsNull()
176+
{
177+
// Arrange
178+
int showCode = 999;
179+
Show expectedShow = null;
180+
181+
_mockRepo.GetItem(showCode).Returns(expectedShow);
182+
183+
// Act
184+
Show result = await _controller.GetShow(showCode);
185+
186+
// Assert
187+
Assert.IsNull(result);
188+
await _mockRepo.Received(1).GetItem(showCode);
189+
}
190+
}
191+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using NSubstitute;
3+
using SamSmithNZ.Service.Controllers.FooFighters;
4+
using SamSmithNZ.Service.DataAccess.FooFighters.Interfaces;
5+
using SamSmithNZ.Service.Models.FooFighters;
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
9+
namespace SamSmithNZ.Tests.Controllers.FooFighters
10+
{
11+
[TestClass]
12+
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
13+
public class SongControllerTests
14+
{
15+
private ISongDataAccess _mockRepo;
16+
private SongController _controller;
17+
18+
[TestInitialize]
19+
public void Setup()
20+
{
21+
_mockRepo = Substitute.For<ISongDataAccess>();
22+
_controller = new SongController(_mockRepo);
23+
}
24+
25+
[TestMethod]
26+
public void SongController_Constructor_InitializesCorrectly()
27+
{
28+
// Arrange & Act
29+
SongController controller = new SongController(_mockRepo);
30+
31+
// Assert
32+
Assert.IsNotNull(controller);
33+
}
34+
35+
[TestMethod]
36+
public async Task GetSongs_ReturnsAllSongs()
37+
{
38+
// Arrange
39+
List<Song> expectedSongs = new List<Song>
40+
{
41+
new Song { SongCode = 1, SongName = "Everlong" },
42+
new Song { SongCode = 2, SongName = "My Hero" },
43+
new Song { SongCode = 3, SongName = "The Pretender" }
44+
};
45+
46+
_mockRepo.GetList().Returns(expectedSongs);
47+
48+
// Act
49+
List<Song> result = await _controller.GetSongs();
50+
51+
// Assert
52+
Assert.IsNotNull(result);
53+
Assert.AreEqual(3, result.Count);
54+
await _mockRepo.Received(1).GetList();
55+
}
56+
57+
[TestMethod]
58+
public async Task GetSongs_NoSongs_ReturnsEmptyList()
59+
{
60+
// Arrange
61+
List<Song> expectedSongs = new List<Song>();
62+
63+
_mockRepo.GetList().Returns(expectedSongs);
64+
65+
// Act
66+
List<Song> result = await _controller.GetSongs();
67+
68+
// Assert
69+
Assert.IsNotNull(result);
70+
Assert.AreEqual(0, result.Count);
71+
}
72+
73+
[TestMethod]
74+
public async Task GetSongsByAlbum_ValidAlbumCode_ReturnsSongs()
75+
{
76+
// Arrange
77+
int albumCode = 5;
78+
List<Song> expectedSongs = new List<Song>
79+
{
80+
new Song { SongCode = 10, SongName = "Everlong", AlbumCode = 5 },
81+
new Song { SongCode = 11, SongName = "My Hero", AlbumCode = 5 }
82+
};
83+
84+
_mockRepo.GetListForAlbumAsync(albumCode).Returns(expectedSongs);
85+
86+
// Act
87+
List<Song> result = await _controller.GetSongsByAlbum(albumCode);
88+
89+
// Assert
90+
Assert.IsNotNull(result);
91+
Assert.AreEqual(2, result.Count);
92+
await _mockRepo.Received(1).GetListForAlbumAsync(albumCode);
93+
}
94+
95+
[TestMethod]
96+
public async Task GetSongsByAlbum_NoSongs_ReturnsEmptyList()
97+
{
98+
// Arrange
99+
int albumCode = 99;
100+
List<Song> expectedSongs = new List<Song>();
101+
102+
_mockRepo.GetListForAlbumAsync(albumCode).Returns(expectedSongs);
103+
104+
// Act
105+
List<Song> result = await _controller.GetSongsByAlbum(albumCode);
106+
107+
// Assert
108+
Assert.IsNotNull(result);
109+
Assert.AreEqual(0, result.Count);
110+
}
111+
112+
[TestMethod]
113+
public async Task GetSongsByShow_ValidShowCode_ReturnsSongs()
114+
{
115+
// Arrange
116+
int showCode = 100;
117+
List<Song> expectedSongs = new List<Song>
118+
{
119+
new Song { SongCode = 1, SongName = "Times Like These" },
120+
new Song { SongCode = 2, SongName = "Best of You" },
121+
new Song { SongCode = 3, SongName = "Learn to Fly" }
122+
};
123+
124+
_mockRepo.GetListForShowAsync(showCode).Returns(expectedSongs);
125+
126+
// Act
127+
List<Song> result = await _controller.GetSongsByShow(showCode);
128+
129+
// Assert
130+
Assert.IsNotNull(result);
131+
Assert.AreEqual(3, result.Count);
132+
await _mockRepo.Received(1).GetListForShowAsync(showCode);
133+
}
134+
135+
[TestMethod]
136+
public async Task GetSongsByShow_NoSongs_ReturnsEmptyList()
137+
{
138+
// Arrange
139+
int showCode = 999;
140+
List<Song> expectedSongs = new List<Song>();
141+
142+
_mockRepo.GetListForShowAsync(showCode).Returns(expectedSongs);
143+
144+
// Act
145+
List<Song> result = await _controller.GetSongsByShow(showCode);
146+
147+
// Assert
148+
Assert.IsNotNull(result);
149+
Assert.AreEqual(0, result.Count);
150+
}
151+
152+
[TestMethod]
153+
public async Task GetSong_ValidSongCode_ReturnsSong()
154+
{
155+
// Arrange
156+
int songCode = 42;
157+
Song expectedSong = new Song
158+
{
159+
SongCode = 42,
160+
SongName = "Everlong",
161+
AlbumCode = 5
162+
};
163+
164+
_mockRepo.GetItem(songCode).Returns(expectedSong);
165+
166+
// Act
167+
Song result = await _controller.GetSong(songCode);
168+
169+
// Assert
170+
Assert.IsNotNull(result);
171+
Assert.AreEqual(42, result.SongCode);
172+
Assert.AreEqual("Everlong", result.SongName);
173+
await _mockRepo.Received(1).GetItem(songCode);
174+
}
175+
176+
[TestMethod]
177+
public async Task GetSong_InvalidSongCode_ReturnsNull()
178+
{
179+
// Arrange
180+
int songCode = 999;
181+
Song expectedSong = null;
182+
183+
_mockRepo.GetItem(songCode).Returns(expectedSong);
184+
185+
// Act
186+
Song result = await _controller.GetSong(songCode);
187+
188+
// Assert
189+
Assert.IsNull(result);
190+
await _mockRepo.Received(1).GetItem(songCode);
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)