Skip to content

Commit

Permalink
Updated all tests to create and delete database files
Browse files Browse the repository at this point in the history
Setup and tear down will manage test related database files, helping in removing unneccessary files from commit
  • Loading branch information
thetutlage committed Dec 27, 2015
1 parent 01676b4 commit 9e4396f
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 47 deletions.
5 changes: 3 additions & 2 deletions src/Runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const log = new CatLog('adonis:lucid')
class Runner {

constructor (Config) {
const config = Config.get('database.connection')
const connection = Config.get('database.connection')
const config = Config.get(`database.${connection}`)
this.knex = require('knex')(config)
this.migrationsTable = Config.get('database.migrationsTable')
this.migrationsTable = Config.get('database.migrationsTable', 'adonis_schema')
this.lockTable = `${this.migrationsTable}_lock`
this.migrations = []
}
Expand Down
18 changes: 13 additions & 5 deletions test/implementation/model-relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
const expect = chai.expect
const co = require('co')
const Database = require('../../src/Database')
const manageDb = require('../unit/blueprints/manage')
const blueprint = require('./blueprints/model-blueprint')
const Model = require('../../src/Orm/Proxy/Model')
const Ioc = require('adonis-fold').Ioc
Expand Down Expand Up @@ -55,16 +56,23 @@ Ioc.bind('App/Model/User', function () {
describe('Database Implementation', function () {

before(function (done) {
blueprint
.setup(db)
.then (function () {
done()
}).catch(done)
manageDb
.make(path.join(__dirname, './storage/blog.sqlite3'))
.then(function () {
return blueprint.setup(db)
})
.then (function () {
done()
})
.catch(done)
})

after(function (done) {
blueprint
.tearDown(db)
.then(function () {
return manageDb.remove(path.join(__dirname, './storage/blog.sqlite3'))
})
.then (function () {
done()
}).catch(done)
Expand Down
29 changes: 19 additions & 10 deletions test/implementation/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const chai = require('chai')
const expect = chai.expect
const Database = require('../../src/Database')
const manageDb = require('../unit/blueprints/manage')
const blueprint = require('./blueprints/model-blueprint')
const Model = require('../../src/Orm/Proxy/Model')

Expand All @@ -15,7 +16,7 @@ let Config = {
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname,'./storage/blog.sqlite3')
filename: path.join(__dirname,'./storage/model.sqlite3')
},
debug: false
}
Expand Down Expand Up @@ -49,19 +50,27 @@ let postId = null
describe('Database Implementation', function () {

before(function (done) {
blueprint
.setup(db)
.then (function () {
done()
}).catch(done)
manageDb
.make(path.join(__dirname, './storage/model.sqlite3'))
.then(function () {
return blueprint.setup(db)
})
.then (function () {
done()
})
.catch(done)
})

after(function (done) {
blueprint
.tearDown(db)
.then (function () {
done()
}).catch(done)
.tearDown(db)
.then(function () {
return manageDb.remove(path.join(__dirname, './storage/model.sqlite3'))
})
.then (function () {
done()
})
.catch(done)
})


Expand Down
Binary file removed test/implementation/storage/blog.sqlite3
Binary file not shown.
34 changes: 34 additions & 0 deletions test/unit/blueprints/manage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/
const fs = require('fs-extra')
module.exports = {
make: function (file) {
return new Promise(function (resolve, reject) {
fs.ensureFile(file, function (error) {
if(error) {
reject(error)
}
else {
resolve()
}
})
})
},
remove: function (file) {
return new Promise(function (resolve, reject) {
fs.remove(file, function (error) {
if(error) {
reject(error)
}
else {
resolve()
}
})
})
}
}
33 changes: 27 additions & 6 deletions test/unit/blueprints/model-relations-blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Q = require('q')
let blueprint = exports = module.exports = {}

blueprint.tearDown = function(knex) {
return Q.all([
const tablesToRemove = [
knex.schema.dropTable('users'),
knex.schema.dropTable('phones'),
knex.schema.dropTable('authors'),
Expand All @@ -17,12 +17,19 @@ blueprint.tearDown = function(knex) {
knex.schema.dropTable('chat_operators'),
knex.schema.dropTable('relation_table'),
knex.schema.dropTable('cbooks')
])
]
let result = Q()
tablesToRemove.forEach(function (item) {
result = result.then(function () {
return item
})
})
return result
}

blueprint.setup = function(knex) {

return Q.all([
const setupTables = [
knex.schema.createTable('users', function (table) {

table.increments()
Expand Down Expand Up @@ -119,7 +126,14 @@ blueprint.setup = function(knex) {
table.string('relation_user_id').references('co_id').inTable('chat_operators').onDelete('CASCADE')

})
])
]
let result = Q()
setupTables.forEach(function (item) {
result = result.then(function () {
return item
})
})
return result
}

blueprint.seed = function(knex){
Expand Down Expand Up @@ -235,7 +249,7 @@ blueprint.seed = function(knex){
]


return Q.all([
const seeds = [
knex.table('users').insert(users),
knex.table('phones').insert(phones),
knex.table('authors').insert(authors),
Expand All @@ -247,5 +261,12 @@ blueprint.seed = function(knex){
knex.table('chat_operators').insert(operators),
knex.table('cbooks').insert(cbooks),
knex.table('relation_table').insert(relational_values)
])
]
let result = Q()
seeds.forEach(function (item) {
result = result.then(function () {
return item
})
})
return result
}
10 changes: 10 additions & 0 deletions test/unit/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ describe('Commands', function () {
})
})

after(function (done) {
fs.emptyDir(path.join(__dirname, './migrations'), function (error) {
if (error) {
done(error)
} else {
done()
}
})
})

context('Make', function () {
it('should create a file inside migrations directory', function (done) {
expect(Make.description).not.equal(undefined)
Expand Down
36 changes: 29 additions & 7 deletions test/unit/database.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const Database = require('../../src/Database')
const path = require('path')
const chai = require('chai')
const manageDb = require('./blueprints/manage')
const expect = chai.expect

let alternateConnection = {
Expand Down Expand Up @@ -38,18 +39,39 @@ let Config = {
}

describe('Database', function () {

before(function (done) {
manageDb
.make(path.join(__dirname, './storage/test.sqlite3'))
.then(function () {
return manageDb.make(path.join(__dirname, './storage/connection.sqlite3'))
})
.then(function () {
done()
})
.catch(done)
})

after(function (done) {
manageDb
.remove(path.join(__dirname, './storage/test.sqlite3'))
.then(function () {
return manageDb.remove(path.join(__dirname, './storage/connection.sqlite3'))
})
.then(function () {
done()
})
.catch(done)
})

it('should make connection with sqlite database', function () {
const db = new Database(Config)
expect(db.client.config.client).to.equal('sqlite3')
})

it('should be able to switch connections using connection method', function (done) {
it('should be able to switch connections using connection method', function () {
const db = new Database(Config)
db.connection('new')
.table('accounts')
.then(function (accounts) {
expect(accounts).to.be.an('array')
done()
}).catch(done)
const newConnection = db.connection('new')
expect(newConnection.client.config.connection.filename).to.equal(path.join(__dirname, './storage/connection.sqlite3'))
})
})
16 changes: 12 additions & 4 deletions test/unit/model-relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const expect = chai.expect
const blueprint = require('./blueprints/model-relations-blueprint')
const co = require('co')
const Ioc = require('adonis-fold').Ioc
const manageDb = require('./blueprints/manage')
const Database = require('../../src/Database')
const Model = require('../../src/Orm/Proxy/Model')
const _ = require('lodash')
Expand All @@ -39,7 +40,7 @@ let Config = {
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, './storage/test.sqlite3')
filename: path.join(__dirname, './storage/relation.sqlite3')
},
debug: false
}
Expand All @@ -57,11 +58,15 @@ const db = new Database(Config)
* Tests begins here
*/
describe('Model Relations', function () {

before(function (done) {
blueprint
.setup(db)
manageDb
.make(path.join(__dirname, './storage/relation.sqlite3'))
.then(function () {
return blueprint.setup(db)
})
.then(function () {
blueprint.seed(db)
return blueprint.seed(db)
})
.then(function () {
done()
Expand All @@ -72,6 +77,9 @@ describe('Model Relations', function () {
after(function (done) {
blueprint
.tearDown(db)
.then(function () {
return manageDb.remove(path.join(__dirname, './storage/relation.sqlite3'))
})
.then(function () {
done()
})
Expand Down
21 changes: 15 additions & 6 deletions test/unit/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const chai = require('chai')
const expect = chai.expect
const co = require('co')
const manageDb = require('./blueprints/manage')
const blueprint = require('./blueprints/model-blueprint')
const Database = require('../../src/Database')
const Model = require('../../src/Orm/Proxy/Model')
Expand All @@ -20,7 +21,7 @@ let Config = {
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, './storage/test.sqlite3')
filename: path.join(__dirname, './storage/model.sqlite3')
},
debug: false
}
Expand All @@ -30,19 +31,27 @@ let Config = {
const db = new Database(Config)

describe('Model', function () {

before(function (done) {
blueprint
.setup(db)
manageDb
.make(path.join(__dirname, './storage/model.sqlite3'))
.then(function () {
return blueprint.setup(db)
})
.then(function () {
blueprint.seed(db)
return blueprint.seed(db)
}).then(function () {
done()
}).catch(done)
done()
})
.catch(done)
})

after(function (done) {
blueprint
.tearDown(db)
.then(function () {
return manageDb.remove(path.join(__dirname, './storage/model.sqlite3'))
})
.then(function () {
done()
}).catch(done)
Expand Down
Loading

0 comments on commit 9e4396f

Please sign in to comment.