Skip to content

Commit a6c5a04

Browse files
committed
tests: Add real-world SQLite test for sea_orm integration
1 parent e41f4ff commit a6c5a04

2 files changed

Lines changed: 178 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ serde = { version = "1", features = [ "derive" ] }
2828
fluent-uri = { version = "0.4", features = [ "serde" ] }
2929
# For Sea-ORM integration
3030
sea-orm = { version = "2.0.0-rc.18", optional = true }
31+
sea-orm-migration = { version = "2.0.0-rc.18", optional = true }
32+
async-tempfile = { version = "0.7", optional = true }
33+
tokio = { version = "1", optional = true }
3134

3235
[dev-dependencies]
3336
serde_json = "1"
@@ -36,7 +39,7 @@ serde_json = "1"
3639
magnet_force_name = []
3740
unknown_tracker_scheme = []
3841
sea_orm = [ "dep:sea-orm" ]
39-
test_sea_orm = [ "dep:sea-orm", "sea-orm/sqlx-sqlite" ]
42+
test_sea_orm = [ "dep:sea-orm", "sea-orm/sqlx-sqlite", "dep:tokio", "tokio/macros", "tokio/rt", "dep:async-tempfile", "dep:sea-orm-migration", "sea-orm-migration/runtime-tokio" ]
4043

4144
[[test]]
4245
name = "magnet_force_name"
@@ -53,5 +56,5 @@ test = true
5356
[[test]]
5457
name = "sea_orm"
5558
path = "tests/sea_orm.rs"
56-
required-features = [ "sea_orm" ]
59+
required-features = [ "sea_orm", "test_sea_orm" ]
5760
test = true

tests/sea_orm.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,78 @@ mod magnet {
4040
impl ActiveModelBehavior for ActiveModel {}
4141
}
4242

