[#13] Further overhaul of the project structure for better testing

master
Jef Roosens 2021-04-08 22:39:04 +02:00
parent e78de73d83
commit 2e73d88ae9
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
10 changed files with 48 additions and 16 deletions

View File

@ -4,8 +4,21 @@ version = "0.0.1"
authors = ["Jef Roosens <roosensjef@gmail.com>"] authors = ["Jef Roosens <roosensjef@gmail.com>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib]
name = "fej_lib"
src = "src/lib.rs"
test = true
bench = true
doc = true
doctest = true
[[bin]]
name = "fej"
src = "src/main.rs"
test = false
bench = false
doc = false
[dependencies] [dependencies]
rocket = "0.4.7" rocket = "0.4.7"
serde = "1.0.124" serde = "1.0.124"

View File

@ -13,11 +13,13 @@ RUN apk update && apk add --no-cache openssl-dev build-base curl && \
COPY Cargo.toml Cargo.lock ./ COPY Cargo.toml Cargo.lock ./
COPY src/ ./src/ COPY src/ ./src/
# Finally, build the project # Run the tests, don't want no broken docker image
# And then finally, build the project
# Thank the lords that this article exists # Thank the lords that this article exists
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172 # https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
# TODO add what these flags do & why they work # TODO add what these flags do & why they work
RUN RUSTFLAGS="-C target-feature=-crt-static" cargo build --release RUN RUSTFLAGS="-C target-feature=-crt-static" cargo test && \
RUSTFLAGS="-C target-feature=-crt-static" cargo build --release --bin fej
# Now, we create the actual image # Now, we create the actual image

View File

@ -23,13 +23,13 @@ push:
# Run # Run
run: run:
@ RUST_BACKTRACE=1 cargo run @ RUST_BACKTRACE=1 cargo run --bin fej
.PHONY: run .PHONY: run
# Testing # Testing
test: test:
@ cargo test @ cargo test --no-fail-fast
.PHONY: test .PHONY: test
format: format:

1
benches/ivago.rs 100644
View File

@ -0,0 +1 @@

View File

@ -1,6 +1,7 @@
// I can probably do this way easier using an external crate, I should do that // I can probably do this way easier using an external crate, I should do that
use rocket::http::Status; use rocket::http::Status;
#[derive(Debug, PartialEq)]
pub enum FejError { pub enum FejError {
InvalidArgument, InvalidArgument,
FailedRequest, FailedRequest,

View File

@ -10,6 +10,7 @@ use std::convert::TryFrom;
/// This class is a simple wrapper around chrono's DateTime. Its sole purpose /// This class is a simple wrapper around chrono's DateTime. Its sole purpose
/// is to avoid error E0117. /// is to avoid error E0117.
#[derive(Debug, PartialEq)]
pub struct BasicDate(pub DateTime<Tz>); pub struct BasicDate(pub DateTime<Tz>);
/// This allows us to use BasicDate as a query parameter in our routes. /// This allows us to use BasicDate as a query parameter in our routes.
@ -54,3 +55,15 @@ impl Serialize for BasicDate {
serializer.serialize_str(&String::from(self)) serializer.serialize_str(&String::from(self))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_invalid_date() {
let val = "2012-13-12";
let date = BasicDate::try_from(val);
assert_eq!(date, Err(FejError::InvalidArgument));
}
}

View File

@ -1,6 +1,4 @@
mod controller; mod controller;
#[cfg(test)]
mod tests;
use controller::structs::{BasicDate, PickupTime, Street}; use controller::structs::{BasicDate, PickupTime, Street};
use controller::{get_pickup_times, search_streets}; use controller::{get_pickup_times, search_streets};

11
src/lib.rs 100644
View File

@ -0,0 +1,11 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
// Route modules
pub mod ivago;
// Helper modules
pub mod catchers;
pub mod errors;

View File

@ -1,14 +1,7 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
extern crate chrono_tz; use fej_lib::{catchers, ivago};
// Route modules
mod catchers;
mod errors;
mod ivago;
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {
rocket::ignite() rocket::ignite()

View File

@ -2,7 +2,7 @@ use rocket::http::Status;
use rocket::local::Client; use rocket::local::Client;
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", super::routes()) rocket::ignite().mount("/", fej_lib::ivago::routes())
} }
/// Test 404 response /// Test 404 response