marine/engine/tests/single_module.rs

128 lines
4.2 KiB
Rust
Raw Normal View History

2020-05-02 18:34:48 +03:00
/*
* Copyright 2020 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.
*/
mod downloader;
2020-08-23 04:04:11 +03:00
use fce::{FCE, IValue};
2020-05-02 18:34:48 +03:00
const REDIS_DOWNLOAD_URL: &str =
2020-08-23 04:04:11 +03:00
"https://github.com/fluencelabs/redis/releases/download/0.9.0_w/redis.wasm";
2020-05-02 18:34:48 +03:00
const SQLITE_DOWNLOAD_URL: &str =
"https://github.com/fluencelabs/sqlite/releases/download/0.4.0_w/sqlite3.wasm";
#[tokio::test]
async fn redis() {
let wasm_bytes = downloader::download(REDIS_DOWNLOAD_URL).await;
2020-05-08 20:38:29 +03:00
let mut fce = FCE::new();
2020-05-02 18:34:48 +03:00
let module_name = "redis";
2020-08-23 04:04:11 +03:00
let config = <_>::default();
2020-05-02 18:34:48 +03:00
2020-08-23 04:04:11 +03:00
fce.load_module(module_name, wasm_bytes.as_ref(), config)
2020-05-08 20:38:29 +03:00
.unwrap_or_else(|e| panic!("can't create FCE: {:?}", e));
2020-05-02 18:34:48 +03:00
2020-05-08 20:38:29 +03:00
let result1 = fce
2020-08-23 04:33:05 +03:00
.call(
module_name,
"invoke",
&[IValue::String(String::from("SET A 10"))],
)
2020-05-02 18:34:48 +03:00
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result2 = fce
2020-08-23 04:33:05 +03:00
.call(
module_name,
"invoke",
&[IValue::String(String::from("SADD B 20"))],
)
2020-05-02 18:34:48 +03:00
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result3 = fce
2020-08-23 04:33:05 +03:00
.call(
module_name,
"invoke",
&[IValue::String(String::from("GET A"))],
)
2020-05-02 18:34:48 +03:00
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result4 = fce
2020-08-23 04:33:05 +03:00
.call(
module_name,
"invoke",
&[IValue::String(String::from("SMEMBERS B"))],
)
2020-05-02 18:34:48 +03:00
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result5 = fce
2020-08-23 04:33:05 +03:00
.call(
module_name,
"invoke",
&[IValue::String(String::from(
"eval \"redis.call('incr', 'A') return redis.call('get', 'A') * 8 + 5\" 0",
))],
)
2020-05-02 18:34:48 +03:00
.expect("error while FCE invocation");
2020-08-23 04:04:11 +03:00
assert_eq!(result1, vec![IValue::String(String::from("+OK\r\n"))]);
assert_eq!(result2, vec![IValue::String(String::from(":1\r\n"))]);
assert_eq!(result3, vec![IValue::String(String::from("$2\r\n10\r\n"))]);
2020-08-23 04:33:05 +03:00
assert_eq!(
result4,
vec![IValue::String(String::from("*1\r\n$2\r\n20\r\n"))]
);
2020-08-23 04:04:11 +03:00
assert_eq!(result5, vec![IValue::String(String::from(":93\r\n"))]);
2020-05-02 18:34:48 +03:00
}
#[tokio::test]
2020-08-23 04:04:11 +03:00
#[ignore]
2020-05-02 18:34:48 +03:00
async fn sqlite() {
let wasm_bytes = downloader::download(SQLITE_DOWNLOAD_URL).await;
2020-05-08 20:38:29 +03:00
let mut fce = FCE::new();
2020-05-02 18:34:48 +03:00
let module_name = "sqlite";
2020-08-23 04:04:11 +03:00
let config = <_>::default();
2020-05-02 18:34:48 +03:00
2020-08-23 04:04:11 +03:00
fce.load_module(module_name, wasm_bytes.as_ref(), config)
2020-05-08 20:38:29 +03:00
.unwrap_or_else(|e| panic!("can't create FCE: {:?}", e));
2020-05-02 18:34:48 +03:00
2020-05-08 20:38:29 +03:00
let result1 = fce
2020-08-23 04:04:11 +03:00
.call(
2020-05-02 18:34:48 +03:00
module_name,
2020-08-23 04:04:11 +03:00
"invoke",
2020-08-23 04:33:05 +03:00
&[IValue::String(String::from(
"CREATE VIRTUAL TABLE users USING FTS5(body)",
))],
2020-05-02 18:34:48 +03:00
)
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result2 = fce
2020-08-23 04:04:11 +03:00
.call(
2020-05-02 18:34:48 +03:00
module_name,
2020-08-23 04:04:11 +03:00
"invoke",
2020-08-23 04:33:05 +03:00
&[IValue::String(String::from(
"INSERT INTO users(body) VALUES('AB'), ('BC'), ('CD'), ('DE')",
))],
2020-05-02 18:34:48 +03:00
)
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-05-08 20:38:29 +03:00
let result3 = fce
2020-08-23 04:04:11 +03:00
.call(
2020-05-02 18:34:48 +03:00
module_name,
2020-08-23 04:04:11 +03:00
"invoke",
2020-08-23 04:33:05 +03:00
&[IValue::String(String::from(
"SELECT * FROM users WHERE users MATCH 'A* OR B*",
))],
2020-05-02 18:34:48 +03:00
)
.unwrap_or_else(|e| panic!("error while FCE invocation: {:?}", e));
2020-08-23 04:04:11 +03:00
assert_eq!(result1, vec![IValue::String(String::from("OK"))]);
assert_eq!(result2, vec![IValue::String(String::from("OK"))]);
assert_eq!(result3, vec![IValue::String(String::from("AB|BC"))]);
2020-05-02 18:34:48 +03:00
}