Skip to content

Commit

Permalink
feat(tests): add acceptance tests for model
Browse files Browse the repository at this point in the history
wrote a simple crud test for lucid models
  • Loading branch information
thetutlage committed Jul 16, 2017
1 parent e51864b commit 21e4de9
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 1 deletion.
23 changes: 23 additions & 0 deletions providers/DatabaseProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const ServiceProvider = require('adonis-fold').ServiceProvider

class DatabaseProvider extends ServiceProvider {
* register () {
this.app.bind('Adonis/Src/Database', function (app) {
const Database = require('../src/Database')
Database._setConfigProvider(app.use('Adonis/Src/Config'))
return Database
})
}
}
module.exports = DatabaseProvider
22 changes: 22 additions & 0 deletions providers/FactoryProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const ServiceProvider = require('adonis-fold').ServiceProvider

class FactoryProvider extends ServiceProvider {
* register () {
this.app.bind('Adonis/Src/Factory', function () {
return require('../src/Factory')
})
}
}

module.exports = FactoryProvider
22 changes: 22 additions & 0 deletions providers/SchemaProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const ServiceProvider = require('adonis-fold').ServiceProvider

class SchemaProvider extends ServiceProvider {
* register () {
this.app.bind('Adonis/Src/Schema', function (app) {
return require('../src/Schema')
})
}
}

module.exports = SchemaProvider
22 changes: 22 additions & 0 deletions providers/SeedsProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const ServiceProvider = require('adonis-fold').ServiceProvider

class SeedsProvider extends ServiceProvider {
* register () {
this.app.bind('Adonis/Src/Seeder', function () {
return require('../src/Seeder')
})
}
}

module.exports = SeedsProvider
2 changes: 1 addition & 1 deletion src/Seeder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Seeder = exports = module.exports = {}
* @public
*/
Seeder.exec = function (seeds) {
cf.forEach(function * (Seed) {
return cf.forEach(function * (Seed) {
const seedInstance = typeof (Seed) === 'string' ? Ioc.make(Seed) : new Seed()
yield seedInstance.run()
}, seeds)
Expand Down
152 changes: 152 additions & 0 deletions test/acceptance/crud.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/*
|--------------------------------------------------------------------------
| CRUD APPLICATION
|--------------------------------------------------------------------------
|
| Crud related tests for Adonis Lucid
|
*/

/* global describe, it, after, before */
const chai = require('chai')
const fold = require('adonis-fold')
const Ioc = fold.Ioc
const setup = require('./setup')
const expect = chai.expect
require('co-mocha')

describe('Crud', function () {
before(function * () {
yield setup.loadProviders()
yield setup.start()

/**
* REQUIREMENTS
*/
const Lucid = Ioc.use('Adonis/Src/Lucid')
const Schema = Ioc.use('Adonis/Src/Schema')
const Factory = Ioc.use('Adonis/Src/Factory')
this.Factory = Factory

/**
* SETUP
*/
class User extends Lucid {
static get computed () {
return ['fullName']
}

getFullName () {
return `${this.firstname} ${this.lastname}`
}
}
Ioc.bind('App/Model/User', function () {
return User
})

class UserSchema extends Schema {
up () {
this.create('users', function (table) {
table.increments()
table.string('username', 40)
table.string('email_address', 120)
table.string('password', 80)
table.string('firstname')
table.string('lastname')
table.timestamps()
})
}

down () {
this.drop('users')
}
}

Factory.blueprint('App/Model/User', function (faker) {
return {
username: faker.internet.userName(),
email_address: faker.internet.email(),
firstname: faker.name.firstName(),
lastname: faker.name.lastName(),
password: faker.internet.password()
}
})

class UserSeed {
* run () {
yield Factory.model('App/Model/User').create(5)
}
}

/**
* EXECUTION
*/
this.migrations = {}
this.migrations[new Date().getTime() + '_users'] = UserSchema

yield setup.migrate(this.migrations, 'up')
yield setup.seed([UserSeed])

this.User = User
})

after(function * () {
yield setup.migrate(this.migrations, 'down')
yield setup.end()
})

it('should return list of all users inside the users table', function * () {
const users = yield this.User.all()
expect(users.size()).to.equal(5)
users.each((user) => {
expect(user instanceof this.User).to.equal(true)
})
})

it('should be able to create a new user inside the database', function * () {
const user = this.Factory.model('App/Model/User').make()
yield user.save()
expect(user.id).not.to.equal(undefined)
expect(user.isNew()).to.equal(false)
})

it('should be able to update a given user', function * () {
const user = yield this.User.find(1)
expect(user.isNew()).to.equal(false)
user.email_address = 'dobby@foo.com'
yield user.save()
const grabUser = yield this.User.query().where('email_address', 'dobby@foo.com').first()
expect(grabUser instanceof this.User).to.equal(true)
expect(grabUser.id).to.equal(1)
})

it('should be able to delete a given user', function * () {
const user = yield this.User.find(1)
expect(user.isNew()).to.equal(false)
yield user.delete()
try {
yield this.User.findOrFail(1)
expect(true).to.equal(false)
} catch (e) {
expect(e.name).to.equal('ModelNotFoundException')
}
})

it('should be able to make the user fullname using computed properties', function * () {
const users = yield this.User.all()
users.toJSON().forEach(function (user) {
expect(user.fullName).to.equal(`${user.firstname} ${user.lastname}`)
})
})
})
55 changes: 55 additions & 0 deletions test/acceptance/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const fold = require('adonis-fold')
const filesFixtures = require('../unit/fixtures/files')
const Ioc = fold.Ioc
const Registrar = fold.Registrar
const path = require('path')
const config = require('../unit/helpers/config')
Ioc.bind('Adonis/Src/Config', function () {
return config
})

const providers = [
path.join(__dirname, '../../providers/DatabaseProvider'),
path.join(__dirname, '../../providers/FactoryProvider'),
path.join(__dirname, '../../providers/LucidProvider'),
path.join(__dirname, '../../providers/MigrationsProvider'),
path.join(__dirname, '../../providers/SchemaProvider'),
path.join(__dirname, '../../providers/SeedsProvider')
]

const setup = exports = module.exports = {}

setup.loadProviders = function () {
return Registrar.register(providers)
}

setup.start = function * () {
yield filesFixtures.createDir()
}

setup.end = function * () {
}

setup.migrate = function * (schemas, direction) {
const Migrations = Ioc.use('Adonis/Src/Migrations')
yield Migrations[direction](schemas)
if (direction === 'down') {
yield Migrations.database.schema.dropTable('adonis_migrations')
}
}

setup.seed = function (seeds) {
const Seeder = Ioc.use('Adonis/Src/Seeder')
return Seeder.exec(seeds)
}
3 changes: 3 additions & 0 deletions test/unit/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const sqliteConnections = require('./sqliteConnections')

module.exports = {
get: function (key) {
if (key === 'database.migrationsTable') {
return 'adonis_migrations'
}
if (key === 'database.connection') {
return process.env.DB
}
Expand Down

0 comments on commit 21e4de9

Please sign in to comment.