Skip to content

Commit

Permalink
Merge pull request #29 from wearefuturegov/feature/tests-and-node-ver…
Browse files Browse the repository at this point in the history
…sion

Feature/tests and node version
  • Loading branch information
apricot13 authored May 1, 2024
2 parents 033fa0d + 9bc5db8 commit 28454db
Show file tree
Hide file tree
Showing 45 changed files with 4,671 additions and 2,266 deletions.
5 changes: 3 additions & 2 deletions .docker/services/mongo/mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ version: "3.7"
services:
mongo:
image: mongo:6
container_name: mongo
ports:
- 27018:27017
volumes:
- mongo-volume:/data/db
- ./setup-mongodb.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-outpost}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-admin}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
MONGO_INITDB_USERNAME: ${MONGO_INITDB_USERNAME:-outpost}
MONGO_INITDB_PASSWORD: ${MONGO_INITDB_PASSWORD:-password}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-outpost_api_development}
networks:
- internal_network
Expand Down
16 changes: 8 additions & 8 deletions .docker/services/mongo/setup-mongodb.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
db = db.getSiblingDB(
process.env.MONGO_INITDB_DATABASE || "outpost_api_development"
);
)

db.createUser({
user: process.env.MONGO_INITDB_ROOT_USERNAME || "outpost",
pwd: process.env.MONGO_INITDB_ROOT_PASSWORD || "password",
user: process.env.MONGO_INITDB_USERNAME || "outpost",
pwd: process.env.MONGO_INITDB_PASSWORD || "password",
roles: [
{
role: "readWrite",
db: process.env.MONGO_INITDB_DATABASE || "outpost_api_development",
},
],
});
})

db.createCollection("indexed_services");
db.createCollection("indexed_services")

db.indexed_services.createIndex(
{
Expand All @@ -26,10 +26,10 @@ db.indexed_services.createIndex(
description: 1,
},
}
);
)
db.indexed_services.createIndex({
"locations.geometry": "2dsphere",
});
})
db.indexed_services.createIndex({
"taxonomies.slug": 1,
});
})
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ yarn-error.log*
.idea
.vscode
.ruby-lsp
coverage

# heroku
Procfile
17 changes: 0 additions & 17 deletions .github/workflows/ci.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run tests
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v4
with:
node-version: lts/Iron

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm run test

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: wearefuturegov/outpost-api-service
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.env
/environment/artifacts
.DS_Store
.DS_Store
coverage
logs
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.18.0
lts/Iron
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG NODE_ENV=development

# ----------------------------------------------------------------
FROM node:16-alpine3.17 as build_frontend
FROM node:iron-alpine as build_frontend
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG NODE_ENV=production
ARG FORCE_SSL=true

# ----------------------------------------------------------------
FROM node:16-alpine3.17 as build_frontend
FROM node:iron-alpine as build_frontend
ARG NODE_ENV
ARG FORCE_SSL
ENV NODE_ENV $NODE_ENV
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO

- [ ] Automate tests with docker and github actions
- [x] Automate tests with docker and github actions
- [ ] Automatic linter on commit and PR's
- [x] An option to push some dummy data to the API
- [ ] Add support for csv export https://developers.openreferraluk.org/API-Guidance/
64 changes: 64 additions & 0 deletions __tests__/mocks/index-geocode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"results": [
{
"address_components": [
{
"long_name": "London",
"short_name": "London",
"types": ["locality", "political"]
},
{
"long_name": "London",
"short_name": "London",
"types": ["postal_town"]
},
{
"long_name": "Greater London",
"short_name": "Greater London",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "England",
"short_name": "England",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "United Kingdom",
"short_name": "GB",
"types": ["country", "political"]
}
],
"formatted_address": "London, UK",
"geometry": {
"bounds": {
"northeast": {
"lat": 51.6723432,
"lng": 0.148271
},
"southwest": {
"lat": 51.38494009999999,
"lng": -0.3514683
}
},
"location": {
"lat": 51.5072178,
"lng": -0.1275862
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 51.6723432,
"lng": 0.148271
},
"southwest": {
"lat": 51.38494009999999,
"lng": -0.3514683
}
}
},
"place_id": "ChIJdd4hrwug2EcRmSrV3Vo6llI",
"types": ["locality", "political"]
}
],
"status": "OK"
}
47 changes: 47 additions & 0 deletions __tests__/unit/db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { MongoClient } = require("mongodb")
const { connect, db } = require("./../../src/db")
const logger = require("./../../utils/logger")

jest.mock("mongodb")
jest.mock("./../../utils/logger")

describe("db", () => {
describe("connect", () => {
it.todo("should connect to the database and call the callback with the db")
// it("should connect to the database and call the callback with the db", async () => {
// const mockDb = { databaseName: "outpost_api_development" }
// const mockClient = { db: jest.fn().mockReturnValue(mockDb) }
// MongoClient.connect.mockResolvedValue(mockClient)

// const cb = jest.fn()
// await connect(cb)

// expect(MongoClient.connect).toHaveBeenCalledWith(process.env.DB_URI)
// expect(mockClient.db).toHaveBeenCalled()
// expect(cb).toHaveBeenCalledWith(mockDb)
// })

it.todo("should log an error if the connection fails")
// it("should log an error if the connection fails", async () => {
// const error = new Error("Connection failed")
// MongoClient.connect.mockRejectedValue(error)

// const cb = jest.fn()
// await connect(cb)

// expect(logger.error).toHaveBeenCalledWith(error)
// })

it.todo("should warn you if it connects but the database is 'test'")
})

describe("db", () => {
it("should throw an error if not connected", () => {
expect(() => db()).toThrow(
"db() called without being connected to the database. Please connect first, see application logs for more details."
)
})

it.todo("should return the db if connected")
})
})
Loading

0 comments on commit 28454db

Please sign in to comment.