2022-12-30 08:23:19 +00:00
|
|
|
// Tests common to all protocols and storage engines
|
|
|
|
|
2024-03-15 11:21:32 +00:00
|
|
|
use surrealdb::fflags::FFLAGS;
|
2024-08-21 12:34:16 +00:00
|
|
|
use surrealdb::value;
|
2024-03-28 16:29:55 +00:00
|
|
|
use surrealdb::Response;
|
2024-08-21 12:34:16 +00:00
|
|
|
use surrealdb_core::sql::{Id, Value as CoreValue};
|
2024-02-20 11:11:49 +00:00
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
static PERMITS: Semaphore = Semaphore::const_new(1);
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn connect() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
db.health().await.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn yuse() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-19 22:35:19 +00:00
|
|
|
let item = Ulid::new().to_string();
|
|
|
|
match db.create(Resource::from(item.as_str())).await.unwrap_err() {
|
|
|
|
// Local engines return this error
|
|
|
|
Error::Db(DbError::NsEmpty) => {}
|
|
|
|
// Remote engines return this error
|
|
|
|
Error::Api(ApiError::Query(error)) if error.contains("Specify a namespace to use") => {}
|
|
|
|
error => panic!("{:?}", error),
|
|
|
|
}
|
2023-05-05 18:12:19 +00:00
|
|
|
db.use_ns(NS).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
match db.create(Resource::from(item.as_str())).await.unwrap_err() {
|
|
|
|
// Local engines return this error
|
|
|
|
Error::Db(DbError::DbEmpty) => {}
|
|
|
|
// Remote engines return this error
|
|
|
|
Error::Api(ApiError::Query(error)) if error.contains("Specify a database to use") => {}
|
|
|
|
error => panic!("{:?}", error),
|
|
|
|
}
|
|
|
|
db.use_db(item.as_str()).await.unwrap();
|
|
|
|
db.create(Resource::from(item)).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-06-09 13:45:07 +00:00
|
|
|
async fn invalidate() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
db.invalidate().await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let error = db.create::<Option<ApiRecordId>>(("user", "john")).await.unwrap_err();
|
2023-07-29 18:47:25 +00:00
|
|
|
assert!(
|
|
|
|
error.to_string().contains("Not enough permissions to perform this action"),
|
|
|
|
"Unexpected error: {:?}",
|
|
|
|
error
|
|
|
|
);
|
2023-06-09 13:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn signup_record() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
let database = Ulid::new().to_string();
|
|
|
|
db.use_ns(NS).use_db(&database).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
let access = Ulid::new().to_string();
|
2023-06-09 13:45:07 +00:00
|
|
|
let sql = format!(
|
|
|
|
"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS `{access}` ON DB TYPE RECORD
|
2023-06-09 13:45:07 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1d FOR TOKEN 15s
|
2023-06-09 13:45:07 +00:00
|
|
|
"
|
|
|
|
);
|
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
response.check().unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
db.signup(RecordAccess {
|
2023-06-09 13:45:07 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-06-09 13:45:07 +00:00
|
|
|
params: AuthParams {
|
|
|
|
email: "john.doe@example.com",
|
|
|
|
pass: "password123",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-06-09 13:45:07 +00:00
|
|
|
async fn signin_ns() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
let user = Ulid::new().to_string();
|
|
|
|
let pass = "password123";
|
2024-01-10 16:43:56 +00:00
|
|
|
let sql = format!("DEFINE USER `{user}` ON NAMESPACE PASSWORD '{pass}'");
|
2023-06-09 13:45:07 +00:00
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
db.signin(Namespace {
|
|
|
|
namespace: NS,
|
|
|
|
username: &user,
|
|
|
|
password: pass,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-06-09 13:45:07 +00:00
|
|
|
async fn signin_db() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
let database = Ulid::new().to_string();
|
|
|
|
db.use_ns(NS).use_db(&database).await.unwrap();
|
|
|
|
let user = Ulid::new().to_string();
|
|
|
|
let pass = "password123";
|
2024-01-10 16:43:56 +00:00
|
|
|
let sql = format!("DEFINE USER `{user}` ON DATABASE PASSWORD '{pass}'");
|
2023-06-09 13:45:07 +00:00
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
db.signin(Database {
|
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
|
|
|
username: &user,
|
|
|
|
password: pass,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn signin_record() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
let database = Ulid::new().to_string();
|
|
|
|
db.use_ns(NS).use_db(&database).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
let access = Ulid::new().to_string();
|
|
|
|
let email = format!("{access}@example.com");
|
2023-06-09 13:45:07 +00:00
|
|
|
let pass = "password123";
|
|
|
|
let sql = format!(
|
|
|
|
"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS `{access}` ON DB TYPE RECORD
|
2023-06-09 13:45:07 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1d FOR TOKEN 15s
|
2023-06-09 13:45:07 +00:00
|
|
|
"
|
|
|
|
);
|
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
response.check().unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
db.signup(RecordAccess {
|
2023-06-09 13:45:07 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-06-09 13:45:07 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
db.signin(RecordAccess {
|
2023-06-09 13:45:07 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-06-09 13:45:07 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn record_access_throws_error() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-10-28 12:39:46 +00:00
|
|
|
let database = Ulid::new().to_string();
|
|
|
|
db.use_ns(NS).use_db(&database).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
let access = Ulid::new().to_string();
|
|
|
|
let email = format!("{access}@example.com");
|
2023-10-28 12:39:46 +00:00
|
|
|
let pass = "password123";
|
|
|
|
let sql = format!(
|
|
|
|
"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS `{access}` ON DB TYPE RECORD
|
2023-10-28 12:39:46 +00:00
|
|
|
SIGNUP {{ THROW 'signup_thrown_error' }}
|
|
|
|
SIGNIN {{ THROW 'signin_thrown_error' }}
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1d FOR TOKEN 15s
|
2023-10-28 12:39:46 +00:00
|
|
|
"
|
|
|
|
);
|
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-10-28 12:39:46 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
|
|
|
|
match db
|
2024-05-22 13:57:25 +00:00
|
|
|
.signup(RecordAccess {
|
2023-10-28 12:39:46 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-10-28 12:39:46 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Err(Error::Db(surrealdb::err::Error::Thrown(e))) => assert_eq!(e, "signup_thrown_error"),
|
2023-12-20 10:31:19 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Query(e))) => assert!(e.contains("signup")),
|
2023-10-28 12:39:46 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Http(e))) => assert_eq!(
|
|
|
|
e,
|
|
|
|
"HTTP status client error (400 Bad Request) for url (http://127.0.0.1:8000/signup)"
|
|
|
|
),
|
|
|
|
v => panic!("Unexpected response or error: {v:?}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
match db
|
2024-05-22 13:57:25 +00:00
|
|
|
.signin(RecordAccess {
|
2023-10-28 12:39:46 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-10-28 12:39:46 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Err(Error::Db(surrealdb::err::Error::Thrown(e))) => assert_eq!(e, "signin_thrown_error"),
|
2023-12-20 10:31:19 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Query(e))) => assert!(e.contains("signin")),
|
2023-10-28 12:39:46 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Http(e))) => assert_eq!(
|
|
|
|
e,
|
|
|
|
"HTTP status client error (400 Bad Request) for url (http://127.0.0.1:8000/signin)"
|
|
|
|
),
|
|
|
|
v => panic!("Unexpected response or error: {v:?}"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn record_access_invalid_query() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-10-28 12:39:46 +00:00
|
|
|
let database = Ulid::new().to_string();
|
|
|
|
db.use_ns(NS).use_db(&database).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
let access = Ulid::new().to_string();
|
|
|
|
let email = format!("{access}@example.com");
|
2023-10-28 12:39:46 +00:00
|
|
|
let pass = "password123";
|
|
|
|
let sql = format!(
|
|
|
|
"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS `{access}` ON DB TYPE RECORD
|
2023-10-28 12:39:46 +00:00
|
|
|
SIGNUP {{ SELECT * FROM ONLY [1, 2] }}
|
|
|
|
SIGNIN {{ SELECT * FROM ONLY [1, 2] }}
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1d FOR TOKEN 15s
|
2023-10-28 12:39:46 +00:00
|
|
|
"
|
|
|
|
);
|
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-10-28 12:39:46 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
|
|
|
|
match db
|
2024-05-22 13:57:25 +00:00
|
|
|
.signup(RecordAccess {
|
2023-10-28 12:39:46 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-10-28 12:39:46 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
2024-05-22 13:57:25 +00:00
|
|
|
Err(Error::Db(surrealdb::err::Error::AccessRecordSignupQueryFailed)) => (),
|
2023-10-28 12:39:46 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Query(e))) => {
|
2024-05-22 13:57:25 +00:00
|
|
|
assert_eq!(
|
|
|
|
e,
|
|
|
|
"There was a problem with the database: The record access signup query failed"
|
|
|
|
)
|
2023-10-28 12:39:46 +00:00
|
|
|
}
|
|
|
|
Err(Error::Api(surrealdb::error::Api::Http(e))) => assert_eq!(
|
|
|
|
e,
|
|
|
|
"HTTP status client error (400 Bad Request) for url (http://127.0.0.1:8000/signup)"
|
|
|
|
),
|
|
|
|
v => panic!("Unexpected response or error: {v:?}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
match db
|
2024-05-22 13:57:25 +00:00
|
|
|
.signin(RecordAccess {
|
2023-10-28 12:39:46 +00:00
|
|
|
namespace: NS,
|
|
|
|
database: &database,
|
2024-05-22 13:57:25 +00:00
|
|
|
access: &access,
|
2023-10-28 12:39:46 +00:00
|
|
|
params: AuthParams {
|
|
|
|
pass,
|
|
|
|
email: &email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
2024-05-22 13:57:25 +00:00
|
|
|
Err(Error::Db(surrealdb::err::Error::AccessRecordSigninQueryFailed)) => (),
|
2023-10-28 12:39:46 +00:00
|
|
|
Err(Error::Api(surrealdb::error::Api::Query(e))) => {
|
2024-05-22 13:57:25 +00:00
|
|
|
assert_eq!(
|
|
|
|
e,
|
|
|
|
"There was a problem with the database: The record access signin query failed"
|
|
|
|
)
|
2023-10-28 12:39:46 +00:00
|
|
|
}
|
|
|
|
Err(Error::Api(surrealdb::error::Api::Http(e))) => assert_eq!(
|
|
|
|
e,
|
|
|
|
"HTTP status client error (400 Bad Request) for url (http://127.0.0.1:8000/signin)"
|
|
|
|
),
|
|
|
|
v => panic!("Unexpected response or error: {v:?}"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-06-09 13:45:07 +00:00
|
|
|
async fn authenticate() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-06-09 13:45:07 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
let user = Ulid::new().to_string();
|
|
|
|
let pass = "password123";
|
2024-01-10 16:43:56 +00:00
|
|
|
let sql = format!("DEFINE USER `{user}` ON NAMESPACE PASSWORD '{pass}'");
|
2023-06-09 13:45:07 +00:00
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-09 13:45:07 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
let token = db
|
|
|
|
.signin(Namespace {
|
|
|
|
namespace: NS,
|
|
|
|
username: &user,
|
|
|
|
password: pass,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
db.authenticate(token).await.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn query() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let _ = db
|
|
|
|
.query(
|
|
|
|
"
|
2022-12-30 08:23:19 +00:00
|
|
|
CREATE user:john
|
|
|
|
SET name = 'John Doe'
|
2023-06-19 22:35:19 +00:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.check()
|
|
|
|
.unwrap();
|
|
|
|
let mut response = db.query("SELECT name FROM user:john").await.unwrap().check().unwrap();
|
|
|
|
let Some(name): Option<String> = response.take("name").unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("query returned no record");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(name, "John Doe");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-09-12 14:34:58 +00:00
|
|
|
async fn query_decimals() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-09-12 14:34:58 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE foo;
|
|
|
|
DEFINE FIELD bar ON foo TYPE decimal;
|
|
|
|
CREATE foo CONTENT { bar: 42.69 };
|
|
|
|
";
|
|
|
|
let _ = db.query(sql).await.unwrap().check().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-09-12 14:34:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn query_binds() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let mut response =
|
|
|
|
db.query("CREATE user:john SET name = $name").bind(("name", "John Doe")).await.unwrap();
|
|
|
|
let Some(record): Option<RecordName> = response.take(0).unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("query returned no record");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(record.name, "John Doe");
|
|
|
|
let mut response = db
|
|
|
|
.query("SELECT * FROM $record_id")
|
2024-08-21 12:34:16 +00:00
|
|
|
.bind(("record_id", "user:john".parse::<RecordId>().unwrap()))
|
2023-06-19 22:35:19 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let Some(record): Option<RecordName> = response.take(0).unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("query returned no record");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(record.name, "John Doe");
|
|
|
|
let mut response = db
|
|
|
|
.query("CREATE user SET name = $name")
|
2022-12-30 08:23:19 +00:00
|
|
|
.bind(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "John Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
let Some(record): Option<RecordName> = response.take(0).unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("query returned no record");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(record.name, "John Doe");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 19:13:52 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn query_with_stats() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let sql = "CREATE foo; SELECT * FROM foo";
|
|
|
|
let mut response = db.query(sql).with_stats().await.unwrap();
|
|
|
|
// First query statement
|
|
|
|
let (stats, result) = response.take(0).unwrap();
|
2023-12-20 10:31:19 +00:00
|
|
|
assert!(stats.execution_time > Some(Duration::ZERO));
|
2023-11-27 19:13:52 +00:00
|
|
|
let _: Value = result.unwrap();
|
|
|
|
// Second query statement
|
|
|
|
let (stats, result) = response.take(1).unwrap();
|
2023-12-20 10:31:19 +00:00
|
|
|
assert!(stats.execution_time > Some(Duration::ZERO));
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = result.unwrap();
|
2023-11-27 19:13:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn query_chaining() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let response = db
|
2024-04-02 20:12:08 +00:00
|
|
|
.query(BeginStatement::default())
|
2022-12-30 08:23:19 +00:00
|
|
|
.query("CREATE account:one SET balance = 135605.16")
|
|
|
|
.query("CREATE account:two SET balance = 91031.31")
|
|
|
|
.query("UPDATE account:one SET balance += 300.00")
|
|
|
|
.query("UPDATE account:two SET balance -= 300.00")
|
2024-04-02 20:12:08 +00:00
|
|
|
.query(CommitStatement::default())
|
2022-12-30 08:23:19 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
response.check().unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-05-26 10:36:37 +00:00
|
|
|
async fn mixed_results_query() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-05-26 10:36:37 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let sql = "CREATE bar SET baz = rand('a'); CREATE foo;";
|
|
|
|
let mut response = db.query(sql).await.unwrap();
|
|
|
|
response.take::<Value>(0).unwrap_err();
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = response.take(1).unwrap();
|
2023-05-26 10:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn create_record_no_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db.create("user").await.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db.create(Resource::from("user")).await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn create_record_with_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.create(("user", "jane")).await.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db.create(Resource::from(("user", "john"))).await.unwrap();
|
|
|
|
let _: Value = db.create(Resource::from("user:doe")).await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn create_record_no_id_with_content() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.create("user")
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "John Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db
|
|
|
|
.create(Resource::from("user"))
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "John Doe".to_owned(),
|
2023-04-28 11:20:57 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn create_record_with_id_with_content() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2024-08-21 12:34:16 +00:00
|
|
|
let record: Option<ApiRecordId> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.create(("user", "john"))
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "John Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(record.unwrap().id, "user:john".parse::<RecordId>().unwrap());
|
2023-04-28 11:20:57 +00:00
|
|
|
let value: Value = db
|
2024-08-21 12:34:16 +00:00
|
|
|
.create(Resource::from("user:jane".parse::<RecordId>().unwrap()))
|
2023-04-28 11:20:57 +00:00
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "Jane Doe".to_owned(),
|
2023-04-28 11:20:57 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(
|
|
|
|
value.into_inner().record(),
|
|
|
|
Some("user:jane".parse::<RecordId>().unwrap().into_inner())
|
|
|
|
);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 11:24:24 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn insert_table() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db.insert(table).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.insert(table).content(json!({ "foo": "bar" })).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.insert(table).content(json!([{ "foo": "bar" }])).await.unwrap();
|
2024-03-20 11:24:24 +00:00
|
|
|
let _: Value = db.insert(Resource::from(table)).await.unwrap();
|
|
|
|
let _: Value = db.insert(Resource::from(table)).content(json!({ "foo": "bar" })).await.unwrap();
|
|
|
|
let _: Value =
|
|
|
|
db.insert(Resource::from(table)).content(json!([{ "foo": "bar" }])).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.insert(table).await.unwrap();
|
2024-03-20 11:24:24 +00:00
|
|
|
assert!(!users.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn insert_thing() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.insert((table, "user1")).await.unwrap();
|
|
|
|
let _: Option<ApiRecordId> =
|
2024-03-20 11:24:24 +00:00
|
|
|
db.insert((table, "user1")).content(json!({ "foo": "bar" })).await.unwrap();
|
|
|
|
let _: Value = db.insert(Resource::from((table, "user2"))).await.unwrap();
|
|
|
|
let _: Value =
|
|
|
|
db.insert(Resource::from((table, "user2"))).content(json!({ "foo": "bar" })).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let user: Option<ApiRecordId> = db.insert((table, "user3")).await.unwrap();
|
2024-03-20 11:24:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
user,
|
2024-08-21 12:34:16 +00:00
|
|
|
Some(ApiRecordId {
|
|
|
|
id: "user:user3".parse().unwrap(),
|
2024-03-20 11:24:24 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-22 21:09:43 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn insert_unspecified() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let tmp: Result<Vec<RecordId>, _> = db.insert(()).await;
|
|
|
|
tmp.unwrap_err();
|
|
|
|
let tmp: Result<Vec<RecordId>, _> = db.insert(()).content(json!({ "foo": "bar" })).await;
|
|
|
|
tmp.unwrap_err();
|
|
|
|
let tmp: Vec<ApiRecordId> = db
|
|
|
|
.insert(())
|
|
|
|
.content("{id: user:user1, foo: 'bar'}".parse::<Value>().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
tmp,
|
|
|
|
vec![ApiRecordId {
|
|
|
|
id: "user:user1".parse::<RecordId>().unwrap(),
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
|
|
|
|
let tmp: Result<Value, _> = db.insert(Resource::from(())).await;
|
|
|
|
tmp.unwrap_err();
|
|
|
|
let tmp: Result<Value, _> =
|
|
|
|
db.insert(Resource::from(())).content(json!({ "foo": "bar" })).await;
|
|
|
|
tmp.unwrap_err();
|
|
|
|
let tmp: Value = db
|
|
|
|
.insert(Resource::from(()))
|
|
|
|
.content("{id: user:user2, foo: 'bar'}".parse::<Value>().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let val = "{id: user:user2, foo: 'bar'}".parse::<Value>().unwrap();
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn select_table() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db.create(Resource::from(table)).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(users.len(), 3);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn select_record_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let record_id = ("user", "john");
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.create(record_id).await.unwrap();
|
|
|
|
let Some(record): Option<ApiRecordId> = db.select(record_id).await.unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("record not found");
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(record.id, "user:john".parse().unwrap());
|
2023-04-28 11:20:57 +00:00
|
|
|
let value: Value = db.select(Resource::from(record_id)).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(
|
|
|
|
value.into_inner().record(),
|
|
|
|
Some("user:john".parse::<RecordId>().unwrap().into_inner())
|
|
|
|
);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn select_record_ranges() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.create((table, "amos")).await.unwrap();
|
|
|
|
let _: Option<ApiRecordId> = db.create((table, "jane")).await.unwrap();
|
|
|
|
let _: Option<ApiRecordId> = db.create((table, "john")).await.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db.create(Resource::from((table, "zoey"))).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let convert = |users: Vec<ApiRecordId>| -> Vec<String> {
|
|
|
|
users
|
|
|
|
.into_iter()
|
|
|
|
.map(|user| {
|
|
|
|
let Id::String(ref x) = user.id.into_inner().id else {
|
|
|
|
panic!()
|
|
|
|
};
|
|
|
|
x.clone()
|
|
|
|
})
|
|
|
|
.collect()
|
2022-12-30 08:23:19 +00:00
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range(..).await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["amos", "jane", "john", "zoey"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range(.."john").await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["amos", "jane"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range(..="john").await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["amos", "jane", "john"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range("jane"..).await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["jane", "john", "zoey"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range("jane".."john").await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["jane"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).range("jane"..="john").await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(convert(users), vec!["jane", "john"]);
|
2024-08-21 12:34:16 +00:00
|
|
|
let v: Value = db.select(Resource::from(table)).range("jane"..="john").await.unwrap();
|
|
|
|
let CoreValue::Array(array) = v.into_inner() else {
|
|
|
|
panic!()
|
2023-08-24 19:02:44 +00:00
|
|
|
};
|
2023-04-28 11:20:57 +00:00
|
|
|
assert_eq!(array.len(), 2);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> =
|
2022-12-30 08:23:19 +00:00
|
|
|
db.select(table).range((Bound::Excluded("jane"), Bound::Included("john"))).await.unwrap();
|
|
|
|
assert_eq!(convert(users), vec!["john"]);
|
|
|
|
}
|
|
|
|
|
2024-03-28 16:29:55 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn select_records_order_by_start_limit() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let sql = "
|
|
|
|
CREATE user:john SET name = 'John';
|
|
|
|
CREATE user:zoey SET name = 'Zoey';
|
|
|
|
CREATE user:amos SET name = 'Amos';
|
|
|
|
CREATE user:jane SET name = 'Jane';
|
|
|
|
";
|
|
|
|
db.query(sql).await.unwrap().check().unwrap();
|
|
|
|
|
|
|
|
let check_start_limit = |mut response: Response, expected: Vec<&str>| {
|
|
|
|
let users: Vec<RecordName> = response.take(0).unwrap();
|
|
|
|
let users: Vec<String> = users.into_iter().map(|user| user.name).collect();
|
|
|
|
assert_eq!(users, expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
let response =
|
|
|
|
db.query("SELECT name FROM user ORDER BY name DESC START 1 LIMIT 2").await.unwrap();
|
|
|
|
check_start_limit(response, vec!["John", "Jane"]);
|
|
|
|
|
|
|
|
let response = db.query("SELECT name FROM user ORDER BY name DESC START 1").await.unwrap();
|
|
|
|
check_start_limit(response, vec!["John", "Jane", "Amos"]);
|
|
|
|
|
|
|
|
let response = db.query("SELECT name FROM user ORDER BY name DESC START 4").await.unwrap();
|
|
|
|
check_start_limit(response, vec![]);
|
|
|
|
|
|
|
|
let response = db.query("SELECT name FROM user ORDER BY name DESC LIMIT 2").await.unwrap();
|
|
|
|
check_start_limit(response, vec!["Zoey", "John"]);
|
|
|
|
|
|
|
|
let response = db.query("SELECT name FROM user ORDER BY name DESC LIMIT 10").await.unwrap();
|
|
|
|
check_start_limit(response, vec!["Zoey", "John", "Jane", "Amos"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn select_records_order_by() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let sql = "
|
|
|
|
CREATE user:john SET name = 'John';
|
|
|
|
CREATE user:zoey SET name = 'Zoey';
|
|
|
|
CREATE user:amos SET name = 'Amos';
|
|
|
|
CREATE user:jane SET name = 'Jane';
|
|
|
|
";
|
|
|
|
db.query(sql).await.unwrap().check().unwrap();
|
|
|
|
let sql = "SELECT name FROM user ORDER BY name DESC";
|
|
|
|
let mut response = db.query(sql).await.unwrap();
|
|
|
|
let users: Vec<RecordName> = response.take(0).unwrap();
|
|
|
|
let convert = |users: Vec<RecordName>| -> Vec<String> {
|
|
|
|
users.into_iter().map(|user| user.name).collect()
|
|
|
|
};
|
|
|
|
assert_eq!(convert(users), vec!["Zoey", "John", "Jane", "Amos"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn select_records_fetch() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let sql = "
|
|
|
|
CREATE tag:rs SET name = 'Rust';
|
|
|
|
CREATE tag:go SET name = 'Golang';
|
|
|
|
CREATE tag:js SET name = 'JavaScript';
|
|
|
|
CREATE person:tobie SET tags = [tag:rs, tag:go, tag:js];
|
|
|
|
CREATE person:jaime SET tags = [tag:js];
|
|
|
|
";
|
|
|
|
db.query(sql).await.unwrap().check().unwrap();
|
|
|
|
|
|
|
|
let check_fetch = |mut response: Response, expected: &str| {
|
|
|
|
let val: Value = response.take(0).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let exp = expected.parse().unwrap();
|
|
|
|
assert_eq!(val, exp);
|
2024-03-28 16:29:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let sql = "SELECT * FROM person LIMIT 1 FETCH tags;";
|
|
|
|
let response = db.query(sql).await.unwrap();
|
|
|
|
check_fetch(
|
|
|
|
response,
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:jaime,
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
id: tag:js,
|
|
|
|
name: 'JavaScript'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
|
|
|
|
let sql = "SELECT * FROM person START 1 LIMIT 1 FETCH tags;";
|
|
|
|
let response = db.query(sql).await.unwrap();
|
|
|
|
check_fetch(
|
|
|
|
response,
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:tobie,
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
id: tag:rs,
|
|
|
|
name: 'Rust'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: tag:go,
|
|
|
|
name: 'Golang'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: tag:js,
|
|
|
|
name: 'JavaScript'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
|
|
|
|
let sql = "SELECT * FROM person ORDER BY id FETCH tags;";
|
|
|
|
let response = db.query(sql).await.unwrap();
|
|
|
|
check_fetch(
|
|
|
|
response,
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:jaime,
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
id: tag:js,
|
|
|
|
name: 'JavaScript'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: person:tobie,
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
id: tag:rs,
|
|
|
|
name: 'Rust'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: tag:go,
|
|
|
|
name: 'Golang'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: tag:js,
|
|
|
|
name: 'JavaScript'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn update_table() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
2023-04-28 11:20:57 +00:00
|
|
|
let _: Value = db.update(Resource::from(table)).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.update(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(users.len(), 2);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn update_record_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.create((table, "john")).await.unwrap();
|
|
|
|
let _: Option<ApiRecordId> = db.create((table, "jane")).await.unwrap();
|
|
|
|
let users: Vec<ApiRecordId> = db.update(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(users.len(), 2);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn update_table_with_content() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let sql = "
|
2022-12-30 08:23:19 +00:00
|
|
|
CREATE type::thing($table, 'amos') SET name = 'Amos';
|
|
|
|
CREATE type::thing($table, 'jane') SET name = 'Jane';
|
|
|
|
CREATE type::thing($table, 'john') SET name = 'John';
|
|
|
|
CREATE type::thing($table, 'zoey') SET name = 'Zoey';
|
|
|
|
";
|
|
|
|
let table = "user";
|
2023-06-19 22:35:19 +00:00
|
|
|
let response = db.query(sql).bind(("table", table)).await.unwrap();
|
|
|
|
response.check().unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
let users: Vec<RecordBuf> = db
|
|
|
|
.update(table)
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
let expected = &[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:amos".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:jane".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:john".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:zoey".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
assert_eq!(users, expected);
|
|
|
|
let users: Vec<RecordBuf> = db.select(table).await.unwrap();
|
|
|
|
assert_eq!(users, expected);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn update_record_range_with_content() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let sql = "
|
2022-12-30 08:23:19 +00:00
|
|
|
CREATE type::thing($table, 'amos') SET name = 'Amos';
|
|
|
|
CREATE type::thing($table, 'jane') SET name = 'Jane';
|
|
|
|
CREATE type::thing($table, 'john') SET name = 'John';
|
|
|
|
CREATE type::thing($table, 'zoey') SET name = 'Zoey';
|
|
|
|
";
|
|
|
|
let table = "user";
|
2023-06-19 22:35:19 +00:00
|
|
|
let response = db.query(sql).bind(("table", table)).await.unwrap();
|
|
|
|
response.check().unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
let users: Vec<RecordBuf> = db
|
|
|
|
.update(table)
|
|
|
|
.range("jane".."zoey")
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(
|
|
|
|
users,
|
|
|
|
&[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:jane".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:john".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
let users: Vec<RecordBuf> = db.select(table).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
users,
|
|
|
|
&[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:amos".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Amos".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:jane".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:john".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:zoey".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Zoey".to_owned(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn update_record_id_with_content() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let record_id = ("user", "john");
|
2023-04-28 11:20:57 +00:00
|
|
|
let user: Option<RecordName> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.create(record_id)
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "Jane Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(user.unwrap().name, "Jane Doe");
|
2023-04-28 11:20:57 +00:00
|
|
|
let user: Option<RecordName> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.update(record_id)
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "John Doe".to_owned(),
|
2022-12-30 08:23:19 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(user.unwrap().name, "John Doe");
|
|
|
|
let user: Option<RecordName> = db.select(record_id).await.unwrap();
|
|
|
|
assert_eq!(user.unwrap().name, "John Doe");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
|
|
|
struct Name {
|
2023-06-19 22:35:19 +00:00
|
|
|
first: Cow<'static, str>,
|
|
|
|
last: Cow<'static, str>,
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2024-08-21 12:34:16 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
|
2022-12-30 08:23:19 +00:00
|
|
|
struct Person {
|
2023-06-19 22:35:19 +00:00
|
|
|
#[serde(skip_serializing)]
|
2024-08-21 12:34:16 +00:00
|
|
|
id: Option<RecordId>,
|
2023-06-19 22:35:19 +00:00
|
|
|
title: Cow<'static, str>,
|
|
|
|
name: Name,
|
|
|
|
marketing: bool,
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn merge_record_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let record_id = ("person", "jaime");
|
|
|
|
let mut jaime: Option<Person> = db
|
|
|
|
.create(record_id)
|
|
|
|
.content(Person {
|
|
|
|
id: None,
|
|
|
|
title: "Founder & COO".into(),
|
|
|
|
name: Name {
|
|
|
|
first: "Jaime".into(),
|
|
|
|
last: "Morgan Hitchcock".into(),
|
|
|
|
},
|
|
|
|
marketing: false,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(jaime.unwrap().id.unwrap(), "person:jaime".parse().unwrap());
|
2023-06-19 22:35:19 +00:00
|
|
|
jaime = db.update(record_id).merge(json!({ "marketing": true })).await.unwrap();
|
|
|
|
assert!(jaime.as_ref().unwrap().marketing);
|
|
|
|
jaime = db.select(record_id).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
jaime.unwrap(),
|
|
|
|
Person {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: Some("person:jaime".parse().unwrap()),
|
2023-06-19 22:35:19 +00:00
|
|
|
title: "Founder & COO".into(),
|
|
|
|
name: Name {
|
|
|
|
first: "Jaime".into(),
|
|
|
|
last: "Morgan Hitchcock".into(),
|
|
|
|
},
|
|
|
|
marketing: true,
|
|
|
|
}
|
|
|
|
);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn patch_record_id() {
|
2024-08-21 12:34:16 +00:00
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
2023-08-27 18:28:38 +00:00
|
|
|
struct Record {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: RecordId,
|
2023-08-27 18:28:38 +00:00
|
|
|
baz: String,
|
|
|
|
hello: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
let id = "john";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.create(("user", id))
|
|
|
|
.content(json!({
|
|
|
|
"baz": "qux",
|
|
|
|
"foo": "bar"
|
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-08-27 18:28:38 +00:00
|
|
|
let _: Option<Record> = db
|
2022-12-30 08:23:19 +00:00
|
|
|
.update(("user", id))
|
|
|
|
.patch(PatchOp::replace("/baz", "boo"))
|
|
|
|
.patch(PatchOp::add("/hello", ["world"]))
|
|
|
|
.patch(PatchOp::remove("/foo"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-08-27 18:28:38 +00:00
|
|
|
let value: Option<Record> = db.select(("user", id)).await.unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
assert_eq!(
|
|
|
|
value,
|
2023-08-27 18:28:38 +00:00
|
|
|
Some(Record {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: format!("user:{id}").parse().unwrap(),
|
2023-08-27 18:28:38 +00:00
|
|
|
baz: "boo".to_owned(),
|
|
|
|
hello: vec!["world".to_owned()],
|
|
|
|
})
|
2022-12-30 08:23:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn delete_table() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let table = "user";
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
|
|
|
let _: Vec<ApiRecordId> = db.create(table).await.unwrap();
|
|
|
|
let users: Vec<ApiRecordId> = db.select(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(users.len(), 3);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.delete(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(users.len(), 3);
|
2024-08-21 12:34:16 +00:00
|
|
|
let users: Vec<ApiRecordId> = db.select(table).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert!(users.is_empty());
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn delete_record_id() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let record_id = ("user", "john");
|
2024-08-21 12:34:16 +00:00
|
|
|
let _: Option<ApiRecordId> = db.create(record_id).await.unwrap();
|
|
|
|
let _: Option<ApiRecordId> = db.select(record_id).await.unwrap();
|
|
|
|
let john: Option<ApiRecordId> = db.delete(record_id).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert!(john.is_some());
|
2024-08-21 12:34:16 +00:00
|
|
|
let john: Option<ApiRecordId> = db.select(record_id).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert!(john.is_none());
|
|
|
|
// non-existing user
|
2024-08-21 12:34:16 +00:00
|
|
|
let jane: Option<ApiRecordId> = db.delete(("user", "jane")).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert!(jane.is_none());
|
2024-08-21 12:34:16 +00:00
|
|
|
let value: Value = db.delete(Resource::from(("user", "jane"))).await.unwrap();
|
|
|
|
assert_eq!(value.into_inner(), CoreValue::None);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn delete_record_range() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let sql = "
|
2022-12-30 08:23:19 +00:00
|
|
|
CREATE type::thing($table, 'amos') SET name = 'Amos';
|
|
|
|
CREATE type::thing($table, 'jane') SET name = 'Jane';
|
|
|
|
CREATE type::thing($table, 'john') SET name = 'John';
|
|
|
|
CREATE type::thing($table, 'zoey') SET name = 'Zoey';
|
|
|
|
";
|
|
|
|
let table = "user";
|
2023-06-19 22:35:19 +00:00
|
|
|
let response = db.query(sql).bind(("table", table)).await.unwrap();
|
|
|
|
response.check().unwrap();
|
2023-03-31 22:49:29 +00:00
|
|
|
let users: Vec<RecordBuf> = db.delete(table).range("jane".."zoey").await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(
|
|
|
|
users,
|
|
|
|
&[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:jane".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Jane".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:john".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "John".to_owned(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
let users: Vec<RecordBuf> = db.select(table).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
users,
|
|
|
|
&[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:amos".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Amos".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:zoey".parse().unwrap(),
|
2023-06-19 22:35:19 +00:00
|
|
|
name: "Zoey".to_owned(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-07-24 16:15:20 +00:00
|
|
|
async fn changefeed() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2023-07-24 16:15:20 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
// Enable change feeds
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE user CHANGEFEED 1h;
|
|
|
|
";
|
|
|
|
let response = db.query(sql).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-07-24 16:15:20 +00:00
|
|
|
response.check().unwrap();
|
|
|
|
// Create and update users
|
|
|
|
let sql = "
|
|
|
|
CREATE user:amos SET name = 'Amos';
|
|
|
|
CREATE user:jane SET name = 'Jane';
|
|
|
|
UPDATE user:amos SET name = 'AMOS';
|
|
|
|
";
|
|
|
|
let table = "user";
|
|
|
|
let response = db.query(sql).await.unwrap();
|
|
|
|
response.check().unwrap();
|
|
|
|
let users: Vec<RecordBuf> = db
|
|
|
|
.update(table)
|
|
|
|
.content(Record {
|
2024-08-13 08:29:43 +00:00
|
|
|
name: "Doe".to_owned(),
|
2023-07-24 16:15:20 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let expected = &[
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:amos".parse().unwrap(),
|
2023-07-24 16:15:20 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
RecordBuf {
|
2024-08-21 12:34:16 +00:00
|
|
|
id: "user:jane".parse().unwrap(),
|
2023-07-24 16:15:20 +00:00
|
|
|
name: "Doe".to_owned(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
assert_eq!(users, expected);
|
|
|
|
let users: Vec<RecordBuf> = db.select(table).await.unwrap();
|
|
|
|
assert_eq!(users, expected);
|
|
|
|
let sql = "
|
|
|
|
SHOW CHANGES FOR TABLE user SINCE 0 LIMIT 10;
|
|
|
|
";
|
|
|
|
let mut response = db.query(sql).await.unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let v: Value = response.take(0).unwrap();
|
|
|
|
let CoreValue::Array(array) = v.into_inner() else {
|
|
|
|
panic!()
|
2023-08-24 19:02:44 +00:00
|
|
|
};
|
2023-09-12 15:55:07 +00:00
|
|
|
assert_eq!(array.len(), 5);
|
|
|
|
// DEFINE TABLE
|
2024-03-08 10:58:07 +00:00
|
|
|
let a = array.first().unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Object(a) = a.clone() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Number(_versionstamp1) = a.get("versionstamp").clone().unwrap() else {
|
2023-09-12 15:55:07 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let changes = a.get("changes").unwrap().clone().to_owned();
|
2023-09-12 15:55:07 +00:00
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2023-09-12 15:55:07 +00:00
|
|
|
{
|
|
|
|
define_table: {
|
|
|
|
name: 'user'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2023-09-12 15:55:07 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
// UPDATE user:amos
|
|
|
|
let a = array.get(1).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Object(a) = a.clone() else {
|
2023-09-12 15:55:07 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Number(versionstamp1) = a.get("versionstamp").unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2023-07-24 16:15:20 +00:00
|
|
|
let changes = a.get("changes").unwrap().to_owned();
|
2024-02-20 11:11:49 +00:00
|
|
|
match FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
true => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
r#"[
|
2024-02-20 11:11:49 +00:00
|
|
|
{
|
|
|
|
create: {
|
|
|
|
id: user:amos,
|
|
|
|
name: 'Amos'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"#
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-02-20 11:11:49 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
2023-07-24 16:15:20 +00:00
|
|
|
}
|
2024-02-20 11:11:49 +00:00
|
|
|
false => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
r#"[
|
2024-02-20 11:11:49 +00:00
|
|
|
{
|
|
|
|
update: {
|
|
|
|
id: user:amos,
|
|
|
|
name: 'Amos'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"#
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-02-20 11:11:49 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 16:15:20 +00:00
|
|
|
// UPDATE user:jane
|
2023-09-12 15:55:07 +00:00
|
|
|
let a = array.get(2).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Object(a) = a.clone() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Number(versionstamp2) = a.get("versionstamp").unwrap().clone() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
assert!(*versionstamp1 < versionstamp2);
|
2023-07-24 16:15:20 +00:00
|
|
|
let changes = a.get("changes").unwrap().to_owned();
|
2024-02-20 11:11:49 +00:00
|
|
|
match FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
true => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2024-02-20 11:11:49 +00:00
|
|
|
{
|
|
|
|
create: {
|
|
|
|
id: user:jane,
|
|
|
|
name: 'Jane'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-02-20 11:11:49 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
2023-07-24 16:15:20 +00:00
|
|
|
}
|
2024-02-20 11:11:49 +00:00
|
|
|
false => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2024-02-20 11:11:49 +00:00
|
|
|
{
|
|
|
|
update: {
|
|
|
|
id: user:jane,
|
|
|
|
name: 'Jane'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-02-20 11:11:49 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 16:15:20 +00:00
|
|
|
// UPDATE user:amos
|
2023-09-12 15:55:07 +00:00
|
|
|
let a = array.get(3).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Object(a) = a.clone() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Number(versionstamp3) = a.get("versionstamp").unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
assert!(versionstamp2 < *versionstamp3);
|
2023-07-24 16:15:20 +00:00
|
|
|
let changes = a.get("changes").unwrap().to_owned();
|
2024-04-23 10:00:23 +00:00
|
|
|
match FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
true => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2024-04-23 10:00:23 +00:00
|
|
|
{
|
|
|
|
create: {
|
|
|
|
id: user:amos,
|
|
|
|
name: 'AMOS'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-04-23 10:00:23 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
2023-07-24 16:15:20 +00:00
|
|
|
}
|
2024-04-23 10:00:23 +00:00
|
|
|
false => {
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2024-04-23 10:00:23 +00:00
|
|
|
{
|
|
|
|
update: {
|
|
|
|
id: user:amos,
|
|
|
|
name: 'AMOS'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2024-04-23 10:00:23 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2023-07-24 16:15:20 +00:00
|
|
|
// UPDATE table
|
2023-09-12 15:55:07 +00:00
|
|
|
let a = array.get(4).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Object(a) = a.clone() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-08-21 12:34:16 +00:00
|
|
|
let CoreValue::Number(versionstamp4) = a.get("versionstamp").unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2023-07-24 16:15:20 +00:00
|
|
|
assert!(versionstamp3 < versionstamp4);
|
|
|
|
let changes = a.get("changes").unwrap().to_owned();
|
|
|
|
assert_eq!(
|
2024-08-21 12:34:16 +00:00
|
|
|
Value::from_inner(changes),
|
|
|
|
"[
|
2023-07-24 16:15:20 +00:00
|
|
|
{
|
|
|
|
update: {
|
|
|
|
id: user:amos,
|
|
|
|
name: 'Doe'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
update: {
|
|
|
|
id: user:jane,
|
|
|
|
name: 'Doe'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]"
|
2024-08-21 12:34:16 +00:00
|
|
|
.parse()
|
2023-07-24 16:15:20 +00:00
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn version() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
drop(permit);
|
2022-12-30 08:23:19 +00:00
|
|
|
db.version().await.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn set_unset() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let (key, value) = ("name", "Doe");
|
|
|
|
let sql = "RETURN $name";
|
2022-12-30 08:23:19 +00:00
|
|
|
db.set(key, value).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
let mut response = db.query(sql).await.unwrap();
|
|
|
|
let Some(name): Option<String> = response.take(0).unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("record not found");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert_eq!(name, value);
|
2024-01-05 11:28:01 +00:00
|
|
|
// `token` is a reserved variable
|
|
|
|
db.set("token", value).await.unwrap_err();
|
|
|
|
// make sure we can still run queries after trying to set a protected variable
|
|
|
|
db.query("RETURN true").await.unwrap().check().unwrap();
|
2022-12-30 08:23:19 +00:00
|
|
|
db.unset(key).await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
let mut response = db.query(sql).await.unwrap();
|
|
|
|
let name: Option<String> = response.take(0).unwrap();
|
|
|
|
assert!(name.is_none());
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
2022-12-30 08:23:19 +00:00
|
|
|
async fn return_bool() {
|
2023-11-13 17:19:47 +00:00
|
|
|
let (permit, db) = new_db().await;
|
2022-12-30 08:23:19 +00:00
|
|
|
let mut response = db.query("RETURN true").await.unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
drop(permit);
|
2023-06-19 22:35:19 +00:00
|
|
|
let Some(boolean): Option<bool> = response.take(0).unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("record not found");
|
|
|
|
};
|
2023-06-19 22:35:19 +00:00
|
|
|
assert!(boolean);
|
2023-04-28 11:20:57 +00:00
|
|
|
let mut response = db.query("RETURN false").await.unwrap();
|
2023-06-19 22:35:19 +00:00
|
|
|
let value: Value = response.take(0).unwrap();
|
2024-08-21 12:34:16 +00:00
|
|
|
assert_eq!(value.into_inner(), CoreValue::Bool(false));
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
2024-08-20 10:50:19 +00:00
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn run() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
drop(permit);
|
|
|
|
let sql = "
|
|
|
|
DEFINE FUNCTION fn::foo() {
|
|
|
|
RETURN 42;
|
|
|
|
};
|
|
|
|
DEFINE FUNCTION fn::bar($val: any) {
|
|
|
|
CREATE foo:1 set val = $val;
|
|
|
|
};
|
|
|
|
DEFINE FUNCTION fn::baz() {
|
|
|
|
RETURN SELECT VALUE val FROM ONLY foo:1;
|
|
|
|
};
|
|
|
|
";
|
|
|
|
let _ = db.query(sql).await;
|
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp: i32 = db.run("fn::foo").await.unwrap();
|
|
|
|
assert_eq!(tmp, 42);
|
2024-08-20 10:50:19 +00:00
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp = db.run::<i32>("fn::foo").args(7).await.unwrap_err();
|
2024-08-20 10:50:19 +00:00
|
|
|
println!("fn::foo res: {tmp}");
|
|
|
|
assert!(tmp.to_string().contains("The function expects 0 arguments."));
|
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp = db.run::<()>("fn::idnotexist").await.unwrap_err();
|
2024-08-20 10:50:19 +00:00
|
|
|
println!("fn::idontexist res: {tmp}");
|
|
|
|
assert!(tmp.to_string().contains("The function 'fn::idnotexist' does not exist"));
|
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp: usize = db.run("count").args(vec![1, 2, 3]).await.unwrap();
|
|
|
|
assert_eq!(tmp, 3);
|
2024-08-20 10:50:19 +00:00
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp: Option<RecordId> = db.run("fn::bar").args(7).await.unwrap();
|
|
|
|
assert_eq!(tmp, None);
|
2024-08-20 10:50:19 +00:00
|
|
|
|
2024-08-21 14:35:26 +00:00
|
|
|
let tmp: i32 = db.run("fn::baz").await.unwrap();
|
|
|
|
assert_eq!(tmp, 7);
|
2024-08-20 10:50:19 +00:00
|
|
|
}
|