Skip to content

Commit

Permalink
Add cuddle pet tests and helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rpothin committed Apr 4, 2024
1 parent b906175 commit 732d4c2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,64 @@ public void FeedPet_ReachingInitialLifePoints()
Assert.True(PetHelper.ArePetLifePointsCorrectlyUpdatedAfterFeedingActivity(_serviceClient, _petId, initialLifePoints, selectedFoodQuantity));
}

/// <summary>
/// Tests the cuddling of a pet entity.
/// </summary>
/// <remarks>
/// The test update the happiness points of the pet entity to a random value between 10000 and 90000.
/// It then create a cuddling activity for the pet entity.
/// The test waits for 20 seconds and validates that the happiness points of the pet entity have increased accordingly.
/// </remarks>
[Fact]
public void CuddlePet_NotReachingInitialHappinessPoints()
{
// Update the happiness points of the pet to a random value between 10000 and 90000
var random = new Random();
var happinessPoints = random.Next(10000, 90000);
PetHelper.UpdatePetHappinessPoints(_serviceClient, _petId, happinessPoints);

// Retrieve the pet
var petBeforeCuddling = _serviceClient.Retrieve("rpo_pet", _petId, new ColumnSet("rpo_happinesspoints"));
int initialHappinessPoints = petBeforeCuddling.GetAttributeValue<int>("rpo_happinesspoints");

// Create a cuddling activity
var cuddlingActivityId = PetHelper.CreateCuddleActivity(_serviceClient, _petId);

// Wait for 20 seconds
Thread.Sleep(20000);

// Assert
Assert.True(PetHelper.ArePetHapppinessPointsCorrectlyUpdatedAfterCuddleActivity(_serviceClient, _petId, initialHappinessPoints));
}

/// <summary>
/// Tests the cuddling of a pet entity.
/// </summary>
/// <remarks>
/// The test update the happiness points of the pet entity to 99990.
/// It then create a cuddling activity for the pet entity.
/// The test waits for 20 seconds and validates that the happiness points have increased without going over the initial happiness points value.
/// </remarks>
[Fact]
public void CuddlePet_ReacingInitialHappinessPoints()
{
// Update the happiness points of the pet to 99990
PetHelper.UpdatePetHappinessPoints(_serviceClient, _petId, 99990);

// Retrieve the pet
var petBeforeCuddling = _serviceClient.Retrieve("rpo_pet", _petId, new ColumnSet("rpo_happinesspoints"));
int initialHappinessPoints = petBeforeCuddling.GetAttributeValue<int>("rpo_happinesspoints");

// Create a cuddling activity
var cuddlingActivityId = PetHelper.CreateCuddleActivity(_serviceClient, _petId);

// Wait for 20 seconds
Thread.Sleep(20000);

// Assert
Assert.True(PetHelper.ArePetHapppinessPointsCorrectlyUpdatedAfterCuddleActivity(_serviceClient, _petId, initialHappinessPoints));

Check failure on line 261 in src/Dataverse.API.Testing/VirtualPetSimulator.Testing/VirtualPetsSimulator.cs

View workflow job for this annotation

GitHub Actions / Dataverse API Testing

Dataverse.API.Testing.DataverseAPITests ► CuddlePet_ReacingInitialHappinessPoints

Failed test found in: src/Dataverse.API.Testing/VirtualPetSimulator.Testing/TestResults/test-results.trx Error: Assert.True() Failure Expected: True Actual: False
Raw output
Assert.True() Failure
Expected: True
Actual:   False
   at Dataverse.API.Testing.DataverseAPITests.CuddlePet_ReacingInitialHappinessPoints() in /home/runner/work/PowerPlatform-DevEx-With-GitHub-Workshop/PowerPlatform-DevEx-With-GitHub-Workshop/src/Dataverse.API.Testing/VirtualPetSimulator.Testing/VirtualPetsSimulator.cs:line 261
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
}

/// <summary>
/// Disposes the resources used by the <see cref="DataverseAPITests"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class PetHelper
{
private static int _initialLifePoints = 100000;
private static int _initialHappinessPoints = 100000;
private static int _cuddleHappinessPoints = 1000;

/// <summary>
/// Create a random pet
Expand Down Expand Up @@ -83,6 +84,23 @@ public static void UpdatePetLifePoints(ServiceClient serviceClient, Guid petId,
serviceClient.Update(pet);
}

/// <summary>
/// Update the happiness points of a pet
/// </summary>
/// <param name="serviceClient">The service client</param>
/// <param name="petId">The id of the pet</param>
/// <param name="happinessPoints">The new happiness points</param>
/// <remarks>
/// This method updates the happiness points of the pet
/// </remarks>
public static void UpdatePetHappinessPoints(ServiceClient serviceClient, Guid petId, int happinessPoints)
{
var pet = new Entity("rpo_pet");
pet.Id = petId;
pet["rpo_happinesspoints"] = happinessPoints;
serviceClient.Update(pet);
}

/// <summary>
/// Create a feeding activity
/// </summary>
Expand Down Expand Up @@ -142,6 +160,50 @@ public static bool ArePetLifePointsCorrectlyUpdatedAfterFeedingActivity(ServiceC
}
}

/// <summary>
/// Create a cuddle activity
/// </summary>
/// <param name="serviceClient">The service client</param>
/// <param name="petId">The id of the pet</param>
/// <returns>The id of the created cuddle activity</returns>
/// <remarks>
/// This method creates a new cuddle activity for the pet
/// </remarks>
public static Guid CreateCuddleActivity(ServiceClient serviceClient, Guid petId)
{
Entity cuddleActivity = new Entity("rpo_cuddle");
cuddleActivity["regardingobjectid"] = new EntityReference("rpo_pet", petId);

return serviceClient.Create(cuddleActivity);
}

/// <summary>
/// Check if the happiness points are correctly updated after a cuddle activity
/// </summary>
/// <param name="serviceClient">The service client</param>
/// <param name="petId">The id of the pet</param>
/// <param name="happinessPointsBeforeCuddle">The happiness points of the pet before the cuddle activity</param>
/// <returns>True if the happiness points are correctly updated, false otherwise</returns>
/// <remarks>
/// This method checks if the happiness points of the pet are correctly updated after a cuddle activity
/// </remarks>
public static bool ArePetHapppinessPointsCorrectlyUpdatedAfterCuddleActivity(ServiceClient serviceClient, Guid petId, int happinessPointsBeforeCuddle)
{
// Retrieve the pet
var pet = serviceClient.Retrieve("rpo_pet", petId, new ColumnSet("rpo_happinesspoints"));

// Get the happiness points
var happinessPoints = pet.GetAttributeValue<int>("rpo_happinesspoints");

// Check if the happiness points are correctly updated
if (happinessPointsBeforeCuddle + _cuddleHappinessPoints >= _initialHappinessPoints) {
return happinessPoints == _initialHappinessPoints;
} else {
// Consider the option that the happiness points already decreased by 10
return happinessPoints == happinessPointsBeforeCuddle + _cuddleHappinessPoints || happinessPoints == happinessPointsBeforeCuddle + _cuddleHappinessPoints - 10;
}
}

/// <summary>
/// Delete a pet
/// </summary>
Expand Down

0 comments on commit 732d4c2

Please sign in to comment.