fix unit test

This commit is contained in:
Namekuji 2023-06-02 07:08:58 -04:00
parent e51deb3794
commit 403e95b673
No known key found for this signature in database
GPG Key ID: B541BD6E646CABC7
9 changed files with 35 additions and 21 deletions

View File

@ -5,7 +5,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
default = [] default = ["napi"]
noarray = [] noarray = []
napi = ["dep:napi", "dep:napi-derive"] napi = ["dep:napi", "dep:napi-derive"]
@ -30,3 +30,6 @@ utoipa = "3.3.0"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.0", default-features = false, features = ["napi4"], optional = true } napi = { version = "2.12.0", default-features = false, features = ["napi4"], optional = true }
napi-derive = { version = "2.12.0", optional = true } napi-derive = { version = "2.12.0", optional = true }
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -8,11 +8,13 @@ use schemars::{schema_for, JsonSchema};
cfg_if! { cfg_if! {
if #[cfg(feature = "napi")] { if #[cfg(feature = "napi")] {
mod napi; mod napi;
pub use napi::antenna::Antenna; pub use self::napi::antenna::Antenna;
pub use napi::antenna::AntennaSrc; pub use self::napi::antenna::AntennaSrc;
} else { } else {
pub use antenna::Antenna; pub use antenna::Antenna;
pub use antenna::AntennaSrc; pub use antenna::AntennaSrc;
pub use app::App;
pub use app::AppPermission;
} }
} }

View File

@ -46,7 +46,7 @@ pub enum AntennaSrc {
Instances, Instances,
} }
impl TryFrom<AntennaSrcEnum> for AntennaSrc { impl TryFrom<AntennaSrcEnum> for super::AntennaSrc {
type Error = crate::error::Error; type Error = crate::error::Error;
fn try_from(value: AntennaSrcEnum) -> Result<Self, Self::Error> { fn try_from(value: AntennaSrcEnum) -> Result<Self, Self::Error> {
@ -55,12 +55,13 @@ impl TryFrom<AntennaSrcEnum> for AntennaSrc {
} }
// ---- TODO: could be macro // ---- TODO: could be macro
impl Schema<Self> for Antenna {} impl Schema<Self> for super::Antenna {}
pub static VALIDATOR: Lazy<JSONSchema> = Lazy::new(|| Antenna::validator()); pub static VALIDATOR: Lazy<JSONSchema> = Lazy::new(|| super::Antenna::validator());
// ---- // ----
#[cfg(test)] #[cfg(test)]
mod unit_test { mod unit_test {
use pretty_assertions::assert_eq;
use serde_json::json; use serde_json::json;
use crate::{entity::sea_orm_active_enums::AntennaSrcEnum, schema::AntennaSrc}; use crate::{entity::sea_orm_active_enums::AntennaSrcEnum, schema::AntennaSrc};
@ -128,20 +129,23 @@ mod unit_test {
let result = VALIDATOR let result = VALIDATOR
.validate(&instance) .validate(&instance)
.expect_err("validation must fail"); .expect_err("validation must fail");
let mut paths: Vec<String> = result.map(|e| e.schema_path.to_string()).collect(); let mut paths: Vec<String> = result
.map(|e| e.instance_path.to_string())
.filter(|e| !e.is_empty())
.collect();
paths.sort(); paths.sort();
assert_eq!( assert_eq!(
paths, paths,
vec![ vec![
"/properties/caseSensitive/type", "/caseSensitive",
"/properties/createdAt/format", #[cfg(not(feature = "napi"))]
"/properties/excludeKeywords/type", "/createdAt",
"/properties/id/type", "/excludeKeywords",
"/properties/keywords/type", "/id",
"/properties/src/enum", "/keywords",
"/properties/userListId/type", "/src",
"/properties/users/items/type", "/userListId",
"/required" "/users/0"
] ]
); );
} }

View File

@ -13,14 +13,14 @@ pub struct App {
#[schemars(url)] #[schemars(url)]
pub callback_url: Option<String>, pub callback_url: Option<String>,
#[schema(inline)] #[schema(inline)]
pub permission: Vec<Permission>, pub permission: Vec<AppPermission>,
pub secret: Option<String>, pub secret: Option<String>,
pub is_authorized: Option<bool>, pub is_authorized: Option<bool>,
} }
/// This represents `permissions` in `packages/calckey-js/src/consts.ts`. /// This represents `permissions` in `packages/calckey-js/src/consts.ts`.
#[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)]
pub enum Permission { pub enum AppPermission {
#[serde(rename = "read:account")] #[serde(rename = "read:account")]
ReadAccount, ReadAccount,
#[serde(rename = "write:account")] #[serde(rename = "write:account")]

View File

@ -1,10 +1,9 @@
use napi::bindgen_prelude::{FromNapiValue, ToNapiValue};
use napi_derive::napi;
use parse_display::FromStr; use parse_display::FromStr;
use schemars::JsonSchema; use schemars::JsonSchema;
use utoipa::ToSchema; use utoipa::ToSchema;
use napi::bindgen_prelude::{FromNapiValue, ToNapiValue};
use napi_derive::napi;
#[napi] #[napi]
#[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]

View File

@ -4,6 +4,7 @@ mod int_test {
repository::Repository, repository::Repository,
schema, schema,
}; };
use pretty_assertions::assert_eq;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use crate::{cleanup, prepare}; use crate::{cleanup, prepare};

View File

@ -10,3 +10,6 @@ cuid2 = "0.1.0"
once_cell = "1.17.1" once_cell = "1.17.1"
rand = "0.8.5" rand = "0.8.5"
thiserror = "1.0.40" thiserror = "1.0.40"
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -22,6 +22,7 @@ pub fn create_id() -> Result<String, ErrorUninitialized> {
#[cfg(test)] #[cfg(test)]
mod unit_test { mod unit_test {
use pretty_assertions::{assert_eq, assert_ne};
use std::thread; use std::thread;
use crate::id; use crate::id;

View File

@ -10,6 +10,7 @@ pub fn gen_string(length: u16) -> String {
#[cfg(test)] #[cfg(test)]
mod unit_test { mod unit_test {
use pretty_assertions::{assert_eq, assert_ne};
use std::thread; use std::thread;
use super::gen_string; use super::gen_string;