I am having trouble writing a test for a piece of code I am working on. I have two tables for which I am trying to create a junction table, as below:
CREATE TABLE charges (
id bigint PRIMARY KEY,
amount float,
some_data hstore
)
CREATE TABLE shipments (
id bigint PRIMARY KEY,
weight float,
some_data hstore
)
CREATE TABLE distributed_details (
charges_id bigint references charges (id),
shipments_id biging references shipments (id),
amount float,
another_calculated_field varchar
)
Based on some criteria in the some_data
fields, I have to pull a set of charges and a set of shipments and distribute each charges.amount
across these shipments by shipments.weight
. The results will be stored distributed_details
. While I am doing this distribution, I also have to perform some other calculations, based on the remaining stuff in some_data
, which will be stored in distributed_details.another_calculated_field
.
class Distributor
{
public $detailMapper;
public function distribute($charges, $shipments)
{
foreach ($charges as $charge) {
$distributedAmounts = $this->calculateDistributedAmounts($charge->getAmount(), $shipments);
foreach ($shipments as $shipment) {
$distributed_detail = $this->buildDistributedDetail($charge, $shipment, array_shift($distributedAmounts));
$this->detailMapper->save($distributed_detail);
}
}
}
public function calculateDistributedAmounts($shipments) {}
public function buildDistributedDetail($charge, $shipment, $distributedAmount) {}
}
Is there a good way of testing this function, and making sure that those distributed amounts are actually pulled and assigned to each record? I have delegated the persisting of each detail to a detailMapper inside this method for reasons of memory limits - I sometimes have to pull tens of thousands of shipments, and returning all of the resulting charges in an array will run me out of memory.