Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add removeAll() helper to C++ tests (#2461)
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef testHelpers_h | ||
#define testHelpers_h | ||
|
||
#include <cpprealm/sdk.hpp> | ||
|
||
/** | ||
Removes all objects of all types from a realm. | ||
*/ | ||
template<typename... Ts> | ||
void removeAll(realm::db<Ts...> &realm) { | ||
// Iterate over the template parameter pack (Ts...) | ||
([&realm] { | ||
// For each T in realm, fetch all objects of type T | ||
auto objects = realm.template objects<Ts>(); | ||
realm.write([&realm, &objects] { | ||
// Iterate over the results to delete each object (at time of writing, | ||
// realm.removeAll() and realm.remove(collection) do not exist) | ||
for (auto object : objects) { | ||
realm.template remove(object); | ||
} | ||
}); | ||
}(), ...); // This is called a "fold expression" | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include "testHelpers.hpp" | ||
|
||
struct SyncDog : realm::object { | ||
realm::persisted<realm::uuid> _id; | ||
realm::persisted<std::string> name; | ||
realm::persisted<int> age; | ||
|
||
static constexpr auto schema = realm::schema("SyncDog", | ||
realm::property<&SyncDog::_id, true>("_id"), | ||
realm::property<&SyncDog::name>("name"), | ||
realm::property<&SyncDog::age>("age")); | ||
}; | ||
|
||
struct Dog : realm::object { | ||
realm::persisted<std::string> name; | ||
realm::persisted<int> age; | ||
|
||
static constexpr auto schema = realm::schema("Dog", | ||
realm::property<&Dog::name>("name"), | ||
realm::property<&Dog::age>("age")); | ||
}; | ||
|
||
struct Person : realm::object { | ||
realm::persisted<std::string> _id; | ||
realm::persisted<std::string> name; | ||
realm::persisted<int> age; | ||
realm::persisted<std::optional<Dog>> dog; | ||
static constexpr auto schema = realm::schema("Person", | ||
realm::property<&Person::_id, true>("_id"), // primary key | ||
realm::property<&Person::name>("name"), | ||
realm::property<&Person::age>("age"), | ||
realm::property<&Person::dog>("dog")); | ||
}; | ||
|
||
TEST_CASE("removeAll removes all", "[meta]") { | ||
auto realm = realm::open<Person, Dog, SyncDog>(); | ||
realm.write([&realm] { | ||
realm.add(Dog { .name = "Rex", .age = 1 }); | ||
realm.add(Person { ._id = "123", .name = "Ali", .age = 1 }); | ||
realm.add(SyncDog { ._id = realm::uuid{}, .name = "Cyber Rex", .age = 1 }); | ||
}); | ||
REQUIRE(realm.objects<Dog>().size() > 0); | ||
REQUIRE(realm.objects<Person>().size() > 0); | ||
REQUIRE(realm.objects<SyncDog>().size() > 0); | ||
removeAll(realm); | ||
REQUIRE(realm.objects<Dog>().size() == 0); | ||
REQUIRE(realm.objects<Person>().size() == 0); | ||
REQUIRE(realm.objects<SyncDog>().size() == 0); | ||
} |