#![allow(unused_imports, dead_code)]

pub mod common;

pub use common::{TestContext, features::*, setup::*};
use pretty_assertions::assert_eq;
use sea_orm::{DatabaseConnection, entity::prelude::*, entity::*};

#[sea_orm_macros::test]
fn main() -> Result<(), DbErr> {
    let ctx = TestContext::new("json_vec_tests");
    create_json_vec_table(&ctx.db)?;
    create_json_string_vec_table(&ctx.db)?;
    create_json_struct_vec_table(&ctx.db)?;
    insert_json_vec(&ctx.db)?;
    insert_json_string_vec_derive(&ctx.db)?;
    insert_json_struct_vec_derive(&ctx.db)?;

    ctx.delete();

    Ok(())
}

pub fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> {
    let json_vec = json_vec::Model {
        id: 1,
        str_vec: Some(json_vec::StringVec(vec![
            "1".to_string(),
            "2".to_string(),
            "3".to_string(),
        ])),
    };

    let result = json_vec.clone().into_active_model().insert(db)?;

    assert_eq!(result, json_vec);

    let model = json_vec::Entity::find()
        .filter(json_vec::Column::Id.eq(json_vec.id))
        .one(db)?;

    assert_eq!(model, Some(json_vec));

    Ok(())
}

pub fn insert_json_string_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
    let json_vec = json_vec_derive::json_string_vec::Model {
        id: 1,
        str_vec: Some(json_vec_derive::json_string_vec::StringVec(vec![
            "4".to_string(),
            "5".to_string(),
            "6".to_string(),
        ])),
    };

    let result = json_vec_derive::json_string_vec::ActiveModel {
        id: NotSet,
        ..json_vec.clone().into_active_model()
    }
    .insert(db)?;

    assert_eq!(result, json_vec);

    let model = json_vec_derive::json_string_vec::Entity::find()
        .filter(json_vec_derive::json_string_vec::Column::Id.eq(json_vec.id))
        .one(db)?;

    assert_eq!(model, Some(json_vec));

    let result = json_vec_derive::json_string_vec::ActiveModel {
        id: NotSet,
        str_vec: Set(None),
    }
    .insert(db)?;

    assert_eq!(result.str_vec, None);

    Ok(())
}

pub fn insert_json_struct_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
    let json_vec = json_vec_derive::json_struct_vec::Model {
        id: 2,
        struct_vec: vec![
            json_vec_derive::json_struct_vec::JsonColumn {
                value: "4".to_string(),
            },
            json_vec_derive::json_struct_vec::JsonColumn {
                value: "5".to_string(),
            },
            json_vec_derive::json_struct_vec::JsonColumn {
                value: "6".to_string(),
            },
        ],
    };

    let _result = json_vec.clone().into_active_model().insert(db)?;

    let model = json_vec_derive::json_struct_vec::Entity::find()
        .filter(json_vec_derive::json_struct_vec::Column::Id.eq(json_vec.id))
        .one(db)?;

    assert_eq!(model, Some(json_vec));

    Ok(())
}
