Skip to content

Commit

Permalink
Add log and env_logger crates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshaughnessy committed Oct 30, 2023
1 parent eaac2d2 commit b1c829a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ chrono = "0.4.31"
diesel = { version = "2.1.0", features = ["postgres", "r2d2", "extras"] }
dotenv = "0.15.0"
env_logger = "0.10.0"
log = "0.4.20"
serde = "1.0.189"
serde_derive = "1.0.189"
serde_json = "1.0.107"
Expand Down
2 changes: 2 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async fn main() -> std::io::Result<()> {
let ip_address = std::env::var("IP_ADDRESS").expect("IP_ADDRESS must be set");
let port = std::env::var("PORT").expect("PORT must be set");

env_logger::init();

let manager = ConnectionManager::<PgConnection>::new(database_url);
let pool: r2d2::Pool<ConnectionManager<PgConnection>> = r2d2::Pool::builder()
.build(manager)
Expand Down
19 changes: 14 additions & 5 deletions server/src/models/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use diesel::deserialize::QueryableByName;
use diesel::prelude::*;
use diesel::update;
use diesel::Insertable;
use log::info;
use serde_derive::{Deserialize, Serialize};

use crate::schema::weights;
Expand Down Expand Up @@ -47,7 +48,7 @@ pub fn create_weight(
.returning(weights::all_columns)
.get_result(conn)?;

println!("create_weight results: \n{:?}", inserted_weight);
info!("create_weight results: \n{:?}", inserted_weight);
Ok(inserted_weight)
}

Expand All @@ -56,7 +57,7 @@ pub fn read_weights(conn: &mut PgConnection) -> Result<Vec<Weight>, diesel::resu

let results: Result<Vec<Weight>, diesel::result::Error> = weights.limit(100).load(conn);

println!("read_weights results: \n{:?}", results);
info!("read_weights results: \n{:?}", results);
results
}

Expand All @@ -74,7 +75,7 @@ pub fn update_weight(
.filter(weight_id.eq(target_weight_id))
.get_result(conn)?;

println!("update_weight results: \n{:?}", updated_weight);
info!("update_weight results: \n{:?}", updated_weight);
Ok(updated_weight)
}

Expand All @@ -87,7 +88,7 @@ pub fn delete_weight(
let num_deleted =
diesel::delete(weights.filter(weight_id.eq(target_weight_id))).execute(conn)?;

println!("delete_weight results: \n{:?}", num_deleted);
info!("delete_weight results: \n{:?}", num_deleted);

if num_deleted != 1 {
return Err(diesel::result::Error::RollbackTransaction);
Expand All @@ -103,6 +104,10 @@ mod tests {

use super::*;

fn setup() {
let _ = env_logger::builder().is_test(true).try_init();
}

fn get_test_connection() -> PooledConnection<ConnectionManager<PgConnection>> {
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = ConnectionManager::<PgConnection>::new(database_url);
Expand All @@ -120,6 +125,7 @@ mod tests {

#[test]
fn test_create_weight() {
setup();
let mut conn = get_test_connection();

let new_weight = CreateWeightPayload {
Expand All @@ -136,6 +142,7 @@ mod tests {

#[test]
fn test_read_weights() {
setup();
let mut conn = get_test_connection();
create_weight_for_test(&mut conn); // Ensure there is at least one weight in the database
let result = read_weights(&mut conn);
Expand All @@ -146,9 +153,10 @@ mod tests {

#[test]
fn test_update_weight() {
setup();
let mut conn = get_test_connection();
let target_weight_id = create_weight_for_test(&mut conn).weight_id;
println!("{:?}", create_weight_for_test(&mut conn));
info!("{:?}", create_weight_for_test(&mut conn));
let update_payload = UpdateWeightPayload {
measured_at: 1635535802,
weight_kg: 71.0,
Expand All @@ -162,6 +170,7 @@ mod tests {

#[test]
fn test_delete_weight() {
setup();
let mut conn = get_test_connection();
let target_weight_id = create_weight_for_test(&mut conn).weight_id;
let result = delete_weight(&mut conn, target_weight_id);
Expand Down

0 comments on commit b1c829a

Please sign in to comment.