release_checker/test_utilities/src/lib.rs

166 lines
4.4 KiB
Rust

use std::error::Error;
use rusqlite::Connection;
use application::configuration::VcsType;
use application::register_current::models::Current;
use application::register_origin::models::Origin;
pub fn get_current(name: String, connection: &Connection) -> Current {
let current = connection
.query_row(
"SELECT name, vcs_type, version FROM Currents WHERE name=?1",
[&name],
|row| {
Ok(Current {
name: row.get(0).unwrap(),
vcs_type: VcsType::Github,
version: row.get(2).unwrap(),
})
},
)
.unwrap();
current
}
pub fn get_origin(name: String, connection: &Connection) -> Origin {
let origin = connection
.query_row(
"SELECT name, vcs_type, owner_name, repo_name FROM Origins WHERE name=?1",
[&name],
|row| {
Ok(Origin {
name: row.get(0).unwrap(),
vcs_type: VcsType::Github,
owner_name: row.get(2).unwrap(),
repo_name: row.get(3).unwrap(),
})
},
)
.unwrap();
origin
}
pub fn get_current_count(connection: &Connection) -> usize {
let count = connection
.query_row("SELECT COUNT(1) FROM Currents", [], |row| row.get(0))
.unwrap();
count
}
pub fn get_origins_count(connection: &Connection) -> usize {
let count = connection
.query_row("SELECT COUNT(1) FROM Origins", [], |row| row.get(0))
.unwrap();
count
}
pub fn insert_current(
name: String,
version: String,
vcs_type: String,
email: String,
connection: &Connection,
) {
connection
.execute(
"INSERT INTO Currents (name, version, vcs_type, contact) VALUES(?1, ?2, ?3, ?4)",
[&name, &version, &vcs_type, &email],
)
.unwrap();
}
pub fn insert_origin(
name: String,
owner_name: String,
repo_name: String,
vcs_type: String,
connection: &Connection,
) {
match connection.execute(
"INSERT INTO Origins (name, owner_name, repo_name, vcs_type) VALUES(?1, ?2, ?3, ?4)",
[&name, &owner_name, &repo_name, &vcs_type],
) {
Ok(x) => {} // TODO: there must be a better way of doing this in rust? To only do something when Err occurs
Err(e) => println!("{}", e),
};
}
pub mod database {
use std::fs;
use httpmock::MockServer;
use test_context::TestContext;
use application::configuration::Settings;
use application::migrate;
pub struct FunctionalTestContext {
pub settings: Settings,
pub mock_server: MockServer,
}
impl TestContext for FunctionalTestContext {
fn setup() -> Self {
let mock_server = MockServer::start(); // TODO: this could be overkill to start a server on every test but for now this is the most easy. It also will start a server on every functional test, and that kind of sets an upper bond on how many tests that can be created :D I need to look into this. Maybe just call .start when i need to?
let settings = Settings::new(Some(mock_server.url("/").as_str())).unwrap();
migrate(&settings);
FunctionalTestContext {
settings,
mock_server,
}
}
fn teardown(self) {
// we dont care about if this went well or not, because there is not much we can do about it
// but we really do want to remove the test database between each run, so we get a nice clean slate
// extra added for test
let _result = fs::remove_file("version_checker_db");
}
}
}
pub mod notification {
use application::request::models::{Current, State};
use application::request::notification::Notification;
pub struct FakeNotification<T>
where
T: Fn(),
{
pub callback: T,
}
impl<T> Notification for FakeNotification<T>
where
T: Fn(),
{
fn send(&self, currents: &Vec<State>) {
(self.callback)();
}
}
}
pub mod console {
use application::console::Print;
pub struct FakeConsole<T>
where
T: Fn(),
{
pub callback: T,
}
impl<T> Print for FakeConsole<T>
where
T: Fn(),
{
fn println(&self, text: &str) {
(self.callback)();
}
}
}