Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
keiji committed Jan 15, 2022
1 parent be6c987 commit 9f4894f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ public async void UploadAsyncTests_Success()
var testZipFileName = "zip-file.zip";
var testTmpPath = Path.Combine("log", "tmp", "path");
var testSasToken = "test-sas-token";
var testZipFilePath = Path.Combine(testTmpPath, testZipFileName);

mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(new ApiResponse<LogStorageSas>(200, new LogStorageSas { SasToken = testSasToken }));

mockLogPathService.Setup(x => x.LogUploadingTmpPath).Returns(testTmpPath);
mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName))).ReturnsAsync(true);
mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath)).ReturnsAsync(true);

var result = await unitUnderTest.UploadAsync(testZipFileName);
var result = await unitUnderTest.UploadAsync(testZipFilePath);

Assert.True(result);
mockHttpDataService.Verify(x => x.GetLogStorageSas(), Times.Once());
mockStorageService.Verify(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName)), Times.Once());
mockStorageService.Verify(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath), Times.Once());
}

[Theory]
Expand Down Expand Up @@ -86,13 +87,14 @@ public async void UploadAsyncTests_UploadAsyncFailure()
var testZipFileName = "zip-file.zip";
var testTmpPath = Path.Combine("log", "tmp", "path");
var testSasToken = "test-sas-token";
var testZipFilePath = Path.Combine(testTmpPath, testZipFileName);

mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(new ApiResponse<LogStorageSas>(200, new LogStorageSas { SasToken = testSasToken }));

mockLogPathService.Setup(x => x.LogUploadingTmpPath).Returns(testTmpPath);
mockStorageService.Setup(x => x.UploadAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);

var result = await unitUnderTest.UploadAsync(testZipFileName);
var result = await unitUnderTest.UploadAsync(testZipFilePath);

Assert.False(result);
mockHttpDataService.Verify(x => x.GetLogStorageSas(), Times.Once());
Expand All @@ -107,11 +109,12 @@ public async void UploadAsyncTests_UnexpectedError()
var testZipFileName = "zip-file.zip";
var testTmpPath = Path.Combine("log", "tmp", "path");
var testSasToken = "test-sas-token";
var testZipFilePath = Path.Combine(testTmpPath, testZipFileName);

mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(new ApiResponse<LogStorageSas>(200, new LogStorageSas { SasToken = testSasToken }));
mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName))).ReturnsAsync(false);
mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath)).ReturnsAsync(false);

var result = await unitUnderTest.UploadAsync(testZipFileName);
var result = await unitUnderTest.UploadAsync(testZipFilePath);

Assert.False(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,57 @@ public void OnClickSendLogCommandTests()
mockNavigationService.Verify(x => x.NavigateAsync("SendLogConfirmationPage", It.IsAny<INavigationParameters>()), Times.Once());
}

[Fact]
public void OnClickSendLogCommand_CreateZipSuccess()
{
var testLogId = "test-log-id";
mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId);

var testZipFileName = "test-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName);
var testPublicZipFileName = "test-public-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());

unitUnderTest.OnClickSendLogCommand.Execute(null);

mockLogFileService.Verify(x => x.CreateLogId(), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once());

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null), Times.Never());

mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny<string>()), Times.Never());
}

[Fact]
public void OnClickSendLogCommand_CreateZipFailure()
{
var testLogId = "test-log-id";
mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId);

var testZipFileName = "test-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName);
mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns<string>(null);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());

unitUnderTest.OnClickSendLogCommand.Execute(null);

mockLogFileService.Verify(x => x.CreateLogId(), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once());

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), "OK", null), Times.Once());
}

[Fact]
public void OnClickEmailCommandTests_Success()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Acr.UserDialogs;
using Covid19Radar.Services.Logs;
using Covid19Radar.ViewModels;
using Covid19Radar.Views;
using Moq;
using Prism.Navigation;
using Xamarin.Forms;
Expand Down Expand Up @@ -46,56 +47,6 @@ private SendLogConfirmationPageViewModel CreateViewModel()
return vm;
}

[Fact]
public void InitializeTests_CreateZipSuccess()
{
var testLogId = "test-log-id";
mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId);

var testZipFileName = "test-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName);
var testPublicZipFileName = "test-public-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());

mockLogFileService.Verify(x => x.CreateLogId(), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once());

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null), Times.Never());

mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny<string>()), Times.Never());
}

[Fact]
public void InitializeTests_CreateZipFailure()
{
var testLogId = "test-log-id";
mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId);

var testZipFileName = "test-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName);
var testPublicZipFileName = "test-public-zip-file-name";
mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());

mockLogFileService.Verify(x => x.CreateLogId(), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once());
mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once());

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), "OK", null), Times.Once());

mockNavigationService.Verify(x => x.GoBackAsync(), Times.Once());
}

[Fact]
public void OnClickConfirmLogCommandTests_Success()
{
Expand All @@ -110,7 +61,7 @@ public void OnClickConfirmLogCommandTests_Success()
mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());
unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath));

mockUserDialogs.Invocations.Clear();
mockLogFileService.Invocations.Clear();
Expand Down Expand Up @@ -158,19 +109,19 @@ public void OnClickSendLogCommandTests_Success()
mockLogFileService.Setup(x => x.DeleteAllLogUploadingFiles()).Returns(true);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());
unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath));

mockUserDialogs.Invocations.Clear();
mockLogFileService.Invocations.Clear();

mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName)).ReturnsAsync(true);
mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFilePath)).ReturnsAsync(true);

unitUnderTest.OnClickSendLogCommand.Execute(null);

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null), Times.Never());
mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName), Times.Once());
mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFilePath), Times.Once());
mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Once());
var expectedParameters = new NavigationParameters { { "logId", testLogId } };
mockNavigationService.Verify(x => x.NavigateAsync("SendLogCompletePage?useModalNavigation=true/", expectedParameters), Times.Once());
Expand All @@ -190,19 +141,19 @@ public void OnClickSendLogCommandTests_Failure()
mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());
unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFileName));

mockUserDialogs.Invocations.Clear();
mockLogFileService.Invocations.Clear();

mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName)).ReturnsAsync(false);
mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFileName)).ReturnsAsync(false);

unitUnderTest.OnClickSendLogCommand.Execute(null);

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), "OK", null), Times.Once());
mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName), Times.Once());
mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFileName), Times.Once());
mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Never());
mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny<string>(), It.IsAny<NavigationParameters>()), Times.Never());
}
Expand All @@ -222,19 +173,21 @@ public void OnClickSendLogCommandTests_DeleteLogFalure()
mockLogFileService.Setup(x => x.DeleteAllLogUploadingFiles()).Returns(false);

var unitUnderTest = CreateViewModel();
unitUnderTest.Initialize(new NavigationParameters());
unitUnderTest.Initialize(
SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath)
);

mockUserDialogs.Invocations.Clear();
mockLogFileService.Invocations.Clear();

mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName)).ReturnsAsync(true);
mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFilePath)).ReturnsAsync(true);

unitUnderTest.OnClickSendLogCommand.Execute(null);

mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny<string>(), null), Times.Once());
mockUserDialogs.Verify(x => x.HideLoading(), Times.Once());
mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null), Times.Never());
mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName), Times.Once());
mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFilePath), Times.Once());
mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Once());
var expectedParameters = new NavigationParameters { { "logId", testLogId } };
mockNavigationService.Verify(x => x.NavigateAsync("SendLogCompletePage?useModalNavigation=true/", expectedParameters), Times.Once());
Expand Down

0 comments on commit 9f4894f

Please sign in to comment.