From 14b6940cb4716e70461133ab0b5b13b4c53cf7f9 Mon Sep 17 00:00:00 2001 From: Bishop S Johnson Date: Thu, 17 Aug 2023 18:59:20 -0700 Subject: [PATCH 1/2] - Added `HasVariable()` member-function to `IGraphQlRequest` - Added `RemoveVariable()` member-function to `IGraphQlRequest` --- .../EnjinPlatformSdk/IGraphQlRequest.hpp | 13 +++++ .../internal/GraphQlRequestBase.hpp | 22 +++++++ tests/unit/graphql/GraphQlRequestBaseTest.cpp | 58 +++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/IGraphQlRequest.hpp b/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/IGraphQlRequest.hpp index d044832..8349b2b 100644 --- a/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/IGraphQlRequest.hpp +++ b/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/IGraphQlRequest.hpp @@ -41,6 +41,13 @@ class IGraphQlRequest : virtual public IGraphQlUploadHolder [[nodiscard]] virtual const std::map& GetVariablesWithoutTypes() const = 0; + /// \brief Determines whether this request has a variable of the given name. + /// \param name The variable name. + /// \return Whether this request has the variable. + [[maybe_unused]] + [[nodiscard]] + virtual bool HasVariable(const std::string& name) const = 0; + /// \brief Determines whether this request has variables. /// \return Whether this request has variables. [[maybe_unused]] @@ -64,6 +71,12 @@ class IGraphQlRequest : virtual public IGraphQlRequest<> /// \return This request for chaining. [[maybe_unused]] virtual TRequest& SetVariable(std::string name, std::string type, SerializablePtr value) = 0; + + /// \brief Removes a variable with the given name from this request. + /// \param name The variable name. + /// \return This request for chaining. + [[maybe_unused]] + virtual TRequest& RemoveVariable(const std::string& name) = 0; }; /// \brief Interface for GraphQL requests with a settable fragment for response data. diff --git a/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/internal/GraphQlRequestBase.hpp b/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/internal/GraphQlRequestBase.hpp index 5645590..f562257 100644 --- a/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/internal/GraphQlRequestBase.hpp +++ b/libs/EnjinPlatformSdk/include/EnjinPlatformSdk/internal/GraphQlRequestBase.hpp @@ -66,6 +66,13 @@ class GraphQlRequestBase : public GraphQlParameterHolder, return _variablesWithoutTypes; } + [[maybe_unused]] + [[nodiscard]] + bool HasVariable(const std::string& name) const override + { + return _variables.contains(name); + } + [[maybe_unused]] [[nodiscard]] bool HasVariables() const override @@ -73,6 +80,7 @@ class GraphQlRequestBase : public GraphQlParameterHolder, return !_variables.empty(); } + [[maybe_unused]] TRequest& SetVariable(std::string name, std::string type, SerializablePtr value) override { if (value == nullptr) @@ -91,6 +99,18 @@ class GraphQlRequestBase : public GraphQlParameterHolder, return static_cast(*this); } + [[maybe_unused]] + TRequest& RemoveVariable(const std::string& name) override + { + if (_variables.contains(name)) + { + _variables.erase(name); + _variablesWithoutTypes.erase(name); + } + + return static_cast(*this); + } + // endregion IGraphQlRequest protected: @@ -98,6 +118,7 @@ class GraphQlRequestBase : public GraphQlParameterHolder, /// \param name The name of the request. /// \param type The type of the request. /// \throws std::out_of_range If request type is not a valid value. + [[maybe_unused]] GraphQlRequestBase(std::string name, GraphQlRequestType type) : _name(std::move(name)), _type(type) @@ -122,6 +143,7 @@ class GraphQlRequestBase : public GraphQlParameterHolder, /// \brief Appends the necessary beginning portion for the compiled request to the given string stream, leaving only /// the closing brace and any fragment fields to be appended. /// \param ss The string stream to write to. + [[maybe_unused]] void AppendHeader(std::stringstream& ss) const { ss << _typeName; diff --git a/tests/unit/graphql/GraphQlRequestBaseTest.cpp b/tests/unit/graphql/GraphQlRequestBaseTest.cpp index 7413a48..951b41f 100644 --- a/tests/unit/graphql/GraphQlRequestBaseTest.cpp +++ b/tests/unit/graphql/GraphQlRequestBaseTest.cpp @@ -149,6 +149,31 @@ TEST_F(GraphQlRequestBaseTest, GetVariablesWithoutTypesWhenRequestHasVariablesRe ASSERT_THAT(actual, Eq(expected)); } +TEST_F(GraphQlRequestBaseTest, HasVariableWhenRequestDoesNotHaveTheGivenVariableReturnsFalse) +{ + // Arrange + const std::string name("var"); + + // Act + const bool actual = classUnderTest->HasVariable(name); + + // Assert + ASSERT_THAT(actual, IsFalse()); +} + +TEST_F(GraphQlRequestBaseTest, HasVariableWhenRequestHasTheGivenVariableReturnsTrue) +{ + // Arrange + const std::string name("var"); + classUnderTest->SetVariable(name, "String!", mockVariableValue); + + // Act + const bool actual = classUnderTest->HasVariable(name); + + // Assert + ASSERT_THAT(actual, IsTrue()); +} + TEST_F(GraphQlRequestBaseTest, HasVariablesWhenRequestHasNoVariablesReturnsFalse) { // Act @@ -169,3 +194,36 @@ TEST_F(GraphQlRequestBaseTest, HasVariablesWhenRequestHasVariablesReturnsTrue) // Assert ASSERT_TRUE(actual); } + +TEST_F(GraphQlRequestBaseTest, SetVariableVariableIsSetForRequest) +{ + // Arrange + const std::string name("var"); + const std::string type("type"); + const std::shared_ptr value = mockVariableValue; + + // Act + classUnderTest->SetVariable(name, type, value); + + // Assert + ASSERT_THAT(classUnderTest->GetVariablesWithoutTypes(), Contains(Key(name))); + ASSERT_THAT(classUnderTest->GetVariablesWithoutTypes().find(name)->second, Eq(value)); +} + +TEST_F(GraphQlRequestBaseTest, RemoveVariableWhenVariableIsSetRemovesVariable) +{ + // Arrange + const std::string name("var"); + classUnderTest->SetVariable(name, "type", mockVariableValue); + + // Assumptions + ASSERT_THAT(classUnderTest->GetVariablesWithoutTypes(), Contains(Key(name))) + << "Assume that variable is set"; + + // Act + classUnderTest->RemoveVariable(name); + + // Assert + ASSERT_THAT(classUnderTest->GetVariablesWithoutTypes(), Not(Contains(Key(name)))) + << "Assume that variable was removed"; +} From 5ce5bf68db4818abe546c0758fadebbafb77fcda Mon Sep 17 00:00:00 2001 From: Bishop S Johnson Date: Thu, 17 Aug 2023 19:02:32 -0700 Subject: [PATCH 2/2] Fixed handling of `CryptoSignatureType::None` in `SetCryptoSignatureType()` member-function of `VerifyAccount` --- .../src/schema/mutations/VerifyAccount.cpp | 9 +- tests/unit/schema/CMakeLists.txt | 1 + tests/unit/schema/mutations/CMakeLists.txt | 3 + .../schema/mutations/VerifyAccountTest.cpp | 101 ++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/unit/schema/mutations/CMakeLists.txt create mode 100644 tests/unit/schema/mutations/VerifyAccountTest.cpp diff --git a/libs/EnjinPlatformSdk/src/schema/mutations/VerifyAccount.cpp b/libs/EnjinPlatformSdk/src/schema/mutations/VerifyAccount.cpp index 321ba7a..7a37b5c 100644 --- a/libs/EnjinPlatformSdk/src/schema/mutations/VerifyAccount.cpp +++ b/libs/EnjinPlatformSdk/src/schema/mutations/VerifyAccount.cpp @@ -44,10 +44,17 @@ VerifyAccount& VerifyAccount::SetAccount(SerializableStringPtr account) [[maybe_unused]] VerifyAccount& VerifyAccount::SetCryptoSignatureType(const CryptoSignatureType cryptoSignatureType) { + constexpr char name[] = "cryptoSignatureType"; + + if (cryptoSignatureType == CryptoSignatureType::None) + { + return RequestType::RemoveVariable(name); + } + std::string s = enjin::platform::sdk::ToString(cryptoSignatureType); SerializableStringPtr sPtr = std::make_shared(std::move(s)); - return RequestType::SetVariable("cryptoSignatureType", CoreTypes::CryptoSignatureType, std::move(sPtr)); + return RequestType::SetVariable(name, CoreTypes::CryptoSignatureType, std::move(sPtr)); } VerifyAccount& VerifyAccount::operator=(const VerifyAccount& rhs) = default; diff --git a/tests/unit/schema/CMakeLists.txt b/tests/unit/schema/CMakeLists.txt index 9d6a869..5316e3a 100644 --- a/tests/unit/schema/CMakeLists.txt +++ b/tests/unit/schema/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(mutations) add_subdirectory(queries) diff --git a/tests/unit/schema/mutations/CMakeLists.txt b/tests/unit/schema/mutations/CMakeLists.txt new file mode 100644 index 0000000..ec9972f --- /dev/null +++ b/tests/unit/schema/mutations/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${TESTS_NAME} + PRIVATE + VerifyAccountTest.cpp) diff --git a/tests/unit/schema/mutations/VerifyAccountTest.cpp b/tests/unit/schema/mutations/VerifyAccountTest.cpp new file mode 100644 index 0000000..c3da246 --- /dev/null +++ b/tests/unit/schema/mutations/VerifyAccountTest.cpp @@ -0,0 +1,101 @@ +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "EnjinPlatformSdk/CryptoSignatureType.hpp" +#include "EnjinPlatformSdk/VerifyAccount.hpp" +#include + +using namespace enjin::platform::sdk; +using namespace testing; + +class VerifyAccountTestSuite +{ +public: + static inline const std::string CryptoSignatureTypeName = "cryptoSignatureType"; +}; + +class VerifyAccountTest : public Test, + public VerifyAccountTestSuite +{ +public: + std::unique_ptr classUnderTest; + +protected: + void SetUp() override + { + Test::SetUp(); + + classUnderTest = std::make_unique(); + } +}; + +class VerifyAccountCryptoSignatureTypeTest : public TestWithParam, + public VerifyAccountTestSuite +{ +public: + std::unique_ptr classUnderTest; + +protected: + void SetUp() override + { + TestWithParam::SetUp(); + + classUnderTest = std::make_unique(); + } +}; + +TEST_F(VerifyAccountTest, SetCryptoSignatureTypeWhenVariableIsNotSetAndGivenNoneDoesNotSetTheVariable) +{ + // Arrange + const CryptoSignatureType cryptoSignatureType = CryptoSignatureType::None; + + // Assumptions + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsFalse()) + << "Assume that the '" << CryptoSignatureTypeName << "' variable is not set"; + + // Act + classUnderTest->SetCryptoSignatureType(cryptoSignatureType); + + // Assert + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsFalse()) + << "Assert that the '" << CryptoSignatureTypeName << "' variable was not set"; +} + +TEST_F(VerifyAccountTest, SetCryptoSignatureTypeWhenVariableIsSetAndGivenNoneRemovesTheVariable) +{ + // Arrange + const CryptoSignatureType cryptoSignatureType = CryptoSignatureType::None; + classUnderTest->SetCryptoSignatureType(CryptoSignatureType::Ed25519); + + // Assumptions + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsTrue()) + << "Assume that the '" << CryptoSignatureTypeName << "' variable is set"; + + // Act + classUnderTest->SetCryptoSignatureType(cryptoSignatureType); + + // Assert + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsFalse()) + << "Assert that the '" << CryptoSignatureTypeName << "' variable was removed"; +} + +TEST_P(VerifyAccountCryptoSignatureTypeTest, SetCryptoSignatureTypeWhenGivenNonNoneTypeVariableIsSet) +{ + // Arrange + const CryptoSignatureType cryptoSignatureType = GetParam(); + + // Assumptions + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsFalse()) + << "Assume that the '" << CryptoSignatureTypeName << "' variable is not set"; + + // Act + classUnderTest->SetCryptoSignatureType(cryptoSignatureType); + + // Assert + ASSERT_THAT(classUnderTest->HasVariable(CryptoSignatureTypeName), IsTrue()) + << "Assert that the '" << CryptoSignatureTypeName << "' variable was set"; +} + +INSTANTIATE_TEST_SUITE_P(NonNoneCryptoSignatureType, + VerifyAccountCryptoSignatureTypeTest, + Values(CryptoSignatureType::Ed25519, + CryptoSignatureType::Sr25519));