Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/Create-ingredients' into fe…
Browse files Browse the repository at this point in the history
…at/Create-ingredients
  • Loading branch information
kth1888 committed Aug 10, 2023
2 parents 5be964c + 9b6dc72 commit 33a7f18
Show file tree
Hide file tree
Showing 49 changed files with 1,906 additions and 241 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @hAMburg3rs/dev
9 changes: 9 additions & 0 deletions .github/auto_assign.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
addReviewers: true
addAssignees: author

reviewers:
- upa-r-upa
- kth1888
- Atralupus

numberOfReviewers: 0
34 changes: 34 additions & 0 deletions .github/workflows/backend-build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Backend build and test

on:
push:
branches:
- main
paths:
- 'backend/app/**'
tags: ["*"]
pull_request:
types: [ready_for_review, opened, reopened, auto_merge_enabled]

jobs:
build-and-tests:
runs-on: ubuntu-latest

defaults:
run:
working-directory: 'backend/app/'

steps:
- uses: actions/checkout@v3

- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x

- name: Install dependencies
run: dotnet restore

- name: Build and test
run: |
dotnet test --no-restore
23 changes: 23 additions & 0 deletions backend/app/Savor22b.Tests/Action/ActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Savor22b.Tests.Action;

using Libplanet;
using Libplanet.Crypto;

public class ActionTests
{
private readonly PrivateKey _signer = new PrivateKey();

public ActionTests()
{
}

public PrivateKey Signer()
{
return _signer;
}

public Address SignerAddress()
{
return _signer.PublicKey.ToAddress();
}
}
69 changes: 69 additions & 0 deletions backend/app/Savor22b.Tests/Action/BuyCookingEquipmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace Savor22b.Tests.Action;

using System;
using Libplanet;
using Libplanet.Assets;
using Libplanet.Crypto;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Constants;
using Savor22b.States;
using Xunit;

public class BuyCookingEquipmentTests
{
private PrivateKey _signer = new PrivateKey();

public BuyCookingEquipmentTests()
{
}

[Fact]
public void BuyCookingEquipmentExecute_AddsCookingEquipmentToKitchenStateList()
{
IAccountStateDelta state = new DummyState();
state = state.MintAsset(
_signer.PublicKey.ToAddress(),
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"10"
));

var random = new DummyRandom(1);
var desiredEquipmentID = 1;

var action = new BuyCookingEquipmentAction(Guid.NewGuid(), desiredEquipmentID);

state = action.Execute(new DummyActionContext
{
PreviousStates = state,
Signer = _signer.PublicKey.ToAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
});

var rootStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState inventoryState = rootState.InventoryState;

Assert.Equal(0, inventoryState.SeedStateList.Count);
Assert.Equal(0, inventoryState.RefrigeratorStateList.Count);
Assert.Equal(1, inventoryState.CookingEquipmentStateList.Count);
Assert.Equal(desiredEquipmentID, inventoryState.CookingEquipmentStateList[0].CookingEquipmentID);
Assert.Equal(
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"0"
),
state.GetBalance(_signer.PublicKey.ToAddress(), Currencies.KeyCurrency));
Assert.Equal(
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"10"
),
state.GetBalance(Addresses.ShopVaultAddress, Currencies.KeyCurrency));
}
}
69 changes: 69 additions & 0 deletions backend/app/Savor22b.Tests/Action/BuyRandomSeedItemActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace Savor22b.Tests.Action;

using System;
using Libplanet;
using Libplanet.Assets;
using Libplanet.Crypto;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Constants;
using Savor22b.States;
using Xunit;

public class BuyRandomSeedItemActionTests : ActionTests
{

public BuyRandomSeedItemActionTests()
{
}

[Fact]
public void BuyRandomSeedItemActionExecute_AddsItemToItemStateList()
{
IAccountStateDelta state = new DummyState();
state = state.MintAsset(
SignerAddress(),
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"10"
));

var random = new DummyRandom(1);
var desiredRandomSeedItemID = 1;

var action = new BuyRandomSeedItemAction(Guid.NewGuid(), desiredRandomSeedItemID);

state = action.Execute(new DummyActionContext
{
PreviousStates = state,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
});

var inventoryStateEncoded = state.GetState(SignerAddress());
InventoryState inventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();

Assert.Equal(0, inventoryState.SeedStateList.Count);
Assert.Equal(0, inventoryState.RefrigeratorStateList.Count);
Assert.Equal(0, inventoryState.CookingEquipmentStateList.Count);
Assert.Equal(1, inventoryState.ItemStateList.Count);
Assert.Equal(desiredRandomSeedItemID, inventoryState.ItemStateList[0].ItemID);
Assert.Equal(
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"0"
),
state.GetBalance(SignerAddress(), Currencies.KeyCurrency));
Assert.Equal(
FungibleAssetValue.Parse(
Currencies.KeyCurrency,
"10"
),
state.GetBalance(Addresses.ShopVaultAddress, Currencies.KeyCurrency));
}
}
23 changes: 13 additions & 10 deletions backend/app/Savor22b.Tests/Action/GenerateFoodActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public IEnumerable<object[]> IngredientsSamples()
yield return new object[] { recipeID, preset.Item1, preset.Item2 };
}

