Qovery / RedisLess

RedisLess is a fast, lightweight, embedded and scalable in-memory Key/Value store library compatible with the Redis API.
MIT License
150 stars 16 forks source link

Refactor server::tests #55

Open tbmreza opened 3 years ago

tbmreza commented 3 years ago

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.