Last active
August 29, 2015 14:27
-
-
Save doublemarked/3da120228c2a51a8afe4 to your computer and use it in GitHub Desktop.
Demonstration of problems with hasManyThrough relations and the relation.findOne helper
This file contains hidden or 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
// mocha hasManyThroughProblems.js | |
var app = require('loopback/test/fixtures/simple-integration-app/server/server.js'); | |
var expect = require('chai').expect; | |
describe('relation check', function() { | |
before(function defineProductAndCategoryModels() { | |
var product = app.model( | |
'product', | |
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' } | |
); | |
var category = app.model( | |
'category', | |
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' } | |
); | |
var connector = app.model( | |
'connector', | |
{ properties: { id: 'string' }, dataSource: 'db' } | |
); | |
product.hasAndBelongsToMany(category, { through: connector }); | |
category.hasMany(product, { through: connector }); | |
}); | |
var testCategory; | |
before(function initializeCategory(done) { | |
app.models.category.create({ name: 'my-category' }, function (err, instance) { | |
if (err) return done(err); | |
testCategory = instance; | |
done(); | |
}); | |
}); | |
before(function initializeProduct(done) { | |
testCategory.products.create({ name: 'a-product' }, done); | |
}); | |
it('should be possible to findOne on the relation and get back a product', function (done) { | |
testCategory.products.findOne(function (err, p) { | |
if (err) return done(err); | |
expect(p).to.be.ok(); | |
expect(p).to.be.an.instanceof(app.models.product); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment