Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[50기 최현수 - ADD : Api/수입지출여부목록조회 리뷰 반영 및 testcode] #4

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TYPEORM_CONNECTION=mysql
TYPEORM_HOST=127.0.0.1
TYPEORM_USERNAME=root
TYPEORM_PASSWORD=1234
TYPEORM_DATABASE=testwonbook
TYPEORM_PORT=3306
TYPEORM_SERVER_PORT=8000
SECRET_KEY=FINPONG
24 changes: 24 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const cors = require('cors');
const morgan = require('morgan')
const routes = require('./src/routes');

const createApp = () => {
const app = express();

app.use(cors())
app.use(express.json())
app.use(morgan('combined'))
app.use(routes)

app.get('/ping',(req,res) => {
res.status(200).json({
message: 'pong'
})
})
return app
}

module.exports = {
createApp
}
27 changes: 27 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const http = require('http');
const dotenv = require('dotenv');

dotenv.config()

const { createApp } = require('./app');
const { appDataSource } = require('./src/utils/dataSource');

const startServer = async () => {
const app = createApp();
appDataSource.initialize()
.then(()=> {
console.log('Data Source has been initialized!')
})
.catch((err) => {
console.error('Error occured during Data Source initialization', err)
})

const server = http.createServer(app)
const PORT = process.env.TYPEORM_SERVER_PORT;

server.listen(PORT, () => {
console.log(`Server is listening on Port ${PORT}`)
})
};

startServer();
15 changes: 15 additions & 0 deletions src/controllers/flowTypeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const flowTypeService = require('../services/flowTypeService');

const getFlowTypes = async (req, res) => {
try {
const flowTypes = await flowTypeService.getFlowTypes();
return res.status(200).json({message: 'GET_SUCCESS', types: flowTypes})
} catch (err) {
console.error(err);
return res.status(err.statusCode || 500).json({message: err.message || 'INTERNAL_SERVER_ERROR'});
}
}

module.exports = {
getFlowTypes
}
14 changes: 14 additions & 0 deletions src/models/flowTypeDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { appDataSource } = require('../utils/dataSource');

const getFlowTypes = async () => {
return await appDataSource.query(
`
SELECT id, status as 'option'
FROM flow_type
`
)
}

module.exports = {
getFlowTypes
}
7 changes: 7 additions & 0 deletions src/routes/flowTypeRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const express = require("express");
const router = express.Router();
const flowTypeController = require("../controllers/flowTypeController");

router.get('/', flowTypeController.getFlowTypes);

module.exports.router = router;
8 changes: 8 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();

const flowTypeRouter = require('./flowTypeRouter');

router.use('/flow-type', flowTypeRouter.router);

module.exports = router;
14 changes: 14 additions & 0 deletions src/services/flowTypeService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const flowTypeDao = require('../models/flowTypeDao');
const error = require('../utils/error');

const getFlowTypes = async () => {
const flowTypes = await flowTypeDao.getFlowTypes();
if (flowTypes.length === 0) {
error.throwErr(404, 'NOT_FOUND_TYPE');
}
return flowTypes;
}

module.exports = {
getFlowTypes
}
14 changes: 14 additions & 0 deletions src/utils/dataSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {DataSource} = require('typeorm');
const dotenv = require('dotenv');
dotenv.config();

const appDataSource = new DataSource({
type : process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE
});

module.exports = { appDataSource }
9 changes: 9 additions & 0 deletions src/utils/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const throwErr = (code, message) => {
const error = new Error(message);
error.statusCode = code;
throw error;
};

module.exports = {
throwErr
}
76 changes: 76 additions & 0 deletions test/flowType.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const request = require('supertest');
const { createApp } = require('../app');
const { appDataSource } = require('../src/utils/dataSource');
const supplies = require('./testSupplies.js');

describe('get ping', () => {
let app;

beforeAll(async() => {
app = createApp();
await appDataSource.initialize();
for (let i=0; i<supplies.startQuery.length; i++){
await appDataSource.query(supplies.startQuery[i])
}
});

afterEach(async() => {
for (let i=0; i<supplies.truncate.length; i++){
await appDataSource.query(supplies.truncate[i])
}
await appDataSource.destroy();
});

test('SUCCESS : get pong', async () => {
const res = await request(app)
.get('/ping');

expect(res.status).toBe(200);
expect(res.body).toEqual({
message : 'pong'
});
});
});

// 수입지출여부목록 조회 성공
describe('get FlowTypes', () => {
let app;

beforeAll(async() => {
app = createApp()
await appDataSource.initialize();
for (let i=0; i<supplies.startQuery.length; i++){
await appDataSource.query(supplies.startQuery[i])
}
});
afterEach(async() => {
for (let i=0; i<supplies.truncate.length; i++){
await appDataSource.query(supplies.truncate[i])
}

await appDataSource.destroy();
});

test('SUCCESS: get FlowTypes', async() => {
const res = await request(app)
.get('/flow-type')
.set('Authorization', `Bearer ${supplies.token}`)
.send();

expect(res.status).toBe(200)
expect(res.body)
.toEqual({
message: "GET_SUCCESS",
types: [
{
id: 1,
option: "수입"
},
{
id: 2,
option: "지출"
}
]
});
});
});
Loading