@@ -368,18 +368,17 @@ impl FactorRuntimeConfigSource<OutboundMqttFactor> for TomlRuntimeConfigSource<'
368368
369369impl FactorRuntimeConfigSource < SqliteFactor > for TomlRuntimeConfigSource < ' _ , ' _ > {
370370 fn get_runtime_config ( & mut self ) -> anyhow:: Result < Option < spin_factor_sqlite:: RuntimeConfig > > {
371- Ok ( self
371+ let mut config = self
372372 . sqlite
373373 . resolve_from_toml ( & self . toml . table ) ?
374- . map ( |mut config| {
375- // If the user did not provide configuration for the default label, add it.
376- if !config. connection_creators . contains_key ( "default" ) {
377- config
378- . connection_creators
379- . insert ( "default" . to_owned ( ) , self . sqlite . default ( ) ) ;
380- }
381- config
382- } ) )
374+ . unwrap_or_default ( ) ;
375+ // If the user did not provide configuration for the default label, add it.
376+ if !config. connection_creators . contains_key ( "default" ) {
377+ config
378+ . connection_creators
379+ . insert ( "default" . to_owned ( ) , self . sqlite . default ( ) ) ;
380+ }
381+ Ok ( Some ( config) )
383382 }
384383}
385384
@@ -446,3 +445,71 @@ fn sqlite_config_resolver(
446445 local_database_dir,
447446 ) )
448447}
448+
449+ #[ cfg( test) ]
450+ mod tests {
451+ use spin_factors:: RuntimeFactors ;
452+
453+ use super :: * ;
454+
455+ #[ test]
456+ fn sqlite_is_configured_correctly ( ) {
457+ #[ derive( RuntimeFactors ) ]
458+ struct Factors {
459+ sqlite : SqliteFactor ,
460+ }
461+ impl TryFrom < TomlRuntimeConfigSource < ' _ , ' _ > > for FactorsRuntimeConfig {
462+ type Error = anyhow:: Error ;
463+
464+ fn try_from ( value : TomlRuntimeConfigSource < ' _ , ' _ > ) -> Result < Self , Self :: Error > {
465+ Self :: from_source ( value)
466+ }
467+ }
468+
469+ impl FactorsRuntimeConfig {
470+ /// Get the labels of the configured sqlite databases.
471+ fn configured_labels ( & self ) -> Vec < & str > {
472+ let mut configured_labels = self
473+ . sqlite
474+ . as_ref ( )
475+ . unwrap ( )
476+ . connection_creators
477+ . keys ( )
478+ . map ( |s| s. as_str ( ) )
479+ . collect :: < Vec < _ > > ( ) ;
480+ // Sort the labels to ensure consistent ordering.
481+ configured_labels. sort ( ) ;
482+ configured_labels
483+ }
484+ }
485+
486+ // Test that the default label is added if not provided.
487+ let toml = toml:: toml! {
488+ [ sqlite_database. foo]
489+ type = "spin"
490+ } ;
491+ let config =
492+ ResolvedRuntimeConfig :: < FactorsRuntimeConfig > :: new ( toml_resolver ( & toml) , None , false )
493+ . unwrap ( ) ;
494+ assert_eq ! (
495+ config. runtime_config. configured_labels( ) ,
496+ vec![ "default" , "foo" ]
497+ ) ;
498+
499+ // Test that the default label is added with an empty toml config.
500+ let toml = toml:: Table :: new ( ) ;
501+ let config =
502+ ResolvedRuntimeConfig :: < FactorsRuntimeConfig > :: new ( toml_resolver ( & toml) , None , false )
503+ . unwrap ( ) ;
504+ assert_eq ! ( config. runtime_config. configured_labels( ) , vec![ "default" ] ) ;
505+ }
506+
507+ fn toml_resolver ( toml : & toml:: Table ) -> TomlResolver < ' _ > {
508+ TomlResolver :: new (
509+ toml,
510+ None ,
511+ UserProvidedPath :: Default ,
512+ UserProvidedPath :: Default ,
513+ )
514+ }
515+ }
0 commit comments