Skip to content

Commit

Permalink
test(model): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LookinGit committed Jun 30, 2021
1 parent bf303a9 commit 086fcd5
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 7 deletions.
8 changes: 1 addition & 7 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function setup (destroyDb: boolean = true) {
table.string('password')
table.integer('company_id')
table.timestamps()
table.timestamp('deleted_at', { useTz: true })
})
}

Expand Down Expand Up @@ -90,13 +91,6 @@ export async function cleanup () {
await fs.cleanup()
}

/**
* Split string to an array using cross platform new lines
*/
export function toNewlineArray (contents: string): string[] {
return contents.split(/\r?\n/)
}

/**
* Setup application
*/
Expand Down
149 changes: 149 additions & 0 deletions test/model.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* adonis-lucid-soft-deletes
*
* (c) Lookin Anton <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import test from 'japa'
import { setup, cleanup, setupApplication, getBaseModel } from '../test-helpers'
import { ModelQueryBuilder } from '@adonisjs/lucid/build/src/Orm/QueryBuilder'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { column } from '@adonisjs/lucid/build/src/Orm/Decorators'
import { compose } from '@poppinss/utils/build/src/Helpers'
import { LucidModel } from '@ioc:Adonis/Lucid/Orm'
import { SoftDeletes } from '../src/SoftDeletes'

/**
* TODO:
* - add the check `beforeFind`, `beforeFetch`, `afterFind`, `afterFetch` hooks
* - add the check `restore` and `forceDelete` methods
*/
test.group('BaseModelFilter', (group) => {
let app: ApplicationContract
let BaseModel: LucidModel

group.before(async () => {
app = await setupApplication()
BaseModel = getBaseModel(app)
await setup()
})
group.after(async () => cleanup())

test('exists methods `withTrashed` and `onlyTrashed`', (assert) => {
class TestModel extends compose(BaseModel, SoftDeletes) {}
TestModel.boot()

assert.isFunction(TestModel.withTrashed)
assert.isFunction(TestModel.onlyTrashed)
assert.instanceOf(TestModel.withTrashed(), ModelQueryBuilder)
assert.instanceOf(TestModel.onlyTrashed(), ModelQueryBuilder)
})

test('not exists methods `withTrashed` and `onlyTrashed` of model without SoftDeletes', (assert) => {
class TestModel extends BaseModel {}
TestModel.boot()

assert.notProperty(TestModel, 'withTrashed')
assert.notProperty(TestModel, 'onlyTrashed')
})

test('exists methods `restore` and `forceDelete` of model instance', (assert) => {
class TestModel extends compose(BaseModel, SoftDeletes) {}
TestModel.boot()

const model = new TestModel()

assert.property(model, 'restore')
assert.property(model, 'forceDelete')
assert.isFunction(model.restore)
assert.isFunction(model.forceDelete)
})

test('exists `deletedAt` of model', (assert) => {
class TestModel extends compose(BaseModel, SoftDeletes) {}
TestModel.boot()

assert.equal(TestModel.$hasColumn('deletedAt'), true)
})

test('exists `$ignoreDeleted` property and is true by default', (assert) => {
class TestModel extends compose(BaseModel, SoftDeletes) {}
TestModel.boot()

assert.property(TestModel, '$ignoreDeleted')
assert.equal(TestModel.$ignoreDeleted, true)
})

test('querying models without trashed models', async (assert) => {
class User extends compose(BaseModel, SoftDeletes) {
@column({ isPrimary: true })
public id: number

@column()
public username: string

@column()
public email: string

@column()
public isAdmin: number

@column()
public companyId: number
}
User.boot()

const user1 = new User()
user1.fill({ username: 'Tony', email: '[email protected]', isAdmin: 1, companyId: 1, deletedAt: null })
await user1.save()

const user2 = new User()
user2.fill({ username: 'Adonis', email: '[email protected]', isAdmin: 0, companyId: 2 })
await user2.save()
await user2.delete()

const users = await User.all()
assert.lengthOf(users, 1)
assert.deepStrictEqual(users[0].toJSON(), user1.toJSON())

await User.truncate()
})

test('querying only trashed models', async (assert) => {
class User extends compose(BaseModel, SoftDeletes) {
@column({ isPrimary: true })
public id: number

@column()
public username: string

@column()
public email: string

@column()
public isAdmin: number

@column()
public companyId: number
}
User.boot()

const user1 = new User()
user1.fill({ username: 'Tony', email: '[email protected]', isAdmin: 1, companyId: 1 })
await user1.save()
await user1.delete()

const user2 = new User()
user2.fill({ username: 'Adonis', email: '[email protected]', isAdmin: 0, companyId: 2 })
await user2.save()

const users = await User.onlyTrashed().exec()
assert.lengthOf(users, 1)
assert.equal(users[0].id, user1.id)

await User.truncate()
})
})

0 comments on commit 086fcd5

Please sign in to comment.