Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('global middleware execution', assert => {
const Gql = ioc.use('Gql')
class Middleware {
async gqlHandle (resolve, root, args, context, info) {}
}
ioc.bind('Adonis/Middleware/Auth', () => {
return new Middleware()
})
class UserMiddlewareController {
user (parent, args, ctx) {}
users (parent, args, ctx) {}
}
ioc.bind('App/Controllers/Gql/UserMiddlewareController', () => {
return UserMiddlewareController
test('attach sideloaded data as meta', async (assert) => {
class Profile extends Model {
}
class User extends Model {
profile () {
return this.hasOne(Profile)
}
}
User._bootIfNotBooted()
Profile._bootIfNotBooted()
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await ioc.use('Database').table('profiles').insert([{ user_id: 1, profile_name: 'virk' }, { user_id: 2, profile_name: 'nikk' }])
const users = await User.query().withCount('profile').paginate()
const json = users.toJSON()
assert.property(json, 'data')
assert.isArray(json.data)
assert.deepEqual(json.data[0].__meta__, { profile_count: helpers.formatNumber(1) })
assert.equal(json.page, 1)
assert.equal(json.total, 2)
assert.equal(json.perPage, 20)
assert.equal(json.lastPage, 1)
})
it('searchableAs returns prefixed model table', () => {
const model = new ModelStub()
ModelStub._bootIfNotBooted()
ioc.use('Config').set('scout.prefix', 'prefix_')
expect(model.searchableAs()).toBe('prefix_posts')
})
group.after(async () => {
await helpers.dropCollections(ioc.use('Database'))
ioc.use('Database').close()
try {
await fs.remove(path.join(__dirname, './tmp'))
} catch (error) {
if (process.platform !== 'win32' || error.code !== 'EBUSY') {
throw error
}
}
}).timeout(0)
group.before(async () => {
ioc.singleton('Adonis/Src/Database', function () {
const config = new Config()
config.set('database', {
connection: 'testing',
testing: helpers.getConfig()
})
return new DatabaseManager(config)
})
ioc.alias('Adonis/Src/Database', 'Database')
await fs.ensureDir(path.join(__dirname, './tmp'))
await helpers.createCollections(ioc.use('Adonis/Src/Database'))
})
class User extends Model {
static get geometries () {
return ['location']
}
}
User._bootIfNotBooted()
const user = await User.create({
location: {
latitude: 1,
longitude: 2
}
})
assert.instanceOf(user.$attributes.location, GeoPoint)
assert.equal(user.$attributes.location.latitude, 1)
assert.equal(user.$attributes.location.longitude, 2)
const newUser = await ioc.use('Database').collection('users').findOne()
assert.instanceOf(newUser.location, Object)
assert.equal(newUser.location.type, 'Point')
assert.deepEqual(newUser.location.coordinates, [2, 1])
})
collection.increments()
})
this.createCollection('schema_profile', (collection) => {
collection.increments()
})
}
}
const userSchema = new UserSchema(ioc.use('Database'))
userSchema.up()
await userSchema.executeActions()
const hasUsers = await userSchema.hasCollection('schema_users')
const hasProfile = await userSchema.hasCollection('schema_profile')
assert.isTrue(hasUsers)
assert.isTrue(hasProfile)
await ioc.use('Database').schema.dropCollection('schema_users')
await ioc.use('Database').schema.dropCollection('schema_profile')
})
}
}
class Country extends Model {
posts () {
return this.manyThrough(User, 'posts')
}
}
User._bootIfNotBooted()
Country._bootIfNotBooted()
Post._bootIfNotBooted()
const rsCountry = await ioc.use('Database').collection('countries').insert({ name: 'India' })
const rsUser = await ioc.use('Database').collection('users').insert({ country_id: rsCountry.insertedIds[0], username: 'virk' })
await ioc.use('Database').collection('posts').insert({ user_id: rsUser.insertedIds[0], title: 'Adonis 101' })
const countries = await Country.query().with('posts', (builder) => {
builder.selectThrough('_id')
}).fetch()
assert.equal(countries.size(), 1)
const country = countries.first()
assert.instanceOf(country.getRelated('posts'), VanillaSerializer)
assert.equal(country.getRelated('posts').size(), 1)
})
})
group.after(async () => {
await helpers.dropTables(ioc.use('Adonis/Src/Database'))
ioc.use('Database').close()
try {
await fs.remove(path.join(__dirname, './tmp'))
} catch (error) {
if (process.platform !== 'win32' || error.code !== 'EBUSY') {
throw error
}
}
}).timeout(0)
test('the TransformerAbstract can be importet from the ioc', async (assert) => {
const TransformerAbstract = ioc.use('Adonis/Addons/Bumblebee/TransformerAbstract')
class UserTransformer extends TransformerAbstract {
transform (model) {
return {
id: model.id
}
}
}
let data = {id: 42}
let transformed = await Bumblebee.create()
.item(data)
.transformWith(UserTransformer)
.toArray()