Skip to content

Commit

Permalink
feat: data model, stubs and test for units
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 2, 2021
1 parent c6e2290 commit 5f64537
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 21 deletions.
6 changes: 0 additions & 6 deletions migrations/20211201194652-create-unit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ module.exports = {
vintageId: {
type: Sequelize.NUMBER
},
qualificationId: {
type: Sequelize.NUMBER
},
owner: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
Expand Down
24 changes: 16 additions & 8 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Unit, UnitMock } from '../models';

export const create = (req, res) => {
res.json({
message: 'Not Yet Implemented',
});
};
export const findAll = (req, res) => {
res.json({
message: 'Not Yet Implemented',
});
export const findAll = async (req, res) => {
if (req.query.useMock) {
res.json(UnitMock.findAll());
return;
}

res.json(await Unit.findAll());
};
export const findOne = (req, res) => {
res.json({
message: 'Not Yet Implemented',
});
export const findOne = async (req, res) => {
if (req.query.useMock) {
res.json(UnitMock.findOne(req.query.id));
return;
}

res.json(await Unit.findByPk(req.query.id));
};
export const update = (req, res) => {
res.json({
Expand Down
74 changes: 68 additions & 6 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
'use strict';
import Sequelize from 'sequelize';
const { DataTypes, Model } = Sequelize;
import { sequelize } from '../database';

class Unit extends Model { }


class UnitModel {
static list(page = 0, limit = 10) {
return stub;
Unit.init({
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
buyer: {
type: Sequelize.STRING
},
registry: {
type: Sequelize.STRING
},
blockIdentifier: {
type: Sequelize.STRING
},
identifier: {
type: Sequelize.STRING
},
unitType: {
type: Sequelize.STRING
},
unitCount: {
type: Sequelize.NUMBER
},
unitStatus: {
type: Sequelize.STRING
},
unitStatusDate: {
type: Sequelize.DATE
},
transactionType: {
type: Sequelize.STRING
},
unitIssuanceLocation: {
type: Sequelize.STRING
},
unitLink: {
type: Sequelize.STRING
},
correspondingAdjustment: {
type: Sequelize.STRING
},
unitTag: {
type: Sequelize.STRING
},
vintageId: {
type: Sequelize.NUMBER
},
qualificationId: {
type: Sequelize.NUMBER
},
owner: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}
}, { sequelize, modelName: 'qualification' });

export { UnitModel };
export { Unit };
108 changes: 107 additions & 1 deletion src/models/units/units.stub.json
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
[]
[
{
"id": 1,
"unitLink": "https://unit.link",
"owner": "owner",
"buyer": "person 1",
"registry": "registry 1",
"blockIdentifier": "bi 1",
"identifier": "i 1",
"qualificationId": 10,
"unitType": "regular",
"unitCount": 10,
"unitStatus": "draft",
"unitStatusDate": "2022-11-05T14:41:00.000Z",
"transactionType": "purchase",
"unitIssuanceLocation": "public",
"correspondingAdjustment": "1",
"unitTag": "tag1",
"vintageId": 1,
"createdAt": "2022-11-05T14:41:00.000Z",
"updatedAt": "2022-11-05T14:41:00.000Z"
},
{
"id": 2,
"unitLink": "https://unit.link",
"owner": "owner",
"buyer": "person 2",
"registry": "registry 2",
"blockIdentifier": "bi 2",
"identifier": "i 2",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 10,
"unitStatus": "draft",
"unitStatusDate": "2020-11-05T14:41:00.000Z",
"transactionType": "purchase",
"unitIssuanceLocation": "public",
"correspondingAdjustment": "1",
"unitTag": "tag2",
"vintageId": 1,
"createdAt": "2020-11-05T14:41:00.000Z",
"updatedAt": "2020-11-05T14:41:00.000Z"
},
{
"id": 3,
"unitLink": "https://unit.link",
"owner": "owner",
"buyer": "person 3",
"registry": "registry 3",
"blockIdentifier": "bi 3",
"identifier": "i 3",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 14,
"unitStatus": "published",
"unitStatusDate": "2021-11-05T14:41:00.000Z",
"transactionType": "purchase",
"unitIssuanceLocation": "public",
"correspondingAdjustment": "1",
"unitTag": "tag3",
"vintageId": 1,
"createdAt": "2021-11-05T14:41:00.000Z",
"updatedAt": "2021-11-05T14:41:00.000Z"
},
{
"id": 4,
"unitLink": "https://unit.link",
"owner": "owner",
"buyer": "person 4",
"registry": "registry 4",
"blockIdentifier": "bi 4",
"identifier": "i 4",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 14,
"unitStatus": "published",
"unitStatusDate": "2025-11-05T14:41:00.000Z",
"transactionType": "purchase",
"unitIssuanceLocation": "public",
"correspondingAdjustment": "1",
"unitTag": "tag4",
"vintageId": 1,
"createdAt": "2025-11-05T14:41:00.000Z",
"updatedAt": "2025-11-05T14:41:00.000Z"
},
{
"id": 5,
"unitLink": "https://unit.link",
"owner": "owner",
"buyer": "person 5",
"registry": "registry 5",
"blockIdentifier": "bi 5",
"identifier": "i 5",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 9,
"unitStatus": "published",
"unitStatusDate": "2030-11-05T14:41:00.000Z",
"transactionType": "purchase",
"unitIssuanceLocation": "public",
"correspondingAdjustment": "1",
"unitTag": "tag4",
"vintageId": 1,
"createdAt": "2030-11-05T14:41:00.000Z",
"updatedAt": "2030-11-05T14:41:00.000Z"
}
]
30 changes: 30 additions & 0 deletions tests/resources/units.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from './../../src/server';

// Configure chai
chai.use(chaiHttp);
chai.should();

describe("Units Routes", () => {
describe("GET /v1/units/", () => {
it("should get all units", () => {
chai.request(app)
.get('/v1/units/?useMock=true')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
});
});
});
describe("GET /v1/units/?id=1", () => {
it("should get a single unit", () => {
chai.request(app)
.get('/v1/units/?useMock=true&id=1')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
});
});
});
});

0 comments on commit 5f64537

Please sign in to comment.