BlinkUX / sequelize-mock

A simple mock interface specifically for testing code relying on Sequelize models
https://sequelize-mock.readthedocs.io
MIT License
139 stars 73 forks source link

Object Association Persistence? #38

Closed tills13 closed 6 years ago

tills13 commented 6 years ago

I have a flow that looks like

return Party.create().then((party) => {
  return PartyBudget.create({ 
    type: input.budgetType || BUDGET_UNLIMITED 
  }).then(pb => {
    return party.setBudget(pb)
  })
})

in my tests, I am making sure that providing no input to the above produces a Party with a PartyBudget with an unlimited budget.

return graphql(schema, query, {}, state).then((result) => {
  assert.equal(result.party.budget.type, BUDGET_UNLIMITED) // this doesn't work (type is null/undefined)

  return Party.findById(result.party.id).then((party) => {
    return party.getBudget().then((budget) => {
      assert.equal(budget.type, BUDGET_UNLIMITED) // this doesn't work (type is null/undefined)
    })
  })
})

For reasons (mostly obfuscation), the default value isn't set at the model level.

It seems that sequelize-mock creates a new instance instead of returning the associated object (in fact, setBudget is a noop).

Am I SOL on this (i.e. beyond the scope of sequelize-mock?)

LoveAndCoding commented 6 years ago

In your asserts, are you trying to check what has been "saved" into a DB? Or are you just trying to check that create is run with BUDGET_UNLIMITED?

tills13 commented 6 years ago

the first assert is checking the result of the create the second assert is checking the "saved" value

it's more of an integration test, which I guess is a little out of scope for this project.

LoveAndCoding commented 6 years ago

Integration tests are entirely possible with this mocking framework, however storing/retrieving results is not. The library is meant for mocking out the data for tests and returning controlled values. We do not store or retrieve data in any way.

If you want tests to run with an "mock" DB behind it so you can run tests that insert and then retrieve data, we recommend using native Sequelize with an in-memory SQLite DB. That will allow you to use Sequelize as you normally would and test data retrieval from an actual DB while also not polluting your real DBs. This is also the way the Sequelize team has recommended doing this sort of testing in the past.

Sorry about the confusion there. If I'm misunderstanding or if you have other questions don't hesitate to ask.

tills13 commented 6 years ago

Nope that's perfect - as I thought about it more and more I came to the same realization. Trying to shoehorn something into an existing testing framework to speed things up - this is the result. Thanks!