1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::Module;
use shiny_common::context::Context;
use shiny_common::error::ServiceError;
use shiny_jobs::Job;
use std::collections::HashMap;
use std::sync::Arc;

pub struct TestApplication {
    jobs: HashMap<String, Arc<dyn Job>>,
}

impl TestApplication {
    pub fn new<TClients, TModule>(clients: TClients, module: TModule) -> Self
    where
        TModule: Module<Clients = TClients>,
    {
        let controllers = module.create(clients);

        TestApplication {
            jobs: controllers.job.take_jobs().into_iter().collect(),
        }
    }

    pub async fn run_job(&self, name: &str, context: &mut Context) -> Result<(), ServiceError> {
        let Some(job) = self.jobs.get(name) else {
            panic!("job `{name}` does not exists")
        };
        job.execute(context).await?;
        return Ok(());
    }
}