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

Decouple with a mocked service #2256

Merged
merged 7 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,8 @@ future<bool> CurrencyDataLoader::TryLoadDataFromWebAsync()
co_return false;
}

// TODO: determine if below getters are awaitables in production.
String ^ staticDataResponse = m_client.GetCurrencyMetadata();
String ^ allRatiosResponse = m_client.GetCurrencyRatios();
String ^ staticDataResponse = co_await m_client.GetCurrencyMetadata();
String ^ allRatiosResponse = co_await m_client.GetCurrencyRatios();
if (staticDataResponse == nullptr || allRatiosResponse == nullptr)
{
co_return false;
Expand Down
8 changes: 4 additions & 4 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ namespace CalculatorApp::ViewModel::DataLoaders
m_responseLanguage = responseLanguage;
}

Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadata() const
{
(void)m_responseLanguage; // to be used in production.
return ref new Platform::String(MockCurrencyStaticData);
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
}

Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatios() const
{
(void)m_sourceCurrencyCode; // to be used in production.
return ref new Platform::String(MockCurrencyConverterData);
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
}
} // namespace CalculatorApp::ViewModel::DataLoaders
26 changes: 24 additions & 2 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@
// Licensed under the MIT License.

#pragma once
#include <cassert>

namespace CalculatorApp::ViewModel::DataLoaders
{
template <class T>
struct MockAwaitable
{
T Value;

bool await_ready() const noexcept
{
return true;
}

void await_suspend(std::experimental::coroutine_handle<>) const noexcept
{
assert(false && "not implemented.");
}

T&& await_resume() noexcept
{
return std::forward<T>(Value);
tian-lt marked this conversation as resolved.
Show resolved Hide resolved
}
};

class CurrencyHttpClient
{
public:
static bool ForceWebFailure;
void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage);

Platform::String ^ GetCurrencyMetadata() const;
Platform::String ^ GetCurrencyRatios() const;
MockAwaitable<Platform::String ^> GetCurrencyMetadata() const;
tian-lt marked this conversation as resolved.
Show resolved Hide resolved
MockAwaitable<Platform::String ^> GetCurrencyRatios() const;

private:
Platform::String ^ m_sourceCurrencyCode;
Expand Down
8 changes: 4 additions & 4 deletions src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ namespace CalculatorApp::ViewModel::DataLoaders
m_responseLanguage = responseLanguage;
}

Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadata() const
{
if (ForceWebFailure)
{
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
}
(void)m_responseLanguage; // to be used in production.
return ref new Platform::String(MockCurrencyStaticData);
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
}

Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatios() const
{
if (ForceWebFailure)
{
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
}
(void)m_sourceCurrencyCode; // to be used in production.
return ref new Platform::String(MockCurrencyConverterData);
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
}
} // namespace CalculatorApp::ViewModel::DataLoaders
10 changes: 5 additions & 5 deletions src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ namespace CalculatorUnitTests

VERIFY_IS_TRUE(DeleteCurrencyCacheFiles());

VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value));
}

TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles();
Expand Down Expand Up @@ -205,7 +205,7 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist)
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);

VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value));

CurrencyDataLoader loader{ L"en-US" };

Expand All @@ -223,7 +223,7 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist)
DateTime now = Utils::GetUniversalSystemTime();
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);

VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));

CurrencyDataLoader loader{ L"en-US" };
Expand All @@ -243,7 +243,7 @@ TEST_METHOD(LoadFromCache_Fail_ResponseLanguageChanged)
// Tests always use en-US as response language. Insert a different lang-code to fail the test.
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheLangcodeKey, L"ar-SA");

VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));

CurrencyDataLoader loader{ L"en-US" };
Expand Down
Loading