Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Ability to specify order of the transactions #456

Closed
tu1ly opened this issue Apr 21, 2016 · 14 comments
Closed

Ability to specify order of the transactions #456

tu1ly opened this issue Apr 21, 2016 · 14 comments

Comments

@tu1ly
Copy link

tu1ly commented Apr 21, 2016

current:
Dredd runs the transactions as they’re specified in blueprint.
expected:
ability to define transactions order. Othervise 2 blueprints which are sematicaly the same could behave differently when sharing data across the transactions

@honzajavorek honzajavorek changed the title ability to spec. the transactions order Ability to specify order of the transactions Apr 21, 2016
@smizell
Copy link
Contributor

smizell commented May 11, 2016

@tu1ly Cool idea :) Did this cause an issue for you? Would it be difficult to keep track of two lists of transactions—one in the blueprint and one in your Dredd hooks/configuration?

Would the ability to randomize the tests be a solution here?

@lazlojuly
Copy link
Contributor

Maybe the "only" option's array order could be used as first implementation?

@Souler
Copy link

Souler commented Sep 16, 2016

I ran into the same issue today, I ended up using the beforeAll hook

var order = [
    // Your API names in the wanted order of execution
]

hooks.beforeAll(function(transactions, done) {
    transactions.sort(function(a, b) {
        var aIdx = order.indexOf(a.name)
        var bIdx = order.indexOf(b.name)
        return aIdx - bIdx
    })
    done()
})

@honzajavorek
Copy link
Contributor

@Souler This is an interesting solution!

Just to add to the discussion, Dredd is primarily a tool designed to perform isolated tests and hooks are here to help you to isolate each test. See docs on this.

If you need to order your transactions, it's a sign you're not creating isolated tests. YMMV if that's good or bad.

@adityasaxena
Copy link

@Souler Thank you very much! You saved my day!

@honzajavorek
Copy link
Contributor

Closing in favor of #995

@shivshankar3578
Copy link

I ran into the same issue today, I ended up using the beforeAll hook

var order = [
    // Your API names in the wanted order of execution
]

hooks.beforeAll(function(transactions, done) {
    transactions.sort(function(a, b) {
        var aIdx = order.indexOf(a.name)
        var bIdx = order.indexOf(b.name)
        return aIdx - bIdx
    })
    done()
})

Not working for me @DrEdd v12.0.7

@honzajavorek
Copy link
Contributor

@shivshankar3578 Could you share more in a separate issue with a full bug report? This should work.

@shivshankar3578
Copy link


let testFlow = [
  "GET /services/ug/{id}",
  "POST /services/ug",
  "POST /services/account/add_user",
  "POST /services/ug/user",
  ]


hooks.beforeAll(function(transactions, done) {
  hooks.log("before all");
  transactions = _(transactions)
  .map(item=> {
    item.name = item.request.method+" "+item.origin.resourceName
    return item
  })
  .filter(item=>{
    return testFlow.indexOf(item.name) >= 0
  })
  .sortBy(item=>{
    return testFlow.indexOf(item.name)
  })
  .value()

  done();
});

And in

hooks.beforeEach(function(transaction, done) {

I'm not getting GET /services/ug/{id} First
Tried with done(transactions), done(null, transactions), hooks.configuration.only = transactionsNames but nothing works

@honzajavorek
Copy link
Contributor

What does _(transactions) do? Does it copy the array? You need to modify the original transactions array.

@shivshankar3578
Copy link

shivshankar3578 commented Sep 30, 2019

No, it doesn't and I'm reassigning transactions again after modification. It's not immutable also.The Array functions (filter, sort or map) return a new modified array so I need to reassigning it again.

@honzajavorek
Copy link
Contributor

It cannot be reassigned. This is a hack which counts with the fact that you modify the original array, the one transactions variable references to. So transactions = won't work, because ten transactions will reference to a different array in memory. This could help you to achieve what you need.

@shivshankar3578
Copy link

Got it, but it's a bit confusing, Either you should freeze the array or impl. it using immutable way

@honzajavorek
Copy link
Contributor

It is confusing, it is a kind of a hack. As you say, ideally there would be a nice immutable interface to achieve this. We're working on decoupling things in Dredd internally which would allow that in the long run.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants