Skip to content

Commit

Permalink
mock web failures
Browse files Browse the repository at this point in the history
  • Loading branch information
tian-lt committed Nov 27, 2024
1 parent 39124be commit bc880f0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace

namespace CalculatorApp::ViewModel::DataLoaders
{
bool CurrencyHttpClient::ForceWebFailure = false;
void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage)
{
m_sourceCurrencyCode = sourceCurrencyCode;
Expand Down
1 change: 1 addition & 0 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace CalculatorApp::ViewModel::DataLoaders
class CurrencyHttpClient
{
public:
static bool ForceWebFailure;
void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage);

Platform::String ^ GetCurrencyMetadata() const;
Expand Down
3 changes: 1 addition & 2 deletions src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@
<ClInclude Include="..\CalcViewModel\Common\TraceLogger.h" />
<ClInclude Include="..\CalcViewModel\Common\Utils.h" />
<ClInclude Include="..\CalcViewModel\DataLoaders\CurrencyDataLoader.h" />
<ClInclude Include="..\CalcViewModel\DataLoaders\CurrencyHttpClient.h" />
<ClInclude Include="..\CalcViewModel\DataLoaders\UnitConverterDataConstants.h" />
<ClInclude Include="..\CalcViewModel\DataLoaders\UnitConverterDataLoader.h" />
<ClInclude Include="..\CalcViewModel\DateCalculatorViewModel.h" />
Expand Down Expand Up @@ -377,7 +376,7 @@
<ClCompile Include="..\CalcViewModel\Snapshots.cpp" />
<ClCompile Include="..\CalcViewModel\StandardCalculatorViewModel.cpp" />
<ClCompile Include="..\CalcViewModel\UnitConverterViewModel.cpp" />
<ClCompile Include="CurrencyHttpClient.cpp" />
<ClCompile Include="DataLoaders\CurrencyHttpClient.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CalcManager\CalcManager.vcxproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

#include "pch.h"
#include "CalcViewModel/DataLoaders/CurrencyHttpClient.h"
#include "DataLoaders/CurrencyHttpClient.h"

namespace
{
Expand All @@ -14,6 +14,7 @@ namespace

namespace CalculatorApp::ViewModel::DataLoaders
{
bool CurrencyHttpClient::ForceWebFailure = false;
void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage)
{
m_sourceCurrencyCode = sourceCurrencyCode;
Expand All @@ -22,12 +23,20 @@ namespace CalculatorApp::ViewModel::DataLoaders

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);
}

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);
}
Expand Down
36 changes: 25 additions & 11 deletions src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ class DataLoadedCallback : public UnitConversionManager::IViewModelCurrencyCallb

namespace CalculatorUnitTests
{
namespace
{
template <class F>
auto ScopeGuard(F&& f)
{
struct ScopeExit
{
explicit ScopeExit(F&& ef)
: ExitFunctor(std::forward<F>(ef))
{
}
~ScopeExit()
{
ExitFunctor();
}
F ExitFunctor;
};
return ScopeExit{ std::forward<F>(f) };
}
}

constexpr auto sc_Language_EN = L"en-US";

String^ SerializeContent(const std::vector<String^>& data)
Expand Down Expand Up @@ -165,6 +186,8 @@ TEST_METHOD(LoadFromCache_Fail_OlderThanADay)
dayOld.UniversalTime = now.UniversalTime - CurrencyDataLoaderConstants::DayDuration - 1;
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, dayOld);

auto guard = ScopeGuard([] { CurrencyHttpClient::ForceWebFailure = false; });
CurrencyHttpClient::ForceWebFailure = true;
CurrencyDataLoader loader{ L"en-US" };

bool didLoad = loader.TryLoadDataFromCacheAsync().get();
Expand Down Expand Up @@ -245,19 +268,10 @@ TEST_METHOD(LoadFromCache_Success)
VERIFY_IS_TRUE(loader.LoadedFromCache());
}

TEST_METHOD(LoadFromWeb_Fail_ClientIsNullptr)
{
CurrencyDataLoader loader{ L"en-US" };

bool didLoad = loader.TryLoadDataFromWebAsync().get();

VERIFY_IS_FALSE(didLoad);
VERIFY_IS_FALSE(loader.LoadFinished());
VERIFY_IS_FALSE(loader.LoadedFromWeb());
}

TEST_METHOD(LoadFromWeb_Fail_WebException)
{
auto guard = ScopeGuard([] { CurrencyHttpClient::ForceWebFailure = false; });
CurrencyHttpClient::ForceWebFailure = true;
CurrencyDataLoader loader{ L"en-US" };

bool didLoad = loader.TryLoadDataFromWebAsync().get();
Expand Down

0 comments on commit bc880f0

Please sign in to comment.