Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Aug 22, 2024
0 parents commit fd855e3
Show file tree
Hide file tree
Showing 13 changed files with 599 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: ci
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup FluentCI
uses: fluentci-io/setup-fluentci@v5
- name: deno test
run: |
fluentci run --wasm postgres start
fluentci run --wasm deno task test
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NIX_CONFIG: "extra-access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}"
POSTGRES_USER: postgres
POSTGRES_DB: demo
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Tsiry Sandratraina <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# FluentCI CI/CD demo for Deno

[![ci](https://github.com/fluentci-demos/fluentci-demo-deno/actions/workflows/ci.yml/badge.svg)](https://github.com/fluentci-demos/fluentci-demo-deno/actions/workflows/ci.yml)

This is an example application and CI/CD pipeline showing how to build, test a [Deno](https:/deno.com) application using FluentCI.

21 changes: 21 additions & 0 deletions database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Client } from "./deps.ts";

const client = new Client({
user: Deno.env.get("POSTGRES_USER") || "postgres",
password: Deno.env.get("POSTGRES_PASSWORD"),
database: Deno.env.get("POSTGRES_DB") || "denoapi_test",
hostname: "localhost",
port: 5432,
});

await client.connect();

await client.queryArray(`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
`);

export default client;
10 changes: 10 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tasks": {
"dev": "deno run --allow-net --allow-read --allow-write --allow-env --watch main.ts",
"start": "deno run --allow-net --allow-read --allow-write --allow-env main.ts",
"test": "deno test --allow-net --allow-read --allow-write --allow-env"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}
405 changes: 405 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { Client } from "https://deno.land/x/[email protected]/mod.ts";
export {
Application,
Router,
Context,
} from "https://deno.land/x/[email protected]/mod.ts";
export { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
export { superoak } from "https://deno.land/x/[email protected]/mod.ts";
13 changes: 13 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Application } from "./deps.ts";
import router from "./src/routes/user.ts";

export const app = new Application();

// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
app.use(router.routes());
app.use(router.allowedMethods());

console.log("Server running on http://localhost:8000");
await app.listen({ port: 8000 });
}
14 changes: 14 additions & 0 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Context } from "../../deps.ts";
import { createUser, getAllUsers } from "../models/user.ts";

export const getUsers = async (ctx: Context) => {
const users = await getAllUsers();
ctx.response.body = { users };
};

export const addUser = async (ctx: Context) => {
const { name, email } = await ctx.request.body.json();
const user = await createUser(name, email);
ctx.response.status = 201;
ctx.response.body = { user };
};
18 changes: 18 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import client from "../../database.ts";

export interface User {
id: number;
name: string;
email: string;
}

export async function createUser(name: string, email: string): Promise<User> {
const result =
await client.queryObject<User>`INSERT INTO users (name, email) VALUES (${name}, ${email}) RETURNING id, name, email`;
return result.rows[0];
}

export async function getAllUsers(): Promise<User[]> {
const result = await client.queryObject<User>`SELECT * FROM users`;
return result.rows;
}
12 changes: 12 additions & 0 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "../../deps.ts";
import { getUsers, addUser } from "../controllers/user.ts";

const router = new Router();

// Define the route for getting all users
router.get("/users", getUsers);

// Define the route for adding a new user
router.post("/users", addUser);

export default router;
33 changes: 33 additions & 0 deletions tests/e2e/userRoutes_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { app } from "../../main.ts";
import { assertEquals, superoak } from "../../deps.ts";
import client from "../../database.ts";
import router from "../../src/routes/user.ts";

app.use(router.routes());
app.use(router.allowedMethods());

Deno.test("Setup: Clean up users table before each test", async () => {
await client.queryArray("TRUNCATE TABLE users RESTART IDENTITY CASCADE");
});

Deno.test("GET /users returns list of users", async () => {
const request = await superoak(app);
await request
.get("/users")
.expect(200)
.expect((res) => {
assertEquals(Array.isArray(res.body.users), true);
});
});

Deno.test("POST /users adds a new user", async () => {
const request = await superoak(app);
await request
.post("/users")
.send({ name: "New User", email: "[email protected]" })
.expect(201)
.expect((res) => {
assertEquals(res.body.user.name, "New User");
assertEquals(res.body.user.email, "[email protected]");
});
});
19 changes: 19 additions & 0 deletions tests/unit/controllers/user_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import client from "../../../database.ts";
import { assertEquals } from "../../../deps.ts";
import { createUser, getAllUsers } from "../../../src/models/user.ts";

Deno.test("Setup: Clean up users table before each test", async () => {
await client.queryArray("TRUNCATE TABLE users RESTART IDENTITY CASCADE");
});

Deno.test("createUser adds a new user", async () => {
const user = await createUser("John Doe", "[email protected]");
assertEquals(user.name, "John Doe");
assertEquals(user.email, "[email protected]");
});

Deno.test("getAllUsers retrieves all users", async () => {
await createUser("Jane Doe", "[email protected]");
const users = await getAllUsers();
assertEquals(users.length, 2); // Adjust this based on the number of test cases.
});

0 comments on commit fd855e3

Please sign in to comment.