1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Test harness for applying a generic test suite to any backend-specific
//! schemer adapter.

use super::*;

/// A trait required for running the generic test suite on an `Adapter`.
pub trait TestAdapter: Adapter {
    /// Construct a mock, no-op migration of the adapter's `MigrationType`.
    ///
    /// For convenience adapters can implement their migration traits on
    /// `TestMigration` and construct those here.
    fn mock(id: Uuid, dependencies: HashSet<Uuid>) -> Box<Self::MigrationType>;
}

/// A trivial struct implementing `Migration` on which adapters can build their
/// mock migrations.
pub struct TestMigration {
    id: Uuid,
    dependencies: HashSet<Uuid>,
}

impl TestMigration {
    pub fn new(id: Uuid, dependencies: HashSet<Uuid>) -> TestMigration {
        TestMigration { id, dependencies }
    }
}

impl Migration for TestMigration {
    fn id(&self) -> Uuid {
        self.id
    }

    fn dependencies(&self) -> HashSet<Uuid> {
        self.dependencies.clone()
    }

    fn description(&self) -> &'static str {
        "Test Migration"
    }
}

/// Test an `Adapter` with the generic test suite.
///
/// Note that the adapter must also implement the `TestAdapter` trait. This
/// should be done only for the testing configuration of the adapter's crate,
/// as it is not necessary for normal behavior.
///
/// # Examples
///
/// ```rust,ignore
/// #[macro_use] extern crate schemer;
///
/// fn construct_my_adapter_test_fixture() -> MyAdapterType {
///     MyAdapterType {}
/// }
///
/// test_schemer_adapter!(construct_my_adapter_test_fixture());
/// ```
#[macro_export]
macro_rules! test_schemer_adapter {
    ($constructor:expr) => {
        test_schemer_adapter!({}, $constructor);
    };
    ($setup:stmt, $constructor:expr) => {
        test_schemer_adapter!($setup, $constructor,
            test_single_migration,
            test_migration_chain,
            test_multi_component_dag,
            test_branching_dag,
        );
    };
    ($setup:stmt, $constructor:expr, $($test_fn:ident),* $(,)*) => {
        $(
            #[test]
            fn $test_fn() {
                $setup
                let adapter = $constructor;
                $crate::testing::$test_fn(adapter);
            }
        )*
    }
}

/// Test the application and reversion of a singleton migration.
pub fn test_single_migration<A: TestAdapter>(adapter: A) {
    let migration1 = A::mock(
        Uuid::parse_str("bc960dc8-0e4a-4182-a62a-8e776d1e2b30").unwrap(),
        HashSet::new(),
    );
    let uuid1 = migration1.id();

    let mut migrator: Migrator<A> = Migrator::new(adapter);

    migrator
        .register(migration1)
        .expect("Migration 1 registration failed");
    migrator.up(None).expect("Up migration failed");

    assert!(migrator
        .adapter
        .applied_migrations()
        .unwrap()
        .contains(&uuid1,));

    migrator.down(None).expect("Down migration failed");

    assert!(!migrator
        .adapter
        .applied_migrations()
        .unwrap()
        .contains(&uuid1,));
}

/// Test the partial application and reversion of a chain of three dependent
/// migrations.
pub fn test_migration_chain<A: TestAdapter>(adapter: A) {
    let migration1 = A::mock(
        Uuid::parse_str("bc960dc8-0e4a-4182-a62a-8e776d1e2b30").unwrap(),
        HashSet::new(),
    );
    let migration2 = A::mock(
        Uuid::parse_str("4885e8ab-dafa-4d76-a565-2dee8b04ef60").unwrap(),
        vec![migration1.id()].into_iter().collect(),
    );
    let migration3 = A::mock(
        Uuid::parse_str("c5d07448-851f-45e8-8fa7-4823d5250609").unwrap(),
        vec![migration2.id()].into_iter().collect(),
    );

    let uuid1 = migration1.id();
    let uuid2 = migration2.id();
    let uuid3 = migration3.id();

    let mut migrator = Migrator::new(adapter);

    migrator
        .register_multiple(vec![migration1, migration2, migration3])
        .expect("Migration registration failed");

    migrator.up(Some(uuid2)).expect("Up migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
    }

    migrator.down(Some(uuid1)).expect("Down migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(!applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
    }
}

