1
// Integration tests for the CRUD operations available in `Canyon` that
2
// generates and executes *SELECT* statements based on a entity
3
// annotated with the `#[foreign_key(... args)]` annotation looking
4
// for the related data with some entity `U` that acts as is parent, where `U`
5
// impls `ForeignKeyable` (isn't required, but it won't unlock the
6
// reverse search features parent -> child, only the child -> parent ones).
7
///
8
// Names of the foreign key methods are autogenerated for the direct and
9
// reverse side of the implementations.
10
// For more info: TODO -> Link to the docs of the foreign key chapter
11
use canyon_sql::crud::CrudOperations;
12

            
13
#[cfg(feature = "mssql")]
14
use crate::constants::MYSQL_DS;
15
#[cfg(feature = "mssql")]
16
use crate::constants::SQL_SERVER_DS;
17

            
18
use crate::tests_models::league::*;
19
use crate::tests_models::tournament::*;
20

            
21
/// Given an entity `T` which has some field declaring a foreign key relation
22
/// with some another entity `U`, for example, performs a search to find
23
/// what is the parent type `U` of `T`
24
#[cfg(feature = "postgres")]
25
19
#[canyon_sql::macros::canyon_tokio_test]
26
1
fn test_crud_search_by_foreign_key() {
27
4
    let some_tournament: Tournament = Tournament::find_by_pk(&1)
28
4
        .await
29
        .expect("Result variant of the query is err")
30
        .expect("No result found for the given parameter");
31

            
32
    // We can get the parent entity for the retrieved child instance
33
4
    let parent_entity: Option<League> = some_tournament
34
        .search_league()
35
4
        .await
36
1
        .expect("Result variant of the query is err");
37

            
38
1
    if let Some(league) = parent_entity {
39
1
        assert_eq!(some_tournament.league, league.id)
40
1
    } else {
41
        assert_eq!(parent_entity, None)
42
    }
43
}
44

            
45
/// Same as the search by foreign key, but with the specified datasource
46
#[cfg(feature = "mssql")]
47
17
#[canyon_sql::macros::canyon_tokio_test]
48
1
fn test_crud_search_by_foreign_key_datasource_mssql() {
49
4
    let some_tournament: Tournament = Tournament::find_by_pk_datasource(&10, SQL_SERVER_DS)
50
3
        .await
51
        .expect("Result variant of the query is err")
52
        .expect("No result found for the given parameter");
53

            
54
    // We can get the parent entity for the retrieved child instance
55
4
    let parent_entity: Option<League> = some_tournament
56
        .search_league_datasource(SQL_SERVER_DS)
57
3
        .await
58
1
        .expect("Result variant of the query is err");
59

            
60
    // These are tests, and we could unwrap the result contained in the option, because
61
    // it always should exist that search for the data inserted when the docker starts.
62
    // But, just for change the style a little bit and offer more options about how to
63
    // handle things done with Canyon
64
1
    if let Some(league) = parent_entity {
65
1
        assert_eq!(some_tournament.league, league.id)
66
1
    } else {
67
        assert_eq!(parent_entity, None)
68
    }
69
}
70

            
71
/// Same as the search by foreign key, but with the specified datasource
72
#[cfg(feature = "mysql")]
73
31
#[canyon_sql::macros::canyon_tokio_test]
74
1
fn test_crud_search_by_foreign_key_datasource_mysql() {
75
4
    let some_tournament: Tournament = Tournament::find_by_pk_datasource(&10, MYSQL_DS)
76
9
        .await
77
        .expect("Result variant of the query is err")
78
        .expect("No result found for the given parameter");
79

            
80
    // We can get the parent entity for the retrieved child instance
81
4
    let parent_entity: Option<League> = some_tournament
82
        .search_league_datasource(MYSQL_DS)
83
9
        .await
84
1
        .expect("Result variant of the query is err");
85

            
86
    // These are tests, and we could unwrap the result contained in the option, because
87
    // it always should exist that search for the data inserted when the docker starts.
88
    // But, just for change the style a little bit and offer more options about how to
89
    // handle things done with Canyon
90
1
    if let Some(league) = parent_entity {
91
1
        assert_eq!(some_tournament.league, league.id)
92
1
    } else {
93
        assert_eq!(parent_entity, None)
94
    }
95
}
96

            
97
/// Given an entity `U` that is know as the "parent" side of the relation with another
98
/// entity `T`, for example, we can ask to the parent for the childrens that belongs
99
/// to `U`.
100
///
101
/// For this to work, `U`, the parent, must have derived the `ForeignKeyable` proc macro
102
#[cfg(feature = "postgres")]
103
20
#[canyon_sql::macros::canyon_tokio_test]
104
1
fn test_crud_search_reverse_side_foreign_key() {
105
4
    let some_league: League = League::find_by_pk(&1)
106
4
        .await
107
        .expect("Result variant of the query is err")
108
        .expect("No result found for the given parameter");
109

            
110
    // Computes how many tournaments are pointing to the retrieved league
111
4
    let child_tournaments: Vec<Tournament> = Tournament::search_league_childrens(&some_league)
112
4
        .await
113
        .expect("Result variant of the query is err");
114

            
115
1
    assert!(!child_tournaments.is_empty());
116
2
    child_tournaments
117
        .iter()
118
2
        .for_each(|t| assert_eq!(t.league, some_league.id));
119
}
120

            
121
/// Same as the search by the reverse side of a foreign key relation
122
/// but with the specified datasource
123
#[cfg(feature = "mssql")]
124
19
#[canyon_sql::macros::canyon_tokio_test]
125
1
fn test_crud_search_reverse_side_foreign_key_datasource_mssql() {
126
4
    let some_league: League = League::find_by_pk_datasource(&1, SQL_SERVER_DS)
127
3
        .await
128
        .expect("Result variant of the query is err")
129
        .expect("No result found for the given parameter");
130

            
131
    // Computes how many tournaments are pointing to the retrieved league
132
    let child_tournaments: Vec<Tournament> =
133
4
        Tournament::search_league_childrens_datasource(&some_league, SQL_SERVER_DS)
134
3
            .await
135
            .expect("Result variant of the query is err");
136

            
137
1
    assert!(!child_tournaments.is_empty());
138
2
    child_tournaments
139
        .iter()
140
2
        .for_each(|t| assert_eq!(t.league, some_league.id));
141
}
142

            
143
/// Same as the search by the reverse side of a foreign key relation
144
/// but with the specified datasource
145
#[cfg(feature = "mysql")]
146
30
#[canyon_sql::macros::canyon_tokio_test]
147
1
fn test_crud_search_reverse_side_foreign_key_datasource_mysql() {
148
4
    let some_league: League = League::find_by_pk_datasource(&1, MYSQL_DS)
149
9
        .await
150
        .expect("Result variant of the query is err")
151
        .expect("No result found for the given parameter");
152

            
153
    // Computes how many tournaments are pointing to the retrieved league
154
    let child_tournaments: Vec<Tournament> =
155
4
        Tournament::search_league_childrens_datasource(&some_league, MYSQL_DS)
156
9
            .await
157
            .expect("Result variant of the query is err");
158

            
159
1
    assert!(!child_tournaments.is_empty());
160
2
    child_tournaments
161
        .iter()
162
2
        .for_each(|t| assert_eq!(t.league, some_league.id));
163
}