From 4494d62be37985efaf3b1f7ee9c44f82c3173811 Mon Sep 17 00:00:00 2001 From: Katie Mummah Date: Tue, 3 Sep 2024 12:51:35 -0500 Subject: [PATCH] failed trades not recorded, tested in sell pol tests --- src/trade_executor.h | 18 +++++---- tests/toolkit/matl_sell_policy_tests.cc | 53 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/trade_executor.h b/src/trade_executor.h index be144952b0..17f12a4f17 100644 --- a/src/trade_executor.h +++ b/src/trade_executor.h @@ -89,14 +89,16 @@ class TradeExecutor { for (v_it = trades.begin(); v_it != trades.end(); ++v_it) { Trade& 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(); + } } } } diff --git a/tests/toolkit/matl_sell_policy_tests.cc b/tests/toolkit/matl_sell_policy_tests.cc index eea8be71dc..9b3c376870 100644 --- a/tests/toolkit/matl_sell_policy_tests.cc +++ b/tests/toolkit/matl_sell_policy_tests.cc @@ -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(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(sim.agent); + + cyclus::toolkit::ResBuf 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("Time", 0)); + + std::vector 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("Quantity", 0)); + + // Not all material should have been transacted, one trade of 5 is the only + // non-zero trade + EXPECT_EQ(2, buf.quantity()); + +} + } }