1- use serde_json:: Value ;
21use serial_test:: serial;
3- use spacetimedb_testing:: modules:: { CompilationMode , CompiledModule , LogLevel , LoggerRecord , DEFAULT_CONFIG } ;
2+ use spacetimedb_testing:: modules:: {
3+ CompilationMode , CompiledModule , LogLevel , LoggerRecord , ModuleHandle , DEFAULT_CONFIG ,
4+ } ;
45
56fn init ( ) {
67 let _ = env_logger:: builder ( )
@@ -13,6 +14,24 @@ fn init() {
1314 . try_init ( ) ;
1415}
1516
17+ async fn read_logs ( module : & ModuleHandle ) -> Vec < String > {
18+ module
19+ . read_log ( None )
20+ . await
21+ . trim ( )
22+ . split ( '\n' )
23+ . map ( |line| {
24+ let record: LoggerRecord = serde_json:: from_str ( line) . unwrap ( ) ;
25+ if matches ! ( record. level, LogLevel :: Panic | LogLevel :: Error | LogLevel :: Warn ) {
26+ panic ! ( "Found an error-like log line: {line}" ) ;
27+ }
28+ record. message
29+ } )
30+ . skip_while ( |line| line != "Database initialized" )
31+ . skip ( 1 )
32+ . collect :: < Vec < _ > > ( )
33+ }
34+
1635// The tests MUST be run in sequence because they read the OS environment
1736// and can cause a race when run in parallel.
1837
@@ -34,25 +53,9 @@ fn test_calling_a_reducer_in_module(module_name: &'static str) {
3453 let json = r#"{"call": {"fn": "list_over_age", "args": [30]}}"# . to_string ( ) ;
3554 module. send ( json) . await . unwrap ( ) ;
3655
37- let logs = module
38- . read_log ( None )
39- . await
40- . trim ( )
41- . split ( '\n' )
42- . map ( |line| {
43- let record: LoggerRecord = serde_json:: from_str ( line) . unwrap ( ) ;
44- if matches ! ( record. level, LogLevel :: Panic | LogLevel :: Error | LogLevel :: Warn ) {
45- panic ! ( "Found an error-like log line: {line}" ) ;
46- }
47- record. message
48- } )
49- . skip_while ( |line| line != "Database initialized" )
50- . collect :: < Vec < _ > > ( ) ;
51-
5256 assert_eq ! (
53- logs ,
57+ read_logs ( & module ) . await ,
5458 [
55- "Database initialized" ,
5659 "Hello, Tyrion!" ,
5760 "Hello, Cersei!" ,
5861 "Hello, World!" ,
@@ -89,18 +92,10 @@ fn test_calling_a_reducer_with_private_table() {
8992 let json = r#"{"call": {"fn": "query_private", "args": []}}"# . to_string ( ) ;
9093 module. send ( json) . await . unwrap ( ) ;
9194
92- let logs = module
93- . read_log ( None )
94- . await
95- . trim ( )
96- . split ( '\n' )
97- . map ( |line| serde_json:: from_str :: < LoggerRecord > ( line) . unwrap ( ) )
98- . collect :: < Vec < _ > > ( ) ;
99-
100- assert_eq ! ( logs. len( ) , 10 ) ;
101-
102- assert_eq ! ( logs[ 7 ] . message, "Private, Tyrion!" ) ;
103- assert_eq ! ( logs[ 8 ] . message, "Private, World!" ) ;
95+ assert_eq ! (
96+ read_logs( & module) . await ,
97+ [ "Private, Tyrion!" , "Private, World!" , ] . map( String :: from)
98+ ) ;
10499 } ,
105100 ) ;
106101}
@@ -120,30 +115,15 @@ fn test_call_query_macro() {
120115 . to_string ( ) ;
121116 module. send ( json) . await . unwrap ( ) ;
122117
123- let lines = module. read_log ( Some ( 13 ) ) . await ;
124- let lines: Vec < & str > = lines. trim ( ) . split ( '\n' ) . collect ( ) ;
125-
126- assert_eq ! ( lines. len( ) , 13 ) ;
127-
128- let json: Value = serde_json:: from_str ( lines[ 6 ] ) . unwrap ( ) ;
129118 assert_eq ! (
130- json[ "message" ] ,
131- Value :: String ( "Row count before delete: 1000" . to_string( ) )
132- ) ;
133- let json: Value = serde_json:: from_str ( lines[ 8 ] ) . unwrap ( ) ;
134- assert_eq ! (
135- json[ "message" ] ,
136- Value :: String ( "Row count after delete: 995" . to_string( ) )
137- ) ;
138- let json: Value = serde_json:: from_str ( lines[ 9 ] ) . unwrap ( ) ;
139- assert_eq ! (
140- json[ "message" ] ,
141- Value :: String ( "Row count filtered by condition: 995" . to_string( ) )
142- ) ;
143- let json: Value = serde_json:: from_str ( lines[ 11 ] ) . unwrap ( ) ;
144- assert_eq ! (
145- json[ "message" ] ,
146- Value :: String ( "Row count filtered by multi-column condition: 199" . to_string( ) )
119+ read_logs( & module) . await ,
120+ [
121+ "Row count before delete: 1000" ,
122+ "Row count after delete: 995" ,
123+ "Row count filtered by condition: 995" ,
124+ "Row count filtered by multi-column condition: 199" ,
125+ ]
126+ . map( String :: from)
147127 ) ;
148128 } ,
149129 ) ;
0 commit comments