-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Framework for testing interactions with CloudClient
#127
Framework for testing interactions with CloudClient
#127
Conversation
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! I think the direction looks good. I've not worked with the mockall
crate before, but it looks nice.
src/commands/link.rs
Outdated
Ok(_) => panic!("Expected error"), | ||
Err(err) => assert_eq!( | ||
err.to_string(), | ||
r#"Database "does-not-exist" does not exist"# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried about doing exact matches on error messages as this can be pretty brittle, but I think it's fine to leave for now and see how it works out in practice.
…all counts Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
45303bf
to
4e5fa59
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great to see. I left a few comments but nothing showstopping.
I did, as expected, find the mock-API style of testing a bit tricky. The lack of asserts at the end of many of the tests threw me - but of course the assertion is actually in setting up the expectation! I think this is part of why I prefer fakes, so that I can make explicit assertions about before and after state rather than setting expectations about specific actions. However setting up fakes for something like this would be a much greater amount of work, and this seems like a pragmatic way to get some unit testing going. Thanks for driving it!
cloud-openapi = { workspace = true } | ||
mime_guess = { version = "2.0" } | ||
mockall = "0.11.4" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other reference to this is a dev dependency. Does the cloud
crate need it at runtime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does because it is in a separate crate. I put it behind a feature flag now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... you should be able to put this behind a dev-dependency
instead of a feature. Are you running into issues with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I cannot seem to figure out how to put it behind a dev dependency and then use it in a testing scope from another crate that imports the crate. Can you import types that are behind a dev dependency from another crate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems i am running into rust-lang/cargo#8379
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rylev I am merging this but happy to do a follow up pr if you have ideas about how to put it behind a dev dep
@@ -1 +1,4 @@ | |||
pub mod client; | |||
mod client_interface; | |||
pub use client_interface::CloudClientInterface; | |||
pub use client_interface::MockCloudClientInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that answers that question! #[cfg(test)]
maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like code compiled with cfg(test)
is not accessible to external crates, so I wasn't able to reference the mock type after putting it behind this test flag. But I was able to put it behind a feature flag and ill push up a change for that
src/commands/link.rs
Outdated
} | ||
|
||
#[tokio::test] | ||
async fn test_sqlite_link_success() -> Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, what this test tests is not "success" but something more like "if the database exists, the link is created". It would be great to express this more explicitly. I'm not sure if it can be done in the test name - it might be too long! - but if not then maybe a comment?
Similarly with many of the other tests e.g. "if the label already points to the requested database, it errors" or "if the label already points to a different database, it..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the sqlite
test names more explicit and readable. Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! The link
test names are definitely more expressive!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggested some names for the create/delete tests - feel free to use or biff them as you prefer! LGTM regardless - thanks!
65bd9fa
to
3ffbb64
Compare
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
3ffbb64
to
0ae71e9
Compare
This is a proposed approach to increasing our unit tests in this repository by making the
CloudClient
mockable. It uses themockall
crate by having theCloudClient
implement aCloudClientInterface
that can be mocked. Also has a few tests for examples.Pros to this approach:
Cons:
(I'm sure there are other pros and cons i am forgetting)
fixes #124