add unit test

This commit is contained in:
Namekuji 2023-06-02 23:01:53 -04:00
parent 7cf2b39cde
commit 874b0a2c69
No known key found for this signature in database
GPG key ID: B541BD6E646CABC7
4 changed files with 53 additions and 13 deletions

View file

@ -41,7 +41,7 @@
"universal": "napi universal", "universal": "napi universal",
"version": "napi version", "version": "napi version",
"format": "cargo fmt", "format": "cargo fmt",
"cargo:unit": "cargo test unit_test", "cargo:unit": "cargo test unit_test && cargo test -F napi unit_test",
"cargo:integration": "cargo test --no-default-features -F noarray int_test -- --test-threads=1" "cargo:integration": "cargo test -F noarray int_test -- --test-threads=1"
} }
} }

View file

@ -142,7 +142,7 @@ mod unit_test {
#[test] #[test]
fn antenna_valid() { fn antenna_valid() {
let instance = json!({ let instance = json!({
"id": "9f4x0bkx1u", "id": "9fil64s6g7cskdrb",
"createdAt": "2023-05-24T06:56:14.323Z", "createdAt": "2023-05-24T06:56:14.323Z",
"name": "Valid Antenna", "name": "Valid Antenna",
"keywords": [["first", "keyword"], ["second"]], "keywords": [["first", "keyword"], ["second"]],
@ -150,7 +150,7 @@ mod unit_test {
"src": "users", "src": "users",
// "userListId" and "userGroupId" can be null or be omitted // "userListId" and "userGroupId" can be null or be omitted
"userListId": null, "userListId": null,
"users": ["9f4yjw6m13", "9f4yk2cp6d"], "users": ["9fil64s6g7cskdrb", "9fil66brl1udxau2"],
"instances": [], "instances": [],
// "caseSensitive", "notify", "withReplies", "withFile", and // "caseSensitive", "notify", "withReplies", "withFile", and
// "hasUnreadNote" are false if ommited // "hasUnreadNote" are false if ommited
@ -175,14 +175,14 @@ mod unit_test {
"keywords": "invalid keyword", "keywords": "invalid keyword",
// "excludeKeywords" is required // "excludeKeywords" is required
"excludeKeywords": null, "excludeKeywords": null,
// "src" should be one of "home", "all", "users", "list", "group", and // "src" must be one of "home", "all", "users", "list", "group", and
// "instances" // "instances"
"src": "invalid_src", "src": "invalid_src",
// "userListId" is string // "userListId" is string
"userListId": ["9f4ziiqfxw"], "userListId": ["9f4ziiqfxw"],
// "users" must be an array of strings // "users" must be an array of strings
"users": [1, "9f4ykyuza6"], "users": [1, "9fil64s6g7cskdrb"],
"instances": ["9f4ykyuybo"], "instances": ["9fil65jzhtjpi3xn"],
// "caseSensitive" is boolean // "caseSensitive" is boolean
"caseSensitive": 0, "caseSensitive": 0,
"notify": true, "notify": true,

View file

@ -95,13 +95,53 @@ pub static VALIDATOR: Lazy<JSONSchema> = Lazy::new(|| App::validator());
#[cfg(test)] #[cfg(test)]
mod unit_test { mod unit_test {
use pretty_assertions::assert_eq;
use serde_json::json;
use crate::util::id::{create_id, init_id};
use crate::util::random::gen_string;
use super::VALIDATOR;
#[test] #[test]
fn valid() { fn app_valid() {
todo!(); init_id(12, "");
let instance = json!({
"id": create_id().unwrap(),
"name": "Test App",
"secret": gen_string(24),
"callbackUrl": "urn:ietf:wg:oauth:2.0:oob",
"permission": ["read:account", "write:account", "read:notes"],
});
assert!(VALIDATOR.is_valid(&instance));
} }
#[test] #[test]
fn invalid() { fn app_invalid() {
todo!(); init_id(12, "");
let instance = json!({
"id": create_id().unwrap(),
// "name" is required
"name": null,
// "permission" must be one of the app permissions
"permission": ["write:invalid_perm", "write:notes"],
// "secret" is a nullable string
"secret": 123,
// "is_authorized" is a nullable boolean
"isAuthorized": "true-ish",
});
let result = VALIDATOR
.validate(&instance)
.expect_err("validation must fail");
let mut paths: Vec<String> = result
.map(|e| e.instance_path.to_string())
.filter(|e| !e.is_empty())
.collect();
paths.sort();
assert_eq!(
paths,
vec!["/isAuthorized", "/name", "/permission/0", "/secret"]
);
} }
} }

View file

@ -72,9 +72,9 @@ mod unit_test {
#[test] #[test]
fn can_generate_aid_compat_ids() { fn can_generate_aid_compat_ids() {
id::native_init_id_generator(16, "".to_string()); id::native_init_id_generator(20, "".to_string());
let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); let id1 = id::native_create_id(Utc::now().timestamp_millis().into());
assert_eq!(id1.len(), 16); assert_eq!(id1.len(), 20);
let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); let id1 = id::native_create_id(Utc::now().timestamp_millis().into());
let id2 = id::native_create_id(Utc::now().timestamp_millis().into()); let id2 = id::native_create_id(Utc::now().timestamp_millis().into());
assert_ne!(id1, id2); assert_ne!(id1, id2);