public InventoryState CreateInventoryStateFromExpectValues(List<int> expectUsedIngredientIDs, List<int> expectUsedFoodIDs)
public RootState CreateRootStateFromExpectValues(List<int> expectUsedIngredientIDs, List<int> expectUsedFoodIDs)
{
RootState rootState = new RootState();
InventoryState inventoryState = new InventoryState();

foreach (var ingredientID in expectUsedIngredientIDs)
Expand All @@ -101,24 +102,26 @@ public InventoryState CreateInventoryStateFromExpectValues(List<int> expectUsedI
inventoryState = inventoryState.AddRefrigeratorItem(RefrigeratorState.CreateFood(Guid.NewGuid(), foodID, "D", 1, 1, 1, 1));
}

return inventoryState;
rootState.SetInventoryState(inventoryState);

return rootState;
}

[Theory]
[ClassData(typeof(IngredientsSamplesData))]
public void GenerateFoodActionExecute_AddsFoodToRefrigeratorStateList(int expectRecipeID, List<int> expectUsedIngredients, List<int> expectUsedFoods)
{
IAccountStateDelta beforeState = new DummyState();
var beforeInventoryState = CreateInventoryStateFromExpectValues(expectUsedIngredients, expectUsedFoods);
beforeState = beforeState.SetState(_signer.PublicKey.ToAddress(), beforeInventoryState.Serialize());
var beforeRootState = CreateRootStateFromExpectValues(expectUsedIngredients, expectUsedFoods);
beforeState = beforeState.SetState(_signer.PublicKey.ToAddress(), beforeRootState.Serialize());

var random = new DummyRandom(1);

var newFoodGuid = Guid.NewGuid();
var action = new GenerateFoodAction(
expectRecipeID,
newFoodGuid,
(from stateList in beforeInventoryState.RefrigeratorStateList
(from stateList in beforeRootState.InventoryState.RefrigeratorStateList
select stateList.StateID).ToList());

var afterState = action.Execute(new DummyActionContext
Expand All @@ -130,11 +133,11 @@ public void GenerateFoodActionExecute_AddsFoodToRefrigeratorStateList(int expect
BlockIndex = 1,
});

var inventoryStateEncoded = afterState.GetState(_signer.PublicKey.ToAddress());
InventoryState afterInventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = afterState.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState afterInventoryState = rootState.InventoryState;

Assert.Equal(afterInventoryState.RefrigeratorStateList.Count, 1);
Assert.Equal(afterInventoryState.RefrigeratorStateList[0].RecipeID, expectRecipeID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ public void GenerateIngredientActionExecute_AddsIngredientToRefrigeratorStateLis
BlockIndex = 1,
});

var inventoryStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
InventoryState inventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState inventoryState = rootState.InventoryState;

Assert.Equal(inventoryState.SeedStateList.Count, 0);
Assert.Equal(inventoryState.RefrigeratorStateList.Count, 1);
}

private (IAccountStateDelta, Guid) AddSeedState(IAccountStateDelta state)
{
RootState rootState = new RootState();
InventoryState inventoryState = new InventoryState();
var newSeed = new SeedState(Guid.NewGuid(), 1);
inventoryState = inventoryState.AddSeed(newSeed);

return (state.SetState(_signer.PublicKey.ToAddress(), inventoryState.Serialize()), newSeed.StateID);
rootState.SetInventoryState(inventoryState);

return (state.SetState(_signer.PublicKey.ToAddress(), rootState.Serialize()), newSeed.StateID);
}
}
86 changes: 0 additions & 86 deletions backend/app/Savor22b.Tests/Action/GenerateSeedActionTests.cs

This file was deleted.

Loading

0 comments on commit 33a7f18

Please sign in to comment.