Skip to content
Extraits de code Groupes Projets
Valider 0bcbe453 rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Add DbDataEmoji

parent a61d53b8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import { DiscordStore } from "../store";
import { IDbData } from "./dbdatainterface";
import * as log from "npmlog";
export class DbGuildEmoji implements IDbData {
public EmojiId: string;
public GuildId: string;
public Name: string;
public MxcUrl: string;
public CreatedAt: number;
public UpdatedAt: number;
public Result: boolean;
public RunQuery(store: DiscordStore, params: any): Promise<null> {
log.silly("DiscordStore", "_get_schema_version");
return store.db.getAsync(`
SELECT *
FROM guild_emoji
WHERE emoji_id = $id`, {
$id: params.emoji_id,
}).then((row) => {
this.Result = row != undefined;
if(this.Result) {
this.EmojiId = row.emoji_id;
this.GuildId = row.guild_id;
this.Name = row.name;
this.MxcUrl = row.mxc_url;
this.CreatedAt = row.created_at;
this.UpdatedAt = row.updated_at;
}
});
}
public Insert(store: DiscordStore): Promise<null> {
this.CreatedAt = new Date().getTime();
this.UpdatedAt = this.CreatedAt;
return store.db.runAsync(`
INSERT INTO guild_emoji
(emoji_id,guild_id,name,mxc_url,created_at,updated_at)
VALUES ($emoji_id,$guild_id,$name,$mxc_url,$created_at,$updated_at);`, {
$emoji_id: this.EmojiId,
$guild_id: this.GuildId,
$name: this.Name,
$mxc_url: this.MxcUrl,
$created_at: this.CreatedAt,
$updated_at: this.UpdatedAt,
});
}
public Update(store: DiscordStore) {
// Ensure this has incremented by 1 for Insert+Update operations.
this.UpdatedAt = new Date().getTime()+1;
return store.db.runAsync(`
UPDATE guild_emoji
SET name = $name,
mxc_url = $mxc_url,
updated_at = $updated_at
WHERE
emoji_id = $emoji_id
AND guild_id = $guild_id`, {
$emoji_id: this.EmojiId,
$guild_id: this.GuildId,
$name: this.Name,
$mxc_url: this.MxcUrl,
$updated_at: this.UpdatedAt,
});
}
}
......@@ -2,7 +2,7 @@ import { DiscordStore } from "../store";
export interface IDbData {
Result: boolean;
RunQuery(store: DiscordStore, params: any);
Insert(store: DiscordStore);
Update(store: DiscordStore);
RunQuery(store: DiscordStore, params: any): Promise<null>;
Insert(store: DiscordStore): Promise<null>;
Update(store: DiscordStore): Promise<null>;
}
......@@ -227,21 +227,22 @@ export class DiscordStore {
});
}
public Get<T extends IDbData>(dbType: {new(): T; }, params: any): T {
public Get<T extends IDbData>(dbType: {new(): T; }, params: any): Promise<T> {
const dType = new dbType();
log.silly("DiscordStore", `get <${dType.constructor.name}>`);
dType.RunQuery(this, params);
return dType.RunQuery(this, params).then(() => {
return dType;
});
}
public Insert<T extends IDbData>(data: T) {
public Insert<T extends IDbData>(data: T): Promise<null> {
log.silly("DiscordStore", `insert <${data.constructor.name}>`);
data.Insert(this);
return data.Insert(this);
}
public Update<T extends IDbData>(data: T) {
public Update<T extends IDbData>(data: T): Promise<null> {
log.silly("DiscordStore", `insert <${data.constructor.name}>`);
data.Update(this);
return data.Update(this);
}
private getSchemaVersion ( ): Promise<number> {
......
......@@ -3,6 +3,7 @@ import * as ChaiAsPromised from "chai-as-promised";
// import * as Proxyquire from "proxyquire";
import { DiscordStore } from "../src/store";
import * as log from "npmlog";
import { DbGuildEmoji } from "../src/db/dbdataemoji";
Chai.use(ChaiAsPromised);
const expect = Chai.expect;
......@@ -33,12 +34,55 @@ describe("DiscordStore", () => {
})).to.eventually.be.fulfilled;
});
});
describe("add_user_token", () => {
it("should not throw when adding an entry", () => {
describe("Get|Insert|Update<DbGuildEmoji>", () => {
it("should insert successfully", () => {
const store = new DiscordStore(":memory:");
return expect(store.init().then(() => {
return store.add_user_token("userid", "token", "discordid");
const emoji = new DbGuildEmoji();
emoji.EmojiId = "123";
emoji.GuildId = "456";
emoji.Name = "TestEmoji";
emoji.MxcUrl = "TestUrl";
return store.Insert(emoji);
})).to.eventually.be.fulfilled;
});
it("should get successfully", async function() {
const store = new DiscordStore(":memory:");
await store.init();
const insert_emoji = new DbGuildEmoji();
insert_emoji.EmojiId = "123";
insert_emoji.GuildId = "456";
insert_emoji.Name = "TestEmoji";
insert_emoji.MxcUrl = "TestUrl";
await store.Insert(insert_emoji);
const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"});
Chai.assert.equal(get_emoji.Name, "TestEmoji");
Chai.assert.equal(get_emoji.MxcUrl, "TestUrl");
});
it("should not return nonexistant emoji", async function() {
const store = new DiscordStore(":memory:");
await store.init();
const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"});
Chai.assert.isFalse(get_emoji.Result);
});
it("should update successfully", async function() {
const store = new DiscordStore(":memory:");
await store.init();
const insert_emoji = new DbGuildEmoji();
insert_emoji.EmojiId = "123";
insert_emoji.GuildId = "456";
insert_emoji.Name = "TestEmoji";
insert_emoji.MxcUrl = "TestUrl";
await store.Insert(insert_emoji);
insert_emoji.EmojiId = "123";
insert_emoji.GuildId = "456";
insert_emoji.Name = "TestEmoji2";
insert_emoji.MxcUrl = "NewURL";
await store.Update(insert_emoji);
const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"});
Chai.assert.equal(get_emoji.Name, "TestEmoji2");
Chai.assert.equal(get_emoji.MxcUrl, "NewURL");
Chai.assert.notEqual(get_emoji.CreatedAt,get_emoji.UpdatedAt);
});
});
});
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter