Skip to content

Commit 64713f3

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

2 files changed

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

0 commit comments

Comments
 (0)