Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Refactor server::tests #55

Open
tbmreza opened this issue Jun 1, 2021 · 0 comments
Open

Refactor server::tests #55

tbmreza opened this issue Jun 1, 2021 · 0 comments

Comments

@tbmreza
Copy link
Contributor

tbmreza commented Jun 1, 2021

context: https://github.com/Qovery/RedisLess/blob/main/redisless/src/server/tests/mod.rs

I have been trying rstest crate that I just learned from another project. I managed to get the following test passes:

#[rstest]
#[case::incr_by_1(
    3001_u16,
    vec![ 
        // precondition:
        con_set(23, 45),
        // test subject:
        con_incr(23, 1),
    ],
    vec![ 
        assert_con_exists(23, true),
        assert_con_get(23, 46),
    ]
)]
fn redis_client(
    #[case] port: u16,
    #[case] commands: Vec<Box<dyn for<'r> Fn(&'r mut Connection)>>,
    #[case] assertions: Vec<Box<dyn for<'r> Fn(&'r mut Connection)>>,
){
    let (server, mut con) = get_server_connection(port);
    commands.iter().for_each(|f| f(&mut con));
    assertions.iter().for_each(|f| f(&mut con));
    assert_eq!(server.stop(), Some(ServerState::Stopped));
}

To declare, for example, a delete command test case, insert the following attribute:

#[case::del_existent_key(
    3002_u16,
    vec![ 
        // precondition:
        con_set(23, 45),
        // test subject:
        con_del(23),
    ],
    vec![ 
        assert_con_exists(23, false),
    ]
)]

If anyone thinks this interface will get a long way, I'll be happy to line up a pull request.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant