generated from jellyfin/jellyfin-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b8a8a7
commit 37192bf
Showing
3 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
Viperinius.Plugin.SpotifyImport.Tests/Db/DbRepositoryWrapper.cs
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,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.Sqlite; | ||
using Viperinius.Plugin.SpotifyImport.Utils; | ||
|
||
namespace Viperinius.Plugin.SpotifyImport.Tests.Db | ||
{ | ||
internal class DbRepositoryWrapper : DbRepository | ||
{ | ||
public DbRepositoryWrapper(string path) : base(path) | ||
{ | ||
} | ||
|
||
public SqliteConnection WrappedConnection => Connection; | ||
|
||
public static DbRepositoryWrapper GetInstance() | ||
{ | ||
return new DbRepositoryWrapper(":memory:"); | ||
} | ||
} | ||
} |
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,80 @@ | ||
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Viperinius.Plugin.SpotifyImport.Tests.Db | ||
{ | ||
public class InitTests | ||
{ | ||
private static Dictionary<string, List<(string, string)>> _tableCols = new Dictionary<string, List<(string, string)>> | ||
{ | ||
{ "ProviderPlaylists", [("Id", "INTEGER"), ("ProviderId", "TEXT"), ("PlaylistId", "TEXT"), ("LastState", "TEXT"), ("LastTimestamp", "TEXT")] }, | ||
{ "ProviderTracks", [("Id", "INTEGER"), ("ProviderId", "TEXT"), ("TrackId", "TEXT"), ("Name", "TEXT"), ("AlbumName", "TEXT"), ("AlbumArtistNames", "TEXT"), ("ArtistNames", "TEXT"), ("Number", "INTEGER"), ("IsrcId", "TEXT")] }, | ||
{ "ProviderPlaylistTracks", [("Id", "INTEGER"), ("PlaylistId", "INTEGER"), ("TrackId", "INTEGER"), ("Position", "INTEGER")] }, | ||
}; | ||
|
||
[Fact] | ||
public void ReturnsPath() | ||
{ | ||
using var db = DbRepositoryWrapper.GetInstance(); | ||
Assert.Equal(":memory:", db.Path); | ||
} | ||
|
||
[Fact] | ||
public void SupportsForeignKeys() | ||
{ | ||
using var db = DbRepositoryWrapper.GetInstance(); | ||
using var cmd = db.WrappedConnection.CreateCommand(); | ||
cmd.CommandText = "PRAGMA foreign_keys;"; | ||
var result = (long?)cmd.ExecuteScalar(); | ||
Assert.Equal(1, result); | ||
} | ||
|
||
[Fact] | ||
public void SetupTables() | ||
{ | ||
using var db = DbRepositoryWrapper.GetInstance(); | ||
using var cmd = db.WrappedConnection.CreateCommand(); | ||
cmd.CommandText = "SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%'"; | ||
var tableRowCount = 0; | ||
using (var reader = cmd.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
tableRowCount++; | ||
} | ||
} | ||
|
||
Assert.Equal(0, tableRowCount); | ||
|
||
db.InitDb(); | ||
|
||
using (var reader = cmd.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
tableRowCount++; | ||
var colRowCount = 0; | ||
var name = reader.GetString(0); | ||
Assert.True(_tableCols.ContainsKey(name)); | ||
|
||
using var colCmd = db.WrappedConnection.CreateCommand(); | ||
colCmd.CommandText = $"SELECT name, type FROM pragma_table_info('{name}')"; | ||
using var colReader = colCmd.ExecuteReader(); | ||
while (colReader.Read()) | ||
{ | ||
colRowCount++; | ||
Assert.Contains((colReader.GetString(0), colReader.GetString(1)), _tableCols[name]); | ||
} | ||
} | ||
} | ||
|
||
Assert.Equal(_tableCols.Count, tableRowCount); | ||
} | ||
} | ||
} |
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