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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//! This module contains entities for storing media file information

/// Library entity
pub mod libraries {
	use flix_model::id::LibraryId;

	use seamantic::model::duration::Seconds;
	use seamantic::model::path::PathBytes;

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

	/// The database representation of a library media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_libraries")]
	pub struct Model {
		/// The library's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub id: LibraryId,
		/// The library's directory
		pub directory: PathBytes,
		/// The library's last scan data
		pub last_scan_date: Option<DateTime<Utc>>,
		/// The library's last scan duration
		pub last_scan_duration: Option<Seconds>,

		/// Collections that are part of this library
		#[sea_orm(has_many)]
		pub collections: HasMany<super::collections::Entity>,
		/// Movies that are part of this library
		#[sea_orm(has_many)]
		pub movies: HasMany<super::movies::Entity>,
		/// Shows that are part of this library
		#[sea_orm(has_many)]
		pub shows: HasMany<super::shows::Entity>,
		/// Seasons that are part of this library
		#[sea_orm(has_many)]
		pub seasons: HasMany<super::seasons::Entity>,
		/// Episodes that are part of this library
		#[sea_orm(has_many)]
		pub episodes: HasMany<super::episodes::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Collection entity
pub mod collections {
	use flix_model::id::{CollectionId, LibraryId};

	use seamantic::model::path::PathBytes;

	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a collection media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_collections")]
	pub struct Model {
		/// The collection's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub id: CollectionId,
		/// The collection's parent
		#[sea_orm(indexed)]
		pub parent_id: Option<CollectionId>,
		/// The collection's library ID
		pub library_id: LibraryId,
		/// The collection's directory
		pub directory: PathBytes,
		/// The collection's poster path
		pub relative_poster_path: Option<String>,

		/// This collection's parent
		#[sea_orm(
			self_ref,
			relation_enum = "Parent",
			from = "parent_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub parent: HasOne<Entity>,
		/// The library this collection belongs to
		#[sea_orm(
			belongs_to,
			from = "library_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub library: HasOne<super::libraries::Entity>,
		/// The info for this collection
		#[sea_orm(
			belongs_to,
			relation_enum = "Info",
			from = "id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::collections::Entity>,

		/// The watched info for this collection
		#[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")]
		pub watched: HasMany<entity::watched::collections::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Movie entity
pub mod movies {
	use flix_model::id::{CollectionId, LibraryId, MovieId};

	use seamantic::model::path::PathBytes;

	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a movie media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_movies")]
	pub struct Model {
		/// The movie's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub id: MovieId,
		/// The movie's parent
		#[sea_orm(indexed)]
		pub parent_id: Option<CollectionId>,
		/// The movie's library
		pub library_id: LibraryId,
		/// The movie's directory
		pub directory: PathBytes,
		/// The movie's media path
		pub relative_media_path: String,
		/// The movie's poster path
		pub relative_poster_path: Option<String>,

		/// This movie's parent
		#[sea_orm(
			belongs_to,
			from = "parent_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub parent: HasOne<super::collections::Entity>,
		/// The library this movie belongs to
		#[sea_orm(
			belongs_to,
			from = "library_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub library: HasOne<super::libraries::Entity>,
		/// The info for this movie
		#[sea_orm(
			belongs_to,
			relation_enum = "Info",
			from = "id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::movies::Entity>,

		/// The watched info for this movie
		#[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")]
		pub watched: HasMany<entity::watched::movies::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Show entity
pub mod shows {
	use flix_model::id::{CollectionId, LibraryId, ShowId};

	use seamantic::model::path::PathBytes;

	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a show media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_shows")]
	pub struct Model {
		/// The show's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub id: ShowId,
		/// The show's parent
		#[sea_orm(indexed)]
		pub parent_id: Option<CollectionId>,
		/// The show's library
		pub library_id: LibraryId,
		/// The show's directory
		pub directory: PathBytes,
		/// The show's poster path
		pub relative_poster_path: Option<String>,

		/// This show's parent
		#[sea_orm(
			belongs_to,
			from = "parent_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub parent: HasOne<super::collections::Entity>,
		/// The library this show belongs to
		#[sea_orm(
			belongs_to,
			from = "library_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub library: HasOne<super::libraries::Entity>,
		/// The info for this show
		#[sea_orm(
			belongs_to,
			relation_enum = "Info",
			from = "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>,
		/// The watched info for this show
		#[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")]
		pub watched: HasMany<entity::watched::shows::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Season entity
pub mod seasons {
	use flix_model::id::{LibraryId, ShowId};
	use flix_model::numbers::SeasonNumber;

	use seamantic::model::path::PathBytes;

	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a season media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_seasons")]
	pub struct Model {
		/// The season's show's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub show_id: ShowId,
		/// The season's number
		#[sea_orm(primary_key, auto_increment = false)]
		pub season_number: SeasonNumber,
		/// The season's library
		pub library_id: LibraryId,
		/// The season's directory
		pub directory: PathBytes,
		/// The season's poster path
		pub relative_poster_path: Option<String>,

		/// This season's show
		#[sea_orm(
			belongs_to,
			from = "show_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub show: HasOne<super::shows::Entity>,
		/// The library this season belongs to
		#[sea_orm(
			belongs_to,
			from = "library_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub library: HasOne<super::libraries::Entity>,
		/// The info for this season
		#[sea_orm(
			belongs_to,
			relation_enum = "Info",
			from = "(show_id, season_number)",
			to = "(show_id, season_number)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::seasons::Entity>,

		/// Episodes that are part of this show
		#[sea_orm(has_many)]
		pub episodes: HasMany<super::episodes::Entity>,
		/// The watched info for this season
		#[sea_orm(
			has_many,
			relation_enum = "Watched",
			from = "(show_id, season_number)",
			to = "(show_id, season_number)"
		)]
		pub watched: HasMany<entity::watched::seasons::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Episode entity
pub mod episodes {
	use flix_model::id::{LibraryId, ShowId};
	use flix_model::numbers::{EpisodeNumber, SeasonNumber};

	use seamantic::model::path::PathBytes;

	use sea_orm::entity::prelude::*;

	use crate::entity;

	/// The database representation of a episode media folder
	#[sea_orm::model]
	#[derive(Debug, Clone, DeriveEntityModel)]
	#[sea_orm(table_name = "flix_episodes")]
	pub struct Model {
		/// The episode's show's ID
		#[sea_orm(primary_key, auto_increment = false)]
		pub show_id: ShowId,
		/// The episode's season's number
		#[sea_orm(primary_key, auto_increment = false)]
		pub season_number: SeasonNumber,
		/// The episode's number
		#[sea_orm(primary_key, auto_increment = false)]
		pub episode_number: EpisodeNumber,
		/// The number of additional contained episodes
		pub count: u8,
		/// The episode's library
		pub library_id: LibraryId,
		/// The episode's directory
		pub directory: PathBytes,
		/// The episode's media path
		pub relative_media_path: String,
		/// The episode's poster path
		pub relative_poster_path: Option<String>,

		/// This episode's show
		#[sea_orm(
			belongs_to,
			from = "show_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub show: HasOne<super::shows::Entity>,
		/// This episode's season
		#[sea_orm(
			belongs_to,
			from = "(show_id, season_number)",
			to = "(show_id, season_number)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub season: HasOne<super::seasons::Entity>,
		/// The library this episode belongs to
		#[sea_orm(
			belongs_to,
			from = "library_id",
			to = "id",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub library: HasOne<super::libraries::Entity>,
		/// The info for this episode
		#[sea_orm(
			belongs_to,
			relation_enum = "Info",
			from = "(show_id, season_number, episode_number)",
			to = "(show_id, season_number, episode_number)",
			on_update = "Cascade",
			on_delete = "Cascade"
		)]
		pub info: HasOne<entity::info::episodes::Entity>,

		/// The watched info for this episode
		#[sea_orm(
			has_many,
			relation_enum = "Watched",
			from = "(show_id, season_number, episode_number)",
			to = "(show_id, season_number, episode_number)"
		)]
		pub watched: HasMany<entity::watched::episodes::Entity>,
	}

