Lightweight 0.20260213.0
Loading...
Searching...
No Matches
TestHelpers.hpp
1#pragma once
2
3#include <Lightweight/SqlBackup.hpp>
4#include <Lightweight/SqlBackup/BatchManager.hpp>
5
6#include <filesystem>
7#include <functional>
8#include <iostream>
9#include <mutex>
10#include <vector>
11
12namespace Lightweight::SqlBackup::Tests
13{
14
15struct LambdaProgressManager: SqlBackup::ProgressManager
16{
17 mutable std::mutex mutex;
18 std::function<void(SqlBackup::Progress const&)> callback;
19
20 explicit LambdaProgressManager(std::function<void(SqlBackup::Progress const&)> cb):
21 callback(std::move(cb))
22 {
23 }
24
25 void Update(SqlBackup::Progress const& p) override
26 {
27 auto const lock = std::scoped_lock(mutex);
28 if (callback)
29 callback(p);
30 }
31
32 void AllDone() override
33 {
34 // do nothing
35 }
36};
37
38struct ScopedFileRemoved
39{
40 std::filesystem::path path;
41
42 void RemoveIfExists() const
43 {
44 std::error_code ec;
45 if (std::filesystem::exists(path, ec))
46 std::filesystem::remove(path, ec);
47 }
48
49 explicit ScopedFileRemoved(std::filesystem::path path):
50 path(std::move(path))
51 {
52 RemoveIfExists();
53 }
54
55 ~ScopedFileRemoved()
56 {
57 RemoveIfExists();
58 }
59
60 ScopedFileRemoved(ScopedFileRemoved const&) = delete;
61 ScopedFileRemoved& operator=(ScopedFileRemoved const&) = delete;
62 ScopedFileRemoved(ScopedFileRemoved&&) = delete;
63 ScopedFileRemoved& operator=(ScopedFileRemoved&&) = delete;
64};
65
66template <typename Executor>
67void RunBatchManagerTest(Executor&& executor,
68 std::vector<SqlColumnDeclaration> const& cols,
69 std::vector<std::vector<SqlBackup::BackupValue>> const& rows,
70 size_t capacity,
71 SqlServerType serverType = SqlServerType::UNKNOWN)
72{
73 Lightweight::detail::BatchManager bm(std::forward<Executor>(executor), cols, capacity, serverType);
74 for (auto const& row: rows)
75 bm.PushRow(row);
76 bm.Flush();
77}
78
79template <typename Executor>
80void RunBatchManagerBatchTest(Executor&& executor,
81 std::vector<SqlColumnDeclaration> const& cols,
82 std::vector<SqlBackup::ColumnBatch> const& batches,
83 size_t capacity,
84 SqlServerType serverType = SqlServerType::UNKNOWN)
85{
86 Lightweight::detail::BatchManager bm(std::forward<Executor>(executor), cols, capacity, serverType);
87 for (auto const& batch: batches)
88 bm.PushBatch(batch);
89 bm.Flush();
90}
91
92} // namespace Lightweight::SqlBackup::Tests