@@ -99,23 +99,153 @@ impl Transaction for MemoryTransaction {
9999 }
100100}
101101
102- #[ cfg( test) ]
103- mod test {
102+ #[ cfg( all( test, target_arch = "wasm32" ) ) ]
103+ mod wasm_tests {
104+ use super :: * ;
105+ use crate :: catalog:: { ColumnCatalog , ColumnDesc , ColumnRef , TableName } ;
106+ use crate :: db:: { DataBaseBuilder , ResultIter } ;
107+ use crate :: expression:: range_detacher:: Range ;
104108 use crate :: storage:: Iter ;
109+ use crate :: types:: tuple:: Tuple ;
110+ use crate :: types:: value:: DataValue ;
111+ use crate :: types:: LogicalType ;
112+ use crate :: utils:: lru:: SharedLruCache ;
113+ use itertools:: Itertools ;
105114 use std:: collections:: { BTreeMap , Bound } ;
106115 use std:: hash:: RandomState ;
107116 use std:: sync:: Arc ;
108- use itertools:: Itertools ;
117+ use wasm_bindgen_test:: * ;
118+
119+ #[ wasm_bindgen_test]
120+ fn memory_storage_roundtrip ( ) -> Result < ( ) , DatabaseError > {
121+ let storage = MemoryStorage :: new ( ) ;
122+ let mut transaction = storage. transaction ( ) ?;
123+ let table_cache = Arc :: new ( SharedLruCache :: new ( 4 , 1 , RandomState :: new ( ) ) ?) ;
124+ let columns = Arc :: new ( vec ! [
125+ ColumnRef :: from( ColumnCatalog :: new(
126+ "c1" . to_string( ) ,
127+ false ,
128+ ColumnDesc :: new( LogicalType :: Integer , Some ( 0 ) , false , None ) . unwrap( ) ,
129+ ) ) ,
130+ ColumnRef :: from( ColumnCatalog :: new(
131+ "c2" . to_string( ) ,
132+ false ,
133+ ColumnDesc :: new( LogicalType :: Boolean , None , false , None ) . unwrap( ) ,
134+ ) ) ,
135+ ] ) ;
136+
137+ let source_columns = columns
138+ . iter ( )
139+ . map ( |col_ref| ColumnCatalog :: clone ( col_ref) )
140+ . collect_vec ( ) ;
141+ transaction. create_table (
142+ & table_cache,
143+ "test" . to_string ( ) . into ( ) ,
144+ source_columns,
145+ false ,
146+ ) ?;
147+
148+ transaction. append_tuple (
149+ & "test" . to_string ( ) ,
150+ Tuple :: new (
151+ Some ( DataValue :: Int32 ( 1 ) ) ,
152+ vec ! [ DataValue :: Int32 ( 1 ) , DataValue :: Boolean ( true ) ] ,
153+ ) ,
154+ & [
155+ LogicalType :: Integer . serializable ( ) ,
156+ LogicalType :: Boolean . serializable ( ) ,
157+ ] ,
158+ false ,
159+ ) ?;
160+ transaction. append_tuple (
161+ & "test" . to_string ( ) ,
162+ Tuple :: new (
163+ Some ( DataValue :: Int32 ( 2 ) ) ,
164+ vec ! [ DataValue :: Int32 ( 2 ) , DataValue :: Boolean ( true ) ] ,
165+ ) ,
166+ & [
167+ LogicalType :: Integer . serializable ( ) ,
168+ LogicalType :: Boolean . serializable ( ) ,
169+ ] ,
170+ false ,
171+ ) ?;
172+
173+ let mut read_columns = BTreeMap :: new ( ) ;
174+ read_columns. insert ( 0 , columns[ 0 ] . clone ( ) ) ;
175+
176+ let mut iter = transaction. read (
177+ & table_cache,
178+ "test" . to_string ( ) . into ( ) ,
179+ ( Some ( 1 ) , Some ( 1 ) ) ,
180+ read_columns,
181+ true ,
182+ ) ?;
183+
184+ let option_1 = iter. next_tuple ( ) ?;
185+ assert_eq ! ( option_1. unwrap( ) . pk, Some ( DataValue :: Int32 ( 2 ) ) ) ;
186+
187+ let option_2 = iter. next_tuple ( ) ?;
188+ assert_eq ! ( option_2, None ) ;
189+
190+ Ok ( ( ) )
191+ }
192+
193+ #[ wasm_bindgen_test]
194+ fn memory_storage_read_by_index ( ) -> Result < ( ) , DatabaseError > {
195+ let kite_sql = DataBaseBuilder :: path ( "./memory" ) . build_in_memory ( ) ?;
196+ kite_sql
197+ . run ( "create table t1 (a int primary key, b int)" ) ?
198+ . done ( ) ?;
199+ kite_sql
200+ . run ( "insert into t1 (a, b) values (0, 0), (1, 1), (2, 2), (3, 4)" ) ?
201+ . done ( ) ?;
202+
203+ let transaction = kite_sql. storage . transaction ( ) ?;
204+ let table_name: TableName = "t1" . to_string ( ) . into ( ) ;
205+ let table = transaction
206+ . table ( kite_sql. state . table_cache ( ) , table_name. clone ( ) ) ?
207+ . unwrap ( )
208+ . clone ( ) ;
209+ let pk_index = table. indexes ( ) . next ( ) . unwrap ( ) . clone ( ) ;
210+ let mut iter = transaction. read_by_index (
211+ kite_sql. state . table_cache ( ) ,
212+ table_name,
213+ ( Some ( 0 ) , None ) ,
214+ table. columns ( ) . cloned ( ) . enumerate ( ) . collect ( ) ,
215+ pk_index,
216+ vec ! [ Range :: Scope {
217+ min: Bound :: Excluded ( DataValue :: Int32 ( 0 ) ) ,
218+ max: Bound :: Included ( DataValue :: Int32 ( 2 ) ) ,
219+ } ] ,
220+ true ,
221+ ) ?;
222+
223+ let mut result = Vec :: new ( ) ;
224+ while let Some ( tuple) = iter. next_tuple ( ) ? {
225+ result. push ( tuple. pk . unwrap ( ) ) ;
226+ }
227+
228+ assert_eq ! ( result, vec![ DataValue :: Int32 ( 1 ) , DataValue :: Int32 ( 2 ) ] ) ;
229+
230+ Ok ( ( ) )
231+ }
232+ }
233+
234+ #[ cfg( all( test, not( target_arch = "wasm32" ) ) ) ]
235+ mod native_tests {
236+ use super :: * ;
109237 use crate :: catalog:: { ColumnCatalog , ColumnDesc , ColumnRef , TableName } ;
110238 use crate :: db:: { DataBaseBuilder , ResultIter } ;
111- use crate :: errors:: DatabaseError ;
112239 use crate :: expression:: range_detacher:: Range ;
113- use crate :: storage:: memory:: MemoryStorage ;
114- use crate :: storage:: { Storage , Transaction } ;
115- use crate :: types:: LogicalType ;
240+ use crate :: storage:: Iter ;
116241 use crate :: types:: tuple:: Tuple ;
117242 use crate :: types:: value:: DataValue ;
243+ use crate :: types:: LogicalType ;
118244 use crate :: utils:: lru:: SharedLruCache ;
245+ use itertools:: Itertools ;
246+ use std:: collections:: { BTreeMap , Bound } ;
247+ use std:: hash:: RandomState ;
248+ use std:: sync:: Arc ;
119249
120250 #[ test]
121251 fn memory_storage_roundtrip ( ) -> Result < ( ) , DatabaseError > {
@@ -139,7 +269,12 @@ mod test {
139269 . iter ( )
140270 . map ( |col_ref| ColumnCatalog :: clone ( col_ref) )
141271 . collect_vec ( ) ;
142- transaction. create_table ( & table_cache, "test" . to_string ( ) . into ( ) , source_columns, false ) ?;
272+ transaction. create_table (
273+ & table_cache,
274+ "test" . to_string ( ) . into ( ) ,
275+ source_columns,
276+ false ,
277+ ) ?;
143278
144279 transaction. append_tuple (
145280 & "test" . to_string ( ) ,
@@ -221,10 +356,7 @@ mod test {
221356 result. push ( tuple. pk . unwrap ( ) ) ;
222357 }
223358
224- assert_eq ! (
225- result,
226- vec![ DataValue :: Int32 ( 1 ) , DataValue :: Int32 ( 2 ) ]
227- ) ;
359+ assert_eq ! ( result, vec![ DataValue :: Int32 ( 1 ) , DataValue :: Int32 ( 2 ) ] ) ;
228360
229361 Ok ( ( ) )
230362 }
0 commit comments