我在編寫我正在處理的一段代碼的測試時遇到了麻煩。我有兩個表的,我想創建一個結合表,如下圖所示:爲計算字段創建結點表的記錄並測試進程
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
)
根據在some_data
領域的一些標準,我也要拉一組的費用和一組出貨量和分發各charges.amount
跨越這些貨件shipments.weight
。結果將被存儲distributed_details
。雖然我正在做這個發佈,但我還需要根據some_data
中的剩餘內容執行一些其他計算,這些內容將存儲在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) {}
}
有沒有一種很好的方法來測試這個函數,並確保這些分佈式數量實際上被拉到並分配給每個記錄?由於內存限制的原因,我已將每個細節的持久性委託給此方法內的detailMapper - 我有時不得不拉動成千上萬的出貨量,並且返回數組中的所有費用將使我內存不足。