From 9553388096dcecb0f3260cfd859a88dafb1b10f0 Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 13:05:11 +0300 Subject: [PATCH 01/12] add test for bind binary not null constraint --- Config.toml | 19 +++++++++++++++++++ build.sh | 19 +++++++++++++++++++ src/test.rs | 23 +++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 Config.toml create mode 100755 build.sh diff --git a/Config.toml b/Config.toml new file mode 100644 index 0000000..0486bbe --- /dev/null +++ b/Config.toml @@ -0,0 +1,19 @@ +modules_dir = "artifacts/" + +[[module]] + name = "sqlite3" + mem_pages_count = 100 + logger_enabled = false + + [module.wasi] + preopened_files = ["/tmp"] + mapped_dirs = { "tmp" = "/tmp" } + +[[module]] + name = "test" + mem_pages_count = 1 + logger_enabled = false + + [module.wasi] + preopened_files = ["/tmp"] + mapped_dirs = { "tmp" = "/tmp" } diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..126789b --- /dev/null +++ b/build.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -o errexit -o nounset -o pipefail + +# set current working directory to script directory to run script from everywhere +cd "$(dirname "$0")" + +# build aqua-dht.wasm +marine build --release + +# copy .wasm to artifacts +rm -f artifacts/* +mkdir -p artifacts +cp target/wasm32-wasi/release/test.wasm artifacts/ + +# download SQLite 3 to use in tests +curl -L https://github.com/fluencelabs/sqlite/releases/download/v0.14.0_w/sqlite3.wasm -o artifacts/sqlite3.wasm + +# generate Aqua bindings +#marine aqua artifacts/aqua-dht.wasm -s AquaDHT -i aqua-dht >../aqua/dht.aqua diff --git a/src/test.rs b/src/test.rs index 2907bde..6a281c8 100644 --- a/src/test.rs +++ b/src/test.rs @@ -84,3 +84,26 @@ pub fn test3() { println!("age = {}", row[1].as_integer().unwrap()); } } + +#[marine] +pub fn test4() { + use marine_sqlite_connector::Value; + + let connection = marine_sqlite_connector::open(":memory:").unwrap(); + + connection + .execute( + " + CREATE TABLE users (name TEXT, age BLOB NOT NULL); + ", + ) + .unwrap(); + + let mut cursor = connection + .prepare("INSERT OR REPLACE INTO users VALUES (?, ?)").unwrap(); + + cursor.bind(1, &Value::Integer(50)).unwrap(); + cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); + + cursor.next().unwrap(); +} From 414cff0779dd9bd1af709be5cd7181d9b7d3110e Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 15:02:39 +0300 Subject: [PATCH 02/12] update test --- src/test.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/test.rs b/src/test.rs index 6a281c8..e1d2002 100644 --- a/src/test.rs +++ b/src/test.rs @@ -94,16 +94,52 @@ pub fn test4() { connection .execute( " - CREATE TABLE users (name TEXT, age BLOB NOT NULL); + CREATE TABLE test (number INTEGER, blob BLOB NOT NULL); ", ) .unwrap(); let mut cursor = connection - .prepare("INSERT OR REPLACE INTO users VALUES (?, ?)").unwrap(); + .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)").unwrap(); cursor.bind(1, &Value::Integer(50)).unwrap(); cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); cursor.next().unwrap(); } + +#[marine] +pub fn test5() { + use marine_sqlite_connector::Value; + + let connection = marine_sqlite_connector::open(":memory:").unwrap(); + + connection + .execute( + " + CREATE TABLE test (number INTEGER, blob BLOB); + ", + ) + .unwrap(); + + let mut cursor = connection + .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)").unwrap(); + + cursor.bind(1, &Value::Integer(50)).unwrap(); + cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); + + cursor.next().unwrap(); + + let mut cursor = connection + .prepare("SELECT blob FROM test WHERE number = ?") + .unwrap() + .cursor(); + + cursor.bind(&[Value::Integer(50)]).unwrap(); + + while let Some(row) = cursor.next().unwrap() { + if vec![1,2 ,3] != row[0].as_binary().unwrap().to_vec() { + println!("expected: {:?}, actual: {:?}", vec![1,2 ,3], row[0].as_binary().unwrap()); + } + } +} \ No newline at end of file From 31145f447e8de73bd61de8e8180aa9e75fac9038 Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 18:58:44 +0300 Subject: [PATCH 03/12] add tests, setup ci --- .circleci/config.yml | 36 ++++ .github/download_marine.sh | 14 ++ Cargo.toml | 1 + build.sh | 7 +- tests/lib.rs | 365 ------------------------------------- tests/tests.rs | 48 +++++ 6 files changed, 101 insertions(+), 370 deletions(-) create mode 100644 .circleci/config.yml create mode 100755 .github/download_marine.sh delete mode 100644 tests/lib.rs create mode 100644 tests/tests.rs diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..9afe6c0 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,36 @@ +version: 2.1 + +orbs: + docker: circleci/docker@1.5.0 + +jobs: + Build and test Rust service: + docker: + - image: circleci/rust:latest + resource_class: xlarge + environment: + RUST_BACKTRACE: full + steps: + - checkout + - run: | + sudo bash .github/download_marine.sh + - restore_cache: + keys: + - connector-test00-{{ checksum "Cargo.lock" }} + - run: | + cd ./service + rustup target add wasm32-wasi + - run: ./build.sh + - run: | + cargo test --no-fail-fast --release --all-features + - save_cache: + paths: + - ~/.cargo + - ~/.rustup + key: connector-test00-{{ checksum "Cargo.lock" }} + +workflows: + version: 2 + CircleCI: + jobs: + - Build and test Rust service diff --git a/.github/download_marine.sh b/.github/download_marine.sh new file mode 100755 index 0000000..c7609aa --- /dev/null +++ b/.github/download_marine.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -o pipefail -o errexit -o nounset +set -x + +MARINE_RELEASE="https://api.github.com/repos/fluencelabs/marine/releases/latest" +OUT_DIR=/usr/local/bin + +# get metadata about release +curl -s -H "Accept: application/vnd.github.v3+json" $MARINE_RELEASE | + # extract url and name for asset with name "marine" + # also append $OUT_DIR to each name so file is saved to $OUT_DIR + jq -r ".assets | .[] | select(.name == \"marine\") | \"\(.browser_download_url) $OUT_DIR/\(.name)\"" | + # download assets + xargs -n2 bash -c 'curl -L $0 -o $1 && chmod +x $1' diff --git a/Cargo.toml b/Cargo.toml index 5d99846..7dac812 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,3 +39,4 @@ marine-rs-sdk = "0.6.10" [dev-dependencies] temporary = "0.6" +marine-rs-sdk-test = "0.2.0" diff --git a/build.sh b/build.sh index 126789b..16c8126 100755 --- a/build.sh +++ b/build.sh @@ -4,8 +4,8 @@ set -o errexit -o nounset -o pipefail # set current working directory to script directory to run script from everywhere cd "$(dirname "$0")" -# build aqua-dht.wasm -marine build --release +# build test.wasm +marine build --release --bin test # copy .wasm to artifacts rm -f artifacts/* @@ -14,6 +14,3 @@ cp target/wasm32-wasi/release/test.wasm artifacts/ # download SQLite 3 to use in tests curl -L https://github.com/fluencelabs/sqlite/releases/download/v0.14.0_w/sqlite3.wasm -o artifacts/sqlite3.wasm - -# generate Aqua bindings -#marine aqua artifacts/aqua-dht.wasm -s AquaDHT -i aqua-dht >../aqua/dht.aqua diff --git a/tests/lib.rs b/tests/lib.rs deleted file mode 100644 index 8e45139..0000000 --- a/tests/lib.rs +++ /dev/null @@ -1,365 +0,0 @@ -extern crate sqlite; -extern crate temporary; - -use sqlite::{Connection, OpenFlags, State, Type, Value}; -use std::path::Path; - -macro_rules! ok(($result:expr) => ($result.unwrap())); - -#[test] -fn connection_changes() { - let connection = setup_users(":memory:"); - assert_eq!(connection.changes(), 1); - assert_eq!(connection.total_changes(), 1); - - ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)")); - assert_eq!(connection.changes(), 1); - assert_eq!(connection.total_changes(), 2); - - ok!(connection.execute("UPDATE users SET name = 'Bob' WHERE id = 1")); - assert_eq!(connection.changes(), 1); - assert_eq!(connection.total_changes(), 3); - - ok!(connection.execute("DELETE FROM users")); - assert_eq!(connection.changes(), 2); - assert_eq!(connection.total_changes(), 5); -} - -#[test] -fn connection_error() { - let connection = setup_users(":memory:"); - match connection.execute(":)") { - Err(error) => assert_eq!( - error.message, - Some(String::from(r#"unrecognized token: ":""#)) - ), - _ => unreachable!(), - } -} - -#[test] -fn connection_iterate() { - macro_rules! pair( - ($one:expr, $two:expr) => (($one, Some($two))); - ); - - let connection = setup_users(":memory:"); - - let mut done = false; - let statement = "SELECT * FROM users"; - ok!(connection.iterate(statement, |pairs| { - assert_eq!(pairs.len(), 5); - assert_eq!(pairs[0], pair!("id", "1")); - assert_eq!(pairs[1], pair!("name", "Alice")); - assert_eq!(pairs[2], pair!("age", "42.69")); - assert_eq!(pairs[3], pair!("photo", "\x42\x69")); - assert_eq!(pairs[4], ("email", None)); - done = true; - true - })); - assert!(done); -} - -#[test] -fn connection_open_with_flags() { - use temporary::Directory; - - let directory = ok!(Directory::new("sqlite")); - let path = directory.path().join("database.sqlite3"); - setup_users(&path); - - let flags = OpenFlags::new().set_read_only(); - let connection = ok!(Connection::open_with_flags(path, flags)); - match connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL)") { - Err(_) => {} - _ => unreachable!(), - } -} - -#[test] -fn connection_set_busy_handler() { - use std::thread; - use temporary::Directory; - - let directory = ok!(Directory::new("sqlite")); - let path = directory.path().join("database.sqlite3"); - setup_users(&path); - - let guards = (0..100) - .map(|_| { - let path = path.to_path_buf(); - thread::spawn(move || { - let mut connection = ok!(sqlite::open(&path)); - ok!(connection.set_busy_handler(|_| true)); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; - let mut statement = ok!(connection.prepare(statement)); - ok!(statement.bind(1, 2i64)); - ok!(statement.bind(2, "Bob")); - ok!(statement.bind(3, 69.42)); - ok!(statement.bind(4, &[0x69u8, 0x42u8][..])); - ok!(statement.bind(5, ())); - assert_eq!(ok!(statement.next()), State::Done); - true - }) - }) - .collect::>(); - - for guard in guards { - assert!(ok!(guard.join())); - } -} - -#[test] -fn cursor_read() { - let connection = setup_users(":memory:"); - ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)")); - let statement = "SELECT id, age FROM users ORDER BY 1 DESC"; - let statement = ok!(connection.prepare(statement)); - - let mut count = 0; - let mut cursor = statement.cursor(); - while let Some(row) = ok!(cursor.next()) { - let id = row[0].as_integer().unwrap(); - if id == 1 { - assert_eq!(row[1].as_float().unwrap(), 42.69); - } else if id == 2 { - assert_eq!(row[1].as_float().unwrap_or(69.42), 69.42); - } else { - assert!(false); - } - count += 1; - } - assert_eq!(count, 2); -} - -#[test] -fn cursor_wildcard() { - let connection = setup_english(":memory:"); - let statement = "SELECT value FROM english WHERE value LIKE '%type'"; - let statement = ok!(connection.prepare(statement)); - - let mut count = 0; - let mut cursor = statement.cursor(); - while let Some(_) = ok!(cursor.next()) { - count += 1; - } - assert_eq!(count, 6); -} - -#[test] -fn cursor_wildcard_with_binding() { - let connection = setup_english(":memory:"); - let statement = "SELECT value FROM english WHERE value LIKE ?"; - let mut statement = ok!(connection.prepare(statement)); - ok!(statement.bind(1, "%type")); - - let mut count = 0; - let mut cursor = statement.cursor(); - while let Some(_) = ok!(cursor.next()) { - count += 1; - } - assert_eq!(count, 6); -} - -#[test] -fn cursor_workflow() { - let connection = setup_users(":memory:"); - - let select = "SELECT id, name FROM users WHERE id = ?"; - let mut select = ok!(connection.prepare(select)).cursor(); - - let insert = "INSERT INTO users (id, name) VALUES (?, ?)"; - let mut insert = ok!(connection.prepare(insert)).cursor(); - - for _ in 0..10 { - ok!(select.bind(&[Value::Integer(1)])); - assert_eq!( - ok!(ok!(select.next())), - &[Value::Integer(1), Value::String("Alice".to_string())] - ); - assert_eq!(ok!(select.next()), None); - } - - ok!(select.bind(&[Value::Integer(42)])); - assert_eq!(ok!(select.next()), None); - - ok!(insert.bind(&[Value::Integer(42), Value::String("Bob".to_string())])); - assert_eq!(ok!(insert.next()), None); - - ok!(select.bind(&[Value::Integer(42)])); - assert_eq!( - ok!(ok!(select.next())), - &[Value::Integer(42), Value::String("Bob".to_string())] - ); - assert_eq!(ok!(select.next()), None); -} - -#[test] -fn statement_bind() { - let connection = setup_users(":memory:"); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; - let mut statement = ok!(connection.prepare(statement)); - - ok!(statement.bind(1, 2i64)); - ok!(statement.bind(2, "Bob")); - ok!(statement.bind(3, 69.42)); - ok!(statement.bind(4, &[0x69u8, 0x42u8][..])); - ok!(statement.bind(5, ())); - assert_eq!(ok!(statement.next()), State::Done); -} - -#[test] -fn statement_bind_with_optional() { - let connection = setup_users(":memory:"); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; - let mut statement = ok!(connection.prepare(statement)); - - ok!(statement.bind(1, None::)); - ok!(statement.bind(2, None::<&str>)); - ok!(statement.bind(3, None::)); - ok!(statement.bind(4, None::<&[u8]>)); - ok!(statement.bind(5, None::<&str>)); - assert_eq!(ok!(statement.next()), State::Done); - - let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; - let mut statement = ok!(connection.prepare(statement)); - - ok!(statement.bind(1, Some(2i64))); - ok!(statement.bind(2, Some("Bob"))); - ok!(statement.bind(3, Some(69.42))); - ok!(statement.bind(4, Some(&[0x69u8, 0x42u8][..]))); - ok!(statement.bind(5, None::<&str>)); - assert_eq!(ok!(statement.next()), State::Done); -} - -#[test] -fn statement_count() { - let connection = setup_users(":memory:"); - let statement = "SELECT * FROM users"; - let mut statement = ok!(connection.prepare(statement)); - - assert_eq!(ok!(statement.next()), State::Row); - - assert_eq!(statement.count(), 5); -} - -#[test] -fn statement_name() { - let connection = setup_users(":memory:"); - let statement = "SELECT id, name, age, photo AS user_photo FROM users"; - let statement = ok!(connection.prepare(statement)); - - let names = statement.names(); - assert_eq!(names, vec!["id", "name", "age", "user_photo"]); - assert_eq!("user_photo", statement.name(3)); -} - -#[test] -fn statement_kind() { - let connection = setup_users(":memory:"); - let statement = "SELECT * FROM users"; - let mut statement = ok!(connection.prepare(statement)); - - assert_eq!(statement.kind(0), Type::Null); - assert_eq!(statement.kind(1), Type::Null); - assert_eq!(statement.kind(2), Type::Null); - assert_eq!(statement.kind(3), Type::Null); - - assert_eq!(ok!(statement.next()), State::Row); - - assert_eq!(statement.kind(0), Type::Integer); - assert_eq!(statement.kind(1), Type::String); - assert_eq!(statement.kind(2), Type::Float); - assert_eq!(statement.kind(3), Type::Binary); -} - -#[test] -fn statement_read() { - let connection = setup_users(":memory:"); - let statement = "SELECT * FROM users"; - let mut statement = ok!(connection.prepare(statement)); - - assert_eq!(ok!(statement.next()), State::Row); - assert_eq!(ok!(statement.read::(0)), 1); - assert_eq!(ok!(statement.read::(1)), String::from("Alice")); - assert_eq!(ok!(statement.read::(2)), 42.69); - assert_eq!(ok!(statement.read::>(3)), vec![0x42, 0x69]); - assert_eq!(ok!(statement.read::(4)), Value::Null); - assert_eq!(ok!(statement.next()), State::Done); -} - -#[test] -fn statement_read_with_optional() { - let connection = setup_users(":memory:"); - let statement = "SELECT * FROM users"; - let mut statement = ok!(connection.prepare(statement)); - - assert_eq!(ok!(statement.next()), State::Row); - assert_eq!(ok!(statement.read::>(0)), Some(1)); - assert_eq!( - ok!(statement.read::>(1)), - Some(String::from("Alice")) - ); - assert_eq!(ok!(statement.read::>(2)), Some(42.69)); - assert_eq!( - ok!(statement.read::>>(3)), - Some(vec![0x42, 0x69]) - ); - assert_eq!(ok!(statement.read::>(4)), None); - assert_eq!(ok!(statement.next()), State::Done); -} - -#[test] -fn statement_wildcard() { - let connection = setup_english(":memory:"); - let statement = "SELECT value FROM english WHERE value LIKE '%type'"; - let mut statement = ok!(connection.prepare(statement)); - - let mut count = 0; - while let State::Row = ok!(statement.next()) { - count += 1; - } - assert_eq!(count, 6); -} - -#[test] -fn statement_wildcard_with_binding() { - let connection = setup_english(":memory:"); - let statement = "SELECT value FROM english WHERE value LIKE ?"; - let mut statement = ok!(connection.prepare(statement)); - ok!(statement.bind(1, "%type")); - - let mut count = 0; - while let State::Row = ok!(statement.next()) { - count += 1; - } - assert_eq!(count, 6); -} - -fn setup_english>(path: T) -> Connection { - let connection = ok!(sqlite::open(path)); - ok!(connection.execute( - " - CREATE TABLE english (value TEXT); - INSERT INTO english VALUES ('cerotype'); - INSERT INTO english VALUES ('metatype'); - INSERT INTO english VALUES ('ozotype'); - INSERT INTO english VALUES ('phenotype'); - INSERT INTO english VALUES ('plastotype'); - INSERT INTO english VALUES ('undertype'); - INSERT INTO english VALUES ('nonsence'); - ", - )); - connection -} - -fn setup_users>(path: T) -> Connection { - let connection = ok!(sqlite::open(path)); - ok!(connection.execute( - " - CREATE TABLE users (id INTEGER, name TEXT, age REAL, photo BLOB, email TEXT); - INSERT INTO users VALUES (1, 'Alice', 42.69, X'4269', NULL); - ", - )); - connection -} diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..fff9f05 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,48 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +extern crate marine_rs_sdk_test; + +mod tests { + use marine_rs_sdk_test::marine_test; + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] + fn test1(test: marine_test_env::test::ModuleInterface) { + test.test1() + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] + fn test2(test: marine_test_env::test::ModuleInterface) { + test.test2() + } + + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] + fn test3(test: marine_test_env::test::ModuleInterface) { + test.test3() + } + + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] + fn test4(test: marine_test_env::test::ModuleInterface) { + test.test4() + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] + fn test5(test: marine_test_env::test::ModuleInterface) { + test.test5() + } +} \ No newline at end of file From 214881d843fdef24c1ec541487012e187b6a9473 Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 19:00:40 +0300 Subject: [PATCH 04/12] turn off travis --- .travis.yml | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b1d8a9c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: rust - -os: - - linux - - osx - -rust: - - stable - - beta - - nightly - -script: - - if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then - cargo bench; - cargo build; - cargo test; - else - cargo build; - cargo test; - fi - -notifications: - email: false From a3ec52bbb91bfa9d4bcfbdff91516466c922ca18 Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 19:10:45 +0300 Subject: [PATCH 05/12] fix ci --- .circleci/config.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9afe6c0..d1b74e7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -18,7 +18,6 @@ jobs: keys: - connector-test00-{{ checksum "Cargo.lock" }} - run: | - cd ./service rustup target add wasm32-wasi - run: ./build.sh - run: | From bd42e1b313bca03f1745a82b5099ead56b42f54d Mon Sep 17 00:00:00 2001 From: Alexey Proshutinskiy Date: Thu, 2 Sep 2021 19:20:43 +0300 Subject: [PATCH 06/12] move to rust 2018 --- Cargo.toml | 1 + src/connection.rs | 10 +++++----- src/cursor.rs | 6 +++--- src/lib.rs | 8 ++++---- src/statement.rs | 8 ++++---- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7dac812..464849f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ repository = "https://github.com/stainless-steel/sqlite" readme = "README.md" categories = ["api-bindings", "database"] keywords = ["database"] +edition = "2018" [lib] name = "marine_sqlite_connector" diff --git a/src/connection.rs b/src/connection.rs index 8f7c30c..871842e 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,9 +1,9 @@ -use sqlite3_connector as ffi; +use crate::sqlite3_connector as ffi; use std::marker::PhantomData; use std::path::Path; -use {Result, Statement}; +use crate::{Result, Statement}; /// A database connection. pub struct Connection { @@ -33,14 +33,14 @@ impl Connection { match result.ret_code { ffi::SQLITE_OK => {} code => { - return match ::last_error(result.db_handle) { + return match crate::last_error(result.db_handle) { Some(error) => { ffi::sqlite3_close(result.db_handle); Err(error) } _ => { ffi::sqlite3_close(result.db_handle); - Err(::Error { + Err(crate::Error { code: Some(code as isize), message: None, }) @@ -91,7 +91,7 @@ impl Connection { /// Create a prepared statement. #[inline] pub fn prepare>(&self, statement: T) -> Result { - ::statement::new(self.raw, statement) + crate::statement::new(self.raw, statement) } /// Return the number of rows inserted, updated, or deleted by the most diff --git a/src/cursor.rs b/src/cursor.rs index 2a749d4..4dd2482 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,6 +1,6 @@ -use sqlite3_connector as ffi; -use statement::{State, Statement}; -use {Result, Value}; +use crate::sqlite3_connector as ffi; +use crate::statement::{State, Statement}; +use crate::{Result, Value}; /// An iterator over rows. pub struct Cursor { diff --git a/src/lib.rs b/src/lib.rs index 674c56f..1fee0e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,9 +108,9 @@ use std::{error, fmt}; macro_rules! error( ($connection:expr, $code:expr) => ( - match ::last_error($connection) { + match crate::last_error($connection) { Some(error) => return Err(error), - _ => return Err(::Error { + _ => return Err(crate::Error { code: Some($code as isize), message: None, }), @@ -121,7 +121,7 @@ macro_rules! error( macro_rules! ok_descr( ($connection:expr, $result:expr) => ( match $result.ret_code { - ::ffi::SQLITE_OK => {} + crate::ffi::SQLITE_OK => {} code => error!($connection, code), } ); @@ -139,7 +139,7 @@ macro_rules! ok_descr( macro_rules! ok_raw( ($connection:expr, $result:expr) => ( match $result { - ::ffi::SQLITE_OK => {} + crate::ffi::SQLITE_OK => {} code => error!($connection, code), } ); diff --git a/src/statement.rs b/src/statement.rs index 1920c9e..79db5ab 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -1,7 +1,7 @@ -use sqlite3_connector as ffi; +use crate::sqlite3_connector as ffi; use std::marker::PhantomData; -use {Cursor, Result, Type, Value}; +use crate::{Cursor, Result, Type, Value}; /// A prepared statement. pub struct Statement { @@ -31,7 +31,7 @@ pub trait Readable: Sized { /// Read from a column. /// /// The leftmost column has the index 0. - fn read(&Statement, usize) -> Result; + fn read(_: &Statement, _: usize) -> Result; } impl Statement { @@ -108,7 +108,7 @@ impl Statement { /// Upgrade to a cursor. #[inline] pub fn cursor(self) -> Cursor { - ::cursor::new(self) + crate::cursor::new(self) } /// Return the raw pointer. From 6f4e0c2d4f8c8da3253388b8fd25fe3e98aadc4b Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 18:07:01 +0300 Subject: [PATCH 07/12] use sqlite 15 --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 16c8126..ad0d3e6 100755 --- a/build.sh +++ b/build.sh @@ -13,4 +13,4 @@ mkdir -p artifacts cp target/wasm32-wasi/release/test.wasm artifacts/ # download SQLite 3 to use in tests -curl -L https://github.com/fluencelabs/sqlite/releases/download/v0.14.0_w/sqlite3.wasm -o artifacts/sqlite3.wasm +curl -L https://github.com/fluencelabs/sqlite/releases/download/v0.15.0_w/sqlite3.wasm -o artifacts/sqlite3.wasm From e4da5c0d4b2e0d85ef5b6b212e07f752be1f93d6 Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 18:08:45 +0300 Subject: [PATCH 08/12] remove externs --- src/connection.rs | 3 +-- src/it_generator.rs | 2 -- src/sqlite3_connector/import_functions.rs | 4 +--- src/statement.rs | 4 ++-- src/test.rs | 19 +++++++++++-------- tests/tests.rs | 4 +--- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 871842e..bf6cd45 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,10 +1,9 @@ use crate::sqlite3_connector as ffi; +use crate::{Result, Statement}; use std::marker::PhantomData; use std::path::Path; -use crate::{Result, Statement}; - /// A database connection. pub struct Connection { raw: ffi::Sqlite3DbHandle, diff --git a/src/it_generator.rs b/src/it_generator.rs index 424e41a..81b9c6d 100644 --- a/src/it_generator.rs +++ b/src/it_generator.rs @@ -1,8 +1,6 @@ #![allow(unused_variables)] #![allow(non_snake_case)] -extern crate marine_rs_sdk; - use marine_rs_sdk::marine; pub fn main() {} diff --git a/src/sqlite3_connector/import_functions.rs b/src/sqlite3_connector/import_functions.rs index 7848c0c..c888b20 100644 --- a/src/sqlite3_connector/import_functions.rs +++ b/src/sqlite3_connector/import_functions.rs @@ -1,6 +1,4 @@ -extern crate marine_rs_sdk; - -use self::marine_rs_sdk::marine; +use marine_rs_sdk::marine; pub(crate) type Sqlite3DbHandle = u32; pub(crate) type Sqlite3StmtHandle = u32; diff --git a/src/statement.rs b/src/statement.rs index 79db5ab..5e00d3b 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -1,8 +1,8 @@ use crate::sqlite3_connector as ffi; -use std::marker::PhantomData; - use crate::{Cursor, Result, Type, Value}; +use std::marker::PhantomData; + /// A prepared statement. pub struct Statement { raw: (ffi::Sqlite3StmtHandle, ffi::Sqlite3DbHandle), diff --git a/src/test.rs b/src/test.rs index e1d2002..8d88717 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,3 @@ -extern crate marine_rs_sdk; -extern crate marine_sqlite_connector; - use marine_rs_sdk::marine; use marine_sqlite_connector::State; @@ -100,7 +97,8 @@ pub fn test4() { .unwrap(); let mut cursor = connection - .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)").unwrap(); + .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)") + .unwrap(); cursor.bind(1, &Value::Integer(50)).unwrap(); cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); @@ -123,7 +121,8 @@ pub fn test5() { .unwrap(); let mut cursor = connection - .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)").unwrap(); + .prepare("INSERT OR REPLACE INTO test VALUES (?, ?)") + .unwrap(); cursor.bind(1, &Value::Integer(50)).unwrap(); cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); @@ -138,8 +137,12 @@ pub fn test5() { cursor.bind(&[Value::Integer(50)]).unwrap(); while let Some(row) = cursor.next().unwrap() { - if vec![1,2 ,3] != row[0].as_binary().unwrap().to_vec() { - println!("expected: {:?}, actual: {:?}", vec![1,2 ,3], row[0].as_binary().unwrap()); + if vec![1, 2, 3] != row[0].as_binary().unwrap().to_vec() { + println!( + "expected: {:?}, actual: {:?}", + vec![1, 2, 3], + row[0].as_binary().unwrap() + ); } } -} \ No newline at end of file +} diff --git a/tests/tests.rs b/tests/tests.rs index fff9f05..2440511 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -29,13 +29,11 @@ mod tests { test.test2() } - #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] fn test3(test: marine_test_env::test::ModuleInterface) { test.test3() } - #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] fn test4(test: marine_test_env::test::ModuleInterface) { test.test4() @@ -45,4 +43,4 @@ mod tests { fn test5(test: marine_test_env::test::ModuleInterface) { test.test5() } -} \ No newline at end of file +} From e054b689ad73a581c1ad77e948933d4a30035b7c Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 19:45:23 +0300 Subject: [PATCH 09/12] improve tests --- .circleci/config.yml | 29 +++++++++++++-------- .gitignore | 2 ++ sqlite3.wit | 2 +- src/lib.rs | 8 +++--- src/test.rs | 61 ++++++++++---------------------------------- tests/tests.rs | 5 ---- 6 files changed, 38 insertions(+), 69 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d1b74e7..05226af 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,7 +4,7 @@ orbs: docker: circleci/docker@1.5.0 jobs: - Build and test Rust service: + sqlite_connector_rust_tests: docker: - image: circleci/rust:latest resource_class: xlarge @@ -12,24 +12,31 @@ jobs: RUST_BACKTRACE: full steps: - checkout - - run: | - sudo bash .github/download_marine.sh - restore_cache: keys: - - connector-test00-{{ checksum "Cargo.lock" }} + - sqlite-connector01-{{ checksum "Cargo.lock" }} - run: | + rustup toolchain install nightly-2021-05-21 + rustup default nightly-2021-05-21 + rustup override set nightly-2021-05-21 + + rustup component add rustfmt --toolchain nightly-2021-05-21 + rustup component add clippy --toolchain nightly-2021-05-21 rustup target add wasm32-wasi - - run: ./build.sh - - run: | - cargo test --no-fail-fast --release --all-features + + cargo fmt --all -- --check --color always + cargo check -v --all-features + + ./build.sh + cargo test --release -v --all-features - save_cache: paths: - ~/.cargo - ~/.rustup - key: connector-test00-{{ checksum "Cargo.lock" }} + key: sqlite-connector01-{{ checksum "Cargo.lock" }} workflows: - version: 2 - CircleCI: + version: 2.1 + marine: jobs: - - Build and test Rust service + - sqlite_connector_rust_tests \ No newline at end of file diff --git a/.gitignore b/.gitignore index e0fcc69..9af404a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .idea/* +/artifacts +.repl_history *.sqlite3 Cargo.lock target diff --git a/sqlite3.wit b/sqlite3.wit index 936a403..8b624af 100644 --- a/sqlite3.wit +++ b/sqlite3.wit @@ -393,7 +393,7 @@ call-core 29 call-core 3 call-core 2 - byte_array.lift_memory + string.lift_memory call-core 1) (@interface func (type 59) arg.get 0 diff --git a/src/lib.rs b/src/lib.rs index 1fee0e5..6acef65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ //! //! Open a connection, create a table, and insert some rows: //! -//! ``` +//! ```ignore //! let connection = sqlite::open(":memory:").unwrap(); //! //! connection @@ -20,7 +20,7 @@ //! //! Select some rows and process them one by one as plain text: //! -//! ``` +//! ```ignore //! # let connection = sqlite::open(":memory:").unwrap(); //! # connection //! # .execute( @@ -44,7 +44,7 @@ //! The same query using a prepared statement, which is much more efficient than //! the previous technique: //! -//! ``` +//! ```ignore //! use sqlite::State; //! # let connection = sqlite::open(":memory:").unwrap(); //! # connection @@ -72,7 +72,7 @@ //! The same query using a cursor, which is a wrapper around a prepared //! statement providing the concept of row and featuring all-at-once binding: //! -//! ``` +//! ```ignore //! use sqlite::Value; //! # let connection = sqlite::open(":memory:").unwrap(); //! # connection diff --git a/src/test.rs b/src/test.rs index 8d88717..e3a0ca4 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,5 +1,6 @@ use marine_rs_sdk::marine; use marine_sqlite_connector::State; +use marine_sqlite_connector::Value; pub fn main() {} @@ -7,31 +8,6 @@ pub fn main() {} pub fn test1() { let connection = marine_sqlite_connector::open(":memory:").unwrap(); - connection - .execute( - " - CREATE TABLE users (name TEXT, age INTEGER); - INSERT INTO users VALUES ('Alice', 42); - INSERT INTO users VALUES ('Bob', 69); - ", - ) - .unwrap(); - - connection - .iterate("SELECT * FROM users WHERE age > 50", |pairs| { - for &(column, value) in pairs.iter() { - println!("{} = {}", column, value.unwrap()); - } - true - }) - .unwrap(); -} - -#[marine] -pub fn test2() { - let connection = marine_sqlite_connector::open(":memory:").unwrap(); - - println!("connection id = {}\n", connection.as_raw()); connection .execute( " @@ -48,15 +24,13 @@ pub fn test2() { statement.bind(1, 50).unwrap(); - while let State::Row = statement.next().unwrap() { - println!("name = {}", statement.read::(0).unwrap()); - println!("age = {}", statement.read::(1).unwrap()); - } + assert_eq!(statement.next().unwrap(), State::Row); + assert_eq!(statement.read::(0).unwrap(), "Bob"); + assert_eq!(statement.read::(1).unwrap(), 69); } -#[marine] -pub fn test3() { - use marine_sqlite_connector::Value; +#[marine] +pub fn test2() { let connection = marine_sqlite_connector::open(":memory:").unwrap(); connection @@ -77,15 +51,13 @@ pub fn test3() { cursor.bind(&[Value::Integer(50)]).unwrap(); while let Some(row) = cursor.next().unwrap() { - println!("name = {}", row[0].as_string().unwrap()); - println!("age = {}", row[1].as_integer().unwrap()); + assert_eq!(row[0].as_string().unwrap(), "Bob"); + assert_eq!(row[1].as_integer().unwrap(), 69); } } #[marine] -pub fn test4() { - use marine_sqlite_connector::Value; - +pub fn test3() { let connection = marine_sqlite_connector::open(":memory:").unwrap(); connection @@ -103,13 +75,12 @@ pub fn test4() { cursor.bind(1, &Value::Integer(50)).unwrap(); cursor.bind(2, &Value::Binary(vec![1, 2, 3])).unwrap(); - cursor.next().unwrap(); + // check that blob is not null + assert!(cursor.next().is_ok()); } #[marine] -pub fn test5() { - use marine_sqlite_connector::Value; - +pub fn test4() { let connection = marine_sqlite_connector::open(":memory:").unwrap(); connection @@ -137,12 +108,6 @@ pub fn test5() { cursor.bind(&[Value::Integer(50)]).unwrap(); while let Some(row) = cursor.next().unwrap() { - if vec![1, 2, 3] != row[0].as_binary().unwrap().to_vec() { - println!( - "expected: {:?}, actual: {:?}", - vec![1, 2, 3], - row[0].as_binary().unwrap() - ); - } + assert_eq!(row[0].as_binary().unwrap().to_vec(), vec![1, 2, 3]); } } diff --git a/tests/tests.rs b/tests/tests.rs index 2440511..b106b2f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -38,9 +38,4 @@ mod tests { fn test4(test: marine_test_env::test::ModuleInterface) { test.test4() } - - #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts/")] - fn test5(test: marine_test_env::test::ModuleInterface) { - test.test5() - } } From f8ea28b0d38cfabb95871e2c19e0548a5a7436c6 Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 19:47:42 +0300 Subject: [PATCH 10/12] install marine for CI --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 05226af..ebeb359 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,6 +24,8 @@ jobs: rustup component add clippy --toolchain nightly-2021-05-21 rustup target add wasm32-wasi + cargo install marine + cargo fmt --all -- --check --color always cargo check -v --all-features From 3748d255ed6c38766217b48647c8de4952214b9e Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 19:51:51 +0300 Subject: [PATCH 11/12] add newline --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ebeb359..57e4782 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -41,4 +41,4 @@ workflows: version: 2.1 marine: jobs: - - sqlite_connector_rust_tests \ No newline at end of file + - sqlite_connector_rust_tests From 8a3a9e9d57e342fb671a7e0c2dc8149141af9f42 Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 3 Sep 2021 20:07:23 +0300 Subject: [PATCH 12/12] delete extern crate marine_rs_sdk_test; --- sqlite3.wit | 2 +- tests/tests.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/sqlite3.wit b/sqlite3.wit index 8b624af..936a403 100644 --- a/sqlite3.wit +++ b/sqlite3.wit @@ -393,7 +393,7 @@ call-core 29 call-core 3 call-core 2 - string.lift_memory + byte_array.lift_memory call-core 1) (@interface func (type 59) arg.get 0 diff --git a/tests/tests.rs b/tests/tests.rs index b106b2f..ff0ce7b 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -14,8 +14,6 @@ * limitations under the License. */ -extern crate marine_rs_sdk_test; - mod tests { use marine_rs_sdk_test::marine_test;