1
#![allow(clippy::nonminimal_bool)]
2

            
3
#[cfg(feature = "mysql")]
4
use crate::constants::MYSQL_DS;
5

            
6
#[cfg(feature = "mssql")]
7
use crate::constants::SQL_SERVER_DS;
8
// Integration tests for the CRUD operations available in `Canyon` that
9
/// generates and executes *SELECT* statements
10
use crate::Error;
11
use canyon_sql::crud::CrudOperations;
12

            
13
use crate::tests_models::league::*;
14
use crate::tests_models::player::*;
15

            
16
/// Tests the behaviour of a SELECT * FROM {table_name} within Canyon, through the
17
/// `::find_all()` associated function derived with the `CanyonCrud` derive proc-macro
18
/// and using the *default datasource*
19
#[cfg(feature = "postgres")]
20
22
#[canyon_sql::macros::canyon_tokio_test]
21
1
fn test_crud_find_all() {
22
    let find_all_result: Result<Vec<League>, Box<dyn Error + Send + Sync>> =
23
3
        League::find_all().await;
24

            
25
    // Connection doesn't return an error
26
1
    assert!(!find_all_result.is_err());
27
1
    assert!(!find_all_result.unwrap().is_empty());
28

            
29
    let find_all_players: Result<Vec<Player>, Box<dyn Error + Send + Sync>> =
30
4
        Player::find_all().await;
31
1
    assert!(!find_all_players.unwrap().is_empty());
32
}
33

            
34
/// Same as the `find_all()`, but with the unchecked variant, which directly returns `Vec<T>` not
35
/// `Result` wrapped
36
#[cfg(feature = "postgres")]
37
18
#[canyon_sql::macros::canyon_tokio_test]
38
1
fn test_crud_find_all_unchecked() {
39
3
    let find_all_result: Vec<League> = League::find_all_unchecked().await;
40
1
    assert!(!find_all_result.is_empty());
41
}
42

            
43
/// Tests the behaviour of a SELECT * FROM {table_name} within Canyon, through the
44
/// `::find_all()` associated function derived with the `CanyonCrud` derive proc-macro
45
/// and using the specified datasource
46
#[cfg(feature = "mssql")]
47
18
#[canyon_sql::macros::canyon_tokio_test]
48
1
fn test_crud_find_all_datasource_mssql() {
49
    let find_all_result: Result<Vec<League>, Box<dyn Error + Send + Sync>> =
50
2
        League::find_all_datasource(SQL_SERVER_DS).await;
51
    // Connection doesn't return an error
52
1
    assert!(!find_all_result.is_err());
53
1
    assert!(!find_all_result.unwrap().is_empty());
54
}
55

            
56
#[cfg(feature = "mysql")]
57
22
#[canyon_sql::macros::canyon_tokio_test]
58
1
fn test_crud_find_all_datasource_mysql() {
59
    let find_all_result: Result<Vec<League>, Box<dyn Error + Send + Sync>> =
60
7
        League::find_all_datasource(MYSQL_DS).await;
61
    // Connection doesn't return an error
62
1
    assert!(!find_all_result.is_err());
63
1
    assert!(!find_all_result.unwrap().is_empty());
64
}
65

            
66
/// Same as the `find_all_datasource()`, but with the unchecked variant and the specified dataosource,
67
/// returning directly `Vec<T>` and not `Result<Vec<T>, Err>`
68
#[cfg(feature = "mssql")]
69
17
#[canyon_sql::macros::canyon_tokio_test]
70
1
fn test_crud_find_all_unchecked_datasource() {
71
2
    let find_all_result: Vec<League> = League::find_all_unchecked_datasource(SQL_SERVER_DS).await;
72
1
    assert!(!find_all_result.is_empty());
73
}
74

            
75
/// Tests the behaviour of a SELECT * FROM {table_name} WHERE <pk> = <pk_value>, where the pk is
76
/// defined with the #[primary_key] attribute over some field of the type.
77
///
78
/// Uses the *default datasource*.
79
#[cfg(feature = "postgres")]
80
18
#[canyon_sql::macros::canyon_tokio_test]
81
1
fn test_crud_find_by_pk() {
82
    let find_by_pk_result: Result<Option<League>, Box<dyn Error + Send + Sync>> =
83
3
        League::find_by_pk(&1).await;
84
1
    assert!(find_by_pk_result.as_ref().unwrap().is_some());
85

            
86
1
    let some_league = find_by_pk_result.unwrap().unwrap();
87
1
    assert_eq!(some_league.id, 1);
88
1
    assert_eq!(some_league.ext_id, 100695891328981122_i64);
89
1
    assert_eq!(some_league.slug, "european-masters");
90
1
    assert_eq!(some_league.name, "European Masters");
91
1
    assert_eq!(some_league.region, "EUROPE");
92
1
    assert_eq!(
93
        some_league.image_url,
94
        "http://static.lolesports.com/leagues/EM_Bug_Outline1.png"
95
    );
96
}
97

            
98
/// Tests the behaviour of a SELECT * FROM {table_name} WHERE <pk> = <pk_value>, where the pk is
99
/// defined with the #[primary_key] attribute over some field of the type.
100
///
101
/// Uses the *specified datasource mssql* in the second parameter of the function call.
102
#[cfg(feature = "mssql")]
103
17
#[canyon_sql::macros::canyon_tokio_test]
104
1
fn test_crud_find_by_pk_datasource_mssql() {
105
    let find_by_pk_result: Result<Option<League>, Box<dyn Error + Send + Sync>> =
106
2
        League::find_by_pk_datasource(&27, SQL_SERVER_DS).await;
107
1
    assert!(find_by_pk_result.as_ref().unwrap().is_some());
108

            
109
1
    let some_league = find_by_pk_result.unwrap().unwrap();
110
1
    assert_eq!(some_league.id, 27);
111
1
    assert_eq!(some_league.ext_id, 107898214974993351_i64);
112
1
    assert_eq!(some_league.slug, "college_championship");
113
1
    assert_eq!(some_league.name, "College Championship");
114
1
    assert_eq!(some_league.region, "NORTH AMERICA");
115
1
    assert_eq!(
116
        some_league.image_url,
117
        "http://static.lolesports.com/leagues/1646396098648_CollegeChampionshiplogo.png"
118
    );
119
}
120

            
121
/// Tests the behaviour of a SELECT * FROM {table_name} WHERE <pk> = <pk_value>, where the pk is
122
/// defined with the #[primary_key] attribute over some field of the type.
123
///
124
/// Uses the *specified datasource mysql* in the second parameter of the function call.
125
#[cfg(feature = "mysql")]
126
22
#[canyon_sql::macros::canyon_tokio_test]
127
1
fn test_crud_find_by_pk_datasource_mysql() {
128
    let find_by_pk_result: Result<Option<League>, Box<dyn Error + Send + Sync>> =
129
8
        League::find_by_pk_datasource(&27, MYSQL_DS).await;
130
1
    assert!(find_by_pk_result.as_ref().unwrap().is_some());
131

            
132
1
    let some_league = find_by_pk_result.unwrap().unwrap();
133
1
    assert_eq!(some_league.id, 27);
134
1
    assert_eq!(some_league.ext_id, 107898214974993351_i64);
135
1
    assert_eq!(some_league.slug, "college_championship");
136
1
    assert_eq!(some_league.name, "College Championship");
137
1
    assert_eq!(some_league.region, "NORTH AMERICA");
138
1
    assert_eq!(
139
        some_league.image_url,
140
        "http://static.lolesports.com/leagues/1646396098648_CollegeChampionshiplogo.png"
141
    );
142
}
143

            
144
/// Counts how many rows contains an entity on the target database.
145
#[cfg(feature = "postgres")]
146
20
#[canyon_sql::macros::canyon_tokio_test]
147
1
fn test_crud_count_operation() {
148
2
    assert_eq!(
149
3
        League::find_all().await.unwrap().len() as i64,
150
3
        League::count().await.unwrap()
151
    );
152
}
153

            
154
/// Counts how many rows contains an entity on the target database using
155
/// the specified datasource mssql
156
#[cfg(feature = "mssql")]
157
19
#[canyon_sql::macros::canyon_tokio_test]
158
1
fn test_crud_count_datasource_operation_mssql() {
159
2
    assert_eq!(
160
4
        League::find_all_datasource(SQL_SERVER_DS)
161
3
            .await
162
            .unwrap()
163
            .len() as i64,
164
2
        League::count_datasource(SQL_SERVER_DS).await.unwrap()
165
    );
166
}
167

            
168
/// Counts how many rows contains an entity on the target database using
169
/// the specified datasource mysql
170
#[cfg(feature = "mysql")]
171
30
#[canyon_sql::macros::canyon_tokio_test]
172
1
fn test_crud_count_datasource_operation_mysql() {
173
2
    assert_eq!(
174
8
        League::find_all_datasource(MYSQL_DS).await.unwrap().len() as i64,
175
8
        League::count_datasource(MYSQL_DS).await.unwrap()
176
    );
177
}