1
//! Integration tests for the CRUD operations available in `Canyon` that
2
//! generates and executes *INSERT* statements
3
use canyon_sql::crud::CrudOperations;
4

            
5
#[cfg(feature = "mysql")]
6
use crate::constants::MYSQL_DS;
7
#[cfg(feature = "postgres")]
8
use crate::constants::PSQL_DS;
9
#[cfg(feature = "mssql")]
10
use crate::constants::SQL_SERVER_DS;
11

            
12
use crate::tests_models::league::*;
13

            
14
/// Deletes a row from the database that is mapped into some instance of a `T` entity.
15
///
16
/// The `t.delete(&self)` operation is only enabled for types that
17
/// has, at least, one of it's fields annotated with a `#[primary_key]`
18
/// operation, because we use that concrete field to construct the clause that targets
19
/// that entity.
20
///
21
/// Attempt of usage the `t.delete(&self)` method on an entity without `#[primary_key]`
22
/// will raise a runtime error.
23
#[cfg(feature = "postgres")]
24
24
#[canyon_sql::macros::canyon_tokio_test]
25
1
fn test_crud_delete_method_operation() {
26
    // For test the delete, we will insert a new instance of the database, and then,
27
    // after inspect it, we will proceed to delete it
28
1
    let mut new_league: League = League {
29
1
        id: Default::default(),
30
        ext_id: 7892635306594_i64,
31
1
        slug: "some-new-league".to_string(),
32
1
        name: "Some New League".to_string(),
33
1
        region: "Bahía de cochinos".to_string(),
34
1
        image_url: "https://nobodyspectsandimage.io".to_string(),
35
    };
36

            
37
    // We insert the instance on the database, on the `League` entity
38
3
    new_league.insert().await.expect("Failed insert operation");
39

            
40
2
    assert_eq!(
41
        new_league.id,
42
4
        League::find_by_pk_datasource(&new_league.id, PSQL_DS)
43
4
            .await
44
            .expect("Request error")
45
            .expect("None value")
46
            .id
47
    );
48

            
49
    // Now that we have an instance mapped to some entity by a primary key, we can now
50
    // remove that entry from the database with the delete operation
51
4
    new_league
52
        .delete()
53
4
        .await
54
        .expect("Failed to delete the operation");
55

            
56
    // To check the success, we can query by the primary key value and check if, after unwrap()
57
    // the result of the operation, the find by primary key contains Some(v) or None
58
    // Remember that `find_by_primary_key(&dyn QueryParameter<'a>) -> Result<Option<T>>, Err>
59
1
    assert_eq!(
60
4
        League::find_by_pk(&new_league.id)
61
4
            .await
62
            .expect("Unwrapping the result, letting the Option<T>"),
63
        None
64
    );
65
}
66

            
67
/// Same as the delete test, but performing the operations with the specified datasource
68
#[cfg(feature = "mssql")]
69
21
#[canyon_sql::macros::canyon_tokio_test]
70
1
fn test_crud_delete_datasource_mssql_method_operation() {
71
    // For test the delete, we will insert a new instance of the database, and then,
72
    // after inspect it, we will proceed to delete it
73
1
    let mut new_league: League = League {
74
1
        id: Default::default(),
75
        ext_id: 7892635306594_i64,
76
1
        slug: "some-new-league".to_string(),
77
1
        name: "Some New League".to_string(),
78
1
        region: "Bahía de cochinos".to_string(),
79
1
        image_url: "https://nobodyspectsandimage.io".to_string(),
80
    };
81

            
82
    // We insert the instance on the database, on the `League` entity
83
4
    new_league
84
        .insert_datasource(SQL_SERVER_DS)
85
3
        .await
86
        .expect("Failed insert operation");
87
2
    assert_eq!(
88
        new_league.id,
89
4
        League::find_by_pk_datasource(&new_league.id, SQL_SERVER_DS)
90
3
            .await
91
            .expect("Request error")
92
            .expect("None value")
93
            .id
94
    );
95

            
96
    // Now that we have an instance mapped to some entity by a primary key, we can now
97
    // remove that entry from the database with the delete operation
98
4
    new_league
99
        .delete_datasource(SQL_SERVER_DS)
100
3
        .await
101
        .expect("Failed to delete the operation");
102

            
103
    // To check the success, we can query by the primary key value and check if, after unwrap()
104
    // the result of the operation, the find by primary key contains Some(v) or None
105
    // Remember that `find_by_primary_key(&dyn QueryParameter<'a>) -> Result<Option<T>>, Err>
106
1
    assert_eq!(
107
4
        League::find_by_pk_datasource(&new_league.id, SQL_SERVER_DS)
108
3
            .await
109
            .expect("Unwrapping the result, letting the Option<T>"),
110
        None
111
    );
112
}
113

            
114
/// Same as the delete test, but performing the operations with the specified datasource
115
#[cfg(feature = "mysql")]
116
33
#[canyon_sql::macros::canyon_tokio_test]
117
1
fn test_crud_delete_datasource_mysql_method_operation() {
118
    // For test the delete, we will insert a new instance of the database, and then,
119
    // after inspect it, we will proceed to delete it
120
1
    let mut new_league: League = League {
121
1
        id: Default::default(),
122
        ext_id: 7892635306594_i64,
123
1
        slug: "some-new-league".to_string(),
124
1
        name: "Some New League".to_string(),
125
1
        region: "Bahía de cochinos".to_string(),
126
1
        image_url: "https://nobodyspectsandimage.io".to_string(),
127
    };
128

            
129
    // We insert the instance on the database, on the `League` entity
130
4
    new_league
131
        .insert_datasource(MYSQL_DS)
132
10
        .await
133
        .expect("Failed insert operation");
134
2
    assert_eq!(
135
        new_league.id,
136
4
        League::find_by_pk_datasource(&new_league.id, MYSQL_DS)
137
8
            .await
138
            .expect("Request error")
139
            .expect("None value")
140
            .id
141
    );
142

            
143
    // Now that we have an instance mapped to some entity by a primary key, we can now
144
    // remove that entry from the database with the delete operation
145
4
    new_league
146
        .delete_datasource(MYSQL_DS)
147
4
        .await
148
        .expect("Failed to delete the operation");
149

            
150
    // To check the success, we can query by the primary key value and check if, after unwrap()
151
    // the result of the operation, the find by primary key contains Some(v) or None
152
    // Remember that `find_by_primary_key(&dyn QueryParameter<'a>) -> Result<Option<T>>, Err>
153
1
    assert_eq!(
154
4
        League::find_by_pk_datasource(&new_league.id, MYSQL_DS)
155
4
            .await
156
            .expect("Unwrapping the result, letting the Option<T>"),
157
        None
158
    );
159
}