Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

[refactor-prompt] Add missing tests for wallet create, verbose and create_addr #733

Merged

Conversation

LysanderGG
Copy link
Contributor

What current issue(s) does this address, or what feature is it adding?
Add missing tests after PR #720 (also see #726 (comment))

The tests are for the following commands wallet create, wallet verbose, wallet create_addr.

How did you solve this problem?
Added some tests.

How did you make sure your solution works?
Run make test.

Are there any special changes in the code that we should be aware of?
No.

Please check the following, if applicable:

  • Did you add any tests?
  • Did you run make lint?
  • Did you run make test?
  • Are you making a PR to a feature branch or development rather than master?
  • Did you add an entry to CHANGELOG.rst? (if not, please do)

Note:

  • I couldn't find any other test related to wallet migration so I was unable to test the command wallet migrate.

@coveralls
Copy link

coveralls commented Dec 2, 2018

Coverage Status

Coverage increased (+0.3%) to 83.838% when pulling 9a7f665 on LysanderGG:add_command_wallet_tests into c4bb448 on CityOfZion:refactor-prompt.

@LysanderGG LysanderGG changed the title Add missing tests for wallet create, verbose and create_addr [refactor-prompt] Add missing tests for wallet create, verbose and create_addr Dec 2, 2018
@jseagrave21
Copy link
Contributor

This looks good. Could you also add a test for wallet?

@jseagrave21
Copy link
Contributor

Also, it would be nice to see a test for an invalid wallet command

@jseagrave21
Copy link
Contributor

jseagrave21 commented Dec 2, 2018

And, I was looking here: https://coveralls.io/builds/20405190/source?filename=neo/Prompt/Commands/Wallet.py#L109
Is the logic to trigger this effect wrong, or maybe the previous returns premature? It seems like we have nothing actually triggering start_wallet_loop
Maybe something like

...
            try:
                PromptData.Wallet = UserWallet.Create(path=path, password=password_key)
                contract = PromptData.Wallet.GetDefaultContract()
                key = PromptData.Wallet.GetKey(contract.PublicKeyHash)
                print("Wallet %s" % json.dumps(PromptData.Wallet.ToJson(), indent=4))
                print("Pubkey %s" % key.PublicKey.encode_point(True))
                # return PromptData.Wallet
            except Exception as e:
                print("Exception creating wallet: %s" % e)
                PromptData.Wallet = None
                if os.path.isfile(path):
                    try:
                        os.remove(path)
                    except Exception as e:
                        print("Could not remove {}: {}".format(path, e))
                return

            if PromptData.Wallet:
                PromptData.Prompt.start_wallet_loop()
                return PromptData.Wallet

Where the commented return PromptData.Wallet would just be deleted.

Copy link
Member

@ixje ixje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding migrate tests. I've never used the command before. I shall check if the command is still useful and otherwise we can remove it. It might be really old.

As for the remaining input; @jseagrave21 pointed them all out (thanks!)

  1. test for wallet (coverage)
  2. test invalid wallet (coverage)
  3. ensure wallet loop is actually started when creating a new wallet (functionality)

@LysanderGG
Copy link
Contributor Author

@ixje @jseagrave21 Thank you for the first review!

  1. test for wallet (coverage)
    -> Done, but there's not much to test.
    Shall I test what is printed to std::out?

  2. test invalid wallet (coverage)
    -> Done, but there's not much to test.

  3. ensure wallet loop is actually started when creating a new wallet (functionality)
    The bug had been introduced with Add support for wallet send, sendmany, sign, open, and close #726.
    I fixed it.

Please review a second time when you have time.

Copy link
Member

@ixje ixje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. could be ok to do that. Just to make sure wallet is actually printing wallet contents. Or perhaps patch and assert that wallet.ToJson() is called
  2. not working as intended, see review
  3. thanks!

