mercredi 6 avril 2016

How to test a method that runs an SQL query

I've made a custom SQL query and I want to create a unit tests to demonstrate that it works. I have tested it in SQLDeveloper and it works as I want it to on my test database but I want to leave a unit test for those who have to maintain this code later.

def report_of_merchants_who_have_not_pressed_the_service_rendered_button
    sql = "SELECT MIN(departure_date), ch_invoice.invoice_id
    FROM ch_invoice
    INNER JOIN ch_trip
    ON ch_invoice.invoice_id = ch_trip.invoice_id
    WHERE departure_date < SYSDATE
    AND service_rendered = 0
    AND paid = 1
    Group By ch_invoice.invoice_id"

    report = ActiveRecord::Base.connection.exec_query(sql)
    render json: report
  end

My thought was to create four invoices, three of which are not meeting the above criteria and one that does. Where I am getting stuck is how to check that the query only returns back one response. How do I test that one row of the query is coming out?

Aucun commentaire:

Enregistrer un commentaire