1
0
Fork 0

orm: fix pluck, add basic tests

This commit is contained in:
Cadence Ember 2023-09-19 01:23:52 +12:00
parent a8f387c161
commit d81aa51787
3 changed files with 36 additions and 1 deletions

View file

@ -32,6 +32,7 @@ class From {
this.sql = ""
this.cols = []
this.using = []
this.isPluck = false
}
/**
@ -69,6 +70,7 @@ class From {
const r = this
r.constructor = Pluck
r.cols = [col]
r.isPluck = true
return r
}
@ -89,7 +91,8 @@ class From {
}
sql += this.sql
/** @type {U.Prepared<Pick<U.Merge<U.Models[Table]>, Col>>} */
const prepared = db.prepare(sql)
let prepared = db.prepare(sql)
if (this.isPluck) prepared = prepared.pluck()
return prepared
}

31
db/orm.test.js Normal file
View file

@ -0,0 +1,31 @@
// @ts-check
const {test} = require("supertape")
const data = require("../test/data")
const {db, select, from} = require("../passthrough")
test("orm: select: get works", t => {
const row = select("guild_space", "guild_id", "WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
t.equal(row?.guild_id, data.guild.general.id)
})
test("orm: from: get works", t => {
const row = from("guild_space").select("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
t.equal(row?.guild_id, data.guild.general.id)
})
test("orm: select: get pluck works", t => {
const guildID = select("guild_space", "guild_id", "WHERE space_id = ?").pluck().get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
t.equal(guildID, data.guild.general.id)
})
test("orm: from: get pluck works", t => {
const guildID = from("guild_space").pluck("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
t.equal(guildID, data.guild.general.id)
})
test("orm: from: join and pluck works", t => {
const mxid = from("sim").join("sim_member", "mxid").and("WHERE discord_id = ? AND room_id = ?").pluck("mxid").get("771520384671416320", "!uCtjHhfGlYbVnPVlkG:cadence.moe")
t.equal(mxid, "@_ooye_bojack_horseman:cadence.moe")
})

View file

@ -25,6 +25,7 @@ passthrough.select = orm.select
const file = sync.require("../matrix/file")
file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not allowed to upload files during testing.\nURL: ${url}`) }
require("../db/orm.test")
require("../matrix/kstate.test")
require("../matrix/api.test")
require("../matrix/read-registration.test")