Skip to content

Commit

Permalink
Integration test - Eloquent Casted Querying
Browse files Browse the repository at this point in the history
  • Loading branch information
nhat-phan committed Apr 2, 2018
1 parent cdf5d21 commit 6410dbd
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 6 deletions.
17 changes: 15 additions & 2 deletions integration/ModelFactory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Factory, Faker } from '../dist/lib/v1'
import { User } from './models/User'
import { Post } from './models/Post'
import { Comment } from './models/Comment'
import { ObjectId } from 'bson'

function createObjectId(): string {
return new ObjectId().toHexString()
}

Factory.define(User.className, (faker: Faker, attributes?: Object): Object => {
Factory.define(User, (faker: Faker, attributes?: Object): Object => {
return Object.assign(
{
email: faker.email(),
Expand All @@ -19,7 +20,7 @@ Factory.define(User.className, (faker: Faker, attributes?: Object): Object => {
)
})

Factory.define(Post.className, (faker: Faker, attributes?: Object): Object => {
Factory.define(Post, (faker: Faker, attributes?: Object): Object => {
return Object.assign(
{
user_id: createObjectId(),
Expand All @@ -30,3 +31,15 @@ Factory.define(Post.className, (faker: Faker, attributes?: Object): Object => {
attributes
)
})

Factory.define(Comment, (faker: Faker, attributes?: Object): Object => {
return Object.assign(
{
email: faker.email(),
name: faker.name(),
content: faker.paragraph(),
like: faker.natural()
},
attributes
)
})
17 changes: 17 additions & 0 deletions integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Integration

There are 3 purposes for this directory

1. **Integration test**: All files in this directory are written like "real-life-usage", it contains tests to ensure all features are working as expectations.
2. **Definition and Syntax test**: Because the `najs-eloquent` is implemented with Proxy and dynamic function names then we have to ensure that all classes have typed-definition correctly.
3. **Develop and debug**: All bugs which are reproducible in model-level should be written, fixed and tested in this directory

The directory structure:

* **models**: Definition of models which are use for integration/definition/syntax testing
* **mongodb**: Setup for mongodb/mongoose driver or environment
* **mysql**: Setup for mysql driver or environment
* **test**: Setup for mysql driver or environment
* * **features**: For integration test
* * **syntax**: For definition and syntax test, if a type-definitions is wrong it's never built successfully
* **ModelFactory.ts**: Factory definition
26 changes: 26 additions & 0 deletions integration/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Eloquent, Mongoose } from '../../dist/lib/v1'

export interface IComment {
user_id?: string
email?: string
name?: string
content: string
like: number
}

export class Comment extends (Eloquent as Mongoose<IComment>) {
static className: string = 'Comment'
protected static timestamps = true
protected static softDeletes = true
protected static schema = {
user_id: { type: String, required: false },
email: { type: String, required: false },
name: { type: String, required: false },
content: { type: String, required: true },
like: { type: Number, required: false }
}

getClassName() {
return Comment.className
}
}
2 changes: 2 additions & 0 deletions integration/mongodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../ModelFactory'
import { Eloquent, MongooseDriver, EloquentDriverProvider, MongooseProvider } from '../../dist/lib/v1'
import { IUser, User } from '../models/User'
import { IPost, Post } from '../models/Post'
import { IComment, Comment } from '../models/Comment'

EloquentDriverProvider.register(MongooseDriver, 'mongoose', true)

Expand All @@ -10,6 +11,7 @@ Eloquent.register(Post)

export { IUser, User }
export { IPost, Post }
export { IComment, Comment }

export function init_mongoose(name: string): Promise<any> {
return new Promise(resolve => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { factory } from '../../../dist/lib/v1'
import { User, init_mongoose, delete_collection } from '../../mongodb/index'
const Moment = require('moment')

describe('Integration Test - Eloquent functions', function() {
describe('Integration Test - Eloquent model functions', function() {
beforeAll(async function() {
await init_mongoose('integration_eloquent')
})
Expand Down
Loading

0 comments on commit 6410dbd

Please sign in to comment.