flix-db 0.0.19

Types for storing persistent data about media
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! This module contains entities for storing dynamic data from TMDB

/// Collection entity
pub mod collections {
	use flix_model::id::CollectionId as FlixId;
	use flix_tmdb::model::id::CollectionId;

	use chrono::{DateTime, Utc};
	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a tmdb collection
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_tmdb_collections")]
	pub struct Model {
		/// The collection's TMDB ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_id: CollectionId,
		/// The collection's ID
		#[sea_orm(unique)]
		pub flix_id: FlixId,
		/// The date of the last update
		pub last_update: DateTime<Utc>,
		/// The number of movies in the collection
		pub movie_count: u16,

		/// The info for this collection
		#[sea_orm(
			belongs_to,
			from = "flix_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::collections::Entity>,

		/// Movies that are in this collection
		#[sea_orm(has_many)]
		pub movies: HasMany<super::movies::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Movie entity
pub mod movies {
	use flix_model::id::MovieId as FlixId;
	use flix_tmdb::model::id::{CollectionId, MovieId};

	use seamantic::model::duration::Seconds;

	use chrono::{DateTime, Utc};
	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a tmdb movie
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_tmdb_movies")]
	pub struct Model {
		/// The movie's TMDB ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_id: MovieId,
		/// The movie's ID
		#[sea_orm(unique)]
		pub flix_id: FlixId,
		/// The date of the last update
		pub last_update: DateTime<Utc>,
		/// The movie's runtime in seconds
		pub runtime: Seconds,
		/// The TMDB ID of the collection this movie belongs to
		#[sea_orm(indexed)]
		pub collection_id: Option<CollectionId>,

		/// The collection this movie belongs to
		#[sea_orm(
			belongs_to,
			from = "collection_id",
			to = "tmdb_id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub collection: HasOne<super::collections::Entity>,
		/// The info for this movie
		#[sea_orm(
			belongs_to,
			from = "flix_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::movies::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Show entity
pub mod shows {
	use flix_model::id::ShowId as FlixId;
	use flix_tmdb::model::id::ShowId;

	use chrono::{DateTime, Utc};
	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a tmdb show
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_tmdb_shows")]
	pub struct Model {
		/// The show's TMDB ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_id: ShowId,
		/// The show's ID
		#[sea_orm(unique)]
		pub flix_id: FlixId,
		/// The movie's runtime in seconds
		pub last_update: DateTime<Utc>,
		/// The number of seasons the show has
		pub number_of_seasons: u32,

		/// The info for this show
		#[sea_orm(
			belongs_to,
			from = "flix_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::shows::Entity>,

		/// Seasons that are part of this show
		#[sea_orm(has_many)]
		pub seasons: HasMany<super::seasons::Entity>,
		/// Episodes that are part of this show
		#[sea_orm(has_many)]
		pub episodes: HasMany<super::episodes::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Season entity
pub mod seasons {
	use flix_model::id::ShowId as FlixId;
	use flix_model::numbers::SeasonNumber;
	use flix_tmdb::model::id::ShowId;

	use chrono::{DateTime, Utc};
	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a tmdb season
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_tmdb_seasons")]
	pub struct Model {
		/// The season's show's TMDB ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_show: ShowId,
		/// The season's TMDB season number
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_season: SeasonNumber,
		/// The season's show's ID
		#[sea_orm(unique_key = "flix")]
		pub flix_show: FlixId,
		/// The season's number
		#[sea_orm(unique_key = "flix")]
		pub flix_season: SeasonNumber,
		/// The date of the last update
		pub last_update: DateTime<Utc>,

		/// The show this season belongs to
		#[sea_orm(
			belongs_to,
			from = "tmdb_show",
			to = "tmdb_id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub show: HasOne<super::shows::Entity>,
		/// The info for this season
		#[sea_orm(
			belongs_to,
			from = "(flix_show, flix_season)",
			to = "(show_id, season_number)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::seasons::Entity>,

		/// Episodes that are part of this season
		#[sea_orm(has_many)]
		pub episodes: HasMany<super::episodes::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Season entity
pub mod episodes {
	use flix_model::id::ShowId as FlixId;
	use flix_model::numbers::{EpisodeNumber, SeasonNumber};
	use flix_tmdb::model::id::ShowId;
	use seamantic::model::duration::Seconds;

	use chrono::{DateTime, Utc};
	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a tmdb episode
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_tmdb_episodes")]
	pub struct Model {
		/// The episode's show's TMDB ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_show: ShowId,
		/// The episode's season's TMDB season number
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_season: SeasonNumber,
		/// The episode's TMDB episode number
		#[sea_orm(primary_key, auto_increment = false)]
		pub tmdb_episode: EpisodeNumber,
		/// The episode's show's ID
		#[sea_orm(unique_key = "flix")]
		pub flix_show: FlixId,
		/// The episode's season's number
		#[sea_orm(unique_key = "flix")]
		pub flix_season: SeasonNumber,
		/// The episode's number
		#[sea_orm(unique_key = "flix")]
		pub flix_episode: EpisodeNumber,
		/// The date of the last update
		pub last_update: DateTime<Utc>,
		/// The episode's runtime in seconds
		pub runtime: Seconds,

		/// The show this episode belongs to
		#[sea_orm(
			belongs_to,
			from = "tmdb_show",
			to = "tmdb_id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub show: HasOne<super::shows::Entity>,
		/// The season this episode belongs to
		#[sea_orm(
			belongs_to,
			from = "(tmdb_show, tmdb_season)",
			to = "(tmdb_show, tmdb_season)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub season: HasOne<super::seasons::Entity>,
		/// The info for this episode
		#[sea_orm(
			belongs_to,
			from = "(flix_show, flix_season, flix_episode)",
			to = "(show_id, season_number, episode_number)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::episodes::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Macros for creating tmdb entities
#[cfg(test)]
pub mod test {
	macro_rules! make_tmdb_collection {
		($db:expr, $id:expr, $flix_id:expr) => {
			$crate::entity::tmdb::collections::ActiveModel {
				tmdb_id: Set(::flix_tmdb::model::id::CollectionId::from_raw($id)),
				flix_id: Set(::flix_model::id::CollectionId::from_raw($flix_id)),
				last_update: Set(::chrono::Utc::now()),
				movie_count: Set(::core::default::Default::default()),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_tmdb_collection;

	macro_rules! make_tmdb_movie {
		($db:expr, $id:expr, $flix_id:expr) => {
			$crate::entity::tmdb::movies::ActiveModel {
				tmdb_id: Set(::flix_tmdb::model::id::MovieId::from_raw($id)),
				flix_id: Set(::flix_model::id::MovieId::from_raw($flix_id)),
				last_update: Set(::chrono::Utc::now()),
				runtime: Set(::core::default::Default::default()),
				collection_id: Set(None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_tmdb_movie;

	macro_rules! make_tmdb_show {
		($db:expr, $id:expr, $flix_id:expr) => {
			$crate::entity::tmdb::shows::ActiveModel {
				tmdb_id: Set(::flix_tmdb::model::id::ShowId::from_raw($id)),
				flix_id: Set(::flix_model::id::ShowId::from_raw($flix_id)),
				last_update: Set(::chrono::Utc::now()),
				number_of_seasons: Set(::core::default::Default::default()),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_tmdb_show;

	macro_rules! make_tmdb_season {
		($db:expr, $show:expr, $season:expr, $flix_show:expr, $flix_season:expr) => {
			$crate::entity::tmdb::seasons::ActiveModel {
				tmdb_show: Set(::flix_tmdb::model::id::ShowId::from_raw($show)),
				tmdb_season: Set(::flix_model::numbers::SeasonNumber::new($season)),
				flix_show: Set(::flix_model::id::ShowId::from_raw($flix_show)),
				flix_season: Set(::flix_model::numbers::SeasonNumber::new($flix_season)),
				last_update: Set(::chrono::Utc::now()),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_tmdb_season;

	macro_rules! make_tmdb_episode {
		($db:expr, $show:expr, $season:expr, $episode:expr, $flix_show:expr, $flix_season:expr, $flix_episode:expr) => {
			$crate::entity::tmdb::episodes::ActiveModel {
				tmdb_show: Set(::flix_tmdb::model::id::ShowId::from_raw($show)),
				tmdb_season: Set(::flix_model::numbers::SeasonNumber::new($season)),
				tmdb_episode: Set(::flix_model::numbers::EpisodeNumber::new($episode)),
				flix_show: Set(::flix_model::id::ShowId::from_raw($flix_show)),
				flix_season: Set(::flix_model::numbers::SeasonNumber::new($flix_season)),
				flix_episode: Set(::flix_model::numbers::EpisodeNumber::new($flix_episode)),
				last_update: Set(::chrono::Utc::now()),
				runtime: Set(::core::default::Default::default()),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_tmdb_episode;
}

#[cfg(test)]
mod tests {
	use core::time::Duration;

	use flix_model::id::{CollectionId, MovieId, ShowId};
	use flix_tmdb::model::id::{
		CollectionId as TmdbCollectionId, MovieId as TmdbMovieId, ShowId as TmdbShowId,
	};

	use chrono::NaiveDate;
	use sea_orm::ActiveValue::{NotSet, Set};
	use sea_orm::entity::prelude::*;
	use sea_orm::sqlx::error::ErrorKind;

	use crate::entity::info::test::{
		make_info_collection, make_info_episode, make_info_movie, make_info_season, make_info_show,
	};
	use crate::tests::new_initialized_memory_db;

	use super::super::tests::get_error_kind;
	use super::super::tests::notsettable;
	use super::test::{
		make_tmdb_collection, make_tmdb_episode, make_tmdb_movie, make_tmdb_season, make_tmdb_show,
	};

	#[tokio::test]
	async fn use_test_macros() {
		let db = new_initialized_memory_db().await;

		make_info_collection!(&db, 1);
		make_info_movie!(&db, 1);
		make_info_show!(&db, 1);
		make_info_season!(&db, 1, 1);
		make_info_episode!(&db, 1, 1, 1);

		make_tmdb_collection!(&db, 1, 1);
		make_tmdb_movie!(&db, 1, 1);
		make_tmdb_show!(&db, 1, 1);
		make_tmdb_season!(&db, 1, 1, 1, 1);
		make_tmdb_episode!(&db, 1, 1, 1, 1, 1, 1);
	}

	#[tokio::test]
	async fn test_round_trip_collections() {
		let db = new_initialized_memory_db().await;

		macro_rules! assert_collection {
			($db:expr, $id:literal, $tid:literal, Success $(; $($skip:ident),+)?) => {
				let model = assert_collection!(@insert, $db, $id, $tid $(; $($skip),+)?)
					.expect("insert");

				assert_eq!(model.tmdb_id, TmdbCollectionId::from_raw($tid));
				assert_eq!(model.flix_id, CollectionId::from_raw($id));
				assert_eq!(model.last_update, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc());
				assert_eq!(model.movie_count, $id);
			};
			($db:expr, $id:literal, $tid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_collection!(@insert, $db, $id, $tid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $tid:literal $(; $($skip:ident),+)?) => {
				super::collections::ActiveModel {
					tmdb_id: notsettable!(tmdb_id, TmdbCollectionId::from_raw($tid) $(, $($skip),+)?),
					flix_id: notsettable!(flix_id, CollectionId::from_raw($id) $(, $($skip),+)?),
					last_update: notsettable!(last_update, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?),
					movie_count: notsettable!(movie_count, $id $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		assert_collection!(&db, 1, 1, ForeignKeyViolation);
		make_info_collection!(&db, 1);
		assert_collection!(&db, 1, 1, Success);
		assert_collection!(&db, 1, 1, UniqueViolation);

		assert_collection!(&db, 1, 2, UniqueViolation);
		assert_collection!(&db, 2, 1, UniqueViolation);
		make_info_collection!(&db, 2);
		assert_collection!(&db, 2, 2, Success);

		make_info_collection!(&db, 3);
		assert_collection!(&db, 3, 3, Success; tmdb_id);
		assert_collection!(&db, 4, 4, NotNullViolation; flix_id);
		assert_collection!(&db, 5, 5, NotNullViolation; last_update);
		assert_collection!(&db, 6, 6, NotNullViolation; movie_count);
	}

	#[tokio::test]
	async fn test_round_trip_movies() {
		let db = new_initialized_memory_db().await;

		macro_rules! assert_movie {
			($db:expr, $id:literal, $tid:literal, $cid:expr, Success $(; $($skip:ident),+)?) => {
				let model = assert_movie!(@insert, $db, $id, $tid, $cid $(; $($skip),+)?)
					.expect("insert");

				assert_eq!(model.tmdb_id, TmdbMovieId::from_raw($tid));
				assert_eq!(model.flix_id, MovieId::from_raw($id));
				assert_eq!(model.last_update, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc());
				assert_eq!(model.runtime, Duration::from_secs($tid).into());
				assert_eq!(model.collection_id, $cid.map(TmdbCollectionId::from_raw));
			};
			($db:expr, $id:literal, $tid:literal, $cid:expr, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_movie!(@insert, $db, $id, $tid, $cid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $tid:literal, $cid:expr $(; $($skip:ident),+)?) => {
				super::movies::ActiveModel {
					tmdb_id: notsettable!(tmdb_id, TmdbMovieId::from_raw($tid) $(, $($skip),+)?),
					flix_id: notsettable!(flix_id, MovieId::from_raw($id) $(, $($skip),+)?),
					last_update: notsettable!(last_update, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?),
					runtime: notsettable!(runtime, Duration::from_secs($tid).into() $(, $($skip),+)?),
					collection_id: notsettable!(collection_id, $cid.map(TmdbCollectionId::from_raw) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		assert_movie!(&db, 1, 1, None, ForeignKeyViolation);
		make_info_movie!(&db, 1);
		assert_movie!(&db, 1, 1, None, Success);
		assert_movie!(&db, 1, 1, None, UniqueViolation);

		make_info_movie!(&db, 2);
		assert_movie!(&db, 2, 2, Some(2), ForeignKeyViolation);
		make_info_collection!(&db, 2);
		make_tmdb_collection!(&db, 2, 2);
		assert_movie!(&db, 2, 2, Some(2), Success);
		assert_movie!(&db, 1, 2, None, UniqueViolation);
		assert_movie!(&db, 2, 1, None, UniqueViolation);

		make_info_movie!(&db, 3);
		assert_movie!(&db, 3, 3, None, Success; tmdb_id);
		assert_movie!(&db, 4, 4, None, NotNullViolation; flix_id);
		assert_movie!(&db, 5, 5, None, NotNullViolation; last_update);
		assert_movie!(&db, 6, 6, None, NotNullViolation; runtime);
		assert_movie!(&db, 7, 7, None, ForeignKeyViolation; collection_id);
	}

	#[tokio::test]
	async fn test_round_trip_shows() {
		let db = new_initialized_memory_db().await;

		macro_rules! assert_show {
			($db:expr, $id:literal, $tid:literal, Success $(; $($skip:ident),+)?) => {
				let model = assert_show!(@insert, $db, $id, $tid $(; $($skip),+)?)
					.expect("insert");

				assert_eq!(model.tmdb_id, TmdbShowId::from_raw($tid));
				assert_eq!(model.flix_id, ShowId::from_raw($id));
				assert_eq!(model.last_update, NaiveDate::from_yo_opt($tid, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc());
				assert_eq!(model.number_of_seasons, $id);
			};
			($db:expr, $id:literal, $tid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_show!(@insert, $db, $id, $tid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(
					get_error_kind(model).expect("get_error_kind"),
					ErrorKind::$error
				);
			};
			(@insert, $db:expr, $id:literal, $tid:literal $(; $($skip:ident),+)?) => {
				super::shows::ActiveModel {
					tmdb_id: notsettable!(tmdb_id, TmdbShowId::from_raw($tid) $(, $($skip),+)?),
					flix_id: notsettable!(flix_id, ShowId::from_raw($id) $(, $($skip),+)?),
					last_update: notsettable!(last_update, NaiveDate::from_yo_opt($tid, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?),
					number_of_seasons: notsettable!(number_of_seasons, $id $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		assert_show!(&db, 1, 1, ForeignKeyViolation);
		make_info_show!(&db, 1);
		assert_show!(&db, 1, 1, Success);
		assert_show!(&db, 1, 1, UniqueViolation);

		assert_show!(&db, 1, 2, UniqueViolation);
		assert_show!(&db, 2, 1, UniqueViolation);
		make_info_show!(&db, 2);
		assert_show!(&db, 2, 2, Success);

		make_info_show!(&db, 3);
		assert_show!(&db, 3, 3, Success; tmdb_id);
		assert_show!(&db, 4, 4, NotNullViolation; flix_id);
		assert_show!(&db, 5, 5, NotNullViolation; last_update);
		assert_show!(&db, 6, 6, NotNullViolation; number_of_seasons);
	}

	#[tokio::test]
	async fn test_round_trip_seasons() {
		let db = new_initialized_memory_db().await;

		macro_rules! assert_season {
			($db:expr, $show:literal, $season:literal, $tshow:literal, $tseason:literal, Success $(; $($skip:ident),+)?) => {
				let model = assert_season!(@insert, $db, $show, $season, $tshow, $tseason $(; $($skip),+)?)
					.expect("insert");

				assert_eq!(model.tmdb_show, TmdbShowId::from_raw($tshow));
				assert_eq!(model.tmdb_season, ::flix_model::numbers::SeasonNumber::new($tseason));
				assert_eq!(model.flix_show, ShowId::from_raw($show));
				assert_eq!(model.flix_season, ::flix_model::numbers::SeasonNumber::new($season));
				assert_eq!(model.last_update, NaiveDate::from_yo_opt($tshow, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc());
			};
			($db:expr, $show:literal, $season:literal, $tshow:literal, $tseason:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_season!(@insert, $db, $show, $season, $tshow, $tseason $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(
					get_error_kind(model).expect("get_error_kind"),
					ErrorKind::$error
				);
			};
			(@insert, $db:expr, $show:literal, $season:literal, $tshow:literal, $tseason:literal $(; $($skip:ident),+)?) => {
				super::seasons::ActiveModel {
					tmdb_show: notsettable!(tmdb_show, TmdbShowId::from_raw($tshow) $(, $($skip),+)?),
					tmdb_season: notsettable!(tmdb_season, ::flix_model::numbers::SeasonNumber::new($tseason) $(, $($skip),+)?),
					flix_show: notsettable!(flix_show, ShowId::from_raw($show) $(, $($skip),+)?),
					flix_season: notsettable!(flix_season, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?),
					last_update: notsettable!(last_update, NaiveDate::from_yo_opt($tshow, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_info_show!(&db, 1);
		make_tmdb_show!(&db, 1, 1);

		assert_season!(&db, 1, 1, 1, 1, ForeignKeyViolation);
		make_info_season!(&db, 1, 1);
		assert_season!(&db, 1, 1, 1, 1, Success);

		assert_season!(&db, 1, 1, 1, 1, UniqueViolation);
		assert_season!(&db, 1, 1, 2, 1, UniqueViolation);
		assert_season!(&db, 2, 1, 1, 1, UniqueViolation);
		make_info_season!(&db, 1, 2);
		assert_season!(&db, 1, 2, 1, 2, Success);

		assert_season!(&db, 1, 3, 1, 3, NotNullViolation; tmdb_show);
		assert_season!(&db, 1, 4, 1, 4, NotNullViolation; tmdb_season);
		assert_season!(&db, 1, 5, 1, 5, NotNullViolation; flix_show);
		assert_season!(&db, 1, 6, 1, 6, NotNullViolation; flix_season);
		assert_season!(&db, 1, 7, 1, 7, NotNullViolation; last_update);
	}

	#[tokio::test]
	async fn test_round_trip_episodes() {
		let db = new_initialized_memory_db().await;

		macro_rules! assert_episode {
			($db:expr, $show:literal, $season:literal, $episode:literal, $tshow:literal, $tseason:literal, $tepisode:literal, Success $(; $($skip:ident),+)?) => {
				let model = assert_episode!(@insert, $db, $show, $season, $episode, $tshow, $tseason, $tepisode $(; $($skip),+)?)
					.expect("insert");

				assert_eq!(model.tmdb_show, TmdbShowId::from_raw($tshow));
				assert_eq!(model.tmdb_season, ::flix_model::numbers::SeasonNumber::new($tseason));
				assert_eq!(model.tmdb_episode, ::flix_model::numbers::EpisodeNumber::new($tepisode));
				assert_eq!(model.flix_show, ShowId::from_raw($show));
				assert_eq!(model.flix_season, ::flix_model::numbers::SeasonNumber::new($season));
				assert_eq!(model.flix_episode, ::flix_model::numbers::EpisodeNumber::new($episode));
				assert_eq!(model.last_update, NaiveDate::from_yo_opt($tshow, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc());
				assert_eq!(model.runtime, Duration::from_secs($tshow).into());
			};
			($db:expr, $show:literal, $season:literal, $episode:literal, $tshow:literal, $tseason:literal, $tepisode:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_episode!(@insert, $db, $show, $season, $episode, $tshow, $tseason, $tepisode $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(
					get_error_kind(model).expect("get_error_kind"),
					ErrorKind::$error
				);
			};
			(@insert, $db:expr, $show:literal, $season:literal, $episode:literal, $tshow:literal, $tseason:literal, $tepisode:literal $(; $($skip:ident),+)?) => {
				super::episodes::ActiveModel {
					tmdb_show: notsettable!(tmdb_show, TmdbShowId::from_raw($tshow) $(, $($skip),+)?),
					tmdb_season: notsettable!(tmdb_season, ::flix_model::numbers::SeasonNumber::new($tseason) $(, $($skip),+)?),
					tmdb_episode: notsettable!(tmdb_episode, ::flix_model::numbers::EpisodeNumber::new($tepisode) $(, $($skip),+)?),
					flix_show: notsettable!(flix_show, ShowId::from_raw($show) $(, $($skip),+)?),
					flix_season: notsettable!(flix_season, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?),
					flix_episode: notsettable!(flix_episode, ::flix_model::numbers::EpisodeNumber::new($episode) $(, $($skip),+)?),
					last_update: notsettable!(last_update, NaiveDate::from_yo_opt($tshow, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?),
					runtime: notsettable!(runtime, Duration::from_secs($tshow).into() $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_info_show!(&db, 1);
		make_info_season!(&db, 1, 1);
		make_tmdb_show!(&db, 1, 1);
		make_tmdb_season!(&db, 1, 1, 1, 1);

		assert_episode!(&db, 1, 1, 1, 1, 1, 1, ForeignKeyViolation);
		make_info_episode!(&db, 1, 1, 1);
		assert_episode!(&db, 1, 1, 1, 1, 1, 1, Success);

		assert_episode!(&db, 1, 1, 1, 1, 1, 1, UniqueViolation);
		assert_episode!(&db, 1, 1, 1, 1, 2, 1, UniqueViolation);
		assert_episode!(&db, 1, 1, 1, 2, 1, 1, UniqueViolation);
		assert_episode!(&db, 1, 2, 1, 1, 1, 1, UniqueViolation);
		assert_episode!(&db, 2, 1, 1, 1, 1, 1, UniqueViolation);
		make_info_episode!(&db, 1, 1, 2);
		assert_episode!(&db, 1, 1, 2, 1, 1, 2, Success);

		assert_episode!(&db, 1, 1, 3, 1, 1, 3, NotNullViolation; tmdb_show);
		assert_episode!(&db, 1, 1, 3, 1, 1, 4, NotNullViolation; tmdb_season);
		assert_episode!(&db, 1, 1, 3, 1, 1, 5, NotNullViolation; tmdb_episode);
		assert_episode!(&db, 1, 1, 3, 1, 1, 6, NotNullViolation; flix_show);
		assert_episode!(&db, 1, 1, 3, 1, 1, 7, NotNullViolation; flix_season);
		assert_episode!(&db, 1, 1, 3, 1, 1, 8, NotNullViolation; flix_episode);
		assert_episode!(&db, 1, 1, 3, 1, 1, 9, NotNullViolation; last_update);
		assert_episode!(&db, 1, 1, 3, 1, 1, 10, NotNullViolation; runtime);
	}
}