Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
increasing test coverage rate (#6)
Browse files Browse the repository at this point in the history
* increasing crawler test coverage

* increasing crawler test coverage again

* increasing listener test coverage again

* increasing schema test coverage

* fix cargo-llvm-cov

* fix cargo-llvm-cov again
  • Loading branch information
tommady authored Oct 26, 2021
1 parent 7852cd9 commit df34ad2
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fugle"
version = "0.3.2"
version = "0.3.3"
authors = ["tommady <tommady@users.noreply.github.com>"]
description = "A Simple, Lightweight, Fast and Safe Fugle Library"
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![forbid(unsafe_code)]
#![warn(clippy::all)]
#![cfg_attr(coverage, feature(no_coverage))]

//! A Simple, Lightweight, Fast and Safe Fugle Library.
//!
Expand Down
27 changes: 22 additions & 5 deletions src/listener.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::schema::{FugleError, Response, Result};
use crate::schema::{FugleError, Response, ResponseType, Result};
use log::error;
use std::sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -77,6 +77,7 @@ impl Intraday {
),
self.sender.clone(),
self.done.clone(),
ResponseType::Chart,
) {
Ok(w) => {
self.workers.push(w);
Expand Down Expand Up @@ -112,6 +113,7 @@ impl Intraday {
),
self.sender.clone(),
self.done.clone(),
ResponseType::Meta,
) {
Ok(w) => {
self.workers.push(w);
Expand Down Expand Up @@ -147,6 +149,7 @@ impl Intraday {
),
self.sender.clone(),
self.done.clone(),
ResponseType::Quote,
) {
Ok(w) => {
self.workers.push(w);
Expand All @@ -173,7 +176,12 @@ struct Worker {
}

impl Worker {
fn new(uri: &str, sender: Sender<Response>, done: Arc<AtomicBool>) -> Result<Worker> {
fn new(
uri: &str,
sender: Sender<Response>,
done: Arc<AtomicBool>,
resposne_type: ResponseType,
) -> Result<Worker> {
let (mut socket, _) = connect(uri)?;

let thread = thread::spawn(move || {
Expand All @@ -197,9 +205,18 @@ impl Worker {
}

let sending = || -> Result<()> {
sender
.send(serde_json::from_str(txt.as_str())?)
.map_err(|_| FugleError::MpscSendError)?;
match resposne_type {
ResponseType::Chart => sender
.send(Response::Chart(serde_json::from_str(txt.as_str())?))
.map_err(|_| FugleError::MpscSendError)?,
ResponseType::Meta => sender
.send(Response::Meta(serde_json::from_str(txt.as_str())?))
.map_err(|_| FugleError::MpscSendError)?,
ResponseType::Quote => sender
.send(Response::Quote(serde_json::from_str(txt.as_str())?))
.map_err(|_| FugleError::MpscSendError)?,
_ => unreachable!("not supported response type"),
}
Ok(())
};

Expand Down
21 changes: 21 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use serde::{Deserialize, Serialize};

pub type Result<T> = std::result::Result<T, FugleError>;

#[cfg_attr(coverage, no_coverage)]
fn default_naive_date() -> NaiveDate {
NaiveDate::from_num_days_from_ce(1)
}

#[cfg_attr(coverage, no_coverage)]
fn default_date_time() -> DateTime<FixedOffset> {
DateTime::<FixedOffset>::from_utc(
NaiveDateTime::from_timestamp(0, 0),
Expand Down Expand Up @@ -36,6 +38,7 @@ pub struct Info {
}

impl Default for Info {
#[cfg_attr(coverage, no_coverage)]
fn default() -> Info {
Info {
last_updated_at: default_date_time(),
Expand Down Expand Up @@ -178,6 +181,7 @@ pub struct QuoteTotal {
}

impl Default for QuoteTotal {
#[cfg_attr(coverage, no_coverage)]
fn default() -> QuoteTotal {
QuoteTotal {
at: default_date_time(),
Expand Down Expand Up @@ -211,6 +215,7 @@ pub struct QuoteTrial {
}

impl Default for QuoteTrial {
#[cfg_attr(coverage, no_coverage)]
fn default() -> QuoteTrial {
QuoteTrial {
at: default_date_time(),
Expand Down Expand Up @@ -240,6 +245,7 @@ pub struct QuoteTrade {
}

impl Default for QuoteTrade {
#[cfg_attr(coverage, no_coverage)]
fn default() -> QuoteTrade {
QuoteTrade {
at: default_date_time(),
Expand Down Expand Up @@ -271,6 +277,7 @@ pub struct QuoteOrder {
}

impl Default for QuoteOrder {
#[cfg_attr(coverage, no_coverage)]
fn default() -> QuoteOrder {
QuoteOrder {
at: default_date_time(),
Expand All @@ -290,6 +297,7 @@ pub struct QuotePrice {
}

impl Default for QuotePrice {
#[cfg_attr(coverage, no_coverage)]
fn default() -> QuotePrice {
QuotePrice {
at: default_date_time(),
Expand Down Expand Up @@ -379,6 +387,7 @@ pub struct Dealt {
}

impl Default for Dealt {
#[cfg_attr(coverage, no_coverage)]
fn default() -> Dealt {
Dealt {
at: default_date_time(),
Expand Down Expand Up @@ -419,6 +428,7 @@ pub enum Response {
Volumes(VolumesResponse),
}

#[cfg_attr(coverage, no_coverage)]
#[derive(Debug)]
pub enum ResponseType {
Chart,
Expand All @@ -443,6 +453,7 @@ pub struct ErrorResponse {
}

impl std::fmt::Display for ErrorResponse {
#[cfg_attr(coverage, no_coverage)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
Expand All @@ -453,11 +464,13 @@ impl std::fmt::Display for ErrorResponse {
}

impl std::error::Error for ErrorResponse {
#[cfg_attr(coverage, no_coverage)]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

#[cfg_attr(coverage, no_coverage)]
#[derive(Debug)]
pub enum FugleError {
MpscSendError,
Expand Down Expand Up @@ -486,6 +499,7 @@ pub enum FugleError {
}

impl std::fmt::Display for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
FugleError::SerdeJson(ref e) => write!(f, "Serde_json Lib error: {}", e),
Expand All @@ -505,6 +519,7 @@ impl std::fmt::Display for FugleError {
}

impl std::error::Error for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
FugleError::SerdeJson(ref e) => Some(e),
Expand All @@ -524,37 +539,43 @@ impl std::error::Error for FugleError {
}

impl From<std::sync::mpsc::RecvError> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: std::sync::mpsc::RecvError) -> FugleError {
FugleError::MpscRecvError(err)
}
}

impl From<std::io::Error> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: std::io::Error) -> FugleError {
FugleError::StdIO(err)
}
}

impl From<ureq::Error> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: ureq::Error) -> FugleError {
FugleError::Ureq(Box::new(err))
}
}

#[cfg(feature = "websocket")]
impl From<tungstenite::Error> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: tungstenite::Error) -> FugleError {
FugleError::Tungstenite(err)
}
}

impl From<serde_json::Error> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: serde_json::Error) -> FugleError {
FugleError::SerdeJson(err)
}
}

impl From<ErrorResponse> for FugleError {
#[cfg_attr(coverage, no_coverage)]
fn from(err: ErrorResponse) -> FugleError {
match err.error.code {
400 => FugleError::General(err),
Expand Down
Loading

0 comments on commit df34ad2

Please sign in to comment.