forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckpointDatabase.cpp
More file actions
140 lines (111 loc) · 5.5 KB
/
Copy pathCheckpointDatabase.cpp
File metadata and controls
140 lines (111 loc) · 5.5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <winget/CheckpointDatabase.h>
#include <Public/winget/Checkpoint.h>
using namespace std::string_literals;
using namespace TestCommon;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::SQLite;
using namespace AppInstaller::Checkpoints;
TEST_CASE("CheckpointDatabaseCreateLatestAndReopen", "[checkpointDatabase]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Version versionCreated;
// Create the database
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, Version::Latest());
versionCreated = database->GetVersion();
}
// Reopen the database
{
INFO("Trying with ReadWrite");
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile, SQLiteStorageBase::OpenDisposition::ReadWrite);
Version versionRead = database->GetVersion();
REQUIRE(versionRead == versionCreated);
}
}
TEST_CASE("CheckpointDatabase_WriteAndRemoveMetadata", "[checkpointDatabase]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string_view testCheckpointName = "testCheckpoint"sv;
std::string testCommand = "install";
std::string testClientVersion = "1.20.1234";
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
CheckpointDatabase::IdType checkpointId = database->AddCheckpoint(testCheckpointName);
database->SetDataValue(checkpointId, AutomaticCheckpointData::Command, {}, { testCommand });
database->SetDataValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}, { testClientVersion });
}
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
const auto& checkpointIds = database->GetCheckpointIds();
REQUIRE_FALSE(checkpointIds.empty());
auto checkpointId = checkpointIds[0];
REQUIRE(testCommand == database->GetDataFieldSingleValue(checkpointId, AutomaticCheckpointData::Command, {}));
REQUIRE(testClientVersion == database->GetDataFieldSingleValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}));
database->RemoveDataType(checkpointId, AutomaticCheckpointData::Command);
database->RemoveDataType(checkpointId, AutomaticCheckpointData::ClientVersion);
}
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
const auto& checkpointIds = database->GetCheckpointIds();
REQUIRE_FALSE(checkpointIds.empty());
auto checkpointId = checkpointIds[0];
REQUIRE(database->GetDataTypes(checkpointId).empty());
}
}
TEST_CASE("CheckpointDatabase_WriteContextData", "[checkpointDatabase]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string_view testCheckpoint = "testCheckpoint"sv;
std::string fieldName1 = "field1";
std::string fieldName2 = "field2";
std::string testValue1 = "value1";
std::string testValue2 = "value2";
std::string testValue3 = "value3";
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
CheckpointDatabase::IdType checkpointId = database->AddCheckpoint(testCheckpoint);
// Add multiple fields.
database->SetDataValue(checkpointId, AutomaticCheckpointData::Arguments, fieldName1, { testValue1 });
database->SetDataValue(checkpointId, AutomaticCheckpointData::Arguments, fieldName2, { testValue2, testValue3 });
}
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
const auto& checkpointIds = database->GetCheckpointIds();
REQUIRE_FALSE(checkpointIds.empty());
auto automaticCheckpointId = checkpointIds.back();
const auto& fieldNames = database->GetDataFieldNames(automaticCheckpointId, AutomaticCheckpointData::Arguments);
REQUIRE(fieldNames[0] == fieldName1);
REQUIRE(fieldNames[1] == fieldName2);
REQUIRE(testValue1 == database->GetDataFieldSingleValue(automaticCheckpointId, AutomaticCheckpointData::Arguments, fieldName1));
const auto& multiValues = database->GetDataFieldMultiValue(automaticCheckpointId, AutomaticCheckpointData::Arguments, fieldName2);
REQUIRE(testValue2 == multiValues[0]);
REQUIRE(testValue3 == multiValues[1]);
}
}
TEST_CASE("CheckpointDatabase_CheckpointOrder", "[checkpointDatabase]")
{
// Verifies that the checkpoints are shown in reverse order (latest first).
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
database->AddCheckpoint("firstCheckpoint"sv);
database->AddCheckpoint("secondCheckpoint"sv);
database->AddCheckpoint("thirdCheckpoint"sv);
}
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
const auto& checkpointIds = database->GetCheckpointIds();
REQUIRE(checkpointIds.size() == 3);
REQUIRE(checkpointIds[0] == 3);
REQUIRE(checkpointIds[1] == 2);
REQUIRE(checkpointIds[2] == 1);
}
}