-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fd855e3
Showing
13 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}); |