Skip to content
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

Is there a need for #[non_exhaustive] in MongoDB Operation Result Structs? #1171

Closed
ricardosc12 opened this issue Jul 17, 2024 · 3 comments
Closed
Assignees

Comments

@ricardosc12
Copy link

I'm currently writing unit tests for a web service using the Rust MongoDB Driver. As part of these tests, I aim to mock both input and return data without actually querying a database.

However, MongoDB marks its operation result structs as #[non_exhaustive]. This prevents direct instantiation when mocking these structs.

Wouldn't it be beneficial to allow users to handle potential versioning issues themselves in the case of operation results structs?

#[cfg(test)]
mod tests {

    use mongodb::results::UpdateResult;

    use crate::{
        modules::user::{repository::MockUserRepository, service::UserService},
        tests::unit::common::get_user_to_test,
    };

    #[tokio::test]
    async fn check_if_set_session_id_in_user() {
        let mut mock_repo = MockUserRepository::new();

        let mock_update_result = UpdateResult { //cannot create non-exhaustive struct using struct expression
            matched_count: 1,
            modified_count: 1,
            upserted_id: None,
        };

        mock_repo
            .expect_update_one()
            .returning(move |_, _, _| Ok(mock_update_result));

        let user_service = UserService::new(mock_repo);

        let mut user = get_user_to_test();

        user.session_id = None;

        assert!(user.session_id.is_none());

        let result = user_service.check_or_add_session(&mut user).await;

        assert!(result.is_ok());
        assert!(user.session_id.is_some());
    }
}

https://stackoverflow.com/questions/78760352/handling-non-exhaustive-structs-in-rust-unit-tests-with-mongodb-and-axum?noredirect=1#comment138862853_78760352

@isabelatkinson
Copy link
Contributor

Hey @ricardosc12! #[non_exhaustive] is required for future-proofing against possible field additions, although I understand that this makes it challenging to mock results. RUST-1891 was filed by another user to add Default to these structs to make it possible to construct them outside of the crate. This likely won't be prioritized by the team in the near future, but we'd be happy to review a PR if you'd like to work on this yourself!

@ricardosc12
Copy link
Author

Thank you for your understanding, and I'm glad this has already been reported, even though it's not currently in your plans to implement it. I will consider evaluating a contribution towards this. Thanks!

@isabelatkinson
Copy link
Contributor

Added in #1289

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

No branches or pull requests

2 participants