1
use crate::tests_models::league::*;
2
// Integration tests for the CRUD operations available in `Canyon` that
3
/// generates and executes *UPDATE* statements
4
use canyon_sql::crud::CrudOperations;
5

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

            
11
/// Update operation is a *CRUD* method defined for some entity `T`, that works by appliying
12
/// some change to a Rust's entity instance, and persisting them into the database.
13
///
14
/// The `t.update(&self)` operation is only enabled for types that
15
/// has, at least, one of it's fields annotated with a `#[primary_key]`
16
/// operation, because we use that concrete field to construct the clause that targets
17
/// that entity.
18
///
19
/// Attempt of usage the `t.update(&self)` method on an entity without `#[primary_key]`
20
/// will raise a runtime error.
21
#[cfg(feature = "postgres")]
22
24
#[canyon_sql::macros::canyon_tokio_test]
23
1
fn test_crud_update_method_operation() {
24
    // We first retrieve some entity from the database. Note that we must make
25
    // the retrieved instance mutable of clone it to a new mutable resource
26
4
    let mut updt_candidate: League = League::find_by_pk(&1)
27
4
        .await
28
        .expect("[1] - Failed the query to the database")
29
        .expect("[1] - No entity found for the primary key value passed in");
30

            
31
    // The ext_id field value is extracted from the sql scripts under the
32
    // docker/sql folder. We are retrieving the first entity inserted at the
33
    // wake up time of the database, and now checking some of its properties.
34
1
    assert_eq!(updt_candidate.ext_id, 100695891328981122_i64);
35

            
36
    // Modify the value, and perform the update
37
1
    let updt_value: i64 = 593064_i64;
38
1
    updt_candidate.ext_id = updt_value;
39
4
    updt_candidate
40
        .update()
41
4
        .await
42
        .expect("Failed the update operation");
43

            
44
    // Retrieve it again, and check if the value was really updated
45
4
    let updt_entity: League = League::find_by_pk(&1)
46
4
        .await
47
        .expect("[2] - Failed the query to the database")
48
        .expect("[2] - No entity found for the primary key value passed in");
49

            
50
1
    assert_eq!(updt_entity.ext_id, updt_value);
51

            
52
    // We rollback the changes to the initial value to don't broke other tests
53
    // the next time that will run
54
1
    updt_candidate.ext_id = 100695891328981122_i64;
55
4
    updt_candidate
56
        .update()
57
4
        .await
58
        .expect("Failed the restablish initial value update operation");
59
}
60

            
61
/// Same as the above test, but with the specified datasource.
62
#[cfg(feature = "mssql")]
63
20
#[canyon_sql::macros::canyon_tokio_test]
64
1
fn test_crud_update_datasource_mssql_method_operation() {
65
    // We first retrieve some entity from the database. Note that we must make
66
    // the retrieved instance mutable of clone it to a new mutable resource
67
4
    let mut updt_candidate: League = League::find_by_pk_datasource(&1, SQL_SERVER_DS)
68
3
        .await
69
        .expect("[1] - Failed the query to the database")
70
        .expect("[1] - No entity found for the primary key value passed in");
71

            
72
    // The ext_id field value is extracted from the sql scripts under the
73
    // docker/sql folder. We are retrieving the first entity inserted at the
74
    // wake up time of the database, and now checking some of its properties.
75
1
    assert_eq!(updt_candidate.ext_id, 100695891328981122_i64);
76

            
77
    // Modify the value, and perform the update
78
1
    let updt_value: i64 = 59306442534_i64;
79
1
    updt_candidate.ext_id = updt_value;
80
4
    updt_candidate
81
        .update_datasource(SQL_SERVER_DS)
82
3
        .await
83
        .expect("Failed the update operation");
84

            
85
    // Retrieve it again, and check if the value was really updated
86
4
    let updt_entity: League = League::find_by_pk_datasource(&1, SQL_SERVER_DS)
87
3
        .await
88
        .expect("[2] - Failed the query to the database")
89
        .expect("[2] - No entity found for the primary key value passed in");
90

            
91
1
    assert_eq!(updt_entity.ext_id, updt_value);
92

            
93
    // We rollback the changes to the initial value to don't broke other tests
94
    // the next time that will run
95
1
    updt_candidate.ext_id = 100695891328981122_i64;
96
4
    updt_candidate
97
        .update_datasource(SQL_SERVER_DS)
98
3
        .await
99
        .expect("Failed to restablish the initial value update operation");
100
}
101

            
102
/// Same as the above test, but with the specified datasource.
103
#[cfg(feature = "mysql")]
104
35
#[canyon_sql::macros::canyon_tokio_test]
105
1
fn test_crud_update_datasource_mysql_method_operation() {
106
    // We first retrieve some entity from the database. Note that we must make
107
    // the retrieved instance mutable of clone it to a new mutable resource
108

            
109
4
    let mut updt_candidate: League = League::find_by_pk_datasource(&1, MYSQL_DS)
110
9
        .await
111
        .expect("[1] - Failed the query to the database")
112
        .expect("[1] - No entity found for the primary key value passed in");
113

            
114
    // The ext_id field value is extracted from the sql scripts under the
115
    // docker/sql folder. We are retrieving the first entity inserted at the
116
    // wake up time of the database, and now checking some of its properties.
117
1
    assert_eq!(updt_candidate.ext_id, 100695891328981122_i64);
118

            
119
    // Modify the value, and perform the update
120
1
    let updt_value: i64 = 59306442534_i64;
121
1
    updt_candidate.ext_id = updt_value;
122
4
    updt_candidate
123
        .update_datasource(MYSQL_DS)
124
9
        .await
125
        .expect("Failed the update operation");
126

            
127
    // Retrieve it again, and check if the value was really updated
128
4
    let updt_entity: League = League::find_by_pk_datasource(&1, MYSQL_DS)
129
4
        .await
130
        .expect("[2] - Failed the query to the database")
131
        .expect("[2] - No entity found for the primary key value passed in");
132

            
133
1
    assert_eq!(updt_entity.ext_id, updt_value);
134

            
135
    // We rollback the changes to the initial value to don't broke other tests
136
    // the next time that will run
137
1
    updt_candidate.ext_id = 100695891328981122_i64;
138
4
    updt_candidate
139
        .update_datasource(MYSQL_DS)
140
4
        .await
141
        .expect("Failed to restablish the initial value update operation");
142
}