Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed Oct 23, 2023
1 parent 83de80b commit ba98543
Showing 1 changed file with 146 additions and 137 deletions.
283 changes: 146 additions & 137 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,140 +1,149 @@
import test from 'ava'

import { Surreal } from '../index.js'

test('Connect in-memory SurrealDB instance', async t => {
const db = new Surreal();
await db.connect("memory");
t.pass();
})

test('set ns/db', async t => {
{
const db = new Surreal();
await db.connect("memory");
await db.use({ 'namespace': 'test' })
}

{
const db = new Surreal();
await db.connect("memory");
await db.use({ 'database': 'test' })
}

{
const db = new Surreal();
await db.connect("memory");
await db.use({ 'namespace': 'test', 'database': 'test' })
}

t.pass();
})

test('test query method', async t => {
const db = new Surreal();
await db.connect("memory");
await db.use({ 'namespace': 'test', 'database': 'test' })

{
const [res] = await db.query('SELECT * FROM person');
t.deepEqual(res, []);
}


{
const [res] = await db.query('CREATE |foo:100|');
t.is(res.length, 100);
}

{
const data = { 'first_name': 'Tobie', 'projects': ['SurrealDB'] };
const [res] = await db.query('CREATE person:tobie content $data', { 'data': data });
data.id = 'person:tobie';
t.deepEqual(res, [data]);
}

})

test('set and and unset', async t => {
const db = new Surreal();
await db.connect("memory");
await db.use({ 'namespace': 'test', 'database': 'test' });

const data = { 'first': 'Tobie', 'last': 'Morgan Hitchcock' };

await db.set('name', data)
{
const [res] = await db.query("RETURN $name");
t.deepEqual(res, [data]);
}

await db.unset('name');

{
const [res] = await db.query("RETURN $name");
t.deepEqual(res, []);
}

})

test.failing('auth', async t => {
const db = new Surreal();
await db.connect("memory");
// await db.connect("ws://127.0.0.1:8000");
await db.use({ 'namespace': 'test', 'database': 'test' });

await db.signin({ username: 'root', password: 'root' })

const scope = await db.query(`DEFINE SCOPE user_scope SESSION 5s
SIGNUP (CREATE user SET email = $email, pass = crypto::argon2::generate($pass))
SIGNIN (SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass))
`);
console.log(scope);


const token = await db.signup({
namespace: 'namespace',
database: 'database',
scope: 'user_scope',
email: '[email protected]',
password: 'password123'
});
console.log(token);

// // const token2 = await db_ws.signin({
// // namespace: 'namespace',
// // database: 'database',
// // scope: 'user_scope',
// // email: '[email protected]',
// // password: 'password123'
// // });
// // console.log(token2);

t.pass()
})

test('test select method', async t => {
const db = new Surreal();
await db.connect("memory");
await db.use({ 'namespace': 'test', 'database': 'test' });

const jason = { 'id': 'person:jason' };
const john = { 'id': 'person:john' };
const jaime = { 'id': 'person:jaime' };
const people = [jaime, jason, john];

await db.create(jason.id);
await db.create(john.id);
await db.create(jaime.id);

{
const res = await db.select('person');
t.deepEqual(new Set(res), new Set(people));
const person = await db.select('person:jason');
t.deepEqual(person, [jason]);
}

})
import test from "ava";

import { Surreal } from "../index.js";

test("Connect in-memory SurrealDB instance", async (t) => {
const db = new Surreal();
await db.connect("memory");
t.pass();
});

test("set ns/db", async (t) => {
{
const db = new Surreal();
await db.connect("memory");
await db.use({ namespace: "test" });
}

{
const db = new Surreal();
await db.connect("memory");
await db.use({ database: "test" });
}

{
const db = new Surreal();
await db.connect("memory");
await db.use({ namespace: "test", database: "test" });
}

t.pass();
});

test("test query method", async (t) => {
const db = new Surreal();
await db.connect("memory");
await db.use({ namespace: "test", database: "test" });

{
const [res] = await db.query("SELECT * FROM person");
t.deepEqual(res, []);
}

{
const [res] = await db.query("CREATE |foo:100|");
t.is(res.length, 100);
}

{
const data = { first_name: "Tobie", projects: ["SurrealDB"] };
const [res] = await db.query("CREATE person:tobie content $data", {
data: data,
});
data.id = "person:tobie";
t.deepEqual(res, [data]);
}
});

test("set and and unset", async (t) => {
const db = new Surreal();
await db.connect("memory");
await db.use({ namespace: "test", database: "test" });

const data = { first: "Tobie", last: "Morgan Hitchcock" };

await db.set("name", data);
{
const [res] = await db.query("RETURN $name");
t.deepEqual(res, [data]);
}

await db.unset("name");

{
const [res] = await db.query("RETURN $name");
t.deepEqual(res, []);
}
});

test("auth", async (t) => {
const db = new Surreal();
await db.connect("memory");
await db.use({
namespace: "test",
database: "test"
});

const scope = /* surql */ `DEFINE SCOPE user SESSION 5s SIGNUP (CREATE type::thing('user', $username) SET email = $email, pass = crypto::argon2::generate($pass)) SIGNIN (SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass))`;
const [_, [{ scopes }]] = await db.query(/* surql */ `
${scope};
INFO FOR DB;
`);
t.is(scopes.user, scope);

{
const token = await db.signup({
namespace: "test",
database: "test",
scope: "user",

username: 'john',
email: "[email protected]",
pass: "password123",
});
t.is(typeof token, 'string');

const [[user_id]] = await db.query(/* surql */ `$auth`);
t.is(user_id, 'user:john');
}

{
const token = await db.signin({
namespace: "test",
database: "test",
scope: "user",

email: "[email protected]",
pass: "password123",
});
t.is(typeof token, 'string');

const [[user_id]] = await db.query(/* surql */ `$auth`);
t.is(user_id, 'user:john');
}
});

test("test select method", async (t) => {
const db = new Surreal();
await db.connect("memory");
await db.use({ namespace: "test", database: "test" });

const jason = { id: "person:jason" };
const john = { id: "person:john" };
const jaime = { id: "person:jaime" };
const people = [jaime, jason, john];

await db.create(jason.id);
await db.create(john.id);
await db.create(jaime.id);

{
const res = await db.select("person");
t.deepEqual(new Set(res), new Set(people));
const person = await db.select("person:jason");
t.deepEqual(person, [jason]);
}
});

// test('examples', async t => {

Expand Down

0 comments on commit ba98543

Please sign in to comment.