43+
mod mixed {
44+
use super::*;
45+
46+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
47+
#[sea_orm(table_name = "mixed")]
48+
pub struct Model {
49+
#[sea_orm(primary_key)]
50+
pub id: i32,
51+
#[sea_orm(unique)]
52+
pub torrent_id: TorrentID,
53+
#[sea_orm(unique)]
54+
pub magnet: MagnetLink,
55+
}
56+
57+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
58+
pub enum Relation {}
59+
60+
#[async_trait::async_trait]
61+
impl ActiveModelBehavior for ActiveModel {}
62+
}
63+
64+
mod migration {
65+
use sea_orm_migration::prelude::*;
66+
67+
mod m20251115_01_mixed {
68+
use sea_orm_migration::{prelude::*, schema::*};
69+
70+
#[derive(DeriveMigrationName)]
71+
pub struct Migration;
72+
73+
#[async_trait::async_trait]
74+
impl MigrationTrait for Migration {
75+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
76+
manager
77+
.create_table(
78+
Table::create()
79+
.table(Mixed::Table)
80+
.if_not_exists()
81+
.col(pk_auto(Mixed::Id))
82+
.col(string(Mixed::TorrentID).unique_key())
83+
.col(string(Mixed::Magnet).unique_key())
84+
.to_owned(),
85+
)
86+
.await
87+
}
88+
89+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
90+
manager
91+
.drop_table(Table::drop().table(Mixed::Table).to_owned())
92+
.await
93+
}
94+
}
95+
96+
#[derive(DeriveIden)]
97+
enum Mixed {
98+
Table,
99+
Id,
100+
TorrentID,
101+
Magnet,
102+
}
103+
}
104+
105+
pub struct Migrator;
106+
107+
#[async_trait::async_trait]
108+
impl MigratorTrait for Migrator {
109+
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
110+
vec![Box::new(m20251115_01_mixed::Migration)]
111+
}
112+
}
113+
}
114+
43115
#[test]
44116
fn test_magnet_active_model() {
45117
let magnet =
@@ -65,3 +137,104 @@ fn test_torrentid_active_model() {
65137
..Default::default()
66138
};
67139
}
140+
141+
#[tokio::test]
142+
async fn test_torrent_real_db() {
143+
use migration::*;
144+
use sea_orm_migration::*;
145+
146+
let tmpdir = async_tempfile::TempDir::new().await.unwrap();
147+
let sqlite = tmpdir.join("db.sqlite");
148+
let sqlite_str = sqlite.to_str().unwrap();
149+
150+
let db = sea_orm::Database::connect(&format!("sqlite://{}?mode=rwc", sqlite_str))
151+
.await
152+
.unwrap();
153+
Migrator::up(&db, None).await.unwrap();
154+
155+
let magnet =
156+
MagnetLink::new(&std::fs::read_to_string("tests/bittorrent-v2-test.magnet").unwrap())
157+
.unwrap();
158+
159+
let model = mixed::ActiveModel {
160+
torrent_id: Set(magnet.id()),
161+
magnet: Set(magnet.clone()),
162+
..Default::default()
163+
}
164+
.save(&db)
165+
.await
166+
.unwrap();
167+
168+
let magnet2 = MagnetLink::new(
169+
&std::fs::read_to_string("tests/bittorrent-v2-hybrid-test.magnet").unwrap(),
170+
)
171+
.unwrap();
172+
173+
let model2 = mixed::ActiveModel {
174+
torrent_id: Set(magnet2.id()),
175+
magnet: Set(magnet2.clone()),
176+
..Default::default()
177+
}
178+
.save(&db)
179+
.await
180+
.unwrap();
181+
182+
let nonactive_model = model.try_into_model().unwrap();
183+
let saved_model_by_id = mixed::Entity::find_by_id(nonactive_model.id)
184+
.one(&db)
185+
.await
186+
.unwrap()
187+
.unwrap();
188+
assert_eq!(saved_model_by_id, nonactive_model);
189+
assert_eq!(saved_model_by_id.magnet, magnet);
190+
assert_eq!(nonactive_model.magnet, magnet);
191+
192+
let nonactive_model2 = model2.try_into_model().unwrap();
193+
let saved_model_by_id2 = mixed::Entity::find_by_id(nonactive_model2.id)
194+
.one(&db)
195+
.await
196+
.unwrap()
197+
.unwrap();
198+
assert_eq!(saved_model_by_id2, nonactive_model2);
199+
assert_eq!(saved_model_by_id2.magnet, magnet2);
200+
assert_eq!(nonactive_model2.magnet, magnet2);
201+
202+
// Try query by TorrentID
203+
let saved_model_by_torrentid = mixed::Entity::find()
204+
.filter(mixed::Column::TorrentId.eq(magnet.id()))
205+
.one(&db)
206+
.await
207+
.unwrap()
208+
.unwrap();
209+
assert_eq!(saved_model_by_id, nonactive_model);
210+
assert_eq!(saved_model_by_torrentid.magnet, magnet);
211+
212+
// Try query by MagnetLink
213+
let saved_model_by_magnet = mixed::Entity::find()
214+
.filter(mixed::Column::Magnet.eq(magnet.clone()))
215+
.one(&db)
216+
.await
217+
.unwrap()
218+
.unwrap();
219+
assert_eq!(saved_model_by_id, nonactive_model);
220+
assert_eq!(saved_model_by_magnet.magnet, magnet);
221+
222+
// Try listing torrents
223+
let mut found_one = false;
224+
let mut found_two = false;
225+
let list = mixed::Entity::find().all(&db).await.unwrap();
226+
println!("{:?}", list);
227+
for entry in list {
228+
if entry.magnet == magnet && entry.torrent_id == magnet.id() {
229+
found_one = true;
230+
continue;
231+
}
232+
233+
if entry.magnet == magnet2 && entry.torrent_id == magnet2.id() {
234+
found_two = true;
235+
}
236+
}
237+
238+
assert!(found_one);
239+
assert!(found_two);
240+
}

0 commit comments

Comments
 (0)