Skip to content

Commit

Permalink
Add removeAll() helper to C++ tests (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbush committed Jan 4, 2023
1 parent a200570 commit 3ef228b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Expand Up @@ -19,6 +19,6 @@ FetchContent_Declare(

FetchContent_MakeAvailable(Catch2 cpprealm)

add_executable(examples examples.cpp define-object-model.cpp authentication.cpp)
add_executable(examples testHelpers.hpp examples.cpp define-object-model.cpp authentication.cpp testHelpersTests.cpp)
target_link_libraries(examples PRIVATE Catch2::Catch2WithMain)
target_link_libraries(examples PRIVATE cpprealm)
5 changes: 2 additions & 3 deletions examples/cpp/examples.cpp
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <thread>
#include <future>
#include "testHelpers.hpp"

// :snippet-start: includes
#include <cpprealm/sdk.hpp>
Expand Down Expand Up @@ -68,9 +69,7 @@ TEST_CASE("first test case", "[test]") {
});
// :snippet-end:
// Clean up after the test
realm.write([&realm, &dog] {
realm.remove(dog);
});
removeAll(realm);
}

TEST_CASE("create a dog", "[write]") {
Expand Down
25 changes: 25 additions & 0 deletions examples/cpp/testHelpers.hpp
@@ -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
51 changes: 51 additions & 0 deletions examples/cpp/testHelpersTests.cpp
@@ -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);
}

0 comments on commit 3ef228b

Please sign in to comment.