mod test_db;

use agdb::DbElement;
use agdb::DbId;
use agdb::QueryBuilder;
use test_db::TestDb;

#[test]
fn select_key_count_ids() {
    let mut db = TestDb::new();
    db.exec_mut(
        QueryBuilder::insert()
            .nodes()
            .aliases("alias")
            .values([[
                ("key", 100).into(),
                (1, "value").into(),
                (vec![1.1_f64], 1).into(),
            ]])
            .query(),
        1,
    );
    db.exec_count_elements(
        QueryBuilder::select().key_count().ids("alias").query(),
        3,
        &[DbElement {
            id: DbId(1),
            from: DbId::default(),
            to: DbId::default(),
            values: vec![("key_count", 3_u64).into()],
        }],
    );
}

#[test]
fn select_keys_count_no_keys() {
    let mut db = TestDb::new();
    db.exec_mut(QueryBuilder::insert().nodes().aliases("alias").query(), 1);
    db.exec_count_elements(
        QueryBuilder::select().key_count().ids("alias").query(),
        0,
        &[DbElement {
            id: DbId(1),
            from: DbId::default(),
            to: DbId::default(),
            values: vec![("key_count", 0_u64).into()],
        }],
    );
}

#[test]
fn select_key_count_search() {
    let mut db = TestDb::new();

    let values = vec![
        ("key1", 1).into(),
        ("key2", 10).into(),
        ("key3", 100).into(),
    ];

    db.exec_mut(
        QueryBuilder::insert()
            .nodes()
            .count(5)
            .values_uniform(values.clone())
            .query(),
        5,
    );
    db.exec_mut(
        QueryBuilder::insert()
            .edges()
            .from([1, 3])
            .to([3, 5])
            .values_uniform(values)
            .query(),
        2,
    );

    db.exec_count_elements(
        QueryBuilder::select()
            .key_count()
            .ids(QueryBuilder::search().from(3).query())
            .query(),
        9,
        &[
            DbElement {
                id: DbId(3),
                from: DbId(-7),
                to: DbId(-6),
                values: vec![("key_count", 3_u64).into()],
            },
            DbElement {
                id: DbId(-7),
                from: DbId(3),
                to: DbId(5),
                values: vec![("key_count", 3_u64).into()],
            },
            DbElement {
                id: DbId(5),
                from: DbId::default(),
                to: DbId(-7),
                values: vec![("key_count", 3_u64).into()],
            },
        ],
    );
}

#[test]
fn select_key_count_search_alt() {
    let mut db = TestDb::new();

    let values = vec![
        ("key1", 1).into(),
        ("key2", 10).into(),
        ("key3", 100).into(),
    ];

    db.exec_mut(
        QueryBuilder::insert()
            .nodes()
            .count(5)
            .values_uniform(values.clone())
            .query(),
        5,
    );
    db.exec_mut(
        QueryBuilder::insert()
            .edges()
            .from([1, 3])
            .to([3, 5])
            .values_uniform(values)
            .query(),
        2,
    );

    db.exec_count_elements(
        QueryBuilder::select().key_count().search().from(3).query(),
        9,
        &[
            DbElement {
                id: DbId(3),
                from: DbId(-7),
                to: DbId(-6),
                values: vec![("key_count", 3_u64).into()],
            },
            DbElement {
                id: DbId(-7),
                from: DbId(3),
                to: DbId(5),
                values: vec![("key_count", 3_u64).into()],
            },
            DbElement {
                id: DbId(5),
                from: DbId::default(),
                to: DbId(-7),
                values: vec![("key_count", 3_u64).into()],
            },
        ],
    );
}
