Skip to content

Commit

Permalink
updated test with File System IO mock
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallet committed Jan 4, 2020
1 parent 66d1ef1 commit 30928b1
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 96 deletions.
7 changes: 7 additions & 0 deletions EspionSpotify.Tests/EspionSpotify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Moq, Version=4.9.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.9.0\lib\net45\Moq.dll</HintPath>
</Reference>
Expand All @@ -63,6 +64,12 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.7.1.1\lib\net40\System.IO.Abstractions.dll</HintPath>
</Reference>
<Reference Include="System.IO.Abstractions.TestingHelpers, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.TestingHelpers.7.1.1\lib\net40\System.IO.Abstractions.TestingHelpers.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
Expand Down
24 changes: 12 additions & 12 deletions EspionSpotify.Tests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class IntExtensionsTests
[InlineData("a", null)]
[InlineData("0", 0)]
[InlineData("1", 1)]
private void StringToNullableInt_ReturnsExpectedInt(string value, int? expected)
internal void StringToNullableInt_ReturnsExpectedInt(string value, int? expected)
{
var actual = value.ToNullableInt();
Assert.Equal(expected, actual);
Expand All @@ -35,7 +35,7 @@ public ResourceManagerTests()
}

[Fact]
private void GetString_ReturnsExpectedTranslatedSpyString()
internal void GetString_ReturnsExpectedTranslatedSpyString()
{
Assert.Equal("Spy", _rm.GetString(TranslationKeys.tabRecord));
}
Expand All @@ -53,7 +53,7 @@ public class StringExtensionsTest
[InlineData("medium", AlbumCoverSize.medium)]
[InlineData("large", AlbumCoverSize.large)]
[InlineData("extralarge", AlbumCoverSize.extralarge)]
private void StringToAlbumCoverSize_ReturnsExpectedCover(string value, AlbumCoverSize? expected)
internal void StringToAlbumCoverSize_ReturnsExpectedCover(string value, AlbumCoverSize? expected)
{
var actual = value.ToAlbumCoverSize();
Assert.Equal(expected, actual);
Expand All @@ -70,7 +70,7 @@ private void StringToAlbumCoverSize_ReturnsExpectedCover(string value, AlbumCove
[InlineData("1.0.10", "1.0.10")]
[InlineData("1.2.3.4", "1.2.3.4")]
[InlineData("v1.2.3.4", "1.2.3.4")]
private void StringToVersionAsString(string value, string expected)
internal void StringToVersionAsString(string value, string expected)
{
var actual = value.ToVersionAsString();
Assert.Equal(expected, actual);
Expand All @@ -84,7 +84,7 @@ private void StringToVersionAsString(string value, string expected)
[InlineData("v1")]
[InlineData("1")]
[InlineData("version1")]
private void StringToVersion_ReturnsNull(string value)
internal void StringToVersion_ReturnsNull(string value)
{
var actual = value.ToVersion();
Assert.Null(actual);
Expand All @@ -97,7 +97,7 @@ private void StringToVersion_ReturnsNull(string value)
[InlineData("1.0.10", 1, 0, 10)]
[InlineData("1.2.3.4", 1, 2, 3, 4)]
[InlineData("v1.2.3.4", 1, 2, 3, 4)]
private void StringToVersion_ReturnsVersion(string value, int major, int minor, int? build = null, int? revision = null)
internal void StringToVersion_ReturnsVersion(string value, int major, int minor, int? build = null, int? revision = null)
{
var actual = value.ToVersion();
if (build == null) Assert.Equal(new Version(major, minor), actual);
Expand All @@ -109,15 +109,15 @@ private void StringToVersion_ReturnsVersion(string value, int major, int minor,
public class LinqExtensionsTest
{
[Fact]
private void EmptyArrayDecimalMedian_ReturnsMedianDecimal()
internal void EmptyArrayDecimalMedian_ReturnsMedianDecimal()
{
var value = new double[] {};

Assert.Throws<InvalidOperationException>(() => value.Median());
}

[Fact]
private void PeerArrayDecimalMedian_ReturnsMedianDecimal()
internal void PeerArrayDecimalMedian_ReturnsMedianDecimal()
{
var value = new double[] { 2.8, 1.4, 1.1, 0.8, -0.4, 1.1, 2.4, 7.77 };
var expected = 1.25;
Expand All @@ -128,7 +128,7 @@ private void PeerArrayDecimalMedian_ReturnsMedianDecimal()
}

[Fact]
private void OddArrayDecimalMedian_ReturnsMedianDecimal()
internal void OddArrayDecimalMedian_ReturnsMedianDecimal()
{
var value = new double[] { 5.5, 0.9, 1.1, 0.8, -0.4, 1.11, 0.004, 2.4, 7.77 };
var expected = 1.1;
Expand All @@ -139,7 +139,7 @@ private void OddArrayDecimalMedian_ReturnsMedianDecimal()
}

[Fact]
private void ArrayIntMedian_ReturnsMedianDecimal()
internal void ArrayIntMedian_ReturnsMedianDecimal()
{
var value = new int[] { 5, 3, 6, 0, -1, 1, 0, 2, 7 };
var expected = 2;
Expand All @@ -150,7 +150,7 @@ private void ArrayIntMedian_ReturnsMedianDecimal()
}

[Fact]
private void LinqArrayDoubleMedian_ReturnsMedianDecimal()
internal void LinqArrayDoubleMedian_ReturnsMedianDecimal()
{
var value = new LinqArrayDouble[] {
new LinqArrayDouble { value = 5.5 },
Expand All @@ -164,7 +164,7 @@ private void LinqArrayDoubleMedian_ReturnsMedianDecimal()
Assert.Equal(expected, actual);
}

private class LinqArrayDouble
internal class LinqArrayDouble
{
internal double value { get; set; }
}
Expand Down
128 changes: 75 additions & 53 deletions EspionSpotify.Tests/FileManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using EspionSpotify.Models;
using NAudio.Lame;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Text.RegularExpressions;
using Xunit;

namespace EspionSpotify.Tests
{
public class FileManagerTests
{
private UserSettings _userSettings;
private Track _track;
private FileManager _fileManager;
private readonly UserSettings _userSettings;
private readonly Track _track;
private readonly FileManager _fileManager;
private readonly IFileSystem _fileSystem;
private readonly string _path = @"C:\path";

private readonly string _windowsExlcudedChars = $"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()))}]";

public FileManagerTests()
{
_userSettings = new UserSettings
{
OutputPath = AppDomain.CurrentDomain.BaseDirectory,
OutputPath = _path,
Bitrate = LAMEPreset.ABR_128,
MediaFormat = Enums.MediaFormat.Mp3,
MinimumRecordedLengthSeconds = 30,
Expand All @@ -42,110 +47,127 @@ public FileManagerTests()
Ad = false
};

_fileManager = new FileManager(_userSettings, _track);
_fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"C:\path\Empty Artist", new MockDirectoryData() },
{ @"C:\path\Artist\Delete Me.mp3", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) }
});

_fileManager = new FileManager(_userSettings, _track, _fileSystem);
}

[Theory]
[InlineData("Artist - Title", 1, null, "Artist - Title.mp3")]
[InlineData("002 Artist - Title", 1, null, "002 Artist - Title.mp3")]
[InlineData("001_Artist_-_Title", 1, null, "001_Artist_-_Title.mp3")]
[InlineData("Artist - Title", 2, null, "Artist - Title 2.mp3")]
[InlineData("Artist_-_Title", 4, "C:\\path", "C:\\path\\Artist_-_Title 4.mp3")]
[InlineData("001 Title", 1, "C:\\path\\Artist", "C:\\path\\Artist\\001 Title.mp3")]
[InlineData("Artist - Title", 1, "C:\\path", "C:\\path\\Artist - Title.mp3")]
private void GetPathName_ReturnsTrackNameWithMediaFormat(string songName, int count, string path, string expectedResult)
[Fact]
internal void GetOutputFile_ReturnsFileNames()
{
var fileName = _fileManager.GetPathName(songName, count, path);
var outputFile = _fileManager.GetOutputFile(_path);

Assert.Equal(expectedResult, fileName);
Assert.Equal(_path, outputFile.Path);
Assert.Equal(_track.ToString(), outputFile.File);
Assert.Equal(_userSettings.MediaFormat.ToString().ToLower(), outputFile.Extension);
Assert.Equal(_userSettings.TrackTitleSeparator, outputFile.Separator);

Assert.Equal(@"C:\path\Artist - Title - Live.spytify", outputFile.ToPendingFileString());
Assert.Equal(@"C:\path\Artist - Title - Live.mp3.spytify", outputFile.ToTranscodingToMP3String());
Assert.Equal(@"C:\path\Artist - Title - Live.mp3", outputFile.ToString());
}

[Theory]
[InlineData("C:\\path\\Artist", "C:\\path\\Artist\\Artist - Title - Live.mp3")]
[InlineData("C:\\path", "C:\\path\\Artist - Title - Live.mp3")]
private void BuildFileName_ReturnsFileName(string path, string expectedResult)
[Fact]
internal void BuildFileName_ReturnsFileName()
{
var fileName = _fileManager.BuildFileName(path);
var fileName = _fileManager.GetOutputFile(_path).ToString();

Assert.Equal(expectedResult, fileName);
Assert.Equal(@"C:\path\Artist - Title - Live.mp3", fileName);
}

[Theory]
[InlineData("C:\\path\\Artist", "C:\\path\\Artist\\100_Artist_-_Title_-_Live.mp3")]
[InlineData("C:\\path", "C:\\path\\100_Artist_-_Title_-_Live.mp3")]
private void BuildFileName_ReturnsFileNameOrderNumbered(string path, string expectedResult)
[Fact]
internal void BuildFileName_ReturnsFileNameOrderNumbered()
{
_userSettings.OrderNumberInfrontOfFileEnabled = true;
_userSettings.TrackTitleSeparator = "_";
_userSettings.InternalOrderNumber = 100;

var fileName = _fileManager.BuildFileName(path);
var fileName = _fileManager.GetOutputFile(_path).ToString();

Assert.Equal(expectedResult, fileName);
Assert.Equal(@"C:\path\100_Artist_-_Title_-_Live.mp3", fileName);
}

[Theory]
[InlineData("C:\\path", true, "C:\\path\\Artist\\Title - Live.mp3")]
[InlineData("C:\\path", false, "C:\\path\\Artist - Title - Live.mp3")]
private void BuildFileName_ReturnsFileNameGroupByFolders(string path, bool isGroupByFolders, string expectedResult)
[InlineData(true, @"C:\path\Artist\Title - Live.mp3")]
[InlineData(false, @"C:\path\Artist - Title - Live.mp3")]
internal void BuildFileName_ReturnsFileNameGroupByFolders(bool isGroupByArtistFolder, string expectedResult)
{
_userSettings.GroupByFoldersEnabled = isGroupByFolders;
_userSettings.GroupByFoldersEnabled = isGroupByArtistFolder;

var fileName = _fileManager.BuildFileName(path);
var fileName = _fileManager.GetOutputFile(_path).ToString();

Assert.Equal(expectedResult, fileName);
}

[Fact]
private void BuildFileName_ReturnsFileNameWav()
internal void BuildFileName_ReturnsFileNameWav()
{
_userSettings.MediaFormat = Enums.MediaFormat.Wav;

var fileName = _fileManager.BuildFileName("C:\\path");
var fileName = _fileManager.GetOutputFile(_path).ToString();

Assert.Equal("C:\\path\\Artist - Title - Live.wav", fileName);
Assert.Equal(@"C:\path\Artist - Title - Live.wav", fileName);
}


[Fact(Skip = "Windows")]
private void GetFolderPath_ReturnsNoArtistFolderPath()
[Fact]
internal void GetFolderPath_ReturnsNoArtistFolderPath()
{
var artistFolder = FileManager.GetFolderPath(_track, _userSettings);
var artistFolder = FileManager.GetFolderPath(_track, _userSettings, _fileSystem);

Assert.Null(artistFolder);
}

[Fact(Skip = "Windows")]
private void GetFolderPath_ReturnsArtistFolderPath()
[Fact]
internal void GetFolderPath_ReturnsArtistFolderPath()
{
_userSettings.GroupByFoldersEnabled = true;

var artistDir = Regex.Replace(_track.Artist, _windowsExlcudedChars, string.Empty);

var artistFolder = FileManager.GetFolderPath(_track, _userSettings);
var artistFolder = FileManager.GetFolderPath(_track, _userSettings, _fileSystem);

Assert.Equal($"\\{artistDir}", artistFolder);
Assert.Equal($@"\{artistDir}", artistFolder);
}

[Fact(Skip = "Windows")]
private void IsPathFileNameExists_ReturnsExists()
[Fact]
internal void IsPathFileNameExists_ReturnsExists()
{
var result = FileManager.IsPathFileNameExists(_track, _userSettings);
var result = FileManager.IsPathFileNameExists(_track, _userSettings, _fileSystem);
Assert.False(result);
}

[Fact(Skip = "Windows")]
private void DeleteFile_DeletesFolderWhenGroupByFoldersEnabled()
[Fact]
internal void DeleteFile_DeletesFile()
{
_track.Title = "Delete Me";
_track.TitleExtended = "";

var extension = _userSettings.MediaFormat.ToString().ToLower();
var currentFile = $@"{_userSettings.OutputPath}\{_track.ToString()}.{extension}";

_fileManager.DeleteFile(currentFile);

Assert.False(_fileSystem.File.Exists(currentFile));
}

[Fact]
internal void DeleteFile_DeletesFolderWhenGroupByFoldersEnabled()
{
var artistDir = Regex.Replace(_track.Artist, _windowsExlcudedChars, string.Empty);
_userSettings.GroupByFoldersEnabled = true;
_track.Artist = "Empty Artist";

var currentFile = $"{_userSettings.OutputPath}//{artistDir}//{_track.ToString()}";
var artistDir = Regex.Replace(_track.Artist, _windowsExlcudedChars, string.Empty);
var extension = _userSettings.MediaFormat.ToString().ToLower();
var currentFile = $@"{_userSettings.OutputPath}\{artistDir}\{_track.ToTitleString()}.{extension}";

_fileManager.DeleteFile(currentFile);

Assert.False(Directory.Exists($"{_userSettings.OutputPath}//{artistDir}"));
Assert.False(File.Exists(currentFile));
Assert.False(_fileSystem.Directory.Exists($@"{_userSettings.OutputPath}\{artistDir}"));
Assert.False(_fileSystem.File.Exists(currentFile));
}
}
}
2 changes: 1 addition & 1 deletion EspionSpotify.Tests/LastFMAPITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public LastFMAPITests()
}

[Fact]
private void MapLastFMAPITrackToTrack_ReturnsExpectedTrack()
internal void MapLastFMAPITrackToTrack_ReturnsExpectedTrack()
{
var track = new Track();

Expand Down
10 changes: 5 additions & 5 deletions EspionSpotify.Tests/SpotifyAPITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SpotifyAPITests()
}

[Fact]
private void MapSpotifyTrackToTrack_ReturnsExpectedTrack()
internal void MapSpotifyTrackToTrack_ReturnsExpectedTrack()
{
var fulltrack = new FullTrack()
{
Expand All @@ -41,7 +41,7 @@ private void MapSpotifyTrackToTrack_ReturnsExpectedTrack()
}

[Fact]
private void MapSpotifyAlbumToTrackMissingAll_ReturnsExpectedTrack()
internal void MapSpotifyAlbumToTrackMissingAll_ReturnsExpectedTrack()
{
var fullAlbum = new FullAlbum()
{
Expand All @@ -64,7 +64,7 @@ private void MapSpotifyAlbumToTrackMissingAll_ReturnsExpectedTrack()
}

[Fact]
private void MapSpotifyAlbumToTrackMissingImages_ReturnsExpectedTrack()
internal void MapSpotifyAlbumToTrackMissingImages_ReturnsExpectedTrack()
{
var fullAlbum = new FullAlbum()
{
Expand Down Expand Up @@ -92,7 +92,7 @@ private void MapSpotifyAlbumToTrackMissingImages_ReturnsExpectedTrack()
}

[Fact]
private void MapSpotifyAlbumToTrackMissingImageSizes_ReturnsExpectedTrack()
internal void MapSpotifyAlbumToTrackMissingImageSizes_ReturnsExpectedTrack()
{
var fullAlbum = new FullAlbum()
{
Expand Down Expand Up @@ -134,7 +134,7 @@ private void MapSpotifyAlbumToTrackMissingImageSizes_ReturnsExpectedTrack()
}

[Fact]
private void MapFullSpotifyAlbumToTrack_ReturnsExpectedTrack()
internal void MapFullSpotifyAlbumToTrack_ReturnsExpectedTrack()
{
var fullAlbum = new FullAlbum()
{
Expand Down
Loading

0 comments on commit 30928b1

Please sign in to comment.