def test_creating_an_empty_population_and_adding_attributes_later_should_be_possible( ): # empty population a = Population(circus=None, size=0) assert a.ids.shape[0] == 0 # empty attributes a.create_attribute("att1") a.create_attribute("att2") dynamically_created = pd.DataFrame( { "att1": [1, 2, 3], "att2": [11, 12, 13], }, index=["ac1", "ac2", "ac3"]) a.update(dynamically_created) assert a.ids.tolist() == ["ac1", "ac2", "ac3"] assert a.get_attribute_values("att1", ["ac1", "ac2", "ac3"]).tolist() == [1, 2, 3] assert a.get_attribute_values( "att2", ["ac1", "ac2", "ac3"]).tolist() == [11, 12, 13]
def test_insert_poppulation_value_for_existing_populations_should_update_all_values( ): # copy of dummy population that will be updated tested_population = Population(circus=None, size=10, ids_gen=SequencialGenerator(max_length=1, prefix="a_")) ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23] tested_population.create_attribute("age", init_values=ages) city = ["a", "b", "b", "a", "d", "e", "r", "a", "z", "c"] tested_population.create_attribute("city", init_values=city) current = tested_population.get_attribute_values("age", ["a_0", "a_7", "a_9"]) assert current.tolist() == [10, 39, 23] update = pd.DataFrame({ "age": [139, 123], "city": ["city_7", "city_9"] }, index=["a_7", "a_9"]) tested_population.update(update) # we should have the same number of populations assert tested_population.ids.shape[0] == 10 updated_age = tested_population.get_attribute_values( "age", ["a_0", "a_7", "a_9"]) updated_city = tested_population.get_attribute_values( "city", ["a_0", "a_7", "a_9"]) assert updated_age.tolist() == [10, 139, 123] assert updated_city.tolist() == ["a", "city_7", "city_9"]
def test_insert_op_population_value_for_existing_populations_should_update_all_values( ): # same as test above but triggered as an Operation on story data # copy of dummy population that will be updated tested_population = Population(circus=None, size=10, ids_gen=SequencialGenerator(max_length=1, prefix="a_")) ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23] tested_population.create_attribute("age", init_values=ages) city = ["a", "b", "b", "a", "d", "e", "r", "a", "z", "c"] tested_population.create_attribute("city", init_values=city) story_data = pd.DataFrame( { "the_new_age": [139, 123, 1, 2], "location": ["city_7", "city_9", "city_11", "city_10"], "updated_populations": ["a_7", "a_9", "a_11", "a_10"] }, index=["d_1", "d_2", "d_4", "d_3"]) update_op = tested_population.ops.update(id_field="updated_populations", copy_attributes_from_fields={ "age": "the_new_age", "city": "location" }) story_data_2, logs = update_op(story_data) # there should be no impact on the story data assert story_data_2.shape == (4, 3) assert sorted(story_data_2.columns.tolist()) == [ "location", "the_new_age", "updated_populations" ] # we should have 2 new populations assert tested_population.ids.shape[0] == 12 updated_age = tested_population.get_attribute_values( "age", ["a_0", "a_7", "a_9", "a_10", "a_11"]) updated_city = tested_population.get_attribute_values( "city", ["a_0", "a_7", "a_9", "a_10", "a_11"]) assert updated_age.tolist() == [10, 139, 123, 2, 1] assert updated_city.tolist() == [ "a", "city_7", "city_9", "city_10", "city_11" ]