Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add removeAll() helper to C++ tests #2461

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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
51 changes: 51 additions & 0 deletions examples/cpp/testHelpersTests.cpp
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);
}