Screenbeetle's avatar

Advice please on how to test real order data (to check that it tallies correctly)

I'm building an ecommerce app (Laravel 5.3) that is replacing one I originally built in Zend.

The order data is being sucked in from the old app via seeds. These seeds are having to populate various new fields including an 'order_total' (which stupidly didn't exist in the old app) as well as fields like pre_discount_total and post_discount_total.

What I would like to do is run through the database to check the new figures all add up correctly. I started writing tests to verify the new totals but since these will have to loop through the entire orders table I'm having second thoughts about the best approach. I though it may be better to do this via sql directly on the db. The issue there is that I can't so easily share the tests with my colleague who is also tinkering with the new orders table and fields.

How would you go about this?

Example test:

 /** @test */
    public function is_vat_calculated_correctly()
    {
        $vat_rate = \Config::get('constants.VAT_RATE'); // UK tax

    // just plucking an order at random for now but was going to 
    // get and loop through all orders before I had second thoughts
        $order    = Order::where('customer_id', 690)->first(); 

        $goods_total = $order->goods_total;
        $delivery    = $order->delivery;
        $vat         = $order->vat;

        $correct_vat = ($goods_total + $delivery) * $vat_rate;

        $this->assertEquals($correct_vat, $vat);
    }
0 likes
3 replies
ohffs's avatar
ohffs
Best Answer
Level 50

In the past when I've had to migrate old db's I've written a small conversion class - then wrote tests to satisfy myself that it was catching all the edge cases. So I would end up with something like :

$oldOrder = new OldOrder(...);
$transformer = new LegacyTransformer;
$newOrder = $transformer->order($oldOrder);

$this->assertBlah...
$this->assertEqualsBlah...

Then I just make my import script/seeder/whatever use that class and I'm pretty confident it'll work. Not sure if that helps!

Screenbeetle's avatar

@ohffs - that helps a lot. For starters writing a conversion class is obviously way better than me doing it in seeders. And would obviously make testing a ton easier.

Nice - ta. Obvious now that I think about it. Much appreciated!

1 like
ohffs's avatar

No worries - glad it helped :-)

Please or to participate in this conversation.