with patch('neo.Prompt.PromptData.PromptData.Prompt'):
# test wallet verbose with no wallet
args = ['verbose']
res = CommandWallet().execute(args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as previous. This does not test verbose, but fails on a not having an open wallet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is specifically testing test wallet verbose with no wallet


# test wallet create address with no argument
args = ['create_addr']
res = CommandWallet().execute(args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not specifically at this line, but in the function it calls (not seeing that code here);
Can you please give a more user friendly error message and/or capture the "not enough arguments" problem sooner? We now get

neo> wallet create_addr
int() argument must be a string, a bytes-like object or a number, not 'NoneType'

which is really confusing because you might try interpret this as it indicating that "create_addr" is read as an int() argument

Copy link
Contributor Author

@LysanderGG LysanderGG Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here:

if len(arguments) < sub_cmd.nb_mandatory_params():

However the early detection makes code like

print("Please specify a path")
unreachable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amend of ^
I removed the early detection and checked the argument in CommandWalletCreateAddress.

@ixje
Copy link
Member

ixje commented Dec 3, 2018

I couldn't find any other test related to wallet migration so I was unable to test the command wallet migrate

Skip it. I'm going to remove all migrate code. It's old an no longer used.

Signed-off-by: Guillaume George <lysandergc@gmail.com>
@LysanderGG LysanderGG force-pushed the add_command_wallet_tests branch from d70e5ee to 9a7f665 Compare December 4, 2018 00:26
@LysanderGG
Copy link
Contributor Author

@ixje @jseagrave21 Thank you for the feedbacks.
I fixed what was requested, please check it. There were a few different suggestions for some tests so I chose one.

Copy link
Contributor

@jseagrave21 jseagrave21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new logic but it looks like several lines were uncovered which is undesirable imo.

Copy link
Contributor

@jseagrave21 jseagrave21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this review closer to the subject lines

@LysanderGG
Copy link
Contributor Author

@ixje @jseagrave21
I pushed a new commit d8d0b04
Please have a look.

I changed the tests to use the subcommand directly instead of testing through CommandWallet. I think it's better for the unit tests to use the smaller unit possible.

If we want integration tests, we should probably test from the prompt directly instead of CommandWallet that is kind of in between.

Please let me know what you think.

Note: testing with the subcommands allows to cover all the code. I added a some edge case detection in the subcommand's execute() method where necessary.
In practice, when called through CommandWallet it is detected earlier.

@jseagrave21
Copy link
Contributor

@LysanderGG I prefer integrated tests which simulate user input as much as possible. When I implemented the testing method here #726 (comment) I wasn't sure how to test directly from prompt.py. But if that is possible, I would prefer that.

@ixje
Copy link
Member

ixje commented Dec 5, 2018

Back here #726 (comment) I kind of reasoned that testing "GroupCommand" vs "SubCommand" doesn't really matter, although "GroupCommand" makes it more obvious what scenario we're working on and will need testing anyway. Which would be in line with @jseagrave21 's last comment.

After your discussion above I kind of realise that people might start calling commands directly when they're extending neo-python. e.g. when they implement a web interface, REST API, new RPC commands or want to control neo-python programmatically. They should not have to re-create the logic for let's say sending assets when there's a CommandSend already doing this. Now being aware of this I don't think we can avoid some code duplication between the "Group Command" and "SubCommand" so to say. For the extensibility reasons mentioned before, I would now lean towards testing commands directly while not excluding testing group commands. (let me know if that last line doesn't make sense and I'll try to rephrase)

@LysanderGG LysanderGG force-pushed the add_command_wallet_tests branch from d8d0b04 to 6161fcf Compare December 5, 2018 09:13
@LysanderGG
Copy link
Contributor Author

Sorry for all the back and forth.

So, here is what comes with the latest commits:

  • I removed the early missing arguments detection. Each function needs to validate its arguments so that we can reuse them.
  • I reverted the change of tests to SubCommands, it's back to using CommandWallet for tests.
  • All lines of Wallet.py are covered, except for Migrate (https://coveralls.io/jobs/42907770/source_files/431067693)

I think that the best to do is keep the functionnalities not related to the command line outside of Commands so that these functions can be used elsewhere.
Commands and SubCommands should not be used from outside (not what I said before, I know 😄 ). Instead, the functions they call can be called (example CreateAddress https://github.com/CityOfZion/neo-python/blob/refactor-prompt/neo/Prompt/Commands/Wallet.py#L220 and fonctions after, or construct_send_basic https://github.com/CityOfZion/neo-python/blob/refactor-prompt/neo/Prompt/Commands/Send.py#L91 and other functions after).

With this we can test both the functions themselves (unit tests like) and the integration in the prompt command. Through root commands should be fine.

I'm a bit lost with this big discussion; can you

  1. Confirm the unresolved conversations?
  2. review again and add new comments if something is wrong or left to be done for this PR?

Thank you!

@ixje
Copy link
Member

ixje commented Dec 5, 2018

I think that the best to do is keep the functionnalities not related to the command line outside of Commands so that these functions can be used elsewhere.

I actually had that in mind (separate presentation from functionality) before writing my previous comment, but thought it might lead to more confusion at this point.

I'm a bit lost with this big discussion; can you
No worries, same here. This is a good showcase why we should keep PR's small, because discussions can still get big :)

I think for this one let's get it in and we'll think about moving out the body of execute() functions before we do the big merge. That way we can have it in a clean PR which is easier to discuss. @jseagrave21 any objections at this point or things that need solving before merging?

PS: I added the last paragraph as a TODO item to the original "CLI refactoring post"

Signed-off-by: Guillaume George <lysandergc@gmail.com>
@LysanderGG LysanderGG force-pushed the add_command_wallet_tests branch from 6161fcf to 57cba0c Compare December 5, 2018 12:30
@jseagrave21
Copy link
Contributor

@ixje @LysanderGG everything looks good to me! Good work!

@ixje ixje merged commit 1caa30d into CityOfZion:refactor-prompt Dec 5, 2018
ixje added a commit that referenced this pull request Jan 10, 2019
* Add the bases for making prompt commands plugin based (#720)

* WIP make prompt commands plugin based

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Merge ixje:refactor-prompt

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Handle help detection in prompt.py

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Use CommandBase for subcommands to be able to have N levels of commands.

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Move "create wallet" command into "wallet"

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Improve CommandBase.register_sub_command to use the subcommand's CommandDesc.command as an id. (#725)

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix 1 word commands (#727)

* Improve CommandBase.register_sub_command to use the subcommand's CommandDesc.command as an id.

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix exception when a command was used without arguments (for example "wallet")

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Add support for `wallet` `send`, `sendmany`, `sign`, `open`, and `close` (#726)

* Prompt cleanup (#730)

* Update description and clarify debug logs

* Make return's explicit, clarify multi-sig help message

* cleanup prompt

* remove colour from initial help, sort commands alphabetically, cleanup descriptions

* fix linting

* process feedback

* [refactor-prompt] Prompt fixes (#732)

* Improve CommandBase.register_sub_command to use the subcommand's CommandDesc.command as an id.

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix CommandWalletCreateAddress missing parameter desc

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix missing words for autocompletion

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* [refactor-prompt] Add missing tests for wallet create, verbose and create_addr (#733)

* Add missing tests for wallet create, verbose and create_addr

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* fix reviews

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* [refactor prompt] Add support for search method group (#739)

* [refactor prompt] Add support for show method group (pt 1) (#738)

* Fix NodeLeader was not reset between test cases. (#742)

This would lead to some problems when the blockchain is reset but the NodeLeader instance members are not.
Transactions were still considered as known.

Some members were not reset in NodeLeader.Setup()
Also removed unsued member NodeLeader.MissionsGlobal

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Migrate command: wallet rebuild (#747)

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* remove deprecated migrate command

* remove deprecated migrate command (#748)

* [refactor prompt] Add support for show method group (pt 2) (#741)

* [refactor-prompt] Implement CommandWalletClaimGas (#740)

* [refactor-prompt] Add wallet token base + delete (#757)

* [refactor-prompt] Migrate command: wallet delete_addr (#752)

* Migrate command: wallet delete_addr

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* UserWallet.DeleteAddress now returns False on error + proper handling of invalid addresses in CommandWalletDeleteAddress

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Migrate command: wallet alias (#753)

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* [refactor-prompt] Add wallet token send (#758)

* remove deprecated migrate command

* Add wallet token send

* Update requirements and use isValidPublicAddress from new neo-python-core version

* - fix send token description
- process feedback

* fix doc string typo

* [refactor prompt] Add support for config group pt1 (#759)

* add wallet import nep2/wif (#765)

* [refactor-prompt] add wallet token history (#763)

* Add wallet token history

* process feedback

* fix broken test after base branch merge

* [refactor-prompt] add wallet export commands (#764)

* add wallet export commands

* Fix export nep2 to ask for passwords to prevent pw leaking to logs

* process feedback

* [refactor-prompt] add wallet import watchaddr (#766)

* [refactor-prompt] add token sendfrom (#761)

* [refactor-prompt] add import multisig_addr (#767)

* add import multsig address

* remove obsolete function

* [refactor-prompt] Migrate command: wallet unspent (#760)

* migrate command: wallet unspent

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* wallet unspent - add tests

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* fix arguments names and missing doc

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Handle review: add feedback when there is no asset matching the arguments + use neocore.Utils.isValidPublicAddress

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* [refactor prompt] Add support for config maxpeers (#762)

* add token approve and token allowance (#769)

* add config nep8 (#771)

* [refactor-prompt] Migrate command: wallet split (#770)

* Migrate command wallet split

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix some comments

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Fix command desc + remove print() calls committed by mistake

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Add tests for CommandWalletSplit

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Review: test_wallet_split use string arguments instead of ints

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Handle Reviews - handle negative fees, improve error messages

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* add token mint (#773)

* [refactor prompt] Fix `search asset` and `search contract` (#774)

* Update prompt.py

- add CommandShow and CommandSearch to prompt

* Update prompt.py

revert changes

* Update Search.py

- update `search contract` and `search asset` per #623 (comment)

* Update test_search_commands.py

- add tests in case no search parameter is provided

* add token register (#775)

* [refactor-prompt] Migrate command: wallet import token (#772)

* test_wallet_commands.py: Move tests of non commands at the end

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Add wallet import token

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Review: return None implicitly where possible

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Add a few tests for SplitUnspentCoin()

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* CommandWalletImportToken - Handle review: better validation of contract_hash

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* Add wallet import contract_addr (#777)

Signed-off-by: Guillaume George <lysandergc@gmail.com>

* split prompt wallet into multiple files (#782)

* [refactor-prompt] add support for the sc group including build, build_run, and load_run (#779)

* [refactor-prompt] cleanup 2 (#783)

* make base command response consistent

* fix plurality

* check input parameters before closing wallet

* streamline missing arguments response
streamline accepted boolean options for config

* process feedback

* [refactor-prompt] add debugstorage command (#784)

* add debugstorage command (and fix auto save linting error)

* correct comments

* [refactor-prompt] add sc deploy (#785)

* add sc deploy (previously known as; import contract)

* process feedback

* [refactor-prompt] add sc invoke (#786)

* add sc invoke (previously known as testinvoke)

* process feedback

* streamline parameter descriptions (#787)

* add wallet password checking before exporting (#790)

* fix exception in help if command has no params (#792)

* [refactor-prompt] enforce pw prompt send (#791)

* remove password bypass

* remove unused import

* [refactor-prompt] update docs (#789)

* update global readme

* update docs

* process feedback

* update show contract output

* [refactor-prompt] restore theming support (#788)

* re-store theming support

* fix comment

* improve wallet usage help (#794)

* clarify insufficient funds error message due to not enough unspent outputs (#793)

* fix prompt printer for lists and other possible objects (#795)

* Fix send/sendmany default wallet source address for tx (#796)

* [refactor-prompt] Improve send & sendmany (third try) (#799)

* Update Send.py

* Update test_send_command.py

* rename test_send_command.py as test_send_commands.py

* [refactor-prompt] Fix #800 (#802)

* Update prompt.py

- add CommandShow and CommandSearch to prompt

* Update prompt.py

revert changes

* Fix #800

- adds support for contracts not requiring params

* fix calculation of change value when using alternating asset inputs (#803)

* test print fix

* oops

* test again

* update makefile

* update changelog to trigger stuck build
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants