Exemplo n.º 1
0
        public void FilesTest()
        {
            var path = Path.Combine(Playnite.PlayniteTests.TempPath, "dbapifiles.db");

            FileSystem.DeleteFile(path);

            var db = new GameDatabase();

            using (db.OpenDatabase(path))
            {
                var dbApi = new DatabaseAPI(db);
                Assert.AreEqual(0, dbApi.GetFiles().Count);

                db.AddFile("testid1", "testname1.png", new byte[] { 0, 1, 2 });
                db.AddFile("testid2", "testname2.png", new byte[] { 2, 1, 0 });

                var dbFiles = dbApi.GetFiles();
                Assert.AreEqual(2, dbFiles.Count);
                Assert.AreEqual("testid1", dbFiles[0].Id);
                Assert.AreEqual("testname1.png", dbFiles[0].Filename);
                Assert.AreEqual(3, dbFiles[0].Length);
                Assert.IsNotNull(dbFiles[0].Metadata["checksum"]);

                dbApi.RemoveFile(dbFiles[0].Id);
                Assert.AreEqual(1, dbApi.GetFiles().Count);

                var filePath = Path.Combine(Playnite.PlayniteTests.TempPath, "testname3.png");
                File.WriteAllBytes(filePath, new byte[] { 2, 1, 0 });

                var duplId = dbApi.AddFile("testid3", filePath);
                Assert.AreEqual("testid2", duplId);
                Assert.AreEqual(1, dbApi.GetFiles().Count);

                filePath = Path.Combine(Playnite.PlayniteTests.TempPath, "testFile2.png");
                FileSystem.DeleteFile(filePath);
                dbApi.SaveFile("testid2", filePath);
                Assert.IsTrue(File.Exists(filePath));
                Assert.AreEqual(3, File.ReadAllBytes(filePath).Count());
            }
        }