mirror of
https://github.com/sebastiandine/Card-Collection-Manager-3.git
synced 2026-08-29 01:08:49 +00:00
patch: Patch/ygo set 25th (#13)
This commit is contained in:
@@ -16,9 +16,15 @@ namespace {
|
||||
class InMemoryRepo final : public ICollectionRepository<MagicCard> {
|
||||
public:
|
||||
Map storage;
|
||||
bool failLoad{false};
|
||||
bool failSave{false};
|
||||
|
||||
Result<Map> load(Game) override { return Result<Map>::ok(storage); }
|
||||
Result<Map> load(Game) override {
|
||||
if (failLoad) return Result<Map>::err("load failed");
|
||||
return Result<Map>::ok(storage);
|
||||
}
|
||||
Result<void> save(Game, const Map& m) override {
|
||||
if (failSave) return Result<void>::err("save failed");
|
||||
storage = m;
|
||||
return Result<void>::ok();
|
||||
}
|
||||
@@ -27,12 +33,14 @@ public:
|
||||
class StubImageStore final : public IImageStore {
|
||||
public:
|
||||
std::vector<std::pair<Game, std::string>> removed;
|
||||
bool failRemove{false};
|
||||
|
||||
Result<std::string> copyIn(Game, const std::filesystem::path&, const std::string& n) override {
|
||||
return Result<std::string>::ok(n);
|
||||
}
|
||||
Result<void> remove(Game g, const std::string& n) override {
|
||||
removed.emplace_back(g, n);
|
||||
if (failRemove) return Result<void>::err("remove failed for " + n);
|
||||
return Result<void>::ok();
|
||||
}
|
||||
std::filesystem::path resolvePath(Game, const std::string& n) const override {
|
||||
@@ -117,4 +125,72 @@ TEST_SUITE("CollectionService<MagicCard>") {
|
||||
|
||||
CHECK(svc.remove(Game::Magic, 12345).isErr());
|
||||
}
|
||||
|
||||
TEST_CASE("load errors are propagated by list and findById") {
|
||||
InMemoryRepo repo;
|
||||
repo.failLoad = true;
|
||||
StubImageStore store;
|
||||
CollectionService<MagicCard> svc{repo, store};
|
||||
|
||||
const auto listed = svc.list(Game::Magic);
|
||||
REQUIRE(listed.isErr());
|
||||
CHECK(listed.error() == "load failed");
|
||||
|
||||
const auto found = svc.findById(Game::Magic, 1);
|
||||
REQUIRE(found.isErr());
|
||||
CHECK(found.error() == "load failed");
|
||||
}
|
||||
|
||||
TEST_CASE("save errors are propagated by add and update") {
|
||||
InMemoryRepo repo;
|
||||
repo.failSave = true;
|
||||
StubImageStore store;
|
||||
CollectionService<MagicCard> svc{repo, store};
|
||||
|
||||
const auto addRes = svc.add(Game::Magic, makeCard("A"));
|
||||
REQUIRE(addRes.isErr());
|
||||
CHECK(addRes.error() == "save failed");
|
||||
|
||||
repo.failSave = false;
|
||||
const auto id = svc.add(Game::Magic, makeCard("B"));
|
||||
REQUIRE(id.isOk());
|
||||
|
||||
repo.failSave = true;
|
||||
MagicCard updated = makeCard("Renamed");
|
||||
updated.id = id.value();
|
||||
const auto updateRes = svc.update(Game::Magic, updated);
|
||||
REQUIRE(updateRes.isErr());
|
||||
CHECK(updateRes.error() == "save failed");
|
||||
}
|
||||
|
||||
TEST_CASE("findById returns nullopt for missing id") {
|
||||
InMemoryRepo repo;
|
||||
StubImageStore store;
|
||||
CollectionService<MagicCard> svc{repo, store};
|
||||
|
||||
const auto out = svc.findById(Game::Magic, 99);
|
||||
REQUIRE(out.isOk());
|
||||
CHECK_FALSE(out.value().has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("remove reports image cleanup issues but still removes card") {
|
||||
InMemoryRepo repo;
|
||||
StubImageStore store;
|
||||
store.failRemove = true;
|
||||
CollectionService<MagicCard> svc{repo, store};
|
||||
|
||||
const auto id = svc.add(
|
||||
Game::Magic, makeCard("With Images", {"a.png", "b.png"}));
|
||||
REQUIRE(id.isOk());
|
||||
|
||||
const auto removed = svc.remove(Game::Magic, id.value());
|
||||
REQUIRE(removed.isErr());
|
||||
CHECK(removed.error().find("Card removed but image cleanup had issues:") != std::string::npos);
|
||||
CHECK(removed.error().find("remove failed for a.png") != std::string::npos);
|
||||
CHECK(removed.error().find("remove failed for b.png") != std::string::npos);
|
||||
|
||||
const auto listed = svc.list(Game::Magic);
|
||||
REQUIRE(listed.isOk());
|
||||
CHECK(listed.value().empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user