@@ -171,13 +171,19 @@ def test__init__(self, fix1_model, postgres_uri):
171171 # model=model, model_reporters={"total_agents": "sum"}
172172 # )
173173
174+ model .test_dc = DataCollector (
175+ model = model ,
176+ agent_reporters = {"wealth" : lambda m : 1 }
177+ )
178+ assert model .test_dc is not None
179+
174180 with pytest .raises (
175- TypeError ,
176- match = "Agent reporter for 'wealth' must be a string" ,
181+ beartype . roar . BeartypeCallHintParamViolation ,
182+ match = "not instance of str" ,
177183 ):
178184 model .test_dc = DataCollector (
179- model = model ,
180- agent_reporters = {"wealth" : lambda m : 1 }, # This is now illegal
185+ model = model ,
186+ agent_reporters = {123 : "wealth" }
181187 )
182188
183189 with pytest .raises (
@@ -729,3 +735,47 @@ def test_batch_save(self, fix2_model):
729735 assert sorted (agent_df_step2_batch1 ["wealth" ].to_list ()) == sorted (
730736 expected_wealth_s2b1
731737 )
738+
739+ def test_collect_no_agentsets_list (self , fix1_model , caplog ):
740+ """Tests that the collector logs an error and exits gracefully if _agentsets is missing."""
741+ model = fix1_model
742+ del model .sets ._agentsets
743+
744+ dc = DataCollector (model = model , agent_reporters = {"wealth" : "wealth" })
745+ dc .collect ()
746+
747+ assert "could not find '_agentsets'" in caplog .text
748+ assert dc .data ["agent" ].shape == (0 , 0 )
749+
750+ def test_collect_agent_set_no_df (self , fix1_model , caplog ):
751+ """Tests that the collector logs a warning and skips a set if it has no .df attribute."""
752+
753+ class NoDfSet :
754+ def __init__ (self ):
755+ self .__class__ = type ("NoDfSet" , (object ,), {})
756+
757+ fix1_model .sets ._agentsets .append (NoDfSet ())
758+
759+ fix1_model .dc = DataCollector (fix1_model , agent_reporters = {"wealth" : "wealth" , "age" : "age" })
760+ fix1_model .dc .collect ()
761+
762+ assert "has no 'df' attribute" in caplog .text
763+ assert fix1_model .dc .data ["agent" ].shape == (12 , 7 )
764+
765+ def test_collect_df_no_unique_id (self , fix1_model , caplog ):
766+ """Tests that the collector logs a warning and skips a set if its df has no unique_id."""
767+ bad_set = fix1_model .sets ._agentsets [0 ]
768+ bad_set ._df = bad_set ._df .drop ("unique_id" )
769+
770+ fix1_model .dc = DataCollector (fix1_model , agent_reporters = {"wealth" : "wealth" , "age" : "age" })
771+ fix1_model .dc .collect ()
772+
773+ assert "has no 'unique_id' column" in caplog .text
774+ assert fix1_model .dc .data ["agent" ].shape == (8 , 7 )
775+
776+ def test_collect_no_matching_reporters (self , fix1_model ):
777+ """Tests that the collector returns an empty frame if no reporters match any columns."""
778+ fix1_model .dc = DataCollector (fix1_model , agent_reporters = {"baz" : "foo" , "qux" : "bar" })
779+ fix1_model .dc .collect ()
780+
781+ assert fix1_model .dc .data ["agent" ].shape == (0 , 0 )
0 commit comments