From 724d7ef1549037a45c2b56648e36699eeba1e092 Mon Sep 17 00:00:00 2001 From: Horhik Date: Mon, 12 Apr 2021 15:44:14 +0300 Subject: [PATCH] write tests for request types --- src/api/request.rs | 87 ++++++++++++++++++++++++++++++++++++++------- src/api/selector.rs | 4 +-- src/db/types.rs | 2 +- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/api/request.rs b/src/api/request.rs index cd96923..84c601c 100644 --- a/src/api/request.rs +++ b/src/api/request.rs @@ -1,9 +1,35 @@ -use std::sync::atomic::AtomicU32; use fcpv2::types::SSK; use serde_derive::{Deserialize, Serialize}; use serde_json::Result; +use std::sync::atomic::AtomicU32; -type Id = crate::db::types::Id; +use crate::db::types::Id; + +#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[serde(rename_all = "camelCase")] +#[serde(tag = "type")] +enum Request { + StartApp, + StopApp, + LoadUsers, + #[serde(rename_all = "camelCase")] + SendMessage { + user_id: Id, + message: String, + }, + #[serde(rename_all = "camelCase")] + LoadMessages { + user_id: Id, + count: u32, + start_index: u32, + }, + #[serde(rename_all = "camelCase")] + AddUser { + name: String, + sign_key: String, + insert_key: String, + }, // CreateInstance TODO v0.3 +} #[derive(Serialize, Deserialize, Debug, PartialEq)] #[serde(rename_all = "camelCase")] @@ -46,7 +72,7 @@ pub struct StartAppReq { pub req_type: StartApp, } -#[derive(Deserialize,Serialize, Debug, PartialEq)] +#[derive(Deserialize, Serialize, Debug, PartialEq)] #[serde(rename_all = "camelCase")] pub struct StopAppReq { pub(crate) req_type: StopApp, @@ -86,18 +112,55 @@ pub struct AddUserReq { #[test] fn request_StoppApp_are_correct() { - let json: &str = "{'reqType':'StopApp'}"; - //log::info!("{} and {}", json, tjsn); - //assert_eq!(jsoned, StopAppReq{req_type: StopApp}); - //assert_eq!(json,tjsn); + let json = "{\"type\":\"stopApp\"}"; + let parsed: Request = serde_json::from_str(json).unwrap(); + assert_eq!(Request::StopApp, parsed); } #[test] -fn request_StartApp_are_correct() {} +fn request_StartApp_are_correct() { + let json = "{\"type\":\"startApp\"}"; + let parsed: Request = serde_json::from_str(json).unwrap(); + assert_eq!(Request::StartApp, parsed); +} #[test] -fn request_LoadUsers_are_correct() {} +fn request_LoadUsers_are_correct() { + let json = "{\"type\":\"loadUsers\"}"; + let parsed: Request = serde_json::from_str(json).unwrap(); + assert_eq!(Request::LoadUsers, parsed); +} #[test] -fn request_SendMessage_are_correct() {} +fn request_SendMessage_are_correct() { + let id = uuid::Uuid::new_v4(); + let json = "{\"type\":\"sendMessage\",\"userId\":\"".to_owned() + + &id.to_string()[..] + + "\",\"message\":\"hey jon\"}"; + let parsed: Request = serde_json::from_str(&json).unwrap(); + assert_eq!(Request::SendMessage{user_id: Id(id), message: "hey jon".to_string()}, parsed); +} #[test] -fn request_LoadMessages_are_correct() {} +fn request_LoadMessages_are_correct() { + let id = uuid::Uuid::new_v4(); + let json = "{\"type\":\"loadMessages\",\"userId\":\"".to_owned() + + &id.to_string()[..] + + "\",\"count\":10,\"startIndex\":343}"; + let parsed: Request = serde_json::from_str(&json).unwrap(); + assert_eq!( + parsed, + Request::LoadMessages { + user_id: Id(id), + count: 10, + start_index: 343 + } + ); +} #[test] -fn request_AddUser_are_correct() {} +fn request_AddUser_are_correct() { + let json = "{\"type\":\"addUser\",\"name\":\"john\",\"signKey\":\"USK@bxouok43eKpx3g4WmURjviGispWzYxeByiWRsmYOy5k,Y9j~lPDUoNlSTbZfDNaUajfePBrW~KM6uvHyOGWeA7Q,AQECAAE\",\"insertKey\":\"USK@bxouok43eKpx3g4WmURjviGispWzYxeByiWRsmYOy5k,Y9j~lPDUoNlSTbZfDNaUajfePBrW~KM6uvHyOGWeA7Q,AQECAAE\"}"; + let parsed: Request = serde_json::from_str(json).unwrap(); + assert_eq!(parsed, Request::AddUser{ + name: "john".to_string(), + sign_key: "USK@bxouok43eKpx3g4WmURjviGispWzYxeByiWRsmYOy5k,Y9j~lPDUoNlSTbZfDNaUajfePBrW~KM6uvHyOGWeA7Q,AQECAAE".to_string(), + insert_key: "USK@bxouok43eKpx3g4WmURjviGispWzYxeByiWRsmYOy5k,Y9j~lPDUoNlSTbZfDNaUajfePBrW~KM6uvHyOGWeA7Q,AQECAAE".to_string(), + }) + +} diff --git a/src/api/selector.rs b/src/api/selector.rs index a190987..49e4d3a 100644 --- a/src/api/selector.rs +++ b/src/api/selector.rs @@ -1,6 +1,4 @@ -use crate::chat::types::{PackedMessage, SP}; -use rusqlite; - +use crate::chat::types::{PackedMessage, SP}; use rusqlite; use super::{ handlers, request::*, diff --git a/src/db/types.rs b/src/db/types.rs index ef8996f..5bfe686 100644 --- a/src/db/types.rs +++ b/src/db/types.rs @@ -16,7 +16,7 @@ pub type Time = chrono::DateTime; pub type SignKey = String; pub type InsertKey = SSK; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct Id(pub uuid::Uuid); #[derive(Debug, Deserialize, Serialize)]