Created
February 15, 2021 14:22
-
-
Save viniciusstroher/a4e98a562bab6849cdc4f9b4db63af9f to your computer and use it in GitHub Desktop.
Test Loopback2 Datasource memory e model
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
const loopback = module.exports = require('loopback'); | |
const datasourceJuggler = require('loopback-datasource-juggler'); | |
describe('Test datasource', () => { | |
let CatalogIntegrationModel | |
let CatalogIntegrationCategoryModel | |
beforeAll(() => { | |
app = loopback({localRegistry: true, loadBuiltinModels: true}); | |
app.set('remoting', {errorHandler: {debug: true, log: false}}); | |
const dataSource = loopback.createDataSource({ | |
connector: loopback.Memory, | |
}); | |
let catalogIntegrationJson = require('../common/models/catalog_integration.json'); | |
CatalogIntegrationModel = | |
loopback.createModel(catalogIntegrationJson.name, | |
catalogIntegrationJson.properties, | |
{relations: catalogIntegrationJson.relations}); | |
CatalogIntegrationModel.attachTo(dataSource) | |
let catalogIntegrationCategoryJson = require('../common/models/catalog_integration_category.json'); | |
CatalogIntegrationCategoryModel = | |
loopback.createModel(catalogIntegrationCategoryJson.name, | |
catalogIntegrationCategoryJson.properties, | |
{relations: catalogIntegrationCategoryJson.relations}); | |
// console.log(catalogIntegrationCategoryJson.relations) | |
CatalogIntegrationCategoryModel.attachTo(dataSource) | |
}) | |
it('Test datasourc insert', async () => { | |
const resultCatalogSave = await CatalogIntegrationModel.create({ | |
id_catalog_integration: 0, | |
quantity_categories: 0, | |
quantity_products: 0, | |
avaliable: 'Y', | |
enabled: 'Y', | |
name:'Teste Catalog' | |
}) | |
await CatalogIntegrationCategoryModel.create({ | |
name: 'Teste Category', | |
partner_category_ref: "53c96ce6-ada6-4512-aab8-4177f8c2ab1f", | |
avaliable: 'Y', | |
id_catalog_integ_category: 0, | |
id_catalog_integration: 1 | |
}) | |
const resultFilter = {where: {enabled: 'Y'}, | |
include: [{relation: 'catalog_integ_category'}]}; | |
const resultFind = await CatalogIntegrationModel.find(resultFilter); | |
console.log(resultFind) | |
expect(resultFind.length).toBe(1) | |
}) | |
}) |
https://github.com/strongloop/loopback/blob/master/test/model.test.js
it('Define a one to many relationship', function(done) {
const Book = dataSource.createModel('book', {title: String, author: String});
const Chapter = dataSource.createModel('chapter', {title: String});
// by referencing model
Book.hasMany(Chapter);
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
// using 'chapters' scope for build:
const c = book.chapters.build({title: 'Chapter 1'});
book.chapters.create({title: 'Chapter 2'}, function() {
c.save(function() {
Chapter.count({bookId: book.id}, function(err, count) {
assert.equal(count, 2);
book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) {
assert.equal(chapters.length, 1);
assert.equal(chapters[0].title, 'Chapter 1');
done();
});
});
});
});
});
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const loopback = module.exports = require('loopback');
const datasourceJuggler = require('loopback-datasource-juggler');
describe('Test datasource', () => {
let CatalogIntegrationModel
let CatalogIntegrationCategoryModel
})