|
| 1 | +package itests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/filecoin-project/curio/harmony/harmonydb" |
| 11 | + |
| 12 | + "github.com/filecoin-project/lotus/itests/kit" |
| 13 | + "github.com/filecoin-project/lotus/node/impl" |
| 14 | +) |
| 15 | + |
| 16 | +func withSetup(t *testing.T, f func(*kit.TestMiner)) { |
| 17 | + _, miner, _ := kit.EnsembleMinimal(t, |
| 18 | + kit.LatestActorsAt(-1), |
| 19 | + kit.MockProofs(), |
| 20 | + kit.WithSectorIndexDB(), |
| 21 | + ) |
| 22 | + |
| 23 | + f(miner) |
| 24 | +} |
| 25 | + |
| 26 | +func TestCrud(t *testing.T) { |
| 27 | + ctx, cancel := context.WithCancel(context.Background()) |
| 28 | + defer cancel() |
| 29 | + |
| 30 | + withSetup(t, func(miner *kit.TestMiner) { |
| 31 | + cdb := miner.BaseAPI.(*impl.StorageMinerAPI).HarmonyDB |
| 32 | + _, err := cdb.Exec(ctx, ` |
| 33 | + INSERT INTO |
| 34 | + itest_scratch (some_int, content) |
| 35 | + VALUES |
| 36 | + (11, 'cows'), |
| 37 | + (5, 'cats') |
| 38 | + `) |
| 39 | + if err != nil { |
| 40 | + t.Fatal("Could not insert: ", err) |
| 41 | + } |
| 42 | + var ints []struct { |
| 43 | + Count int `db:"some_int"` |
| 44 | + Animal string `db:"content"` |
| 45 | + Unpopulated int |
| 46 | + } |
| 47 | + err = cdb.Select(ctx, &ints, "SELECT content, some_int FROM itest_scratch") |
| 48 | + if err != nil { |
| 49 | + t.Fatal("Could not select: ", err) |
| 50 | + } |
| 51 | + if len(ints) != 2 { |
| 52 | + t.Fatal("unexpected count of returns. Want 2, Got ", len(ints)) |
| 53 | + } |
| 54 | + if ints[0].Count != 11 || ints[1].Count != 5 { |
| 55 | + t.Fatal("expected [11,5] got ", ints) |
| 56 | + } |
| 57 | + if ints[0].Animal != "cows" || ints[1].Animal != "cats" { |
| 58 | + t.Fatal("expected, [cows, cats] ", ints) |
| 59 | + } |
| 60 | + fmt.Println("test completed") |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +func TestTransaction(t *testing.T) { |
| 65 | + ctx, cancel := context.WithCancel(context.Background()) |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + withSetup(t, func(miner *kit.TestMiner) { |
| 69 | + testID := harmonydb.ITestNewID() |
| 70 | + cdb := setupTestDB(t, testID) |
| 71 | + if _, err := cdb.Exec(ctx, "INSERT INTO itest_scratch (some_int) VALUES (4), (5), (6)"); err != nil { |
| 72 | + t.Fatal("E0", err) |
| 73 | + } |
| 74 | + _, err := cdb.BeginTransaction(ctx, func(tx *harmonydb.Tx) (commit bool, err error) { |
| 75 | + if _, err := tx.Exec("INSERT INTO itest_scratch (some_int) VALUES (7), (8), (9)"); err != nil { |
| 76 | + t.Fatal("E1", err) |
| 77 | + } |
| 78 | + |
| 79 | + // sum1 is read from OUTSIDE the transaction so it's the old value |
| 80 | + var sum1 int |
| 81 | + if err := cdb.QueryRow(ctx, "SELECT SUM(some_int) FROM itest_scratch").Scan(&sum1); err != nil { |
| 82 | + t.Fatal("E2", err) |
| 83 | + } |
| 84 | + if sum1 != 4+5+6 { |
| 85 | + t.Fatal("Expected 15, got ", sum1) |
| 86 | + } |
| 87 | + |
| 88 | + // sum2 is from INSIDE the transaction, so the updated value. |
| 89 | + var sum2 int |
| 90 | + if err := tx.QueryRow("SELECT SUM(some_int) FROM itest_scratch").Scan(&sum2); err != nil { |
| 91 | + t.Fatal("E3", err) |
| 92 | + } |
| 93 | + if sum2 != 4+5+6+7+8+9 { |
| 94 | + t.Fatal("Expected 39, got ", sum2) |
| 95 | + } |
| 96 | + return false, nil // rollback |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + t.Fatal("ET", err) |
| 100 | + } |
| 101 | + |
| 102 | + var sum2 int |
| 103 | + // Query() example (yes, QueryRow would be preferred here) |
| 104 | + q, err := cdb.Query(ctx, "SELECT SUM(some_int) FROM itest_scratch") |
| 105 | + if err != nil { |
| 106 | + t.Fatal("E4", err) |
| 107 | + } |
| 108 | + defer q.Close() |
| 109 | + var rowCt int |
| 110 | + for q.Next() { |
| 111 | + err := q.Scan(&sum2) |
| 112 | + if err != nil { |
| 113 | + t.Fatal("error scanning ", err) |
| 114 | + } |
| 115 | + rowCt++ |
| 116 | + } |
| 117 | + if sum2 != 4+5+6 { |
| 118 | + t.Fatal("Expected 15, got ", sum2) |
| 119 | + } |
| 120 | + if rowCt != 1 { |
| 121 | + t.Fatal("unexpected count of rows") |
| 122 | + } |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestPartialWalk(t *testing.T) { |
| 127 | + ctx, cancel := context.WithCancel(context.Background()) |
| 128 | + defer cancel() |
| 129 | + |
| 130 | + withSetup(t, func(miner *kit.TestMiner) { |
| 131 | + testID := harmonydb.ITestNewID() |
| 132 | + cdb := setupTestDB(t, testID) |
| 133 | + if _, err := cdb.Exec(ctx, ` |
| 134 | + INSERT INTO |
| 135 | + itest_scratch (content, some_int) |
| 136 | + VALUES |
| 137 | + ('andy was here', 5), |
| 138 | + ('lotus is awesome', 6), |
| 139 | + ('hello world', 7), |
| 140 | + ('3rd integration test', 8), |
| 141 | + ('fiddlesticks', 9) |
| 142 | + `); err != nil { |
| 143 | + t.Fatal("e1", err) |
| 144 | + } |
| 145 | + |
| 146 | + // TASK: FIND THE ID of the string with a specific SHA256 |
| 147 | + needle := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" |
| 148 | + q, err := cdb.Query(ctx, `SELECT id, content FROM itest_scratch`) |
| 149 | + if err != nil { |
| 150 | + t.Fatal("e2", err) |
| 151 | + } |
| 152 | + defer q.Close() |
| 153 | + |
| 154 | + var tmp struct { |
| 155 | + Src string `db:"content"` |
| 156 | + ID int |
| 157 | + } |
| 158 | + |
| 159 | + var done bool |
| 160 | + for q.Next() { |
| 161 | + |
| 162 | + if err := q.StructScan(&tmp); err != nil { |
| 163 | + t.Fatal("structscan err " + err.Error()) |
| 164 | + } |
| 165 | + |
| 166 | + bSha := sha256.Sum256([]byte(tmp.Src)) |
| 167 | + if hex.EncodeToString(bSha[:]) == needle { |
| 168 | + done = true |
| 169 | + break |
| 170 | + } |
| 171 | + } |
| 172 | + if !done { |
| 173 | + t.Fatal("We didn't find it.") |
| 174 | + } |
| 175 | + // Answer: tmp.ID |
| 176 | + }) |
| 177 | +} |
0 commit comments