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

Practice/005001 answer #1

Open
wants to merge 15 commits into
base: practice/005001_base
Choose a base branch
from
24 changes: 5 additions & 19 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,16 @@ pipeline {
}

}
stages {
stage('run install') {
stages {
stage('wellcome') {
steps {
sh 'npm install'
}
}
stage('run migration') {
steps {
sh 'node_modules/.bin/sequelize db:migrate'
}
}
stage('run test') {
steps {
sh 'npm run test-jenkins'
}
}
stage('test report') {
steps {
junit(testResults: 'jenkins-test-results.xml', allowEmptyResults: true)
sh 'echo "wellcome to dojo !"'
}
}
}
environment {
npm_config_cache = 'npm-cache'
HOME = '.'

}
}
}
7 changes: 7 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = {
username: "user no name",
log: {
host: "172.20.10.2",
port: "5000",
tag_prefix: "cargocms.pos"

},
development: {
dialect: "sqlite",
storage: "./db.development.sqlite"
Expand Down
12 changes: 12 additions & 0 deletions dojo/dojo1_fin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var FluentLogHelper = require('fluent-log-helper');

var config = require('./../config/config');

var logConfig = config.log;
var fluentLogHelper = new FluentLogHelper(logConfig);

console.log("config.username ==>", config.username);
fluentLogHelper.log({
task: "Dojo 1: TDD",
username: config.username
});
12 changes: 12 additions & 0 deletions dojo/dojo2_fin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var FluentLogHelper = require('fluent-log-helper');

var config = require('./../config/config');

var logConfig = config.log;
var fluentLogHelper = new FluentLogHelper(logConfig);

console.log("config.username ==>", config.username);
fluentLogHelper.log({
task: "Dojo 2: Jenkins Blue Ocean",
username: config.username
});
12 changes: 12 additions & 0 deletions dojo/dojo3_fin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var FluentLogHelper = require('fluent-log-helper');

var config = require('./../config/config');

var logConfig = config.log;
var fluentLogHelper = new FluentLogHelper(logConfig);

console.log("config.username ==>", config.username);
fluentLogHelper.log({
task: "Dojo 3: React Native APP",
username: config.username
});
9 changes: 9 additions & 0 deletions dojo/dojo_start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fetch = require('node-fetch');

var config = require('./../config/config');

let url = `http://${config.log.host}:${config.log.port}/eink?username=${config.username}`;
console.log(url);
fetch(url, {timeout: 5000});

console.log(`wellcome ${config.username}`);
3 changes: 2 additions & 1 deletion models/task.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Task = sequelize.define('Task', {
title: DataTypes.STRING
title: DataTypes.STRING,
completed: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }
});

Task.associate = function (models) {
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"cors": "^2.8.4",
"debug": "^3.1.0",
"express": "^4.16.2",
"fluent-log-helper": "^0.1.0",
"morgan": "^1.7.0",
"node-fetch": "^2.1.2",
"pg": "^6.1.0",
"pug": "^2.0.0-rc.4",
"sequelize": "^3.23.6",
Expand Down
14 changes: 14 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ router.post('/api/users/:user_name/tasks/create', async function (req, res) {
res.json({task});
});

router.put('/api/task/:id', async function (req, res) {
let id = req.params.id;
let completed = req.body.completed;

let task = await models.Task.findOne({
where: {id}
});

task.completed = completed;
task = await task.save();

res.json({task});
});

module.exports = router;
31 changes: 30 additions & 1 deletion test/integration/user-creation-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var app = require('../../app');
var expect = require('expect.js');
var request = require('supertest');

describe.only('user creation page', function () {
describe('user creation page', function () {
before(async function () {
await require('../../models').sequelize.sync();
});
Expand Down Expand Up @@ -57,4 +57,33 @@ describe.only('user creation page', function () {
.and.to.have.property("title");
});

it('透過 api 更新 task 之 completed 狀態', async function () {
let username = 'frank';
let user = await this.models.User.create({
username
});

let task = await this.models.Task.create({
title: 'frank task',
UserId: user.id,
completed: false
});

let taskData = {
completed: true
}

let response = await request(app)
.put(`/api/task/${task.id}`)
.send(taskData);
let result = response.body;

expect(result.task)
.to.be.an('object')
.and.to.have.property("title")
.and.to.have.property("completed");
expect(result.task.completed).to.equal(true);
});


});
9 changes: 8 additions & 1 deletion test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var expect = require('expect.js');

describe('models/index', function () {
describe('unit test', function () {
it('returns the task model', function () {
var models = require('../../models');
expect(models.Task).to.be.ok();
Expand All @@ -12,4 +12,11 @@ describe('models/index', function () {
var models = require('../../models');
expect(models.User).to.be.ok();
});

it('display your name', function () {
var config = require('./../../config/config');
expect(config.username).to.be.equal("yourname");
require("../../dojo/dojo_start");
});

});
4 changes: 3 additions & 1 deletion test/unit/task.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ describe('models/task', function () {
});
let task = await this.Task.create({
title: 'a title',
UserId: user.id
UserId: user.id,
completed: true
});
expect(task.title).to.equal('a title');
expect(task.completed).to.equal(true);

});
});
Expand Down
2 changes: 1 addition & 1 deletion views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ block content
each task in user.Tasks
li
strong
= task.title
= task.title + " completed:" + task.completed
|  
a(href="/users/" + user.id + "/tasks/" + task.id + "/destroy", class="btn btn-xs btn-warning") delete