-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add acceptance tests for model
wrote a simple crud test for lucid models
- Loading branch information
1 parent
e51864b
commit 21e4de9
Showing
8 changed files
with
300 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters