winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

CheckpointDatabase.cpp (5629B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include <winget/CheckpointDatabase.h>
      6 #include <Public/winget/Checkpoint.h>
      7 
      8 using namespace std::string_literals;
      9 using namespace TestCommon;
     10 using namespace AppInstaller::Repository::Microsoft;
     11 using namespace AppInstaller::SQLite;
     12 using namespace AppInstaller::Checkpoints;
     13 
     14 TEST_CASE("CheckpointDatabaseCreateLatestAndReopen", "[checkpointDatabase]")
     15 {
     16     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
     17     INFO("Using temporary file named: " << tempFile.GetPath());
     18 
     19     Version versionCreated;
     20 
     21     // Create the database
     22     {
     23         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, Version::Latest());
     24         versionCreated = database->GetVersion();
     25     }
     26 
     27     // Reopen the database
     28     {
     29         INFO("Trying with ReadWrite");
     30         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile, SQLiteStorageBase::OpenDisposition::ReadWrite);
     31         Version versionRead = database->GetVersion();
     32         REQUIRE(versionRead == versionCreated);
     33     }
     34 }
     35 
     36 TEST_CASE("CheckpointDatabase_WriteAndRemoveMetadata", "[checkpointDatabase]")
     37 {
     38     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
     39     INFO("Using temporary file named: " << tempFile.GetPath());
     40 
     41     std::string_view testCheckpointName = "testCheckpoint"sv;
     42     std::string testCommand = "install";
     43     std::string testClientVersion = "1.20.1234";
     44 
     45     {
     46         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
     47         CheckpointDatabase::IdType checkpointId = database->AddCheckpoint(testCheckpointName);
     48         database->SetDataValue(checkpointId, AutomaticCheckpointData::Command, {}, { testCommand });
     49         database->SetDataValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}, { testClientVersion });
     50     }
     51 
     52     {
     53         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
     54         const auto& checkpointIds = database->GetCheckpointIds();
     55         REQUIRE_FALSE(checkpointIds.empty());
     56 
     57         auto checkpointId = checkpointIds[0];
     58         REQUIRE(testCommand == database->GetDataFieldSingleValue(checkpointId, AutomaticCheckpointData::Command, {}));
     59         REQUIRE(testClientVersion == database->GetDataFieldSingleValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}));
     60 
     61         database->RemoveDataType(checkpointId, AutomaticCheckpointData::Command);
     62         database->RemoveDataType(checkpointId, AutomaticCheckpointData::ClientVersion);
     63     }
     64 
     65     {
     66         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
     67         const auto& checkpointIds = database->GetCheckpointIds();
     68         REQUIRE_FALSE(checkpointIds.empty());
     69 
     70         auto checkpointId = checkpointIds[0];
     71         REQUIRE(database->GetDataTypes(checkpointId).empty());
     72     }
     73 }
     74 
     75 TEST_CASE("CheckpointDatabase_WriteContextData", "[checkpointDatabase]")
     76 {
     77     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
     78     INFO("Using temporary file named: " << tempFile.GetPath());
     79 
     80     std::string_view testCheckpoint = "testCheckpoint"sv;
     81 
     82     std::string fieldName1 = "field1";
     83     std::string fieldName2 = "field2";
     84 
     85     std::string testValue1 = "value1";
     86     std::string testValue2 = "value2";
     87     std::string testValue3 = "value3";
     88 
     89     {
     90         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
     91         CheckpointDatabase::IdType checkpointId = database->AddCheckpoint(testCheckpoint);
     92 
     93         // Add multiple fields.
     94         database->SetDataValue(checkpointId, AutomaticCheckpointData::Arguments, fieldName1, { testValue1 });
     95         database->SetDataValue(checkpointId, AutomaticCheckpointData::Arguments, fieldName2, { testValue2, testValue3 });
     96     }
     97 
     98     {
     99         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
    100         const auto& checkpointIds = database->GetCheckpointIds();
    101         REQUIRE_FALSE(checkpointIds.empty());
    102 
    103         auto automaticCheckpointId = checkpointIds.back();
    104 
    105         const auto& fieldNames = database->GetDataFieldNames(automaticCheckpointId, AutomaticCheckpointData::Arguments);
    106 
    107         REQUIRE(fieldNames[0] == fieldName1);
    108         REQUIRE(fieldNames[1] == fieldName2);
    109 
    110         REQUIRE(testValue1 == database->GetDataFieldSingleValue(automaticCheckpointId, AutomaticCheckpointData::Arguments, fieldName1));
    111 
    112         const auto& multiValues = database->GetDataFieldMultiValue(automaticCheckpointId, AutomaticCheckpointData::Arguments, fieldName2);
    113 
    114         REQUIRE(testValue2 == multiValues[0]);
    115         REQUIRE(testValue3 == multiValues[1]);
    116     }
    117 }
    118 
    119 TEST_CASE("CheckpointDatabase_CheckpointOrder", "[checkpointDatabase]")
    120 {
    121     // Verifies that the checkpoints are shown in reverse order (latest first).
    122     TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
    123     INFO("Using temporary file named: " << tempFile.GetPath());
    124 
    125     {
    126         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempFile, { 1, 0 });
    127         database->AddCheckpoint("firstCheckpoint"sv);
    128         database->AddCheckpoint("secondCheckpoint"sv);
    129         database->AddCheckpoint("thirdCheckpoint"sv);
    130     }
    131 
    132     {
    133         std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::Open(tempFile);
    134         const auto& checkpointIds = database->GetCheckpointIds();
    135         REQUIRE(checkpointIds.size() == 3);
    136         REQUIRE(checkpointIds[0] == 3);
    137         REQUIRE(checkpointIds[1] == 2);
    138         REQUIRE(checkpointIds[2] == 1);
    139     }
    140 }