forked from vincent-herlemont/native_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeserialization_error.rs
41 lines (37 loc) · 1.4 KB
/
deserialization_error.rs
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
use native_db::db_type::Error;
use native_db::db_type::Result;
use native_db::*;
use itertools::Itertools;
use native_model::{native_model, Error as ModelError, Model};
use serde::{Deserialize, Serialize};
use shortcut_assert_fs::TmpFs;
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
#[native_model(id = 1, version = 1)]
#[native_db]
struct Item1 {
// The type of the primary key must be u32, see generation of the test "create_local_database_for_tests".
// We change the type of the primary key to String to generate a deserialization error.
#[primary_key]
id: String,
#[secondary_key(unique)]
name: String,
}
use include_dir::{include_dir, Dir};
static PROJECT_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/tests/data");
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[test]
fn create_local_database_for_tests() {
let tmp = TmpFs::new().unwrap();
tmp.copy_assets(&PROJECT_DIR).unwrap();
tmp.display_dir_entries();
let database_path = tmp.path("db_0_8-pre-0").to_path_buf();
let mut models = Models::new();
models.define::<Item1>().unwrap();
let db = Builder::new().open(&models, &database_path).unwrap();
let r = db.r_transaction().unwrap();
let result: Result<Vec<Item1>> = r.scan().primary().unwrap().all().unwrap().try_collect();
assert!(matches!(
result,
Err(Error::ModelError(ModelError::DecodeBodyError(_)))
));
}