write tests for request types

This commit is contained in:
Horhik 2021-04-12 15:44:14 +03:00
parent f46404eb7e
commit 724d7ef154
3 changed files with 77 additions and 16 deletions

View file

@ -1,9 +1,35 @@
use std::sync::atomic::AtomicU32;
use fcpv2::types::SSK; use fcpv2::types::SSK;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json::Result; 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)] #[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -46,7 +72,7 @@ pub struct StartAppReq {
pub req_type: StartApp, pub req_type: StartApp,
} }
#[derive(Deserialize,Serialize, Debug, PartialEq)] #[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StopAppReq { pub struct StopAppReq {
pub(crate) req_type: StopApp, pub(crate) req_type: StopApp,
@ -86,18 +112,55 @@ pub struct AddUserReq {
#[test] #[test]
fn request_StoppApp_are_correct() { fn request_StoppApp_are_correct() {
let json: &str = "{'reqType':'StopApp'}"; let json = "{\"type\":\"stopApp\"}";
//log::info!("{} and {}", json, tjsn); let parsed: Request = serde_json::from_str(json).unwrap();
//assert_eq!(jsoned, StopAppReq{req_type: StopApp}); assert_eq!(Request::StopApp, parsed);
//assert_eq!(json,tjsn);
} }
#[test] #[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] #[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] #[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] #[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] #[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(),
})
}

View file

@ -1,6 +1,4 @@
use crate::chat::types::{PackedMessage, SP}; use crate::chat::types::{PackedMessage, SP}; use rusqlite;
use rusqlite;
use super::{ use super::{
handlers, handlers,
request::*, request::*,

View file

@ -16,7 +16,7 @@ pub type Time = chrono::DateTime<chrono::Local>;
pub type SignKey = String; pub type SignKey = String;
pub type InsertKey = SSK; pub type InsertKey = SSK;
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Id(pub uuid::Uuid); pub struct Id(pub uuid::Uuid);
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]