#! /usr/bin/env bats load $BATS_TEST_DIRNAME/helper/common.bash setup() { skiponwindows "Need to install expect and make this script work on windows." setup_common dolt sql < | 3 | SkyBlue | 0 | 128 | 255 | # +---+----+---------+-----+-------+------+ # diff --dolt a/coordinates b/coordinates # --- a/coordinates # +++ b/coordinates # +---+----+-------+---------+ # | | pk | x | y | # +---+----+-------+---------+ # | - | 2 | 3.3 | 4.4 | # | < | 3 | 5.5 | 6.6 | # | > | 3 | 5.5 | 100.001 | # | + | 4 | 42.24 | 23.32 | # +---+----+-------+---------+ # diff --dolt a/names b/names # --- a/names # +++ b/names # +---+----+------+ # | | pk | name | # +---+----+------+ # | < | 1 | neil | # | > | 1 | joey | # | - | 2 | sami | # | + | 4 | john | # +---+----+------+ # } teardown() { teardown_common } # bats test_tags=no_lambda @test "add-patch: clean workspace" { dolt reset --hard run dolt add --patch [ "$status" -eq 0 ] [[ "$output" =~ "No changes." ]] || false } # bats test_tags=no_lambda @test "add-patch: all changes staged" { dolt add . run dolt add --patch [ "$status" -eq 0 ] [[ "$output" =~ "No changes." ]] || false } # bats test_tags=no_lambda @test "add-patch: help and quit" { run dolt sql -r csv -q "select dolt_hashof_db()" [ $status -eq 0 ] ORIG_DB_HASH=$(echo "$output" | awk 'NR==2') run $BATS_TEST_DIRNAME/add-patch-expect/help_quit.expect [ $status -eq 0 ] run dolt sql -r csv -q "select dolt_hashof_db()" [ $status -eq 0 ] DB_HASH=$(echo "$output" | awk 'NR==2') # Verify that the state of the database hasn't changed. [[ "$DB_HASH" == "$ORIG_DB_HASH" ]] || false } # bats test_tags=no_lambda @test "add-patch: a then d for two tables" { # This test does: `add -p coordinates colors` -> 'a' -> 'd' run $BATS_TEST_DIRNAME/add-patch-expect/all_none.expect [ $status -eq 0 ] run dolt sql -q "select name from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "Red" ]] || false [[ "$output" =~ "SkyBlue" ]] || false [[ "$output" =~ "Yellow" ]] || false [[ ! "$output" =~ "Green" ]] || false # Should be no changes on coordinates. run dolt sql -q "select pk, y from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | 2.2 |" ]] || false [[ "$output" =~ "| 2 | 4.4 |" ]] || false [[ "$output" =~ "| 3 | 6.6 |" ]] || false [[ ! "$output" =~ "23.32" ]] || false # Value for inserted row - should not be there. # Should be no changes on names. run dolt sql -q "select pk, name from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | neil |" ]] || false [[ "$output" =~ "| 2 | sami |" ]] || false [[ "$output" =~ "| 3 | jane |" ]] || false [[ ! "$output" =~ "john" ]] || false # Value for inserted row - should not be there. } # bats test_tags=no_lambda @test "add-patch: y/n repeatedly with restarts" { # This test repeatedly does 'y/n/y/s' until the program exits. run $BATS_TEST_DIRNAME/add-patch-expect/restart_multiple_times.expect [ $status -eq 0 ] run dolt sql -q "select name from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "Red" ]] || false [[ "$output" =~ "SkyBlue" ]] || false [[ "$output" =~ "Yellow" ]] || false [[ ! "$output" =~ "Green" ]] || false run dolt sql -q "select pk, y from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | 2.2 |" ]] || false [[ "$output" =~ "| 3 | 100.001 |" ]] || false [[ "$output" =~ "| 4 | 23.32 |" ]] || false run dolt sql -q "select pk, name from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | joey |" ]] || false [[ "$output" =~ "| 2 | sami |" ]] || false [[ "$output" =~ "| 3 | jane |" ]] || false [[ "$output" =~ "| 4 | john |" ]] || false } # bats test_tags=no_lambda @test "add-patch: summary updates are correct" { # Similar to the previous test, but this time we're ensuring that the summary updates are correct. run $BATS_TEST_DIRNAME/add-patch-expect/summary_updates.expect [ $status -eq 0 ] # Status should be identical to the previous test. run dolt sql -q "select name from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "Red" ]] || false [[ "$output" =~ "SkyBlue" ]] || false [[ "$output" =~ "Yellow" ]] || false [[ ! "$output" =~ "Green" ]] || false run dolt sql -q "select pk, y from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | 2.2 |" ]] || false [[ "$output" =~ "| 3 | 100.001 |" ]] || false [[ "$output" =~ "| 4 | 23.32 |" ]] || false run dolt sql -q "select pk, name from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | joey |" ]] || false [[ "$output" =~ "| 2 | sami |" ]] || false [[ "$output" =~ "| 3 | jane |" ]] || false [[ "$output" =~ "| 4 | john |" ]] || false } # bats test_tags=no_lambda @test "add-patch: y then d" { # Accept the first change for each table, then skip the rest. run $BATS_TEST_DIRNAME/add-patch-expect/yes_then_d.expect [ $status -eq 0 ] run dolt sql -q "select pk,name from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "0 | Yellow" ]] || false [[ "$output" =~ "1 | Red" ]] || false [[ "$output" =~ "2 | Green" ]] || false [[ "$output" =~ "3 | Blue" ]] || false # verify no extra rows in table we didn't look for. run dolt sql -q "select sum(pk) as s from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 6 |" ]] || false # Yellow added as pk=0, so 0+1+2+3 run dolt sql -q "select pk, y from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | 2.2 |" ]] || false [[ "$output" =~ "| 3 | 6.6 |" ]] || false # verify no extra rows in table we didn't look for. run dolt sql -q "select sum(pk) as s from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 4 |" ]] || false run dolt sql -q "select pk, name from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | joey |" ]] || false [[ "$output" =~ "| 2 | sami |" ]] || false [[ "$output" =~ "| 3 | jane |" ]] || false run dolt sql -q "select sum(pk) as s from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 6 |" ]] || false } # bats test_tags=no_lambda @test "add-patch: n then a" { # Accept the reject fir change, then accept the rest. run $BATS_TEST_DIRNAME/add-patch-expect/no_then_a.expect [ $status -eq 0 ] run dolt sql -q "select pk,name from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | Red |" ]] || false [[ "$output" =~ "| 3 | SkyBlue |" ]] || false # verify no extra rows in table we didn't look for. run dolt sql -q "select sum(pk) as s from colors AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 4 |" ]] || false run dolt sql -q "select pk, y from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | 2.2 |" ]] || false [[ "$output" =~ "| 2 | 4.4 |" ]] || false [[ "$output" =~ "| 3 | 100.001 |" ]] || false [[ "$output" =~ "| 4 | 23.32 |" ]] || false # verify no extra rows in table we didn't look for. run dolt sql -q "select sum(pk) as s from coordinates AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 10 |" ]] || false run dolt sql -q "select pk, name from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 1 | neil |" ]] || false [[ "$output" =~ "| 3 | jane |" ]] || false [[ "$output" =~ "| 4 | john |" ]] || false run dolt sql -q "select sum(pk) as s from names AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "| 8 |" ]] || false } # bats test_tags=no_lambda @test "add-patch: keyless table" { dolt add . dolt commit -m "make clean workspace" dolt sql -q "create table keyless (x int, y int)" dolt sql -q "insert into keyless values (1,1), (2,2), (3,3), (1,1), (2,2), (3,3)" dolt commit -A -m "add keyless table with data." # This update, while it updates "all rows", the diff will be: # diff --dolt a/keyless b/keyless # --- a/keyless # +++ b/keyless # +---+---+---+ # | | x | y | # +---+---+---+ # | - | 1 | 1 | # | - | 1 | 1 | # | + | 4 | 4 | # | + | 4 | 4 | # +---+---+---+ dolt sql -q "update keyless set x = x + 1, y = y + 1" run $BATS_TEST_DIRNAME/add-patch-expect/keyless.expect [ $status -eq 0 ] run dolt sql -q "select * from keyless AS OF STAGED" # Output should be: # +---+---+ # | x | y | # +---+---+ # | 3 | 3 | # | 3 | 3 | # | 2 | 2 | # | 2 | 2 | # | 1 | 1 | # | 4 | 4 | # +---+---+ [ $status -eq 0 ] [[ "$output" =~ "3 | 3" ]] || false [[ "$output" =~ "2 | 2" ]] || false [[ "$output" =~ "1 | 1" ]] || false [[ "$output" =~ "4 | 4" ]] || false # verify no extra rows in table we didn't look for. 3 + 3 + 2 + 2 + 1 + 4 = 15 run dolt sql -q "select sum(x) as s from keyless AS OF STAGED" [ $status -eq 0 ] [[ "$output" =~ "15" ]] || false } # bats test_tags=no_lambda @test "add-patch: new table partial staging" { # Create a new table that doesn't exist in HEAD dolt sql -q "CREATE TABLE new_table (pk int primary key, name varchar(32));" dolt sql -q "INSERT INTO new_table VALUES (1, 'alice'), (2, 'bob'), (3, 'charlie');" # Verify table is untracked run dolt status [ $status -eq 0 ] [[ "$output" =~ "new table:" ]] || false [[ "$output" =~ "new_table" ]] || false # Use add -p to stage only the first row (using expect script) run $BATS_TEST_DIRNAME/add-patch-expect/new_table.expect [ $status -eq 0 ] # Verify the table is now in staging with partial changes run dolt sql -q "SELECT * FROM new_table AS OF STAGED ORDER BY pk" [ $status -eq 0 ] [[ "$output" =~ "alice" ]] || false # bob and charlie should NOT be in staged ! [[ "$output" =~ "bob" ]] || false ! [[ "$output" =~ "charlie" ]] || false # Verify workspace table shows correct state run dolt sql -q "SELECT IF(staged, 'true', 'false'), to_pk, to_name FROM dolt_workspace_new_table ORDER BY to_pk" -r csv [ $status -eq 0 ] # First row should be staged (true), others should not be (false) [[ "$output" =~ "true,1,alice" ]] || false [[ "$output" =~ "false,2,bob" ]] || false [[ "$output" =~ "false,3,charlie" ]] || false # Commit the staged changes dolt commit -m "partial staging test" # Verify the table and only the first row were committed run dolt show [ $status -eq 0 ] [[ "$output" =~ "added table" ]] || false [[ "$output" =~ "| + | 1 | alice |" ]] || false ! [[ "$output" =~ "bob" ]] || false ! [[ "$output" =~ "charlie" ]] || false # Verify working still has the other rows run dolt sql -q "SELECT * FROM dolt_workspace_new_table" [ $status -eq 0 ] [[ "$output" =~ "bob" ]] || false [[ "$output" =~ "charlie" ]] || false }