/// Test that application and reversion of two DAG components are independent.
pub fn test_multi_component_dag<A: TestAdapter>(adapter: A) {
    let migration1 = A::mock(
        Uuid::parse_str("bc960dc8-0e4a-4182-a62a-8e776d1e2b30").unwrap(),
        HashSet::new(),
    );
    let migration2 = A::mock(
        Uuid::parse_str("4885e8ab-dafa-4d76-a565-2dee8b04ef60").unwrap(),
        vec![migration1.id()].into_iter().collect(),
    );
    let migration3 = A::mock(
        Uuid::parse_str("c5d07448-851f-45e8-8fa7-4823d5250609").unwrap(),
        HashSet::new(),
    );
    let migration4 = A::mock(
        Uuid::parse_str("9433a432-386f-467e-a59f-a9fb7e249767").unwrap(),
        vec![migration3.id()].into_iter().collect(),
    );

    let uuid1 = migration1.id();
    let uuid2 = migration2.id();
    let uuid3 = migration3.id();
    let uuid4 = migration4.id();

    let mut migrator = Migrator::new(adapter);

    migrator
        .register_multiple(vec![migration1, migration2, migration3, migration4])
        .expect("Migration registration failed");

    migrator.up(Some(uuid2)).expect("Up migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
        assert!(!applied.contains(&uuid4));
    }

    migrator.down(Some(uuid1)).expect("Down migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(!applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
        assert!(!applied.contains(&uuid4));
    }

    migrator.up(Some(uuid3)).expect("Up migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(!applied.contains(&uuid2));
        assert!(applied.contains(&uuid3));
        assert!(!applied.contains(&uuid4));
    }

    migrator.up(None).expect("Up migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(applied.contains(&uuid2));
        assert!(applied.contains(&uuid3));
        assert!(applied.contains(&uuid4));
    }

    migrator.down(None).expect("Down migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(!applied.contains(&uuid1));
        assert!(!applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
        assert!(!applied.contains(&uuid4));
    }
}

/// Test application and reversion on a branching DAG.
pub fn test_branching_dag<A: TestAdapter>(adapter: A) {
    let migration1 = A::mock(
        Uuid::parse_str("bc960dc8-0e4a-4182-a62a-8e776d1e2b30").unwrap(),
        HashSet::new(),
    );
    let migration2 = A::mock(
        Uuid::parse_str("4885e8ab-dafa-4d76-a565-2dee8b04ef60").unwrap(),
        HashSet::new(),
    );
    let migration3 = A::mock(
        Uuid::parse_str("c5d07448-851f-45e8-8fa7-4823d5250609").unwrap(),
        vec![migration1.id(), migration2.id()].into_iter().collect(),
    );
    let migration4 = A::mock(
        Uuid::parse_str("9433a432-386f-467e-a59f-a9fb7e249767").unwrap(),
        vec![migration3.id()].into_iter().collect(),
    );
    let migration5 = A::mock(
        Uuid::parse_str("0940acb1-0e2e-4b99-9d69-2302a9c74524").unwrap(),
        vec![migration3.id()].into_iter().collect(),
    );

    let uuid1 = migration1.id();
    let uuid2 = migration2.id();
    let uuid3 = migration3.id();
    let uuid4 = migration4.id();
    let uuid5 = migration5.id();

    let mut migrator = Migrator::new(adapter);

    migrator
        .register_multiple(vec![
            migration1, migration2, migration3, migration4, migration5,
        ])
        .expect("Migration registration failed");

    migrator.up(Some(uuid4)).expect("Up migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(applied.contains(&uuid2));
        assert!(applied.contains(&uuid3));
        assert!(applied.contains(&uuid4));
        assert!(!applied.contains(&uuid5));
    }

    migrator.down(Some(uuid1)).expect("Down migration failed");

    {
        let applied = migrator.adapter.applied_migrations().unwrap();
        assert!(applied.contains(&uuid1));
        assert!(applied.contains(&uuid2));
        assert!(!applied.contains(&uuid3));
        assert!(!applied.contains(&uuid4));
        assert!(!applied.contains(&uuid5));
    }
}