From 93b9597c1452c7fbfd18f564f16e4cbbe47089d9 Mon Sep 17 00:00:00 2001 From: PJ Date: Wed, 28 Feb 2024 11:30:21 +0100 Subject: [PATCH] stores: fix TestTypeMerkleProof --- stores/metadata_test.go | 97 ----------------------------- stores/types_test.go | 132 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 123 insertions(+), 106 deletions(-) diff --git a/stores/metadata_test.go b/stores/metadata_test.go index 205812287..b39816c1f 100644 --- a/stores/metadata_test.go +++ b/stores/metadata_test.go @@ -8,7 +8,6 @@ import ( "fmt" "os" "reflect" - "sort" "strings" "testing" "time" @@ -4308,99 +4307,3 @@ func TestUpdateObjectReuseSlab(t *testing.T) { } } } - -func TestTypeCurrency(t *testing.T) { - ss := newTestSQLStore(t, defaultTestSQLStoreConfig) - defer ss.Close() - - // prepare the table - if isSQLite(ss.db) { - if err := ss.db.Exec("CREATE TABLE currencies (id INTEGER PRIMARY KEY AUTOINCREMENT,c BLOB);").Error; err != nil { - t.Fatal(err) - } - } else { - if err := ss.db.Exec("CREATE TABLE currencies (id INT AUTO_INCREMENT PRIMARY KEY, c BLOB);").Error; err != nil { - t.Fatal(err) - } - } - - // insert currencies in random order - if err := ss.db.Exec("INSERT INTO currencies (c) VALUES (?),(?),(?);", bCurrency(types.MaxCurrency), bCurrency(types.NewCurrency64(1)), bCurrency(types.ZeroCurrency)).Error; err != nil { - t.Fatal(err) - } - - // fetch currencies and assert they're sorted - var currencies []bCurrency - if err := ss.db.Raw(`SELECT c FROM currencies ORDER BY c ASC`).Scan(¤cies).Error; err != nil { - t.Fatal(err) - } else if !sort.SliceIsSorted(currencies, func(i, j int) bool { - return types.Currency(currencies[i]).Cmp(types.Currency(currencies[j])) < 0 - }) { - t.Fatal("currencies not sorted", currencies) - } - - // convenience variables - c0 := currencies[0] - c1 := currencies[1] - cM := currencies[2] - - tests := []struct { - a bCurrency - b bCurrency - cmp string - }{ - { - a: c0, - b: c1, - cmp: "<", - }, - { - a: c1, - b: c0, - cmp: ">", - }, - { - a: c0, - b: c1, - cmp: "!=", - }, - { - a: c1, - b: c1, - cmp: "=", - }, - { - a: c0, - b: cM, - cmp: "<", - }, - { - a: cM, - b: c0, - cmp: ">", - }, - { - a: cM, - b: cM, - cmp: "=", - }, - } - for i, test := range tests { - var result bool - query := fmt.Sprintf("SELECT ? %s ?", test.cmp) - if !isSQLite(ss.db) { - query = strings.Replace(query, "?", "HEX(?)", -1) - } - if err := ss.db.Raw(query, test.a, test.b).Scan(&result).Error; err != nil { - t.Fatal(err) - } else if !result { - t.Errorf("unexpected result in case %d/%d: expected %v %s %v to be true", i+1, len(tests), types.Currency(test.a).String(), test.cmp, types.Currency(test.b).String()) - } else if test.cmp == "<" && types.Currency(test.a).Cmp(types.Currency(test.b)) >= 0 { - t.Fatal("invalid result") - } else if test.cmp == ">" && types.Currency(test.a).Cmp(types.Currency(test.b)) <= 0 { - t.Fatal("invalid result") - } else if test.cmp == "=" && types.Currency(test.a).Cmp(types.Currency(test.b)) != 0 { - t.Fatal("invalid result") - } - } -} diff --git a/stores/types_test.go b/stores/types_test.go index a0bc19950..2308c0fd4 100644 --- a/stores/types_test.go +++ b/stores/types_test.go @@ -1,33 +1,147 @@ package stores import ( + "fmt" + "sort" "strings" "testing" "go.sia.tech/core/types" ) +func TestTypeCurrency(t *testing.T) { + ss := newTestSQLStore(t, defaultTestSQLStoreConfig) + defer ss.Close() + + // prepare the table + if isSQLite(ss.db) { + if err := ss.db.Exec("CREATE TABLE currencies (id INTEGER PRIMARY KEY AUTOINCREMENT,c BLOB);").Error; err != nil { + t.Fatal(err) + } + } else { + if err := ss.db.Exec("CREATE TABLE currencies (id INT AUTO_INCREMENT PRIMARY KEY, c BLOB);").Error; err != nil { + t.Fatal(err) + } + } + + // insert currencies in random order + if err := ss.db.Exec("INSERT INTO currencies (c) VALUES (?),(?),(?);", bCurrency(types.MaxCurrency), bCurrency(types.NewCurrency64(1)), bCurrency(types.ZeroCurrency)).Error; err != nil { + t.Fatal(err) + } + + // fetch currencies and assert they're sorted + var currencies []bCurrency + if err := ss.db.Raw(`SELECT c FROM currencies ORDER BY c ASC`).Scan(¤cies).Error; err != nil { + t.Fatal(err) + } else if !sort.SliceIsSorted(currencies, func(i, j int) bool { + return types.Currency(currencies[i]).Cmp(types.Currency(currencies[j])) < 0 + }) { + t.Fatal("currencies not sorted", currencies) + } + + // convenience variables + c0 := currencies[0] + c1 := currencies[1] + cM := currencies[2] + + tests := []struct { + a bCurrency + b bCurrency + cmp string + }{ + { + a: c0, + b: c1, + cmp: "<", + }, + { + a: c1, + b: c0, + cmp: ">", + }, + { + a: c0, + b: c1, + cmp: "!=", + }, + { + a: c1, + b: c1, + cmp: "=", + }, + { + a: c0, + b: cM, + cmp: "<", + }, + { + a: cM, + b: c0, + cmp: ">", + }, + { + a: cM, + b: cM, + cmp: "=", + }, + } + for i, test := range tests { + var result bool + query := fmt.Sprintf("SELECT ? %s ?", test.cmp) + if !isSQLite(ss.db) { + query = strings.Replace(query, "?", "HEX(?)", -1) + } + if err := ss.db.Raw(query, test.a, test.b).Scan(&result).Error; err != nil { + t.Fatal(err) + } else if !result { + t.Errorf("unexpected result in case %d/%d: expected %v %s %v to be true", i+1, len(tests), types.Currency(test.a).String(), test.cmp, types.Currency(test.b).String()) + } else if test.cmp == "<" && types.Currency(test.a).Cmp(types.Currency(test.b)) >= 0 { + t.Fatal("invalid result") + } else if test.cmp == ">" && types.Currency(test.a).Cmp(types.Currency(test.b)) <= 0 { + t.Fatal("invalid result") + } else if test.cmp == "=" && types.Currency(test.a).Cmp(types.Currency(test.b)) != 0 { + t.Fatal("invalid result") + } + } +} + +// TODO: can't .Take() a single merkleProof func TestTypeMerkleProof(t *testing.T) { ss := newTestSQLStore(t, defaultTestSQLStoreConfig) defer ss.Close() + // prepare the table + if isSQLite(ss.db) { + if err := ss.db.Exec("CREATE TABLE merkle_proofs (id INTEGER PRIMARY KEY AUTOINCREMENT,merkle_proof BLOB);").Error; err != nil { + t.Fatal(err) + } + } else { + ss.db.Exec("DROP TABLE IF EXISTS merkle_proofs;") + if err := ss.db.Exec("CREATE TABLE merkle_proofs (id INT AUTO_INCREMENT PRIMARY KEY, merkle_proof BLOB);").Error; err != nil { + t.Fatal(err) + } + } + + // insert merkle proof + if err := ss.db.Exec("INSERT INTO merkle_proofs (id, merkle_proof) VALUES (1,?),(2,?);", merkleProof([]types.Hash256{}), merkleProof([]types.Hash256{{2}, {1}, {3}})).Error; err != nil { + t.Fatal(err) + } + + // fetch invalid proof var proofs []merkleProof if err := ss.db. - Raw(`WITH input(merkle_proof) as (values (?)) SELECT merkle_proof FROM input`, merkleProof([]types.Hash256{})). - Scan(&proofs). + Raw(`SELECT merkle_proof FROM merkle_proofs WHERE id=1`). + Take(&proofs). Error; err == nil || !strings.Contains(err.Error(), "no bytes found") { t.Fatalf("expected error 'no bytes found', got '%v'", err) } + // fetch valid proof if err := ss.db. - Raw(`WITH input(merkle_proof) as (values (?)) SELECT merkle_proof FROM input`, merkleProof([]types.Hash256{{2}, {1}, {3}})). - Scan(&proofs). + Raw(`SELECT merkle_proof FROM merkle_proofs WHERE id=2`). + Take(&proofs). Error; err != nil { - t.Fatal("unexpected err", err) - } else if len(proofs) != 1 { - t.Fatal("expected 1 proof") - } else if len(proofs[0]) != 3 { - t.Fatalf("expected 3 hashes, got %v", len(proofs[0])) + t.Fatalf("unexpected error '%v'", err) } else if proofs[0][0] != (types.Hash256{2}) || proofs[0][1] != (types.Hash256{1}) || proofs[0][2] != (types.Hash256{3}) { t.Fatalf("unexpected proof %+v", proofs[0]) }