-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,53 @@ | ||
const test = require("node:test") | ||
const assert = require("node:assert") | ||
const userDb = require("../UserSqliteDb.js") | ||
const UserDb = require("../UserSqliteDb.js") | ||
const User = require("../user.js") | ||
|
||
test("test initialize sqlite db", async () => { | ||
// db.serialize() | ||
// const tables = await db.getTables() | ||
// assert.deepStrictEqual(tables, ["users"]) | ||
const usersDb = new UserDb(":memory:") | ||
const tables = await usersDb._getTableNames() | ||
assert.deepStrictEqual(tables, ["users"]) | ||
}) | ||
|
||
test("test insert user", async () => { | ||
// const user = new User("u001", "TestExperiment") | ||
// await db.saveUser(user) | ||
// const rows = await db.findAll() | ||
// const firstUser = rows[0] | ||
// const u = JSON.parse(firstUser.jsonObj) | ||
// assert.strictEqual("TestExperiment", u.experimentId) | ||
const usersDb = new UserDb(":memory:") | ||
const user = new User("u001", "TestExperiment") | ||
await usersDb.save(user) | ||
const rows = await usersDb.findAll() | ||
const firstUser = rows[0] | ||
const u = JSON.parse(firstUser.jsonObj) | ||
assert.strictEqual("TestExperiment", u.experimentId) | ||
}) | ||
|
||
test("test upsert user", async () => { | ||
const usersDb = new UserDb(":memory:") | ||
const user = new User("u001", "TestExperiment") | ||
await usersDb.save(user) | ||
let rows = await usersDb.findAll() | ||
let firstUser = rows[0] | ||
let u = JSON.parse(firstUser.jsonObj) | ||
assert.strictEqual("TestExperiment", u.experimentId) | ||
|
||
user.redirectedUrl = "http://127.0.0.1/etgohome" | ||
await usersDb.upsert(user) | ||
rows = await usersDb.findAll() | ||
firstUser = rows[0] | ||
u = JSON.parse(firstUser.jsonObj) | ||
assert.strictEqual("u001", u.userId) | ||
assert.strictEqual("http://127.0.0.1/etgohome", u.redirectedUrl) | ||
|
||
user.experimentId = "AnotherExperiment" | ||
await usersDb.upsert(user) | ||
rows = await usersDb.findAll() | ||
firstUser = rows[0] | ||
u = JSON.parse(firstUser.jsonObj) | ||
assert.strictEqual("u001", u.userId) | ||
assert.notEqual("AnotherExperiment", u.experimentId) | ||
|
||
assert.strictEqual(2, rows.length) | ||
|
||
let secondUser = rows[1] | ||
let u2 = JSON.parse(secondUser.jsonObj) | ||
assert.strictEqual("u001", u2.userId) | ||
assert.strictEqual("AnotherExperiment", u2.experimentId) | ||
}) |