Skip to content

Commit

Permalink
add unit tests for the proxy contract
Browse files Browse the repository at this point in the history
  • Loading branch information
zamrokk committed Sep 4, 2023
1 parent 916929b commit 14d177b
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 61 deletions.
127 changes: 69 additions & 58 deletions solution/.taq/development-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,59 @@
}
]
},
"@taqueria/plugin-ligo.test.1691396856682": {
"task": "test",
"@taqueria/plugin-ligo.compile.1691764048195": {
"task": "compile",
"plugin": "@taqueria/plugin-ligo",
"time": 1691396856682,
"time": 1691764048195,
"output": [
{
"contract": "unit_pokeGame.jsligo",
"testResults": "\"Sender 1 has balance : \"\n3800000000000mutez\n\"contract deployed with values : \"\nKT1CWLhYPpX17qpCyLgTPKxijHwAkbqvF89H(None)\nSuccess (1298n)\n{feedback = \"kiss\" ; pokeTraces = [tz1hkMbkLPkvhxyqsQoBoLPqb1mruSzZx3zy -> {feedback = \"\" ; receiver = KT1CWLhYPpX17qpCyLgTPKxijHwAkbqvF89H}]}\nEverything at the top-level was executed.\n- testSender1Poke exited with value ().\n\n🎉 All tests passed 🎉"
}
]
},
"@taqueria/plugin-ligo.test.1691397097944": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1691397097944,
"output": [
"contract": "proxy.jsligo",
"artifact": "artifacts/proxy.tz"
},
{
"contract": "mutation_pokeGame.jsligo",
"testResults": "Some tests failed :("
"contract": "proxy.storageList.jsligo",
"artifact": "artifacts/proxy.default_storage.tz\n"
},
{
"contract": "proxy.parameterList.jsligo",
"artifact": ""
}
]
},
"@taqueria/plugin-ligo.test.1691397133161": {
"task": "test",
"@taqueria/plugin-ligo.compile.1691764068045": {
"task": "compile",
"plugin": "@taqueria/plugin-ligo",
"time": 1691397133161,
"time": 1691764068045,
"output": [
{
"contract": "mutation_pokeGame.jsligo",
"testResults": "\"Sender 1 has balance : \"\n3800000000000mutez\n\"contract deployed with values : \"\nKT1USnxrNkXwdetJQJ18uNgWJtaMDY4gdzPd(None)\nSuccess (1330n)\n{feedback = \"kiss\" ; pokeTraces = [tz1hkMbkLPkvhxyqsQoBoLPqb1mruSzZx3zy -> {feedback = \"\" ; receiver = KT1USnxrNkXwdetJQJ18uNgWJtaMDY4gdzPd}]}\n\"Sender 1 has balance : \"\n3800000000000mutez\nEverything at the top-level was executed.\n- test_mutation exited with value ().\n\n🎉 All tests passed 🎉"
"contract": "pokeGameLambda.jsligo",
"artifact": "artifacts/pokeGameLambda.tz"
}
]
},
"@taqueria/plugin-ligo.test.1691399461929": {
"task": "test",
"@taqueria/plugin-ligo.compile.1691764160054": {
"task": "compile",
"plugin": "@taqueria/plugin-ligo",
"time": 1691399461929,
"time": 1691764160054,
"output": [
{
"contract": "unit_pokeGame.jsligo",
"testResults": "\"Sender 1 has balance : \"\n3800000000000mutez\n\"*** Run test to pass ***\"\n\"contract deployed with values : \"\nKT1BKUzj4fKj9yD3U5w4HcCwSBum28pELgfi(None)\nSuccess (1860n)\n\"*** Check initial ticket is here ***\"\n{feedback = \"kiss\" ; pokeTraces = [] ; ticketOwnership = [tz1hkMbkLPkvhxyqsQoBoLPqb1mruSzZx3zy -> (KT1BKUzj4fKj9yD3U5w4HcCwSBum28pELgfi , (\"can_poke\" , 1n))]}\nSuccess (1024n)\n{feedback = \"kiss\" ; pokeTraces = [tz1hkMbkLPkvhxyqsQoBoLPqb1mruSzZx3zy -> {feedback = \"\" ; receiver = KT1BKUzj4fKj9yD3U5w4HcCwSBum28pELgfi}] ; ticketOwnership = []}\n\"*** Run test to fail ***\"\n\"contract deployed with values : \"\nKT1TvWVCW7y9jEWaEoBq6b5UHDY6PVrQ9ifQ(None)\nSuccess (1401n)\n\"*** Check initial ticket is here ***\"\n{feedback = \"kiss\" ; pokeTraces = [] ; ticketOwnership = []}\nFail (Rejected ((\"User does not have tickets => not allowed\" , KT1TvWVCW7y9jEWaEoBq6b5UHDY6PVrQ9ifQ)))\n{feedback = \"kiss\" ; pokeTraces = [] ; ticketOwnership = []}\nEverything at the top-level was executed.\n- testSender1Poke exited with value ().\n- testSender1PokeWithNoTicketsToFail exited with value ().\n\n🎉 All tests passed 🎉"
"contract": "pokeGameLambda.jsligo",
"artifact": "artifacts/pokeGameLambda.tz"
},
{
"contract": "pokeGameLambda.storageList.jsligo",
"artifact": "No storage expressions found\n"
},
{
"contract": "pokeGameLambda.parameterList.jsligo",
"artifact": "No parameter expressions found\n"
}
]
},
"@taqueria/plugin-ligo.compile.1691417387724": {
"@taqueria/plugin-ligo.compile.1693837601857": {
"task": "compile",
"plugin": "@taqueria/plugin-ligo",
"time": 1691417387725,
"time": 1693837601857,
"output": [
{
"contract": "pokeGame.jsligo",
Expand All @@ -75,10 +80,10 @@
}
]
},
"@taqueria/plugin-ligo.compile.1691417744480": {
"@taqueria/plugin-ligo.compile.1693837613825": {
"task": "compile",
"plugin": "@taqueria/plugin-ligo",
"time": 1691417744480,
"time": 1693837613825,
"output": [
{
"contract": "proxy.jsligo",
Expand All @@ -94,52 +99,58 @@
}
]
},
"@taqueria/plugin-ligo.compile.1691764048195": {
"task": "compile",
"@taqueria/plugin-ligo.test.1693840595479": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1691764048195,
"time": 1693840595479,
"output": [
{
"contract": "proxy.jsligo",
"artifact": "artifacts/proxy.tz"
},
{
"contract": "proxy.storageList.jsligo",
"artifact": "artifacts/proxy.default_storage.tz\n"
},
{
"contract": "proxy.parameterList.jsligo",
"artifact": ""
"contract": "unit_pokeGame.jsligo",
"testResults": "Some tests failed :("
}
]
},
"@taqueria/plugin-ligo.compile.1691764068045": {
"task": "compile",
"@taqueria/plugin-ligo.test.1693840607049": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1691764068045,
"time": 1693840607049,
"output": [
{
"contract": "pokeGameLambda.jsligo",
"artifact": "artifacts/pokeGameLambda.tz"
"contract": "unit_pokeGame.jsligo",
"testResults": "Some tests failed :("
}
]
},
"@taqueria/plugin-ligo.compile.1691764160054": {
"task": "compile",
"@taqueria/plugin-ligo.test.1693840649257": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1691764160054,
"time": 1693840649257,
"output": [
{
"contract": "pokeGameLambda.jsligo",
"artifact": "artifacts/pokeGameLambda.tz"
},
"contract": "unit_pokeGame.jsligo",
"testResults": "Some tests failed :("
}
]
},
"@taqueria/plugin-ligo.test.1693840690077": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1693840690077,
"output": [
{
"contract": "pokeGameLambda.storageList.jsligo",
"artifact": "No storage expressions found\n"
},
"contract": "unit_pokeGame.jsligo",
"testResults": "Some tests failed :("
}
]
},
"@taqueria/plugin-ligo.test.1693841086075": {
"task": "test",
"plugin": "@taqueria/plugin-ligo",
"time": 1693841086075,
"output": [
{
"contract": "pokeGameLambda.parameterList.jsligo",
"artifact": "No parameter expressions found\n"
"contract": "unit_pokeGame.jsligo",
"testResults": "\"Sender 1 has balance : \"\n3800000000000mutez\nSuccess (2807n)\n{entrypoints = [\"changeVersion\" -> {addr = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh ; method = \"changeVersion\"} ; \"Init\" -> {addr = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh ; method = \"Init\"} ; \"feedback\" -> {addr = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh ; method = \"feedback\"} ; \"Poke\" -> {addr = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh ; method = \"Poke\"} ; \"PokeAndGetFeedback\" -> {addr = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh ; method = \"PokeAndGetFeedback\"}] ; governance = tz1MBWU1WkszFfkEER2pgn4ATKXE9ng7x1sR}\n\"calling _testPokeGame on proxy : \"\nKT1UE3DgZCk9HK7GMQ9yTMDS6vkwheN2dX3a(None)\nSuccess (1359n)\n\"calling _testPokeGame on proxy : \"\nKT1UE3DgZCk9HK7GMQ9yTMDS6vkwheN2dX3a(None)\nSuccess (1357n)\n{feedback = \"kiss\" ; pokeTraces = [tz1hkMbkLPkvhxyqsQoBoLPqb1mruSzZx3zy -> {feedback = \"\" ; receiver = KT1HWpsJxenWT6m1Rjr71iQR5DXr5ffUYyEh}] ; ticketOwnership = [] ; tzip18 = {contractNext = None (()) ; contractPrevious = None (()) ; proxy = KT1UE3DgZCk9HK7GMQ9yTMDS6vkwheN2dX3a ; version = 1n}}\nEverything at the top-level was executed.\n- testSender1Poke exited with value ().\n\n🎉 All tests passed 🎉"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion solution/.taq/state.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// WARNING: This file is autogenerated and should NOT be modified
{
"build": "6a57340",
"build": "72f27cf",
"configHash": "f2656a55015d2ecada534d41983a72fd2b6c2498405360dc60ec1c1bbbc169e7",
"tasks": {
"ligo": {
Expand Down
1 change: 1 addition & 0 deletions solution/contracts/.ligo/repository_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
93de71ba-4efe-481e-2438-5678fc53dd32
6 changes: 5 additions & 1 deletion solution/contracts/proxy.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type changeVersion = {

type storage = {
governance: address, //admins
entrypoints: big_map<string, entrypointType> //interface schema map
entrypoints: big_map<
string,
entrypointType
> //interface schema map

};

type _return = [list<operation>, storage];
Expand Down
6 changes: 5 additions & 1 deletion solution/contracts/proxy.storageList.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ const default_storage =
{
governance: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb" as address, //admins
entrypoints: Big_map.empty as
big_map<string, entrypointType> //interface schema map
big_map<
string,
entrypointType
> //interface schema map

};
172 changes: 172 additions & 0 deletions solution/contracts/unit_pokeGame.jsligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#import "./pokeGame.jsligo" "PokeGame"
#import "./proxy.jsligo" "Proxy"
// reset state

const _ = Test.reset_state(2 as nat, list([]) as list<tez>);

const admin = Test.nth_bootstrap_account(0);

const sender1: address = Test.nth_bootstrap_account(1);

const _ = Test.log("Sender 1 has balance : ");

const _ = Test.log(Test.get_balance(sender1));

const _ = Test.set_baker(admin);

const _ = Test.set_source(admin);

const initial_tez = 0 as tez;

//functions

export const _testPokeGame = (
taddr: typed_address<parameter_of Proxy, Proxy.storage>,
entrypoint: string,
payload: bytes,
s: address
): unit => {
const contr = Test.to_contract(taddr);
const contrAddress = Tezos.address(contr);
const _ = Test.log("calling _testPokeGame on proxy : ");
const _ = Test.log(contr);
Test.set_source(s);
const status =
Test.transfer_to_contract(
contr,
CallContract({ entrypointName: entrypoint, payload: payload }),
0 as tez
);
Test.log(status)
};

//********** TESTS *************/

const testSender1Poke =
(
(): unit => {
/***PROXY **/

const _ = Test.set_source(admin);
const proxy_initial_storage =
{
governance: admin, //admins
entrypoints: Big_map.empty as
big_map<
string,
Proxy.entrypointType
> //interface schema map

};
const [tProxyaddr, _, _] =
Test.originate_module(
contract_of (Proxy),
proxy_initial_storage,
initial_tez
);
const proxyContr = Test.to_contract(tProxyaddr);
const proxyContrAddress = Tezos.address(proxyContr);
/***CONTRACT **/

const _ = Test.set_source(admin);
const initial_storage =
{
pokeTraces: Map.empty as map<address, PokeGame.pokeMessage>,
feedback: "kiss",
ticketOwnership: Map.empty as map<address, ticket<string>>, //ticket of claims
tzip18: {
proxy: proxyContrAddress,
version: 1 as nat,
contractPrevious: None() as option<address>,
contractNext: None() as option<address>
}
};
const [addr, _, _] =
Test.originate_from_file(
"./pokeGame.jsligo",
"main",
list([]),
Test.eval(initial_storage),
initial_tez
);
/**Upgrade proxy **/

const status =
Test.transfer_to_contract(
proxyContr,
Upgrade(
[
list(
[
{
name: "Poke",
isRemoved: false,
entrypoint: Some({ method: "Poke", addr: addr })
},
{
name: "PokeAndGetFeedback",
isRemoved: false,
entrypoint: Some(
{ method: "PokeAndGetFeedback", addr: addr }
)
},
{
name: "Init",
isRemoved: false,
entrypoint: Some({ method: "Init", addr: addr })
},
{
name: "changeVersion",
isRemoved: false,
entrypoint: Some({ method: "changeVersion", addr: addr })
},
{
name: "feedback",
isRemoved: false,
entrypoint: Some({ method: "feedback", addr: addr })
}
]
) as list<Proxy.entrypointOperation>,
None() as option<Proxy.changeVersion>
]
),
0 as tez
);
Test.log(status);
const storeProxy: Proxy.storage = Test.get_storage(tProxyaddr);
Test.log(storeProxy);
/** RUN TEST*/

_testPokeGame(
tProxyaddr,
"Init",
Bytes.pack([sender1, 10 as nat]),
sender1
);
_testPokeGame(tProxyaddr, "Poke", (bytes `dummy`), sender1);
const taddr =
Test.cast_address(addr) as
typed_address<PokeGame.parameter, PokeGame.storage>;
const store: PokeGame.storage = Test.get_storage(taddr);
Test.log(store);
//check poke is registered

match(
Map.find_opt(sender1, store.pokeTraces),
{
Some: (pokeMessage: PokeGame.pokeMessage) => {
assert_with_error(
pokeMessage.feedback == "",
"feedback " + pokeMessage.feedback + " is not equal to expected " +
"(empty)"
);
assert_with_error(
pokeMessage.receiver == addr,
"receiver is not equal"
)
},
None: () => assert_with_error(false, "don't find traces")
}
)
}
)();

0 comments on commit 14d177b

Please sign in to comment.