2022-04-19 22:58:18 +00:00
|
|
|
|
using System;
|
|
|
|
|
using IdentityServer4.EntityFramework.Options;
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Start.Server.Data;
|
|
|
|
|
using Start.Server.Models;
|
|
|
|
|
|
2022-04-20 22:34:13 +00:00
|
|
|
|
namespace Start_Tests.Server {
|
2022-04-19 22:58:18 +00:00
|
|
|
|
public class UnitTestWithDb : IDisposable {
|
|
|
|
|
private const string InMemoryConnectionString = "DataSource=:memory:";
|
|
|
|
|
private SqliteConnection _connection;
|
|
|
|
|
|
2022-04-20 22:34:13 +00:00
|
|
|
|
protected string TestUserId { get; } = "test_user";
|
|
|
|
|
protected string InvalidUserId { get; } = "invalid_user";
|
|
|
|
|
protected BookmarkContainer TestBookmarkContainer { get; set; }
|
|
|
|
|
protected BookmarkGroup TestBookmarkGroup { get; set; }
|
|
|
|
|
protected Bookmark TestBookmark { get; set; }
|
|
|
|
|
|
2022-04-19 22:58:18 +00:00
|
|
|
|
protected readonly ApplicationDbContext _db;
|
|
|
|
|
|
|
|
|
|
public UnitTestWithDb() {
|
|
|
|
|
_connection = new SqliteConnection(InMemoryConnectionString);
|
|
|
|
|
_connection.Open();
|
|
|
|
|
|
|
|
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
|
|
|
.UseSqlite(_connection)
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
this._db = new ApplicationDbContext(options,
|
|
|
|
|
Options.Create(new OperationalStoreOptions()));
|
|
|
|
|
this._db.Database.EnsureCreated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void ResetDb() {
|
|
|
|
|
_db.Database.EnsureDeleted();
|
|
|
|
|
_db.Database.EnsureCreated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void FillDbTestData() {
|
|
|
|
|
ApplicationUser testUser = new ApplicationUser {
|
2022-04-20 22:34:13 +00:00
|
|
|
|
Id = this.TestUserId,
|
|
|
|
|
UserName = "test_user_name"
|
2022-04-19 22:58:18 +00:00
|
|
|
|
};
|
|
|
|
|
_db.Users.Add(testUser);
|
|
|
|
|
_db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
BookmarkContainer testContainer = new BookmarkContainer(testUser.Id, "Test Container",
|
|
|
|
|
0);
|
|
|
|
|
_db.BookmarkContainers.Add(testContainer);
|
|
|
|
|
_db.SaveChanges();
|
2022-04-20 22:34:13 +00:00
|
|
|
|
this.TestBookmarkContainer = testContainer;
|
2022-04-19 22:58:18 +00:00
|
|
|
|
|
|
|
|
|
BookmarkGroup testGroup = new BookmarkGroup("Test Group", "#000000", 0,
|
|
|
|
|
testContainer.BookmarkContainerId);
|
|
|
|
|
_db.BookmarkGroups.Add(testGroup);
|
|
|
|
|
_db.SaveChanges();
|
2022-04-20 22:34:13 +00:00
|
|
|
|
this.TestBookmarkGroup = testGroup;
|
2022-04-19 22:58:18 +00:00
|
|
|
|
|
|
|
|
|
Bookmark testBookmark = new Bookmark("Test Bookmark", "http://example.com",
|
|
|
|
|
"Test Notes", 0, testGroup.BookmarkGroupId);
|
|
|
|
|
_db.Bookmarks.Add(testBookmark);
|
|
|
|
|
_db.SaveChanges();
|
2022-04-20 22:34:13 +00:00
|
|
|
|
this.TestBookmark = testBookmark;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void ResetAndFillDb() {
|
|
|
|
|
this.ResetDb();
|
|
|
|
|
this.FillDbTestData();
|
2022-04-19 22:58:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks the DB connection works. Note that MSTest won't run this - you need to do so in
|
|
|
|
|
/// inheriting classes like this:
|
|
|
|
|
///
|
|
|
|
|
/// <code>
|
|
|
|
|
/// [TestMethod]
|
|
|
|
|
/// public override void TestDatabaseOK() {
|
|
|
|
|
/// base.TestDatabaseOK();
|
|
|
|
|
/// }
|
|
|
|
|
/// </code>
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public virtual void TestDatabaseOK() {
|
|
|
|
|
Assert.IsTrue(this._db.Database.CanConnect());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
_connection.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|