From f35de9f20b4bad479fa547b7489471530272e487 Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Wed, 24 Aug 2022 18:55:29 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20mutualize=20config=20data?= =?UTF-8?q?=20-=20create=20config=20file=20at=20each=20folder=20level=20-?= =?UTF-8?q?=20create=20a=20python=20file=20to=20mutualize=20hints=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/__init__.py | 43 +++++++++++++++++++++ tests/config.yml | 3 ++ tests/integrations/config.yml | 9 +++++ tests/integrations/minter/config.yml | 14 ------- tests/integrations/minter/library.cairo | 51 ++++++++++++------------- tests/integrations/yield/config.yml | 14 ------- tests/integrations/yield/library.cairo | 39 +++++++++---------- tests/units/config.yml | 2 + tests/units/minter/config.yml | 5 --- tests/units/minter/library.cairo | 13 ++----- tests/units/yielder/config.yml | 5 --- tests/units/yielder/library.cairo | 13 ++----- 12 files changed, 107 insertions(+), 104 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/config.yml create mode 100644 tests/integrations/config.yml create mode 100644 tests/units/config.yml diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..a6496c06 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,43 @@ +import yaml +import collections.abc +from collections import namedtuple +from pathlib import Path + + +class Object(): + pass + + +def update(ref: dict, new: dict) -> dict: + """Update ref recursively with new.""" + for key, value in new.items(): + if isinstance(value, collections.abc.Mapping): + ref[key] = update(ref.get(key, {}), value) + else: + ref[key] = value + return ref + + +def objectify(parent: object, attributes: dict) -> object: + """Set attributes recursively to parent object.""" + for key, value in attributes.items(): + if isinstance(value, dict): + value = objectify(Object(), value) + setattr(parent, key, value) + return parent + + +def load(path: str, context: object): + """Read config files and setup context.""" + # load config + path = Path(path) + config = {} + for parent in path.parents: + config_path = parent / path.name + if not config_path.exists(): + continue + with open(config_path, 'r') as file_instance: + update(config, yaml.safe_load(file_instance)) + + # set up context + context = objectify(context, config) diff --git a/tests/config.yml b/tests/config.yml new file mode 100644 index 00000000..47da4816 --- /dev/null +++ b/tests/config.yml @@ -0,0 +1,3 @@ +signers: + admin: 1000 + anyone: 1001 \ No newline at end of file diff --git a/tests/integrations/config.yml b/tests/integrations/config.yml new file mode 100644 index 00000000..eb48e2eb --- /dev/null +++ b/tests/integrations/config.yml @@ -0,0 +1,9 @@ +project: + name: Project + symbol: NFT + +token: + name: Token + symbol: TOK + decimals: 6 + initial_supply: 1000000 \ No newline at end of file diff --git a/tests/integrations/minter/config.yml b/tests/integrations/minter/config.yml index 08fdfd5c..f00b7f36 100644 --- a/tests/integrations/minter/config.yml +++ b/tests/integrations/minter/config.yml @@ -1,17 +1,3 @@ -user: - admin: 1000 - anyone: 1001 - -nft: - name: Project - symbol: NFT - -token: - name: Token - symbol: TOK - decimals: 6 - initial_supply: 1000000 - minter: public_sale_open: 0 max_buy_per_tx: 5 diff --git a/tests/integrations/minter/library.cairo b/tests/integrations/minter/library.cairo index 97ba3bb1..08b569ca 100644 --- a/tests/integrations/minter/library.cairo +++ b/tests/integrations/minter/library.cairo @@ -23,21 +23,18 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): local merkle_root %{ # Load config - import yaml - with open("./tests/integrations/minter/config.yml", 'r') as file_instance: - config = yaml.safe_load(file_instance) - for section, subconfig in config.items(): - for key, value in subconfig.items(): - name = f"{section.lower()}_{key.lower()}" - setattr(context, name, value) + import sys + sys.path.append('.') + from tests import load + load("./tests/integrations/minter/config.yml", context) # ERC-721 deployment context.project_nft_contract = deploy_contract( "./src/nft/project/CarbonableProjectNFT.cairo", { - "name": context.nft_name, - "symbol": context.nft_symbol, - "owner": context.user_admin, + "name": context.project.name, + "symbol": context.project.symbol, + "owner": context.signers.admin, }, ).contract_address @@ -45,11 +42,11 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): context.payment_token_contract = deploy_contract( "./tests/mocks/token/erc20.cairo", { - "name": context.token_name, - "symbol": context.token_symbol, - "decimals": context.token_decimals, - "initial_supply": context.token_initial_supply, - "recipient": context.user_anyone + "name": context.token.name, + "symbol": context.token.symbol, + "decimals": context.token.decimals, + "initial_supply": context.token.initial_supply, + "recipient": context.signers.anyone }, ).contract_address @@ -57,20 +54,20 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): context.carbonable_minter_contract = deploy_contract( "./src/mint/minter.cairo", { - "owner": context.user_admin, + "owner": context.signers.admin, "project_nft_address": context.project_nft_contract, "payment_token_address": context.payment_token_contract, - "public_sale_open": context.minter_public_sale_open, - "max_buy_per_tx": context.minter_max_buy_per_tx, - "unit_price": context.minter_unit_price, - "max_supply_for_mint": context.minter_max_supply_for_mint, - "reserved_supply_for_mint": context.minter_reserved_supply_for_mint, + "public_sale_open": context.minter.public_sale_open, + "max_buy_per_tx": context.minter.max_buy_per_tx, + "unit_price": context.minter.unit_price, + "max_supply_for_mint": context.minter.max_supply_for_mint, + "reserved_supply_for_mint": context.minter.reserved_supply_for_mint, }, ).contract_address # Externalize required variables ids.carbonable_minter = context.carbonable_minter_contract - ids.merkle_root = context.whitelist_merkle_root + ids.merkle_root = context.whitelist.merkle_root %} # Transfer project nft ownershop from admin to minter @@ -340,7 +337,7 @@ namespace admin_instance: func get_address() -> (address : felt): tempvar admin - %{ ids.admin = context.user_admin %} + %{ ids.admin = context.signers.admin %} return (admin) end @@ -508,19 +505,19 @@ namespace anyone_instance: func get_address() -> (address : felt): tempvar anyone - %{ ids.anyone = context.user_anyone %} + %{ ids.anyone = context.signers.anyone %} return (anyone) end func get_slots() -> (slots : felt): tempvar slots - %{ ids.slots = context.whitelist_slots %} + %{ ids.slots = context.whitelist.slots %} return (slots) end func get_proof_len() -> (proof_len : felt): tempvar proof_len - %{ ids.proof_len = context.whitelist_merkle_proof_len %} + %{ ids.proof_len = context.whitelist.merkle_proof_len %} return (proof_len) end @@ -528,7 +525,7 @@ namespace anyone_instance: alloc_locals let (local proof : felt*) = alloc() %{ - for index, node in enumerate(context.whitelist_merkle_proof): + for index, node in enumerate(context.whitelist.merkle_proof): memory[ids.proof + index] = node %} return (proof) diff --git a/tests/integrations/yield/config.yml b/tests/integrations/yield/config.yml index f30d25d5..636543f4 100644 --- a/tests/integrations/yield/config.yml +++ b/tests/integrations/yield/config.yml @@ -1,17 +1,3 @@ -user: - admin: 1000 - anyone: 1001 - -nft: - name: Project - symbol: NFT - -token: - name: Token - symbol: TOK - decimals: 6 - initial_supply: 1000000 - carbonable_token: name: Carbonable symbol: CARBZ diff --git a/tests/integrations/yield/library.cairo b/tests/integrations/yield/library.cairo index 1e5bd04b..b697e41c 100644 --- a/tests/integrations/yield/library.cairo +++ b/tests/integrations/yield/library.cairo @@ -10,21 +10,18 @@ from starkware.cairo.common.bool import TRUE, FALSE func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): %{ # Load config - import yaml - with open("./tests/integrations/yield/config.yml", 'r') as file_instance: - config = yaml.safe_load(file_instance) - for section, subconfig in config.items(): - for key, value in subconfig.items(): - name = f"{section.lower()}_{key.lower()}" - setattr(context, name, value) + import sys + sys.path.append('.') + from tests import load + load("./tests/integrations/yield/config.yml", context) # ERC-721 deployment context.project_nft_contract = deploy_contract( "./src/nft/project/CarbonableProjectNFT.cairo", { - "name": context.nft_name, - "symbol": context.nft_symbol, - "owner": context.user_admin, + "name": context.project.name, + "symbol": context.project.symbol, + "owner": context.signers.admin, }, ).contract_address @@ -32,11 +29,11 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): context.reward_token_contract = deploy_contract( "./tests/mocks/token/erc20.cairo", { - "name": context.token_name, - "symbol": context.token_symbol, - "decimals": context.token_decimals, - "initial_supply": context.token_initial_supply, - "recipient": context.user_anyone + "name": context.token.name, + "symbol": context.token.symbol, + "decimals": context.token.decimals, + "initial_supply": context.token.initial_supply, + "recipient": context.signers.anyone }, ).contract_address @@ -44,11 +41,11 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): context.carbonable_token_contract = deploy_contract( "./tests/mocks/token/erc20.cairo", { - "name": context.carbonable_token_name, - "symbol": context.carbonable_token_symbol, - "decimals": context.carbonable_token_decimals, - "initial_supply": context.carbonable_token_initial_supply, - "recipient": context.user_anyone + "name": context.carbonable_token.name, + "symbol": context.carbonable_token.symbol, + "decimals": context.carbonable_token.decimals, + "initial_supply": context.carbonable_token.initial_supply, + "recipient": context.signers.anyone }, ).contract_address @@ -56,7 +53,7 @@ func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): context.yield_manager_contract = deploy_contract( "./src/yield/yield_manager.cairo", { - "owner": context.user_admin, + "owner": context.signers.admin, "project_nft_address": context.project_nft_contract, "carbonable_token_address": context.carbonable_token_contract, "reward_token_address": context.reward_token_contract, diff --git a/tests/units/config.yml b/tests/units/config.yml new file mode 100644 index 00000000..5bcccde8 --- /dev/null +++ b/tests/units/config.yml @@ -0,0 +1,2 @@ +mocks: + project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46 \ No newline at end of file diff --git a/tests/units/minter/config.yml b/tests/units/minter/config.yml index f8d98157..21d62d7e 100644 --- a/tests/units/minter/config.yml +++ b/tests/units/minter/config.yml @@ -1,9 +1,4 @@ -signers: - admin: 1000 - anyone: 1001 - mocks: - project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46 payment_token_address: 0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82 whitelist: diff --git a/tests/units/minter/library.cairo b/tests/units/minter/library.cairo index 3b98672d..e34f2bf7 100644 --- a/tests/units/minter/library.cairo +++ b/tests/units/minter/library.cairo @@ -39,15 +39,10 @@ end func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): %{ # Load config - import yaml - from collections import namedtuple - with open("./tests/units/minter/config.yml", 'r') as file_instance: - config = yaml.safe_load(file_instance) - for section, subconfig in config.items(): - obj = namedtuple(section, list(subconfig.keys())) - for key, value in subconfig.items(): - setattr(obj, key, value) - setattr(context, section, obj) + import sys + sys.path.append('.') + from tests import load + load("./tests/units/minter/config.yml", context) %} return () diff --git a/tests/units/yielder/config.yml b/tests/units/yielder/config.yml index 8e46f193..99212eb0 100644 --- a/tests/units/yielder/config.yml +++ b/tests/units/yielder/config.yml @@ -1,8 +1,3 @@ -signers: - admin: 1000 - anyone: 1001 - mocks: - project_nft_address: 0x056d4ffea4ca664ffe1256af4b029998014471a87dec8036747a927ab3320b46 carbonable_token_address: 0x043eB0d3D84CC1f850D4b6dEe9630cAc35Af99980BAA30577A76adEa72475598 reward_token_address: 0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82 \ No newline at end of file diff --git a/tests/units/yielder/library.cairo b/tests/units/yielder/library.cairo index 845e2407..1c4c46c2 100644 --- a/tests/units/yielder/library.cairo +++ b/tests/units/yielder/library.cairo @@ -31,15 +31,10 @@ end func setup{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): %{ # Load config - import yaml - from collections import namedtuple - with open("./tests/units/yielder/config.yml", 'r') as file_instance: - config = yaml.safe_load(file_instance) - for section, subconfig in config.items(): - obj = namedtuple(section, list(subconfig.keys())) - for key, value in subconfig.items(): - setattr(obj, key, value) - setattr(context, section, obj) + import sys + sys.path.append('.') + from tests import load + load("./tests/units/yielder/config.yml", context) %} return ()