Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexes wrong format in integration test #2809

Closed
ghost opened this issue Apr 29, 2019 · 6 comments
Closed

indexes wrong format in integration test #2809

ghost opened this issue Apr 29, 2019 · 6 comments

Comments

@ghost
Copy link

ghost commented Apr 29, 2019

I've written an integration test for a function from a repository. I want to check if the model is created correctly but when i am calling "npm test" i am getting an error.
In the function i am accessing the index definition with "repository.modelClass.definition.settings.indexes.preisliste_bku".
When i start the app and debug to this point, the value of the index is an array of objects so that i can use the forEach loop.

The index definition in the "Preisliste"-model:
image

The array of objects:
image

But when i am calling the function from the test i am getting the error because "bku.keys" is not an array of objects but an object so the forEach loop does not work.

The format of bku.keys in the test:
image

I do not understand why the format is diffrent when i start the app and when i am running the test.

Hope the information are sufficient. Thank you guys for any response.

@bajtos
Copy link
Member

bajtos commented Apr 29, 2019

What connector are you using? It's difficult to answer your question without an application we could use to reproduce the problem ourselves. Please create a small app per our bug reporting instructions.

@dhmlau dhmlau added the needs steps to reproduce Issues missing a small app and/or instructions for reproducing the problem label Apr 29, 2019
@ghost
Copy link
Author

ghost commented Apr 30, 2019

I've created a little app (with postgres-connector in todo-example) for you to understand my problem:
https://github.com/nike171196/loopback-next/tree/wrong-index-format

Steps to reproduce:

  1. Open the API-explorer and call the action "/todos/index" from the todo.controller. The return value is in format array of objects. This is what i except from the test too.

  2. Run "npm test". In the test i wrote, the same function (getModelIndex()) is called like in the controller but the return value is not the same.

I hope this helps you to understand what i am talking about.

@dhmlau dhmlau removed the needs steps to reproduce Issues missing a small app and/or instructions for reproducing the problem label May 17, 2019
@bajtos
Copy link
Member

bajtos commented Jun 4, 2019

I am afraid I am not able to run your application because I automigration does not work and thus I don't have any way how to create necessary database schemas :(

However, I see a problem in your test:

https://github.com/nike171196/loopback-next/blob/e95d46c2728b125eae887a646a24156346a5cac7/examples/todo/src/__tests__/integration/repositories/todo.repository.integration.ts#L11-L13

    const index = repository.getModelIndex();
    console.log(index);
    expect(index).to.be.type('object[]', 'Should be type array of objects.');

type assertion (docs) is accepting one of string values that typeof operator returns (see their list e.g. here). object[] is not a valid typeof value.

In JavaScript, Arrays are considered as objects.

expect(index).to.be.type('object');

You can use instanceof operator to verify that the value is an instance of Array:

expect(index).to.be.instanceof(Array);

Another problematic part I noticed: your application is loading & binding BaseRepository as if it was a repository bound to a model. That's why the migration script fails with the following error:

Cannot resolve injected arguments for BaseRepository.[0]: The arguments[0] is not decorated for dependency injection, but a value is not supplied

The solution I recommended in the past is to move generic repository classes to a different location where @loopback/boot is not going to pick them up. For example, src/base-repositories/base.repository.ts You will need to run npm run clean to remove the base repository from dist too.


With that change in place, I managed to automigrate the database and run the test suite. AFAICT, this is the value returned by getModelIndex method:

{
  todo_bku: { keys: { title: 1, desc: 1 }, options: { unique: true } }
}

The value look reasonable to me it's a key-value map where the key is the index name and the value is an object describing (defining) the index. What makes you expect an array value instead?

@bajtos
Copy link
Member

bajtos commented Jun 4, 2019

I was relatively recently looking into foreign keys and (unique) indexes in #2712, I described the currently available syntax in this document. For the future, I proposed a slightly improved syntax, see #2766.

@bajtos bajtos self-assigned this Jun 4, 2019
@bajtos
Copy link
Member

bajtos commented Jun 4, 2019

Open the API-explorer and call the action "/todos/index" from the todo.controller. The return value is in format array of objects. This is what i except from the test too.

I see an object, not an array.

Screen Shot 2019-06-04 at 15 11 10

@bajtos bajtos removed their assignment Jun 18, 2019
@mastermunj
Copy link
Contributor

Another problematic part I noticed: your application is loading & binding BaseRepository as if it was a repository bound to a model. That's why the migration script fails with the following error:

Cannot resolve injected arguments for BaseRepository.[0]: The arguments[0] is not decorated for dependency injection, but a value is not supplied

The solution I recommended in the past is to move generic repository classes to a different location where @loopback/boot is not going to pick them up. For example, src/base-repositories/base.repository.ts You will need to run npm run clean to remove the base repository from dist too.

I believe the above must be documented somewhere in repositories docs. I was struggling with migration for the past 3 days and finally stumbled upon this message. Priceless!

Thank you so much @bajtos

@deepakrkris deepakrkris self-assigned this May 7, 2020
@deepakrkris deepakrkris removed the Docs label May 7, 2020
@bajtos bajtos closed this as completed Mar 11, 2021
@loopbackio loopbackio locked and limited conversation to collaborators Mar 11, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants