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

fix: Parse.ParseUser.LogOutAsync optimization #403

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 35 additions & 3 deletions Parse.Tests/AnalyticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,46 @@ namespace Parse.Tests;
[TestClass]
public class AnalyticsTests
{
#warning Skipped post-test-evaluation cleaning method may be needed.

// [TestCleanup]
// public void TearDown() => (Client.Services as ServiceHub).Reset();
private Mock<IParseAnalyticsController> _mockAnalyticsController;
private Mock<IParseCurrentUserController> _mockCurrentUserController;
private MutableServiceHub _hub;
private ParseClient _client;


[TestInitialize]
public void Initialize()
{
_mockAnalyticsController = new Mock<IParseAnalyticsController>();
_mockCurrentUserController = new Mock<IParseCurrentUserController>();

_mockCurrentUserController
.Setup(controller => controller.GetCurrentSessionTokenAsync(It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.ReturnsAsync("sessionToken");


_hub = new MutableServiceHub
{
AnalyticsController = _mockAnalyticsController.Object,
CurrentUserController = _mockCurrentUserController.Object
};
_client = new ParseClient(new ServerConnectionData { Test = true }, _hub);
}

[TestCleanup]
public void Cleanup()
{
_mockAnalyticsController = null;
_mockCurrentUserController = null;
_hub = null;
_client = null;
}


[TestMethod]
public async Task TestTrackEvent()
{

// Arrange
var hub = new MutableServiceHub();
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
Expand Down
121 changes: 70 additions & 51 deletions Parse.Tests/CloudControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

using System;
using System.Collections.Generic;
using System.Net;
Expand All @@ -13,53 +14,75 @@

namespace Parse.Tests;

#warning Class refactoring requires completion.

[TestClass]
public class CloudControllerTests
{
ParseClient Client { get; set; }
private Mock<IParseCommandRunner> _mockRunner;
private ParseCloudCodeController _cloudCodeController;
private ParseClient Client { get; set; }

[TestInitialize]
public void SetUp() => Client = new ParseClient(new ServerConnectionData { ApplicationID = "", Key = "", Test = true });
public void SetUp()
{
Client = new ParseClient(new ServerConnectionData { ApplicationID = "", Key = "", Test = true });
_mockRunner = new Mock<IParseCommandRunner>();
}

[TestCleanup]
public void Cleanup()
{
_mockRunner = null;
_cloudCodeController = null;
Client = null;
}


[TestMethod]
public async Task TestEmptyCallFunction()
{
// Arrange: Create a mock runner that simulates a response with an accepted status but no data
var mockRunner = CreateMockRunner(
new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, null)
);
// Arrange: Setup mock runner and controller

_mockRunner.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, null)));

_cloudCodeController = new ParseCloudCodeController(_mockRunner.Object, Client.Decoder);

var controller = new ParseCloudCodeController(mockRunner.Object, Client.Decoder);

// Act & Assert: Call the function and verify the task faults as expected
try
{
await controller.CallFunctionAsync<string>("someFunction", null, null, Client, CancellationToken.None);
await _cloudCodeController.CallFunctionAsync<string>("someFunction", null, null, Client, CancellationToken.None);
Assert.Fail("Expected the task to fault, but it succeeded.");
}
catch (ParseFailureException ex)
{
Assert.AreEqual(ParseFailureException.ErrorCode.OtherCause, ex.Code);
Assert.AreEqual("Cloud function returned no data.", ex.Message);
}

}


[TestMethod]
public async Task TestCallFunction()
{
// Arrange: Create a mock runner with a predefined response
// Arrange: Setup mock runner and controller with a response
var responseDict = new Dictionary<string, object> { ["result"] = "gogo" };
var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, responseDict);
var mockRunner = CreateMockRunner(response);
_mockRunner.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, responseDict)));

_cloudCodeController = new ParseCloudCodeController(_mockRunner.Object, Client.Decoder);

var cloudCodeController = new ParseCloudCodeController(mockRunner.Object, Client.Decoder);

// Act: Call the function and capture the result
var result = await cloudCodeController.CallFunctionAsync<string>(
var result = await _cloudCodeController.CallFunctionAsync<string>(
"someFunction",
parameters: null,
sessionToken: null,
Expand All @@ -76,24 +99,29 @@ public async Task TestCallFunction()
[TestMethod]
public async Task TestCallFunctionWithComplexType()
{
// Arrange: Create a mock runner with a complex type response
// Arrange: Setup mock runner and controller with a complex type response
var complexResponse = new Dictionary<string, object>
{
{ "result", new Dictionary<string, object>
{
{ "fosco", "ben" },
{ "list", new List<object> { 1, 2, 3 } }
}
{
{ "fosco", "ben" },
{ "list", new List<object> { 1, 2, 3 } }
}
}
};
var mockRunner = CreateMockRunner(
new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, complexResponse)
);

var cloudCodeController = new ParseCloudCodeController(mockRunner.Object, Client.Decoder);
_mockRunner.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, complexResponse)));

_cloudCodeController = new ParseCloudCodeController(_mockRunner.Object, Client.Decoder);


// Act: Call the function with a complex return type
var result = await cloudCodeController.CallFunctionAsync<IDictionary<string, object>>(
var result = await _cloudCodeController.CallFunctionAsync<IDictionary<string, object>>(
"someFunction",
parameters: null,
sessionToken: null,
Expand All @@ -107,25 +135,32 @@ public async Task TestCallFunctionWithComplexType()
Assert.AreEqual("ben", result["fosco"]);
Assert.IsInstanceOfType(result["list"], typeof(IList<object>));
}

[TestMethod]
public async Task TestCallFunctionWithWrongType()
{
// a mock runner with a response that doesn't match the expected type

var wrongTypeResponse = new Dictionary<string, object>
{
{ "result", "gogo" }
};
var mockRunner = CreateMockRunner(
new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, wrongTypeResponse)
);
{
{ "result", "gogo" }
};

_mockRunner.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, wrongTypeResponse)));

var cloudCodeController = new ParseCloudCodeController(mockRunner.Object, Client.Decoder);

_cloudCodeController = new ParseCloudCodeController(_mockRunner.Object, Client.Decoder);

// Act & Assert: Expect the call to fail with a ParseFailureException || This is fun!

await Assert.ThrowsExceptionAsync<ParseFailureException>(async () =>
{
await cloudCodeController.CallFunctionAsync<int>(
await _cloudCodeController.CallFunctionAsync<int>(
"someFunction",
parameters: null,
sessionToken: null,
Expand All @@ -134,20 +169,4 @@ await cloudCodeController.CallFunctionAsync<int>(
);
});
}



private Mock<IParseCommandRunner> CreateMockRunner(Tuple<HttpStatusCode, IDictionary<string, object>> response)
{
var mockRunner = new Mock<IParseCommandRunner>();
mockRunner.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
)).Returns(Task.FromResult(response));

return mockRunner;
}

}
}
96 changes: 46 additions & 50 deletions Parse.Tests/CloudTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -19,80 +20,78 @@ namespace Parse.Tests;
[TestClass]
public class CloudTests
{
#warning Skipped post-test-evaluation cleaning method may be needed.
private Mock<IParseCommandRunner> _commandRunnerMock;
private Mock<IParseDataDecoder> _decoderMock;
private MutableServiceHub _hub;
private ParseClient _client;

// [TestCleanup]
// public void TearDown() => ParseCorePlugins.Instance.Reset();
[TestMethod]
public async Task TestCloudFunctionsMissingResultAsync()
[TestInitialize]
public void Initialize()
{
// Arrange
var commandRunnerMock = new Mock<IParseCommandRunner>();
var decoderMock = new Mock<IParseDataDecoder>();
_commandRunnerMock = new Mock<IParseCommandRunner>();
_decoderMock = new Mock<IParseDataDecoder>();
}

[TestCleanup]
public void Cleanup()
{
_commandRunnerMock = null;
_decoderMock = null;
_hub = null;
_client = null;

}



// Mock CommandRunner
commandRunnerMock
private void SetupMocksForMissingResult()
{
_commandRunnerMock
.Setup(runner => runner.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
))
.ReturnsAsync(new Tuple<System.Net.HttpStatusCode, IDictionary<string, object>>(
System.Net.HttpStatusCode.OK,
.ReturnsAsync(new Tuple<HttpStatusCode, IDictionary<string, object>>(
HttpStatusCode.OK,
new Dictionary<string, object>
{
["unexpectedKey"] = "unexpectedValue" // Missing "result" key
}));

// Mock Decoder
decoderMock
_decoderMock
.Setup(decoder => decoder.Decode(It.IsAny<object>(), It.IsAny<IServiceHub>()))
.Returns(new Dictionary<string, object> { ["unexpectedKey"] = "unexpectedValue" });
}



[TestMethod]
public async Task TestCloudFunctionsMissingResultAsync()
{
// Arrange
SetupMocksForMissingResult();

// Set up service hub
var hub = new MutableServiceHub
_hub = new MutableServiceHub
{
CommandRunner = commandRunnerMock.Object,
CloudCodeController = new ParseCloudCodeController(commandRunnerMock.Object, decoderMock.Object)
CommandRunner = _commandRunnerMock.Object,
CloudCodeController = new ParseCloudCodeController(_commandRunnerMock.Object, _decoderMock.Object)
};

var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
_client = new ParseClient(new ServerConnectionData { Test = true }, _hub);

// Act & Assert
await Assert.ThrowsExceptionAsync<ParseFailureException>(async () =>
await client.CallCloudCodeFunctionAsync<IDictionary<string, object>>("someFunction", null, CancellationToken.None));
await _client.CallCloudCodeFunctionAsync<IDictionary<string, object>>("someFunction", null, CancellationToken.None));
}

[TestMethod]
public async Task TestParseCloudCodeControllerMissingResult()
{
// Arrange
var commandRunnerMock = new Mock<IParseCommandRunner>();
var decoderMock = new Mock<IParseDataDecoder>();

// Mock the CommandRunner response
commandRunnerMock
.Setup(runner => runner.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()
))
.ReturnsAsync(new Tuple<System.Net.HttpStatusCode, IDictionary<string, object>>(
System.Net.HttpStatusCode.OK, // Simulated HTTP status code
new Dictionary<string, object>
{
["unexpectedKey"] = "unexpectedValue" // Missing "result" key
}));

// Mock the Decoder response
decoderMock
.Setup(decoder => decoder.Decode(It.IsAny<object>(), It.IsAny<IServiceHub>()))
.Returns(new Dictionary<string, object> { ["unexpectedKey"] = "unexpectedValue" });

// Initialize the controller
var controller = new ParseCloudCodeController(commandRunnerMock.Object, decoderMock.Object);
//Arrange
SetupMocksForMissingResult();
var controller = new ParseCloudCodeController(_commandRunnerMock.Object, _decoderMock.Object);

// Act & Assert
await Assert.ThrowsExceptionAsync<ParseFailureException>(async () =>
Expand All @@ -103,7 +102,4 @@ await controller.CallFunctionAsync<IDictionary<string, object>>(
null,
CancellationToken.None));
}



}
}
Loading
Loading