Skip to content

Commit

Permalink
failed trades not recorded, tested in sell pol tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Sep 9, 2024
1 parent c4e92ef commit 807e9f3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/trade_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ class TradeExecutor {
for (v_it = trades.begin(); v_it != trades.end(); ++v_it) {
Trade<T>& trade = v_it->first;
typename T::Ptr rsrc = v_it->second;
ctx->NewDatum("Transactions")
->AddVal("TransactionId", ctx->NextTransactionID())
->AddVal("SenderId", supplier->id())
->AddVal("ReceiverId", requester->id())
->AddVal("ResourceId", rsrc->state_id())
->AddVal("Commodity", trade.request->commodity())
->AddVal("Time", ctx->time())
->Record();
if (rsrc->quantity() > cyclus::eps_rsrc()) {
ctx->NewDatum("Transactions")
->AddVal("TransactionId", ctx->NextTransactionID())
->AddVal("SenderId", supplier->id())
->AddVal("ReceiverId", requester->id())
->AddVal("ResourceId", rsrc->state_id())
->AddVal("Commodity", trade.request->commodity())
->AddVal("Time", ctx->time())
->Record();
}
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/toolkit/matl_sell_policy_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,58 @@ TEST_F(MatlSellPolicyTests, PackageLimit) {
ASSERT_THROW(sim.Run(), cyclus::ValueError);
}

TEST_F(MatlSellPolicyTests, PackageFailedTrade) {
using cyclus::QueryResult;

int dur = 1;

cyclus::MockSim sim(dur);
cyclus::Agent* a = new TestFacility(sim.context());

sim.context()->AddPrototype(a->prototype(), a);
sim.agent = sim.context()->CreateAgent<cyclus::Agent>(a->prototype());
// Add two sinks that both want commod
sim.AddSink("commod").capacity(5).Finalize();
sim.AddSink("commod").capacity(5).Finalize();
TestFacility* fac = dynamic_cast<TestFacility*>(sim.agent);

cyclus::toolkit::ResBuf<cyclus::Material> buf;

double qty = 7;
CompMap cm;
cm[922380000] = 1;
Composition::Ptr comp = Composition::CreateFromMass(cm);
mat = Material::Create(a, qty, comp, Package::unpackaged_name());

buf.Push(mat);

sim.context()->AddPackage("foo", 5, 5, "first");
Package::Ptr p = sim.context()->GetPackage("foo");

cyclus::toolkit::MatlSellPolicy sellpol;
sellpol.Init(fac, &buf, "buf", cyclus::CY_LARGE_DOUBLE, false, 0, p->name())
.Set("commod").Start();

EXPECT_NO_THROW(sim.Run());

// One of the sinks is given 5 (their request), and the other is given 2.
// Two kg can't be packaged, so the trade fails and the material traded
// is an untracked zero quantity. Therefore, only one transaction shows up
QueryResult qr_trans = sim.db().Query("Transactions", NULL);
EXPECT_EQ(1, qr_trans.rows.size());
EXPECT_EQ(0, qr_trans.GetVal<int>("Time", 0));

std::vector<cyclus::Cond> conds;
conds.push_back(cyclus::Cond("PackageName", "==", std::string("foo")));
QueryResult qr_res = sim.db().Query("Resources", &conds);
EXPECT_EQ(1, qr_res.rows.size());
EXPECT_EQ(5, qr_res.GetVal<double>("Quantity", 0));

// Not all material should have been transacted, one trade of 5 is the only
// non-zero trade
EXPECT_EQ(2, buf.quantity());

}

}
}

0 comments on commit 807e9f3

Please sign in to comment.