use crate::common::*;
use bevy::prelude::*;
use bevy_persistence_database::{
Guid, PersistentQuery, commit_sync, persistence_plugin::PersistencePlugins,
};
use bevy_persistence_database_derive::db_matrix_test;
fn test_persistent_query_system(mut query: PersistentQuery<(&Health, &Position)>) {
query.ensure_loaded();
for (entity, (health, position)) in query.iter() {
println!(
"Entity {:?} has health {} and position ({}, {})",
entity, health.value, position.x, position.y
);
}
}
#[db_matrix_test]
fn test_persistent_query_system_param() {
let (db, _container) = setup();
let mut app = App::new();
app.add_plugins(PersistencePlugins::new(db.clone()));
let entity_high_health = app
.world_mut()
.spawn((Health { value: 150 }, Position { x: 10.0, y: 20.0 }))
.id();
let entity_low_health = app
.world_mut()
.spawn((Health { value: 50 }, Position { x: 5.0, y: 5.0 }))
.id();
app.update();
commit_sync(&mut app, db.clone(), TEST_STORE).expect("Initial commit failed");
let high_health_guid = app
.world()
.get::<Guid>(entity_high_health)
.unwrap()
.id()
.to_string();
let low_health_guid = app
.world()
.get::<Guid>(entity_low_health)
.unwrap()
.id()
.to_string();
let mut app2 = App::new();
app2.add_plugins(PersistencePlugins::new(db.clone()));
app2.add_systems(bevy::prelude::Update, test_persistent_query_system);
app2.update();
let mut health_query = app2.world_mut().query::<&Health>();
let health_count = health_query.iter(&app2.world()).count();
assert_eq!(
health_count, 2,
"Should have loaded two entities with Health component"
);
let mut position_query = app2.world_mut().query::<&Position>();
let position_count = position_query.iter(&app2.world()).count();
assert_eq!(
position_count, 2,
"Should have loaded two entities with Position component"
);
let mut guid_query = app2.world_mut().query::<&Guid>();
let guids: Vec<String> = guid_query
.iter(&app2.world())
.map(|guid| guid.id().to_string())
.collect();
assert!(
guids.contains(&high_health_guid),
"High health entity not loaded"
);
assert!(
guids.contains(&low_health_guid),
"Low health entity not loaded"
);
}
#[db_matrix_test]
fn test_persistent_query_with_filter() {
let (db, _container) = setup();
let mut app = App::new();
app.add_plugins(PersistencePlugins::new(db.clone()));
app.world_mut()
.spawn((Health { value: 150 }, Position { x: 10.0, y: 20.0 }));
app.world_mut()
.spawn((Health { value: 50 }, Position { x: 5.0, y: 5.0 }));
app.world_mut()
.spawn((Health { value: 100 }, Position { x: 15.0, y: 15.0 }));
app.update();
commit_sync(&mut app, db.clone(), TEST_STORE).expect("Initial commit failed");
let mut app2 = App::new();
app2.add_plugins(PersistencePlugins::new(db.clone()));
fn filtered_query_system(mut query: PersistentQuery<(&Health, &Position)>) {
query = query.filter(Health::value().gt(100));
query.ensure_loaded();
for (_entity, (health, position)) in query.iter() {
println!(
"Entity has health {} and position ({}, {})",
health.value, position.x, position.y
);
assert!(
health.value > 100,
"Filter should only return entities with health > 100"
);
}
}
app2.add_systems(bevy::prelude::Update, filtered_query_system);
app2.update();
let mut health_query = app2.world_mut().query::<&Health>();
let entities_with_health: Vec<i32> = health_query
.iter(&app2.world())
.map(|health| health.value)
.collect();
assert_eq!(
entities_with_health.len(),
1,
"Should have loaded only one entity"
);
assert!(
entities_with_health.contains(&150),
"Should have loaded only the high health entity"
);
}