use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Deserialize, Serialize, Debug, Clone, FromRow, PartialEq)]
pub struct Car {
pub year: i16,
pub make: String,
pub model: String,
pub description: String,
}
impl Car {
pub fn new(year: i16, make: String, model: String, description: String) -> Self {
Self {
year,
make,
model,
description,
}
}
}
#[allow(dead_code)]
pub const CREATE_CARS_TABLE_SQL: &str = "CREATE TABLE IF NOT EXISTS cars (
year INTEGER NOT NULL,
make VARCHAR(25) NOT NULL,
model VARCHAR(25) NOT NULL,
description VARCHAR(25) NOT NULL
);";
#[allow(dead_code)]
pub const SELECT_ALL_CARS_SQL: &str = "SELECT year, make, model, description FROM cars";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_car_creation() {
let car = Car::new(
1967,
"Ford".to_string(),
"Mustang fastback 1967".to_string(),
"American car".to_string(),
);
assert_eq!(car.year, 1967);
assert_eq!(car.make, "Ford");
assert_eq!(car.model, "Mustang fastback 1967");
assert_eq!(car.description, "American car");
}
}