Skip to content

Commit

Permalink
[backend] feat: User unit tests (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
helloitsdave authored Mar 17, 2024
1 parent 0cf7348 commit 0b2ec7c
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 76 deletions.
23 changes: 23 additions & 0 deletions backend/prisma/migrations/20240316221424_user_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- AlterTable
ALTER TABLE "Note" ADD COLUMN "user_id" INTEGER;

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"username" TEXT NOT NULL,
"password" TEXT NOT NULL,
"email" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Note" ADD CONSTRAINT "Note_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
21 changes: 21 additions & 0 deletions backend/prisma/migrations/20240316224317_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Warnings:
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- DropForeignKey
ALTER TABLE "Note" DROP CONSTRAINT "Note_user_id_fkey";

-- AlterTable
ALTER TABLE "Note" ALTER COLUMN "user_id" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "User" DROP CONSTRAINT "User_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "User_id_seq";

-- AddForeignKey
ALTER TABLE "Note" ADD CONSTRAINT "Note_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
12 changes: 12 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ model Note {
content String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("updated_at")
userID String? @map("user_id")
author User? @relation(fields: [userID], references: [id])
}

model User {
id String @id @default(uuid())
username String @unique
password String
email String? @unique
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @map("updated_at")
notes Note[]
}
23 changes: 22 additions & 1 deletion backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,80 @@ export const seed = [
"content": "Discussed project timelines and goals.",
"createdAt": "2024-02-05T23:33:42.252Z",
"updatedAt": "2024-02-05T23:33:42.252Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Shopping List",
"content": "Milk, eggs, bread, and fruits.",
"createdAt": "2024-02-05T23:33:42.253Z",
"updatedAt": "2024-02-05T23:33:42.253Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Recipe",
"content": "Ingredients: Chicken, tomatoes, onions, garlic.",
"createdAt": "2024-02-05T23:33:42.254Z",
"updatedAt": "2024-02-05T23:33:42.254Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Ideas",
"content": "Brainstorming ideas for the next feature release. 🚀",
"createdAt": "2024-02-05T23:33:42.255Z",
"updatedAt": "2024-02-05T23:33:42.255Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Personal Goals",
"content": "Exercise for 30 minutes daily. Read a book every week.",
"createdAt": "2024-02-05T23:33:42.256Z",
"updatedAt": "2024-02-05T23:33:42.256Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Fête d'anniversaire",
"content": "Préparer une surprise pour la fête d'anniversaire.",
"createdAt": "2024-02-05T23:33:42.257Z",
"updatedAt": "2024-02-05T23:33:42.257Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "日本旅行",
"content": "計画: 東京、京都、大阪を訪れる。",
"createdAt": "2024-02-05T23:33:42.258Z",
"updatedAt": "2024-02-05T23:33:42.258Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Семейный ужин",
"content": "Приготовить вкусный ужин для всей семьи.",
"createdAt": "2024-02-05T23:33:42.259Z",
"updatedAt": "2024-02-05T23:33:42.259Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
{
"title": "Coding Project",
"content": "Implement new features using React and Express.",
"createdAt": "2024-02-05T23:33:42.260Z",
"updatedAt": "2024-02-05T23:33:42.260Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
}
];

async function main() {
// Seed data here
// Seed user data
await prisma.user.create({
data: {
id : 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
email: '[email protected]',
password: 'n0te$App!23',
username: 'Test User',
createdAt: "2024-02-05T23:33:42.260Z",
updatedAt: "2024-02-05T23:33:42.260Z",
}
});

// Seed note data
await prisma.note.createMany({
data: seed,
});
Expand Down
32 changes: 32 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ app.put("/api/notes/:id", async (req, res) => {
}
});

app.get("/api/users", async (req, res) => {
try {
const users = await prisma.user.findMany();

const usersWithPasswordsRemoved = users.map((user) => {
delete user.password;
return user;
});
res.json(usersWithPasswordsRemoved);
} catch (error) {
res.status(500).send({ "error": "Oops, something went wrong"});
}
});

app.post("/api/users", async (req, res) => {
const { email, password, username } = req.body;

if (!email || !password || !username) {
return res.status(400).send({ "error": "email, password, and username fields required"});
}

try {
const user = await prisma.user.create({
data: { email, password, username },
});
delete user.password;
res.json(user);
} catch (error) {
res.status(500).send({ "error": "Oops, something went wrong"});
}
});

app.listen(PORT, () => {
console.log("server running on localhost", PORT);
});
Expand Down
1 change: 1 addition & 0 deletions backend/tests/service/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test("Get the list of Notes", async () => {
id: 1,
title: "Meeting Notes",
updatedAt: "2024-02-05T23:33:42.252Z",
userID: "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272",
});
});

Expand Down
Loading

1 comment on commit 0b2ec7c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   index.ts100%100%100%
   prisma.ts100%100%100%
src/__mocks__
   prisma.ts100%100%100%

Please sign in to comment.