	impl ActiveModelBehavior for ActiveModel {}
}

/// Macros for creating content entities
#[cfg(test)]
pub mod test {
	macro_rules! make_content_library {
		($db:expr, $id:expr) => {
			$crate::entity::content::libraries::ActiveModel {
				id: Set(::flix_model::id::LibraryId::from_raw($id)),
				directory: Set(::std::path::PathBuf::new().into()),
				last_scan_date: Set(None),
				last_scan_duration: Set(None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_library;

	macro_rules! make_content_collection {
		($db:expr, $lid:expr, $id:expr, $pid:expr) => {
			$crate::entity::info::test::make_info_collection!($db, $id);
			$crate::entity::content::collections::ActiveModel {
				id: Set(::flix_model::id::CollectionId::from_raw($id)),
				parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)),
				library_id: Set(::flix_model::id::LibraryId::from_raw($lid)),
				directory: Set(::std::path::PathBuf::new().into()),
				relative_poster_path: Set(::core::option::Option::None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_collection;

	macro_rules! make_content_movie {
		($db:expr, $lid:expr, $id:expr, $pid:expr) => {
			$crate::entity::info::test::make_info_movie!($db, $id);
			$crate::entity::content::movies::ActiveModel {
				id: Set(::flix_model::id::MovieId::from_raw($id)),
				parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)),
				library_id: Set(::flix_model::id::LibraryId::from_raw($lid)),
				directory: Set(::std::path::PathBuf::new().into()),
				relative_media_path: Set(::std::string::String::new()),
				relative_poster_path: Set(::core::option::Option::None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_movie;

	macro_rules! make_content_show {
		($db:expr, $lid:expr, $id:expr, $pid:expr) => {
			$crate::entity::info::test::make_info_show!($db, $id);
			$crate::entity::content::shows::ActiveModel {
				id: Set(::flix_model::id::ShowId::from_raw($id)),
				parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)),
				library_id: Set(::flix_model::id::LibraryId::from_raw($lid)),
				directory: Set(::std::path::PathBuf::new().into()),
				relative_poster_path: Set(::core::option::Option::None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_show;

	macro_rules! make_content_season {
		($db:expr, $lid:expr, $show:expr, $season:expr) => {
			$crate::entity::info::test::make_info_season!($db, $show, $season);
			$crate::entity::content::seasons::ActiveModel {
				show_id: Set(::flix_model::id::ShowId::from_raw($show)),
				season_number: Set(::flix_model::numbers::SeasonNumber::new($season)),
				library_id: Set(::flix_model::id::LibraryId::from_raw($lid)),
				directory: Set(::std::path::PathBuf::new().into()),
				relative_poster_path: Set(::core::option::Option::None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_season;

	macro_rules! make_content_episode {
		($db:expr, $lid:expr, $show:expr, $season:expr, $episode:expr) => {
			make_content_episode!(@make, $db, $lid, $show, $season, $episode, 0);
		};
		($db:expr, $lid:literal, $show:literal, $season:literal, $episode:literal, >1) => {
			make_content_episode!(@make, $db, $lid, $show, $season, $episode, 1);
		};
		(@make, $db:expr, $lid:expr, $show:expr, $season:expr, $episode:expr, $count:literal) => {
			$crate::entity::info::test::make_info_episode!($db, $show, $season, $episode);
			$crate::entity::content::episodes::ActiveModel {
				show_id: Set(::flix_model::id::ShowId::from_raw($show)),
				season_number: Set(::flix_model::numbers::SeasonNumber::new($season)),
				episode_number: Set(::flix_model::numbers::EpisodeNumber::new($episode)),
				count: Set($count),
				library_id: Set(::flix_model::id::LibraryId::from_raw($lid)),
				directory: Set(::std::path::PathBuf::new().into()),
				relative_media_path: Set(::std::string::String::new()),
				relative_poster_path: Set(::core::option::Option::None),
			}
			.insert($db)
			.await
			.expect("insert");
		};
	}
	pub(crate) use make_content_episode;
}

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

	use flix_model::id::{CollectionId, LibraryId, MovieId, ShowId};

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

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

	use crate::entity::content::test::{
		make_content_collection, make_content_episode, make_content_library, make_content_movie,
		make_content_season, make_content_show,
	};
	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::{noneable, notsettable};

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

		make_content_library!(&db, 1);
		make_content_collection!(&db, 1, 1, None);
		make_content_movie!(&db, 1, 1, None);
		make_content_show!(&db, 1, 1, None);
		make_content_season!(&db, 1, 1, 1);
		make_content_episode!(&db, 1, 1, 1, 1);
	}

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

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

				assert_eq!(model.id, LibraryId::from_raw($id));
				assert_eq!(model.directory, Path::new(concat!("L Directory ", $id)).to_owned().into());
				assert_eq!(model.last_scan_date, noneable!(last_scan_date, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc() $(, $($skip),+)?));
				assert_eq!(model.last_scan_duration, noneable!(last_scan_duration, Seconds(Duration::from_secs($id)) $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_library!(@insert, $db, $id $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal $(; $($skip:ident),+)?) => {
				super::libraries::ActiveModel {
					id: notsettable!(id, LibraryId::from_raw($id) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("L Directory ", $id)).to_owned().into() $(, $($skip),+)?),
					last_scan_date: notsettable!(last_scan_date, Some(NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt").and_hms_opt(0, 0, 0).expect("and_hms_opt").and_utc()) $(, $($skip),+)?),
					last_scan_duration: notsettable!(last_scan_duration, Some(Seconds(Duration::from_secs($id))) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		assert_library!(&db, 1, Success);
		assert_library!(&db, 1, UniqueViolation);
		assert_library!(&db, 2, Success);
		assert_library!(&db, 3, Success; id);
		assert_library!(&db, 4, NotNullViolation; directory);
		assert_library!(&db, 5, Success; last_scan_date);
		assert_library!(&db, 6, Success; last_scan_duration);
	}

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

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

				assert_eq!(model.id, CollectionId::from_raw($id));
				assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw));
				assert_eq!(model.library_id, LibraryId::from_raw($lid));
				assert_eq!(model.directory, Path::new(concat!("C Directory ", $id)).to_owned().into());
				assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("C Poster ", $id).to_owned() $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $pid:expr, $lid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_collection!(@insert, $db, $id, $pid, $lid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $pid:expr, $lid:literal $(; $($skip:ident),+)?) => {
				super::collections::ActiveModel {
					id: notsettable!(id, CollectionId::from_raw($id) $(, $($skip),+)?),
					parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?),
					library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("C Directory ", $id)).to_owned().into() $(, $($skip),+)?),
					relative_poster_path: notsettable!(relative_poster_path, Some(concat!("C Poster ", $id).to_owned()) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_content_library!(&db, 1);
		assert_collection!(&db, 1, None, 1, ForeignKeyViolation);
		make_info_collection!(&db, 1);
		assert_collection!(&db, 1, None, 1, Success);
		make_info_collection!(&db, 2);
		assert_collection!(&db, 2, None, 2, ForeignKeyViolation);
		make_content_library!(&db, 2);
		assert_collection!(&db, 2, None, 2, Success);

		assert_collection!(&db, 1, None, 1, UniqueViolation);
		make_info_collection!(&db, 3);
		make_info_collection!(&db, 4);
		make_info_collection!(&db, 5);
		make_info_collection!(&db, 6);
		make_info_collection!(&db, 7);
		make_info_collection!(&db, 8);
		assert_collection!(&db, 3, None, 1, Success; id);
		assert_collection!(&db, 4, None, 1, Success; parent_id);
		assert_collection!(&db, 5, None, 1, NotNullViolation; library_id);
		assert_collection!(&db, 6, None, 1, NotNullViolation; directory);
		assert_collection!(&db, 7, None, 1, Success; relative_poster_path);
	}

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

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

				assert_eq!(model.id, MovieId::from_raw($id));
				assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw));
				assert_eq!(model.library_id, LibraryId::from_raw($lid));
				assert_eq!(model.directory, Path::new(concat!("M Directory ", $id)).to_owned().into());
				assert_eq!(model.relative_media_path, concat!("M Media ", $id));
				assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("M Poster ", $id).to_owned() $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $pid:expr, $lid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_movie!(@insert, $db, $id, $pid, $lid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $pid:expr, $lid:literal $(; $($skip:ident),+)?) => {
				super::movies::ActiveModel {
					id: notsettable!(id, MovieId::from_raw($id) $(, $($skip),+)?),
					parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?),
					library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("M Directory ", $id)).to_owned().into() $(, $($skip),+)?),
					relative_media_path: notsettable!(relative_media_path, concat!("M Media ", $id).to_owned() $(, $($skip),+)?),
					relative_poster_path: notsettable!(relative_poster_path, Some(concat!("M Poster ", $id).to_owned()) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_content_library!(&db, 1);
		assert_movie!(&db, 1, None, 1, ForeignKeyViolation);
		make_info_movie!(&db, 1);
		assert_movie!(&db, 1, Some(1), 1, ForeignKeyViolation);
		make_content_collection!(&db, 1, 1, None);
		assert_movie!(&db, 1, Some(1), 1, Success);
		assert_movie!(&db, 2, None, 2, ForeignKeyViolation);
		make_info_movie!(&db, 2);
		assert_movie!(&db, 2, None, 2, ForeignKeyViolation);
		make_content_library!(&db, 2);
		assert_movie!(&db, 2, None, 2, Success);

		assert_movie!(&db, 1, None, 1, UniqueViolation);
		make_info_movie!(&db, 3);
		make_info_movie!(&db, 4);
		make_info_movie!(&db, 5);
		make_info_movie!(&db, 6);
		make_info_movie!(&db, 7);
		make_info_movie!(&db, 8);
		make_info_movie!(&db, 9);
		assert_movie!(&db, 3, None, 1, Success; id);
		assert_movie!(&db, 4, None, 1, Success; parent_id);
		assert_movie!(&db, 5, None, 1, NotNullViolation; library_id);
		assert_movie!(&db, 6, None, 1, NotNullViolation; directory);
		assert_movie!(&db, 7, None, 1, NotNullViolation; relative_media_path);
		assert_movie!(&db, 8, None, 1, Success; relative_poster_path);
	}

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

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

				assert_eq!(model.id, ShowId::from_raw($id));
				assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw));
				assert_eq!(model.library_id, LibraryId::from_raw($lid));
				assert_eq!(model.directory, Path::new(concat!("S Directory ", $id)).to_owned().into());
				assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("S Poster ", $id).to_owned() $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $pid:expr, $lid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_show!(@insert, $db, $id, $pid, $lid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $pid:expr, $lid:literal $(; $($skip:ident),+)?) => {
				super::shows::ActiveModel {
					id: notsettable!(id, ShowId::from_raw($id) $(, $($skip),+)?),
					parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?),
					library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("S Directory ", $id)).to_owned().into() $(, $($skip),+)?),
					relative_poster_path: notsettable!(relative_poster_path, Some(concat!("S Poster ", $id).to_owned()) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_content_library!(&db, 1);
		assert_show!(&db, 1, None, 1, ForeignKeyViolation);
		make_info_show!(&db, 1);
		assert_show!(&db, 1, Some(1), 1, ForeignKeyViolation);
		make_content_collection!(&db, 1, 1, None);
		assert_show!(&db, 1, Some(1), 1, Success);
		assert_show!(&db, 2, None, 2, ForeignKeyViolation);
		make_info_show!(&db, 2);
		assert_show!(&db, 2, None, 2, ForeignKeyViolation);
		make_content_library!(&db, 2);
		assert_show!(&db, 2, None, 2, Success);

		assert_show!(&db, 1, None, 1, UniqueViolation);
		make_info_show!(&db, 3);
		make_info_show!(&db, 4);
		make_info_show!(&db, 5);
		make_info_show!(&db, 6);
		make_info_show!(&db, 7);
		make_info_show!(&db, 8);
		assert_show!(&db, 3, None, 1, Success; id);
		assert_show!(&db, 4, None, 1, Success; parent_id);
		assert_show!(&db, 5, None, 1, NotNullViolation; library_id);
		assert_show!(&db, 6, None, 1, NotNullViolation; directory);
		assert_show!(&db, 7, None, 1, Success; relative_poster_path);
	}

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

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

				assert_eq!(model.show_id, ShowId::from_raw($id));
				assert_eq!(model.season_number, ::flix_model::numbers::SeasonNumber::new($season));
				assert_eq!(model.library_id, LibraryId::from_raw($lid));
				assert_eq!(model.directory, Path::new(concat!("SS Directory ", $id, ",", $season)).to_owned().into());
				assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("SS Poster ", $id, ",", $season).to_owned() $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $season:literal, $lid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_season!(@insert, $db, $id, $season, $lid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $season:literal, $lid:literal $(; $($skip:ident),+)?) => {
				super::seasons::ActiveModel {
					show_id: notsettable!(show_id, ShowId::from_raw($id) $(, $($skip),+)?),
					season_number: notsettable!(season_number, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?),
					library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("SS Directory ", $id, ",", $season)).to_owned().into() $(, $($skip),+)?),
					relative_poster_path: notsettable!(relative_poster_path, Some(concat!("SS Poster ", $id, ",", $season).to_owned()) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

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

		assert_season!(&db, 1, 1, 1, UniqueViolation);
		make_info_season!(&db, 1, 3);
		make_info_season!(&db, 1, 4);
		make_info_season!(&db, 1, 5);
		make_info_season!(&db, 1, 6);
		make_info_season!(&db, 1, 7);
		make_info_season!(&db, 1, 8);
		assert_season!(&db, 1, 3, 1, NotNullViolation; show_id);
		assert_season!(&db, 1, 4, 1, NotNullViolation; season_number);
		assert_season!(&db, 1, 5, 1, NotNullViolation; library_id);
		assert_season!(&db, 1, 6, 1, NotNullViolation; directory);
		assert_season!(&db, 1, 7, 1, Success; relative_poster_path);
	}

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

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

				assert_eq!(model.show_id, ShowId::from_raw($id));
				assert_eq!(model.season_number, ::flix_model::numbers::SeasonNumber::new($season));
				assert_eq!(model.episode_number, ::flix_model::numbers::EpisodeNumber::new($episode));
				assert_eq!(model.library_id, LibraryId::from_raw($lid));
				assert_eq!(model.directory, Path::new(concat!("SS Directory ", $id, ",", $season, $episode)).to_owned().into());
				assert_eq!(model.relative_media_path, concat!("SS Media ", $id, ",", $season, $episode));
				assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("SS Poster ", $id, ",", $season, $episode).to_owned() $(, $($skip),+)?));
			};
			($db:expr, $id:literal, $season:literal, $episode:literal, $lid:literal, $error:ident $(; $($skip:ident),+)?) => {
				let model = assert_episode!(@insert, $db, $id, $season, $episode, $lid $(; $($skip),+)?)
					.expect_err("insert");

				assert_eq!(get_error_kind(model).expect("get_error_kind"), ErrorKind::$error);
			};
			(@insert, $db:expr, $id:literal, $season:literal, $episode:literal, $lid:literal $(; $($skip:ident),+)?) => {
				super::episodes::ActiveModel {
					show_id: notsettable!(show_id, ShowId::from_raw($id) $(, $($skip),+)?),
					season_number: notsettable!(season_number, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?),
					episode_number: notsettable!(episode_number, ::flix_model::numbers::EpisodeNumber::new($episode) $(, $($skip),+)?),
					count: notsettable!(count, 0 $(, $($skip),+)?),
					library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?),
					directory: notsettable!(directory, Path::new(concat!("SS Directory ", $id, ",", $season, $episode)).to_owned().into() $(, $($skip),+)?),
					relative_media_path: notsettable!(relative_media_path, concat!("SS Media ", $id, ",", $season, $episode).to_owned() $(, $($skip),+)?),
					relative_poster_path: notsettable!(relative_poster_path, Some(concat!("SS Poster ", $id, ",", $season, $episode).to_owned()) $(, $($skip),+)?),
				}.insert($db).await
			};
		}

		make_content_library!(&db, 1);
		make_content_show!(&db, 1, 1, None);
		make_content_season!(&db, 1, 1, 1);
		assert_episode!(&db, 1, 1, 1, 1, ForeignKeyViolation);
		make_info_episode!(&db, 1, 1, 1);
		assert_episode!(&db, 1, 1, 1, 1, Success);

		assert_episode!(&db, 1, 1, 1, 1, UniqueViolation);
		make_info_episode!(&db, 1, 1, 3);
		make_info_episode!(&db, 1, 1, 4);
		make_info_episode!(&db, 1, 1, 5);
		make_info_episode!(&db, 1, 1, 6);
		make_info_episode!(&db, 1, 1, 7);
		make_info_episode!(&db, 1, 1, 8);
		make_info_episode!(&db, 1, 1, 9);
		make_info_episode!(&db, 1, 1, 10);
		assert_episode!(&db, 1, 1, 3, 1, NotNullViolation; show_id);
		assert_episode!(&db, 1, 1, 4, 1, NotNullViolation; season_number);
		assert_episode!(&db, 1, 1, 5, 1, NotNullViolation; episode_number);
		assert_episode!(&db, 1, 1, 6, 1, NotNullViolation; library_id);
		assert_episode!(&db, 1, 1, 7, 1, NotNullViolation; directory);
		assert_episode!(&db, 1, 1, 8, 1, NotNullViolation; relative_media_path);
		assert_episode!(&db, 1, 1, 9, 1, Success; relative_poster_path);
	}
}