Skip to content

Commit

Permalink
improved demo content
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvanrijsse committed Jun 23, 2020
1 parent 2ad7597 commit 6cbbd8f
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
**/*.DS_Store
**/*-secret.json
**/*.sh
.idea
10 changes: 10 additions & 0 deletions Week2/QA-session-content/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
This folder contains a small codebase to setup a database with some content.
The database will contain information about users their projects and tasks.
Tasks can be assigned to users optionally which make for some nice example queries.

Create a database called `db_qa_session` or make changes to the `knexfile.js`

```
npm install
npx knex migrate:latest
npx knex seed:run
```

Your database should be filled with some demo content :)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports.up = async (knex) => {
table.date('start_date');
table.date('end_date');
table.string('code').unique();
table.timestamps();
})
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports.up = async (knex) => {
table.increments();
table.string('first_name').notNullable();
table.string('last_name').notNullable();
table.timestamps();
})
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ exports.up = async (knex) => {
.unsigned()
.references('id')
.inTable('projects');
table.timestamps();
})
};

Expand Down
10 changes: 10 additions & 0 deletions Week2/QA-session-content/seeds/0-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.seed = async (knex) => {

await knex.raw('SET foreign_key_checks = 0;');
await knex('project_users').truncate();
await knex('tasks').truncate();
await knex('projects').truncate();
await knex('users').truncate();
await knex.raw('SET foreign_key_checks = 1;');

};
30 changes: 24 additions & 6 deletions Week2/QA-session-content/seeds/1-addProjects.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
const faker = require('faker');

const createProject = () => ({
name: faker.name.jobTitle(),
})
const createProject = function (i) {
let companyName = faker.company.companyName();
let abbreviation = companyName
.replace(/,|and|-/gi, '')
.split(' ')
.map((name) => name[0])
.join('');

exports.seed = async function(knex) {
return {
name: companyName + ' ' + (i % 2 ? 'website' : 'app'),
start_date: faker.date.past(),
end_date: faker.date.future(),
code: abbreviation
}
}

exports.seed = async function (knex) {

const fakes = [];
const codes = [];
const amount = 20;
for(let i = 0; i< amount; i++){
fakes.push(createProject());
for (let i = 0; i < amount; i++) {
let project = createProject(i);
if(codes.includes(project.code)){
project.code + i;
}
codes.push(project.code);
fakes.push(project);
}

await knex('projects').insert(fakes);
Expand Down
12 changes: 6 additions & 6 deletions Week2/QA-session-content/seeds/3-addTasks.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const faker = require('faker');

const createTask = () => ({
name: faker.name.jobDescriptor(),
const createTask = (i) => ({
name: faker.random.words(),
project_id: Math.ceil(Math.random() * 20),
assigned_to: Math.ceil(Math.random() * 10),
assigned_to: (i % 3 ? null : Math.ceil(Math.random() * 10)),
})

exports.seed = async function(knex) {
exports.seed = async function (knex) {

const fakes = [];
const amount = 100;
for(let i = 0; i< amount; i++){
fakes.push(createTask());
for (let i = 0; i < amount; i++) {
fakes.push(createTask(i));
}

await knex('tasks').insert(fakes);
Expand Down
17 changes: 17 additions & 0 deletions Week2/QA-session-content/seeds/4-addProjectUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const createRelation = (i) => ({
// user 2 to 9 will have projects
user_id: 1 + Math.ceil(Math.random() * 8),
// project 4 to 18 will have projects
project_id: 3 + Math.ceil(Math.random() * 15)
})

exports.seed = async function (knex) {

const fakes = [];
const amount = 30;
for (let i = 0; i < amount; i++) {
fakes.push(createRelation(i));
}

await knex('project_users').insert(fakes);
};

0 comments on commit 6cbbd8f

Please sign in to comment.