Refactored namespaces

This commit is contained in:
emeric
2024-03-12 08:32:08 +01:00
parent 487960b413
commit 4b7c4295ec
501 changed files with 12605 additions and 12631 deletions
+101 -99
View File
@@ -40,117 +40,119 @@
#include "database/Types.hpp"
#include "database/User.hpp"
template <typename T>
class [[nodiscard]] ScopedEntity
namespace lms::db::tests
{
public:
using IdType = typename T::IdType;
template <typename... Args>
ScopedEntity(Database::Session& session, Args&& ...args)
: _session{ session }
template <typename T>
class [[nodiscard]] ScopedEntity
{
auto transaction{ _session.createWriteTransaction() };
public:
using IdType = typename T::IdType;
auto entity{ _session.create<T>(std::forward<Args>(args)...) };
EXPECT_TRUE(entity);
_id = entity->getId();
}
template <typename... Args>
ScopedEntity(db::Session& session, Args&& ...args)
: _session{ session }
{
auto transaction{ _session.createWriteTransaction() };
~ScopedEntity()
auto entity{ _session.create<T>(std::forward<Args>(args)...) };
EXPECT_TRUE(entity);
_id = entity->getId();
}
~ScopedEntity()
{
auto transaction{ _session.createWriteTransaction() };
auto entity{ T::find(_session, _id) };
// could not be here due to "on delete cascade" constraints...
if (entity)
entity.remove();
}
ScopedEntity(const ScopedEntity&) = delete;
ScopedEntity(ScopedEntity&&) = delete;
ScopedEntity& operator=(const ScopedEntity&) = delete;
ScopedEntity& operator=(ScopedEntity&&) = delete;
typename T::pointer lockAndGet()
{
auto transaction{ _session.createReadTransaction() };
return get();
}
typename T::pointer get()
{
_session.checkReadTransaction();
auto entity{ T::find(_session, _id) };
EXPECT_TRUE(entity);
return entity;
}
typename T::pointer operator->()
{
return get();
}
IdType getId() const { return _id; }
private:
db::Session& _session;
IdType _id{};
};
using ScopedArtist = ScopedEntity<db::Artist>;
using ScopedCluster = ScopedEntity<db::Cluster>;
using ScopedClusterType = ScopedEntity<db::ClusterType>;
using ScopedMediaLibrary = ScopedEntity<db::MediaLibrary>;
using ScopedRelease = ScopedEntity<db::Release>;
using ScopedTrack = ScopedEntity<db::Track>;
using ScopedTrackList = ScopedEntity<db::TrackList>;
using ScopedUser = ScopedEntity<db::User>;
class ScopedFileDeleter final
{
auto transaction{ _session.createWriteTransaction() };
public:
ScopedFileDeleter(const std::filesystem::path& path) : _path{ path } {}
~ScopedFileDeleter() { std::filesystem::remove(_path); }
auto entity{ T::find(_session, _id) };
// could not be here due to "on delete cascade" constraints...
if (entity)
entity.remove();
}
private:
ScopedFileDeleter(const ScopedFileDeleter&) = delete;
ScopedFileDeleter(ScopedFileDeleter&&) = delete;
ScopedFileDeleter operator=(const ScopedFileDeleter&) = delete;
ScopedFileDeleter operator=(ScopedFileDeleter&&) = delete;
ScopedEntity(const ScopedEntity&) = delete;
ScopedEntity(ScopedEntity&&) = delete;
ScopedEntity& operator=(const ScopedEntity&) = delete;
ScopedEntity& operator=(ScopedEntity&&) = delete;
const std::filesystem::path _path;
};
typename T::pointer lockAndGet()
class TmpDatabase final
{
auto transaction{ _session.createReadTransaction() };
return get();
}
public:
TmpDatabase();
typename T::pointer get()
db::Db& getDb();
private:
const std::filesystem::path _tmpFile;
ScopedFileDeleter _fileDeleter;
db::Db _db;
};
class DatabaseFixture : public ::testing::Test
{
_session.checkReadTransaction();
public:
~DatabaseFixture();
auto entity{ T::find(_session, _id) };
EXPECT_TRUE(entity);
return entity;
}
public:
static void SetUpTestCase();
static void TearDownTestCase();
typename T::pointer operator->()
{
return get();
}
private:
void testDatabaseEmpty();
IdType getId() const { return _id; }
private:
Database::Session& _session;
IdType _id{};
};
using ScopedArtist = ScopedEntity<Database::Artist>;
using ScopedCluster = ScopedEntity<Database::Cluster>;
using ScopedClusterType = ScopedEntity<Database::ClusterType>;
using ScopedMediaLibrary = ScopedEntity<Database::MediaLibrary>;
using ScopedRelease = ScopedEntity<Database::Release>;
using ScopedTrack = ScopedEntity<Database::Track>;
using ScopedTrackList = ScopedEntity<Database::TrackList>;
using ScopedUser = ScopedEntity<Database::User>;
class ScopedFileDeleter final
{
public:
ScopedFileDeleter(const std::filesystem::path& path) : _path{ path } {}
~ScopedFileDeleter() { std::filesystem::remove(_path); }
private:
ScopedFileDeleter(const ScopedFileDeleter&) = delete;
ScopedFileDeleter(ScopedFileDeleter&&) = delete;
ScopedFileDeleter operator=(const ScopedFileDeleter&) = delete;
ScopedFileDeleter operator=(ScopedFileDeleter&&) = delete;
const std::filesystem::path _path;
};
class TmpDatabase final
{
public:
TmpDatabase();
Database::Db& getDb();
private:
const std::filesystem::path _tmpFile;
ScopedFileDeleter _fileDeleter;
Database::Db _db;
};
class DatabaseFixture : public ::testing::Test
{
public:
~DatabaseFixture();
public:
static void SetUpTestCase();
static void TearDownTestCase();
private:
void testDatabaseEmpty();
static inline std::unique_ptr<TmpDatabase> _tmpDb{};
public:
Database::Session session{ _tmpDb->getDb() };
};
static inline std::unique_ptr<TmpDatabase> _tmpDb{};
public:
db::Session session{ _tmpDb->getDb() };
};
}