Skip to content

Commit 6f56cd0

Browse files
authored
Add tests for Table.get_row and fix a bug (#13795)
Fixes #12317 Adds tests for get_row and fixes get_row -1 out of bounds for In_DB
1 parent 0db2757 commit 6f56cd0

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

distribution/lib/Standard/Database/0.0.0-dev/src/Internal/DB_Table_Implementation.enso

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ type DB_Table_Implementation
113113

114114
get_row (this_table : Table & DB_Table) index:Integer ~if_missing:Any =
115115
Feature.Sample.if_supported_else_throw this_table.connection.dialect "get_row" <|
116-
if index == -1 then this_table.last_row else
117-
real_row = (if index < 0 then index + this_table.row_count else index)
118-
if real_row < 0 then if_missing else
119-
this_table.rows real_row+1 . get real_row if_missing
116+
real_row = (if index < 0 then index + this_table.row_count else index)
117+
if real_row < 0 then if_missing else
118+
this_table.rows real_row+1 . get real_row if_missing
120119

121120
first_value (this_table : Table & DB_Table) =
122121
Feature.Column_Operations.if_supported_else_throw this_table.connection.dialect "first_value" <|

distribution/lib/Standard/Table/0.0.0-dev/docs/api/Table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
- from_union tables:(Standard.Base.Data.Vector.Vector Standard.Table.Table.Table) columns_to_keep:Standard.Table.Columns_To_Keep.Columns_To_Keep= match_columns:Standard.Table.Match_Columns.Match_Columns= on_problems:Standard.Base.Errors.Problem_Behavior.Problem_Behavior= -> (Standard.Table.Table.Table&Standard.Table.In_Memory_Table.In_Memory_Table&Standard.Base.Any.Any)!(Standard.Table.Errors.No_Output_Columns|Standard.Base.Errors.Illegal_Argument.Illegal_Argument)
3838
- generate_rows self over:Standard.Table.Generate_Rows_Range.Generate_Rows_Range= as:Standard.Base.Data.Text.Text= on_problems:Standard.Base.Errors.Problem_Behavior.Problem_Behavior= -> Standard.Base.Any.Any
3939
- get self selector:(Standard.Base.Data.Numbers.Integer|Standard.Base.Data.Text.Text)= ~if_missing:Standard.Base.Any.Any= -> (Standard.Table.Column.Column|Standard.Base.Any.Any)
40-
- get_row self index:Standard.Base.Data.Numbers.Integer= ~if_missing:Standard.Base.Any.Any= -> Standard.Table.Row.Row
40+
- get_row self index:Standard.Base.Data.Numbers.Integer= ~if_missing:Standard.Base.Any.Any= -> (Standard.Table.Row.Row|Standard.Base.Any.Any)
4141
- get_value self selector:(Standard.Base.Data.Text.Text|Standard.Base.Data.Numbers.Integer)= index:Standard.Base.Data.Numbers.Integer= ~if_missing:Standard.Base.Any.Any= -> Standard.Base.Any.Any
4242
- input columns:(Standard.Base.Data.Vector.Vector Standard.Base.Any.Any) -> (Standard.Table.Table.Table&Standard.Table.In_Memory_Table.In_Memory_Table&Standard.Base.Any.Any)
4343
- join self right:Standard.Table.Table.Table= join_kind:Standard.Table.Join_Kind.Join_Kind= on:(Standard.Base.Any.Any|Standard.Base.Data.Text.Text|Standard.Table.Join_Condition.Join_Condition)= right_prefix:Standard.Base.Data.Text.Text= on_problems:Standard.Base.Errors.Problem_Behavior.Problem_Behavior= -> (Standard.Table.Table.Table&Standard.Base.Any.Any)

distribution/lib/Standard/Table/0.0.0-dev/src/Table.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ type Table
463463
- `index`: The index of the row to get within the table.
464464
- `if_missing`: The value to use if the selector isn't present.
465465
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.row_count-1)
466-
get_row self index:Integer=0 ~if_missing:Any=Nothing -> Row =
466+
get_row self index:Integer=0 ~if_missing:Any=Nothing -> Row | Any =
467467
self.implementation.get_row self.underlying index if_missing
468468

469469
## ---

test/Table_Tests/src/Common_Table_Operations/Core_Spec.enso

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,38 @@ add_core_specs suite_builder setup =
261261
last_row.at "Y" . should_equal 8
262262
last_row.at "Z" . should_equal "D"
263263

264+
group_builder.specify "should let you get an indexed row using get_row" <|
265+
row = data.table.get_row 0
266+
row . length . should_equal 3
267+
row.at "X" . should_equal 1
268+
row.at "Y" . should_equal 5
269+
row.at "Z" . should_equal "A"
270+
271+
group_builder.specify "calling get_row with negative indexes, indexes from right" <|
272+
last_row = data.table.get_row -1
273+
last_row . length . should_equal 3
274+
last_row.at "X" . should_equal 4
275+
last_row.at "Y" . should_equal 8
276+
last_row.at "Z" . should_equal "D"
277+
first_row = data.table.get_row -4
278+
first_row . length . should_equal 3
279+
first_row.at "X" . should_equal 1
280+
first_row.at "Y" . should_equal 5
281+
first_row.at "Z" . should_equal "A"
282+
283+
group_builder.specify "calling get_row with out of bounds should return if_missing" <|
284+
out_of_bounds_top_row = data.table.get_row 4 Nothing
285+
out_of_bounds_top_row . should_equal Nothing
286+
out_of_bounds_bottom_row = data.table.get_row -5 Nothing
287+
out_of_bounds_bottom_row . should_equal Nothing
288+
289+
group_builder.specify "empty table get_row should return if_missing" <|
290+
empty_table = data.table.take 0
291+
empty_table.get_row -2 . should_equal Nothing
292+
empty_table.get_row -1 . should_equal Nothing
293+
empty_table.get_row 0 . should_equal Nothing
294+
empty_table.get_row 1 . should_equal Nothing
295+
264296
group_builder.specify "should fetch rows up to the specified limit" <|
265297
data.table.rows max_rows=2 . map .to_vector . should_equal [[1, 5, "A"], [2, 6, "B"]]
266298

0 commit comments

Comments
 (0)