Skip to content

Commit 62bd001

Browse files
committed
add integration test, and expose more as library
1 parent b409e08 commit 62bd001

File tree

10 files changed

+119
-63
lines changed

10 files changed

+119
-63
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rust-analyzer.linkedProjects": [
3+
"./Cargo.toml"
4+
]
5+
}

src/app_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ fn default_database_connection_acquire_timeout_seconds() -> f64 {
103103
}
104104

105105
#[cfg(test)]
106-
pub(crate) mod tests {
106+
pub mod tests {
107107
use super::AppConfig;
108108

109+
#[must_use]
109110
pub fn test_config() -> AppConfig {
110111
serde_json::from_str::<AppConfig>(
111112
r#"{

src/file_cache.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ pub struct FileCache<T: AsyncFromStrWithState> {
7272
static_files: HashMap<PathBuf, Cached<T>>,
7373
}
7474

75+
impl<T: AsyncFromStrWithState> Default for FileCache<T> {
76+
fn default() -> Self {
77+
Self::new()
78+
}
79+
}
80+
7581
impl<T: AsyncFromStrWithState> FileCache<T> {
82+
#[must_use]
7683
pub fn new() -> Self {
7784
Self {
7885
cache: Arc::default(),

src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![deny(clippy::pedantic)]
2+
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
3+
4+
extern crate core;
5+
6+
pub mod app_config;
7+
pub mod file_cache;
8+
pub mod filesystem;
9+
pub mod render;
10+
pub mod templates;
11+
pub mod utils;
12+
pub mod webserver;
13+
14+
use crate::app_config::AppConfig;
15+
use crate::filesystem::FileSystem;
16+
use crate::webserver::database::{FileCache, ParsedSqlFile};
17+
use std::env;
18+
use std::net::SocketAddr;
19+
use std::path::PathBuf;
20+
use templates::AllTemplates;
21+
use webserver::Database;
22+
23+
pub const TEMPLATES_DIR: &str = "sqlpage/templates";
24+
pub const MIGRATIONS_DIR: &str = "sqlpage/migrations";
25+
26+
pub struct AppState {
27+
pub db: Database,
28+
all_templates: AllTemplates,
29+
sql_file_cache: FileCache<ParsedSqlFile>,
30+
file_system: FileSystem,
31+
}
32+
33+
impl AppState {
34+
pub async fn init(config: &AppConfig) -> anyhow::Result<Self> {
35+
// Connect to the database
36+
let db = Database::init(config).await?;
37+
let all_templates = AllTemplates::init()?;
38+
let web_root = get_web_root();
39+
let mut sql_file_cache = FileCache::new();
40+
let file_system = FileSystem::init(&web_root, &db).await;
41+
sql_file_cache.add_static(
42+
PathBuf::from("index.sql"),
43+
ParsedSqlFile::new(&db, include_str!("../index.sql")).await,
44+
);
45+
Ok(AppState {
46+
db,
47+
all_templates,
48+
sql_file_cache,
49+
file_system,
50+
})
51+
}
52+
}
53+
54+
pub fn get_web_root() -> PathBuf {
55+
env::var("WEB_ROOT").map_or_else(
56+
|_| PathBuf::from(&std::path::Component::CurDir),
57+
PathBuf::from,
58+
)
59+
}
60+
61+
pub struct Config {
62+
pub listen_on: SocketAddr,
63+
}

src/main.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,4 @@
1-
#![deny(clippy::pedantic)]
2-
extern crate core;
3-
4-
mod app_config;
5-
mod file_cache;
6-
mod filesystem;
7-
mod render;
8-
mod templates;
9-
mod utils;
10-
mod webserver;
11-
12-
use crate::app_config::AppConfig;
13-
use crate::filesystem::FileSystem;
14-
use crate::webserver::database::{FileCache, ParsedSqlFile};
15-
use crate::webserver::Database;
16-
use std::env;
17-
use std::net::SocketAddr;
18-
use std::path::PathBuf;
19-
use templates::AllTemplates;
20-
21-
const TEMPLATES_DIR: &str = "sqlpage/templates";
22-
const MIGRATIONS_DIR: &str = "sqlpage/migrations";
23-
24-
pub struct AppState {
25-
db: Database,
26-
all_templates: AllTemplates,
27-
sql_file_cache: FileCache<ParsedSqlFile>,
28-
file_system: FileSystem,
29-
}
30-
31-
impl AppState {
32-
async fn init(config: &AppConfig) -> anyhow::Result<Self> {
33-
// Connect to the database
34-
let db = Database::init(config).await?;
35-
let all_templates = AllTemplates::init()?;
36-
let web_root = get_web_root();
37-
let mut sql_file_cache = FileCache::new();
38-
let file_system = FileSystem::init(&web_root, &db).await;
39-
sql_file_cache.add_static(
40-
PathBuf::from("index.sql"),
41-
ParsedSqlFile::new(&db, include_str!("../index.sql")).await,
42-
);
43-
Ok(AppState {
44-
db,
45-
all_templates,
46-
sql_file_cache,
47-
file_system,
48-
})
49-
}
50-
}
51-
52-
fn get_web_root() -> PathBuf {
53-
env::var("WEB_ROOT").map_or_else(
54-
|_| PathBuf::from(&std::path::Component::CurDir),
55-
PathBuf::from,
56-
)
57-
}
58-
59-
pub struct Config {
60-
listen_on: SocketAddr,
61-
}
1+
use sqlpage::{app_config, webserver, AppState, Config};
622

633
#[actix_web::main]
644
async fn main() {

src/templates.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct SplitTemplate {
2020
}
2121

2222
impl SplitTemplate {
23+
#[must_use]
2324
pub fn name(&self) -> Option<&str> {
2425
self.before_list.name.as_deref()
2526
}

src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde_json::{Map, Value};
22

3+
#[must_use]
34
pub fn add_value_to_map(
45
mut map: Map<String, Value>,
56
(key, value): (String, Value),

src/webserver/database/sql.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ fn function_arg_expr(arg: &mut FunctionArg) -> Option<&mut Expr> {
365365
}
366366

367367
#[inline]
368+
#[must_use]
368369
pub fn make_placeholder(db_kind: AnyKind, arg_number: usize) -> String {
369370
if let Some((_, prefix)) = PLACEHOLDER_PREFIXES
370371
.iter()

src/webserver/http.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ impl SingleOrVec {
311311
}
312312
}
313313

314+
#[must_use]
314315
pub fn as_json_str(&self) -> Cow<'_, str> {
315316
match self {
316317
SingleOrVec::Single(x) => Cow::Borrowed(x),
@@ -464,7 +465,9 @@ async fn serve_file(
464465
})
465466
}
466467

467-
async fn main_handler(mut service_request: ServiceRequest) -> actix_web::Result<ServiceResponse> {
468+
pub async fn main_handler(
469+
mut service_request: ServiceRequest,
470+
) -> actix_web::Result<ServiceResponse> {
468471
let path = req_path(&service_request);
469472
let sql_file_path = path_to_sql_file(&path);
470473
if let Some(sql_path) = sql_file_path {

tests/index.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use actix_web::{
2+
http::{self, header::ContentType},
3+
test,
4+
};
5+
use sqlpage::{app_config::AppConfig, webserver::http::main_handler, AppState};
6+
7+
#[actix_web::test]
8+
async fn test_index_ok() {
9+
let config = test_config();
10+
let state = AppState::init(&config).await.unwrap();
11+
let data = actix_web::web::Data::new(state);
12+
let req = test::TestRequest::default()
13+
.app_data(data)
14+
.insert_header(ContentType::plaintext())
15+
.to_srv_request();
16+
let resp = main_handler(req).await.unwrap();
17+
assert_eq!(resp.status(), http::StatusCode::OK);
18+
let body = test::read_body(resp).await;
19+
assert!(body.starts_with(b"<!DOCTYPE html>"));
20+
// the body should contain the strint "It works!" and should not contain the string "error"
21+
let body = String::from_utf8(body.to_vec()).unwrap();
22+
assert!(body.contains("It works !"));
23+
assert!(!body.contains("error"));
24+
}
25+
26+
pub fn test_config() -> AppConfig {
27+
serde_json::from_str::<AppConfig>(
28+
r#"{
29+
"database_url": "sqlite::memory:",
30+
"listen_on": "111.111.111.111:1"
31+
}"#,
32+
)
33+
.unwrap()
34+
}

0 commit comments

Comments
 (0)