-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConnectionManagerTests.cs
104 lines (87 loc) · 4.42 KB
/
ConnectionManagerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using NUnit.Framework;
using SpotifyAPI.Web;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tests.Util;
using static Backend.ConnectionManager;
namespace Backend.Tests
{
public class ConnectionManagerTests : BaseTests
{
private const string DST_FOLDER_NAME = "ChangeDatabaseFolderTest";
private const string USER_ID = "ChangeDatabaseTestUserId";
[SetUp]
public override void SetUp()
{
base.SetUp();
DataContainer.Instance.Clear();
// change folder back to base path
ConnectionManager.Instance.ChangeDatabaseFolder(Directory.GetCurrentDirectory());
// remove previous run files
if (Directory.Exists(DST_FOLDER_NAME))
Directory.Delete(DST_FOLDER_NAME, true);
File.Delete($"{USER_ID}.sqlite");
}
[Test]
public void ChangeDatabaseFolder_NotLoggedIn()
{
var dstFolderPath = Path.Combine(Directory.GetCurrentDirectory(), DST_FOLDER_NAME);
// dst folder does not exist
Assert.AreEqual(ChangeDatabaseFolderResult.Failed, ConnectionManager.Instance.ChangeDatabaseFolder(dstFolderPath));
Directory.CreateDirectory(DST_FOLDER_NAME);
Assert.AreEqual(ChangeDatabaseFolderResult.ChangedPath, ConnectionManager.Instance.ChangeDatabaseFolder(dstFolderPath));
Assert.AreEqual(dstFolderPath, File.ReadAllText(ConnectionManager.DB_FOLDER_FILE));
Assert.AreEqual(dstFolderPath, ConnectionManager.Instance.DbPath);
}
[Test]
public void ChangeDatabaseFolder_CopiedToNewFolder()
{
InitSpotify(user: new PrivateUser { Id = USER_ID });
// check if dbfile exists
Assert.IsTrue(File.Exists(DataContainer.Instance.DbFileName));
// insert some data
DatabaseOperations.AddTag(new Entities.Tag { Name = "TestTag" });
Assert.AreEqual(1, DatabaseOperations.GetTagsWithGroups().Count);
// change directory
Directory.CreateDirectory(DST_FOLDER_NAME);
var dstFolderPath = Path.Combine(Directory.GetCurrentDirectory(), DST_FOLDER_NAME);
Assert.AreEqual(ChangeDatabaseFolderResult.CopiedToNewFolder, ConnectionManager.Instance.ChangeDatabaseFolder(dstFolderPath));
// check if dbfile was copied
Assert.IsTrue(File.Exists(DataContainer.Instance.DbFileName));
Assert.IsTrue(File.Exists(Path.Combine(dstFolderPath, DataContainer.Instance.DbFileName)));
// check if data is preserved
Assert.AreEqual(1, DatabaseOperations.GetTagsWithGroups().Count);
// in new directory there should be 1 file
Assert.AreEqual(1, Directory.GetFiles(DST_FOLDER_NAME).Length);
}
[Test]
public void ChangeDatabaseFolder_WithOverwriting()
{
InitSpotify(user: new PrivateUser { Id = USER_ID });
// check if dbfile exists
Assert.IsTrue(File.Exists(DataContainer.Instance.DbFileName));
// insert some data
DatabaseOperations.AddTag(new Entities.Tag { Name = "TestTag" });
Assert.AreEqual(1, DatabaseOperations.GetTagsWithGroups().Count);
// copy it to output directory
Directory.CreateDirectory(DST_FOLDER_NAME);
File.Copy(DataContainer.Instance.DbFileName, Path.Combine(DST_FOLDER_NAME, DataContainer.Instance.DbFileName));
// add some more data
DatabaseOperations.AddTag(new Entities.Tag { Name = "TestTag2" });
// change directory
var dstFolderPath = Path.Combine(Directory.GetCurrentDirectory(), DST_FOLDER_NAME);
Assert.AreEqual(ChangeDatabaseFolderResult.UseExistingDbInNewFolder, ConnectionManager.Instance.ChangeDatabaseFolder(dstFolderPath));
// check if dbfile was copied
Assert.IsTrue(File.Exists(DataContainer.Instance.DbFileName));
Assert.IsTrue(File.Exists(Path.Combine(dstFolderPath, DataContainer.Instance.DbFileName)));
// check if data is preserved
Assert.AreEqual(1, DatabaseOperations.GetTagsWithGroups().Count);
// in new directory there should be 1 file (the existing db file)
Assert.AreEqual(1, Directory.GetFiles(DST_FOLDER_NAME).Length);
}
}
}