Skip to content

Add Uri unit tests - #13

Merged
Kixunil merged 2 commits into
Kixunil:masterfrom
RCasatta:uri_unit
Aug 4, 2021
Merged

Kixunil merged 2 commits into
Kixunil:masterfrom
RCasatta:uri_unit

Conversation

@RCasatta

@RCasatta RCasatta commented Aug 3, 2021

Copy link
Copy Markdown
Collaborator

Some unit tests to cover errors and valid Uris

They are written so they require equality comparison to errors

@Kixunil Kixunil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks! I'm not confident about changes as they are. Some tests test properties that this actually shouldn't have. I think it'd be cleaner to introduce them together with fixes as separate commits/PRs.

Also, can we avoid promising PartialEq for publicly-facing types?

Comment thread bip78/src/uri.rs Outdated

assert_eq!(
Uri::from_str("bitcoin:TB1Q6D3A2W975YNY0ASUVD9A67NER4NKS58FF0Q8G4?pj=https://testnet.demo.btcpayserver.org/BTC/pj"),
Err::<Uri<'_>, ParseUriError>(InternalPjParseError::MissingAmount.into())

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

FYI we have this wrong (see e-mail from Nicolas).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

moved wrong tests in specific ignored test

Comment thread bip78/src/uri.rs Outdated
//TODO we may want endpoint to be a valid URL
assert!(Uri::from_str("bitcoin:TB1Q6D3A2W975YNY0ASUVD9A67NER4NKS58FF0Q8G4?amount=1&pj=http://a").is_ok());

let uri = Uri::from_str("bitcoin:TB1Q6D3A2W975YNY0ASUVD9A67NER4NKS58FF0Q8G4?amount=1&pj=http://example.com?ciao").unwrap();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The latter ? should be url-encoded, I believe. Or at least & have to be to distinguish them from BIP21 ampersands.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

moved wrong tests in specific ignored test

Comment thread bip78/src/uri.rs

#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct PjParseError(InternalPjParseError);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not confident in publicly promising equality now. I know it should be no-brainer but sadly io::Error exists and it's too popular and it doesn't impl PartialEq. Same goes for std::error::Error. Promising PartialEq means promising the inside will never contain io::Error. :(

I want to avoid io::Error like a plague but one never knows...

Note that those derives on internal types are OK, it's just public types I'm worried about.
Also feel free to hack together some macro for matching them if it helps in tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Sounds great! Trivial to review and being dev-only is a great combination.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed Eq, PartialEq, added assert_matches

Note I could have matched to the Err variant, but it was a little too verbose because you have to specify the types, so I match against unwrap_err.

@Kixunil Kixunil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Good idea writing the tests to test the desired but unimplemented properties and adding #[ignore].
Also, unwrap_err is completely fine.

Comment thread bip78/Cargo.toml Outdated
rand = { version = "0.8.4", optional = true }

[dev-dependencies]
assert_matches = "1.5.0" No newline at end of file

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do you feel like adding the newline?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread bip78/src/uri.rs Outdated
}

let parse_err = ParseUriError::from(InternalBip21Error::Address(
util::address::Error::Base58(util::base58::Error::BadChecksum(291609738, 694262922)),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Going into specifics like this seems too much - basically we're testing what the dependency should have tested already. Just knowing it's InternalBip21Error::Address should be sufficient.

I don't feel strongly against it, so feel free to keep it but I suggest you don't bother with testing dependency details next time.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I agree, done. It was so specific because copied from old equality tests but now we have pattern matching

@RCasatta

RCasatta commented Aug 4, 2021 •

Copy link
Copy Markdown
Collaborator Author

Good idea writing the tests to test the desired but unimplemented properties and adding #[ignore].

thanks!

Note I added a commit to fix the CI

@RCasatta RCasatta mentioned this pull request Aug 4, 2021
@Kixunil
Kixunil merged commit 3bd47fb into Kixunil:master Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants