Skip to content

Commit

Permalink
fix sqlite in memory and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nociza committed Mar 25, 2023
1 parent 9ae3f98 commit 73232db
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ jobs:
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
sqlite:
image: kartoza/sqlite
env:
SQLITE_DATABASE: test.db
ports:
- 5432:5432
env:
MYSQL_DATABASE_URL: mysql://root:password@0.0.0.0:3306/test_db
SQLITE_DATABASE_URL: sqlite://test.db
steps:
- uses: actions/checkout@v3
- name: Build
Expand Down
14 changes: 11 additions & 3 deletions src/dbc/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::dbc;
use std::sync::Arc;

use rusqlite;

use crate::dbc;

pub(crate) struct SQLiteConnection {
connection: rusqlite::Connection,
}

impl SQLiteConnection {
pub(crate) fn get_connection(url: &str) -> Result<Box<dyn dbc::Connection>, dbc::Error> {
let connection;
if url == "sqlite://:memory:" {
connection = rusqlite::Connection::open_in_memory()?;
} else {
connection = rusqlite::Connection::open(url)?;
}
Ok(Box::new(SQLiteConnection {
connection: rusqlite::Connection::open(url)?,
connection,
}) as Box<dyn dbc::Connection>)
}
}
Expand Down Expand Up @@ -42,7 +50,7 @@ impl dbc::Connection for SQLiteConnection {
columns: Arc::clone(&columns),
});
}
Ok(dbc::QueryResult{
Ok(dbc::QueryResult {
rows,
})
}
Expand Down
19 changes: 4 additions & 15 deletions tests/sqlite_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ use rdbc2::dbc;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

fn _get_sqlite_connection_url() -> String {
if std::env::var("SQLITE_DATABASE_URL").is_ok() {
std::env::var("SQLITE_DATABASE_URL").unwrap()
} else {
"sqlite://:memory:".to_owned()
}
}

#[tokio::test]
async fn test_sqlite_simple_query() -> Result<(), Error> {
let url = _get_sqlite_connection_url();
let mut database = dbc::Database::new(url.as_str())?;
let mut database = dbc::Database::new("sqlite://:memory:")?;
let query = "SELECT 1";
let result = database.execute_query(query)?;
assert_eq!(result.rows.len(), 1);
Expand All @@ -23,8 +14,7 @@ async fn test_sqlite_simple_query() -> Result<(), Error> {

#[tokio::test]
async fn test_sqlite_query_with_params() -> Result<(), Error> {
let url = _get_sqlite_connection_url();
let mut database = dbc::Database::new(url.as_str())?;
let mut database = dbc::Database::new("sqlite://:memory:")?;
let query = "SELECT ? + ?";
let result = database.execute_query_with_params(query, &["1", "2"])?;
assert_eq!(result.rows.len(), 1);
Expand All @@ -34,11 +24,10 @@ async fn test_sqlite_query_with_params() -> Result<(), Error> {

#[tokio::test]
async fn test_sqlite_query_with_params_and_serialize() -> Result<(), Error> {
let url = _get_sqlite_connection_url();
let mut database = dbc::Database::new(url.as_str())?;
let mut database = dbc::Database::new("sqlite://:memory:")?;
let query = "SELECT ? + ?";
let result = database.execute_query_with_params_and_serialize(query, &["1", "2"])?;
assert_eq!(result, r#"{"rows":[{"values":[{"Bytes":[50]}],"columns":[{"name":"1 + 1","column_type":"LONGLONG"}]}]}"#);
assert_eq!(result, r#"{"rows":[{"values":[{"Int":2}],"columns":[{"name":"1 + 1","column_type":"STRING"}]}]}"#); // currently all columns are STRING

Ok(())
}

0 comments on commit 73232db

Please sign in to comment.