Skip to content

Commit

Permalink
feature: add user database tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeSchlangen authored Mar 15, 2023
1 parent dd84d90 commit 17d2c03
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 53 deletions.
40 changes: 25 additions & 15 deletions src/lib/__test__/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ import {Firestore} from "@google-cloud/firestore";
import {Database} from "../database";
import {User} from "../../models/User";

const directDatabaseConnectionForTestReset = new Firestore({
projectId: "birds-of-paradise",
// keyFilename: '/path/to/keyfile.json',
});

describe("database tests", () => {
let fs: Database;

beforeAll(async () => {
await directDatabaseConnectionForTestReset.collection('users').doc('Bob').delete()
fs = new Database();
})

it.skip("should pass (just a placeholder)", () => {
expect(true).toBeTruthy();
});
it("should add and get a user", async () => {
await fs.setUser({userId: 'Bob'});
const user = await fs.getUser({userId: 'Bob'});

it("should add a doc", async () => {
expect(user).toEqual({ userId: "Bob", completedMissions: [] });
});

const docRef = fs.db.collection('users').doc("bob");
it("should add completed missions", async () => {
await fs.setUser({userId: 'Bob'});
await fs.addCompletedMission({userId: 'Bob', missionId: 'Mission0001aweifjwek'});
const firstUserResponse = await fs.getUser({userId: 'Bob'});

await docRef.set({
userId: "1",
email: "bob@example.com",
expect(firstUserResponse).toEqual({
"userId": "Bob",
completedMissions: ['Mission0001aweifjwek']
});

const snapshot = await fs.db.collection('users').get();
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
});
await fs.addCompletedMission({userId: 'Bob', missionId: 'Mission0002aweifjwek'});
const secondUserResponse = await fs.getUser({userId: 'Bob'});

expect(snapshot.size).toEqual(1);
await fs.db.collection("users").doc("bob").delete()
expect(secondUserResponse).toEqual({
"userId": "Bob",
completedMissions: ['Mission0001aweifjwek', 'Mission0002aweifjwek']
});
});
});
});
33 changes: 31 additions & 2 deletions src/lib/database.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import {Firestore} from "@google-cloud/firestore";
import { User } from "src/models/User";

export class Database {
db: Firestore;
private db: Firestore;

constructor() {
this.db = new Firestore({
projectId: "birds-of-paradise",
// keyFilename: '/path/to/keyfile.json',
});
}
}


async setUser({userId} :any): Promise<any> {
const userDoc = this.db.collection('users').doc(userId);

return userDoc.set({
userId,
}, {merge: true});
}

async getUser({userId}: any): Promise<User> {
const userDoc = this.db.collection('users').doc(userId);
const snapshot = await userDoc.get();
const completedMissions = snapshot.data()?.completedMissions || [];

return { userId, completedMissions }
}

async addCompletedMission({userId, missionId} :any): Promise<any> {
const userDoc = this.db.collection('users').doc(userId);
const { completedMissions } = await this.getUser({userId});
const updatedMissions = [ ...completedMissions, missionId ]


return userDoc.set({
completedMissions: updatedMissions,
}, {merge: true});
}
}
4 changes: 4 additions & 0 deletions src/models/LearningResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class LearningResource {
title!: string;
link!: string;
}
11 changes: 11 additions & 0 deletions src/models/Mission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LearningResource } from "./LearningResource";

export class Mission {
id!: string;
title!: string;
technologies!: string[];
learningResources!: LearningResource[];

// status is not stored in the database, it is calculated from the user object
status!: string;
}
11 changes: 0 additions & 11 deletions src/models/ProjectResult.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/models/ResourceCategory.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/models/ResourceType.ts

This file was deleted.

17 changes: 3 additions & 14 deletions src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
// TODO
export type Email = string;
export type Timestamp = number;
export type SessionId = string;
export type SessionResult = object | SessionId;

export class User {
userId?: string;

email?: Email;

results?: SessionResult[];

lastSession?: Timestamp;
}
userId!: string;
completedMissions!: string[];
}

0 comments on commit 17d2c03

Please sign in to comment.