Skip to content

Commit

Permalink
Merge pull request #11 from GT3CH1/10-testing-suite
Browse files Browse the repository at this point in the history
10 testing suite
  • Loading branch information
GT3CH1 authored Apr 7, 2022
2 parents 3bacdf6 + 586bcde commit 7044499
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: .NET

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ riderModule.iml
**/.idea
songs.db
Songs
*.user
6 changes: 6 additions & 0 deletions YouTubeDownloader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeDownloader", "YouTubeDownloader\YouTubeDownloader.csproj", "{6844BF98-F92A-49EB-ACAB-8CBEF7100BDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeDownloaderTests", "YouTubeDownloaderTests\YouTubeDownloaderTests.csproj", "{0402AF16-5186-41DD-8A15-FD4FEACE23C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{6844BF98-F92A-49EB-ACAB-8CBEF7100BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6844BF98-F92A-49EB-ACAB-8CBEF7100BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6844BF98-F92A-49EB-ACAB-8CBEF7100BDE}.Release|Any CPU.Build.0 = Release|Any CPU
{0402AF16-5186-41DD-8A15-FD4FEACE23C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0402AF16-5186-41DD-8A15-FD4FEACE23C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0402AF16-5186-41DD-8A15-FD4FEACE23C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0402AF16-5186-41DD-8A15-FD4FEACE23C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion YouTubeDownloader/YouTubeDownloader.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,6 +12,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.2" />
Expand Down
208 changes: 208 additions & 0 deletions YouTubeDownloaderTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using YouTubeDownloader.Contexts;
using YouTubeDownloader.Controllers;
using YouTubeDownloader.Models;

namespace YouTubeDownlaoderTests;

[TestFixture]
public class Tests
{
// The songs db context
private static SongsDbContext _context;

// The controller
private static SongController _controller;

// A list of songs to experiment with
private static List<Song> _songs;

[OneTimeSetUp]
public void Setup()
{
// Use the database in memory.
var options = new DbContextOptionsBuilder<SongsDbContext>()
.UseInMemoryDatabase("Songs")
.Options;
_context = new SongsDbContext(options);
_controller = new SongController(_context);
// Create a list of songs.
_songs = new List<Song>
{
new()
{
Title = "Test Song",
Artist = "Test Artist",
Album = "Test Album",
// NOTE: This will fail in downloading.
Url = "https://peasenet.com"
},
new()
{
Title = "Test Song 1",
Artist = "Test Artist 1",
Album = "Test Album 1",
// NOTE: This will fail in downloading.
Url = "https://peasenet.com/1"
},
new()
{
Title = "Test Song 2",
Artist = "Test Artist 2",
Album = "Test Album 2",
// NOTE: This will fail in downloading.
Url = "https://peasenet.com/2"
}
};
}

[OneTimeTearDown]
public void TearDown()
{
_context.Database.EnsureDeleted();

_context.Dispose();
}

/// <summary>
/// Tests adding a single song to the database.
/// </summary>
[Test, Order(1)]
public void TestAddSong()
{
// Get the previous count of songs
var count = _context.Songs.Count();
var song = _songs[0];
_controller.AddSong(song);
// Ensure that it is created in the database.
Assert.AreEqual(count + 1, _context.Songs.Count());
}

/// <summary>
/// Tests adding a list of songs to the database.
/// </summary>
[Test, Order(2)]
public void TestAddList()
{
// Get the previous count of songs
var count = _context.Songs.Count();
// Create + add a list of songs
var songs = _songs.GetRange(1, 2);
_controller.AddSongList(songs);
// Check count
Assert.AreEqual(count + 2, _context.Songs.Count());
}

/// <summary>
/// Checks whether or not the three added songs are in the database, and the information is correct.
/// </summary>
[Test, Order(3)]
public void TestAddSongsInfo()
{
var songsInDb = _context.Songs.ToList();
// Songs are in newest first, need to reverse list here.
var tmpList = new List<Song>(_songs);
// tmpList.Reverse();
// Check that the songs are in the database.
Assert.AreEqual(3, songsInDb.Count);
// Check that the information is correct.
for (var i = 0; i < songsInDb.Count; i++)
{
Assert.AreEqual(tmpList[i].Title, songsInDb[i].Title);
Assert.AreEqual(tmpList[i].Artist, songsInDb[i].Artist);
Assert.AreEqual(tmpList[i].Album, songsInDb[i].Album);
Assert.AreEqual(tmpList[i].Url, songsInDb[i].Url);
}

}

/// <summary>
/// Tests to see if all songs in the database are listed as Json.
/// </summary>
[Test, Order(4)]
public void TestGetAllSongs()
{
var songs = _controller.GetAllSongs();
// Deserialize
var songsFromJsonList = (List<Song>)songs.Value;
// Check that each song from json list are equal to the the songs in _songs.
for (var i = 0; i < songsFromJsonList.Count; i++)
{
Assert.AreEqual(_songs[i].Title, songsFromJsonList[i].Title);
Assert.AreEqual(_songs[i].Artist, songsFromJsonList[i].Artist);
Assert.AreEqual(_songs[i].Album, songsFromJsonList[i].Album);
Assert.AreEqual(_songs[i].Url, songsFromJsonList[i].Url);
}
}

/// <summary>
/// Tests whether or not the song with the given id is deleted.
/// </summary>
[Test, Order(5)]
public void TestDeleteSong()
{
var id = 1;
// Get the count of songs
var count = _context.Songs.Count();
// Delete the song
_controller.DeleteSong(id);
// Check the count is one less.
Assert.AreEqual(count - 1, _context.Songs.Count());
// Check that the song is not in the database.
Assert.IsFalse(_context.Songs.Any(s => s.Id == id));
}

[Test, Order(6)]
public void TestEditSong()
{
// Get the song to edit
var song = _context.Songs.First();
// Edit the song
song.Title = "Edited Title";
song.Artist = "Edited Artist";
song.Album = "Edited Album";
song.Url = "https://peasenet.com/edited";
// Save the song
_controller.EditSong(song.Id,song);
// Check that the song is in the database.
Assert.IsTrue(_context.Songs.Any(s => s.Id == song.Id));
// Check that the song is the same as the edited song.
Assert.AreEqual(song.Title, _context.Songs.First(s => s.Id == song.Id).Title);
Assert.AreEqual(song.Artist, _context.Songs.First(s => s.Id == song.Id).Artist);
Assert.AreEqual(song.Album, _context.Songs.First(s => s.Id == song.Id).Album);
Assert.AreEqual(song.Url, _context.Songs.First(s => s.Id == song.Id).Url);
Assert.False(song.IsDownloaded());
}

/// <summary>
/// Deletes a list of songs based off of id.
/// </summary>
[Test, Order(7)]
public void TestDeleteList()
{
// Get the count of songs
var count = _context.Songs.Count();
// Get the songs in the database
var songs = _context.Songs.ToList();
// Get the Ids of the songs to delete
var ids = new List<int>();
for (var i = 0; i < songs.Count; i++)
{
ids.Add(songs[i].Id);
}
// Delete the songs
_controller.DeleteSongList(ids);
// Check that the count is count-ids less
Assert.AreEqual(count - ids.Count, _context.Songs.Count());
// Check that the songs are not in the database.
foreach (var id in ids)
{
Assert.IsFalse(_context.Songs.Any(s => s.Id == id));
}
}
}
23 changes: 23 additions & 0 deletions YouTubeDownloaderTests/YouTubeDownloaderTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<RootNamespace>YouTubeDownlaoderTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\YouTubeDownloader\YouTubeDownloader.csproj" />
</ItemGroup>

</Project>

0 comments on commit 7044499

Please sign in to comment.