1
use crate::constants::SQL_SERVER_CREATE_TABLES;
2
use crate::constants::SQL_SERVER_DS;
3
use crate::constants::SQL_SERVER_FILL_TABLE_VALUES;
4
use crate::tests_models::league::League;
5

            
6
use canyon_sql::crud::CrudOperations;
7
use canyon_sql::db_clients::tiberius::{Client, Config};
8
use canyon_sql::runtime::tokio::net::TcpStream;
9
use canyon_sql::runtime::tokio_util::compat::TokioAsyncWriteCompatExt;
10

            
11
/// In order to initialize data on `SqlServer`. we must manually insert it
12
/// when the docker starts. SqlServer official docker from Microsoft does
13
/// not allow you to run `.sql` files against the database (not at least, without)
14
/// using a workaround. So, we are going to query the `SqlServer` to check if already
15
/// has some data (other processes, persistence or multi-threading envs), af if not,
16
/// we are going to retrieve the inserted data on the `postgreSQL` at start-up and
17
/// inserting into the `SqlServer` instance.
18
///
19
/// This will be marked as `#[ignore]`, so we can force to run first the marked as
20
/// ignored, check the data available, perform the necessary init operations and
21
/// then *cargo test <args...>* the real integration tests
22
15
#[canyon_sql::macros::canyon_tokio_test]
23
#[ignore]
24
1
fn initialize_sql_server_docker_instance() {
25
    static CONN_STR: &str =
26
        "server=tcp:localhost,1434;User Id=SA;Password=SqlServer-10;TrustServerCertificate=true";
27

            
28
17
    canyon_sql::runtime::futures::executor::block_on(async {
29
1
        let config = Config::from_ado_string(CONN_STR).unwrap();
30

            
31
3
        let tcp = TcpStream::connect(config.get_addr()).await.unwrap();
32
3
        let tcp2 = TcpStream::connect(config.get_addr()).await.unwrap();
33
1
        tcp.set_nodelay(true).ok();
34

            
35
4
        let mut client = Client::connect(config.clone(), tcp.compat_write())
36
6
            .await
37
            .unwrap();
38

            
39
        // Create the tables
40
2
        let query_result = client.query(SQL_SERVER_CREATE_TABLES, &[]).await;
41
1
        assert!(query_result.is_ok());
42

            
43
2
        let leagues_sql = League::find_all_datasource(SQL_SERVER_DS).await;
44
1
        println!("LSQL ERR: {leagues_sql:?}");
45
1
        assert!(leagues_sql.is_ok());
46

            
47
1
        match leagues_sql {
48
1
            Ok(ref leagues) => {
49
1
                let leagues_len = leagues.len();
50
1
                println!("Leagues already inserted on SQLSERVER: {:?}", &leagues_len);
51
1
                if leagues.len() < 10 {
52
4
                    let mut client2 = Client::connect(config, tcp2.compat_write())
53
6
                        .await
54
                        .expect("Can't connect to MSSQL");
55
2
                    let result = client2.query(SQL_SERVER_FILL_TABLE_VALUES, &[]).await;
56
1
                    assert!(result.is_ok());
57
1
                }
58
            }
59
            Err(e) => eprintln!("Error retrieving the leagues: {e}"),
60
        }
61
1
    });
62
}