package postgres import ( "fmt" "strings" "testing" "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql" ) func TestFlatPath(t *testing.T) { for _, c := range []struct{ dir, name, want string }{ {"/", "", "/"}, {"/", "a.txt", "/a.txt"}, {"/dir/sub", "a.txt", "/dir/sub/a.txt"}, {"", "", ""}, } { if got := flatPath(abstract_sql.EntryRowKey(c.dir, c.name)); got != c.want { t.Errorf("flatPath(%q, %q) = %q, want %q", c.dir, c.name, got, c.want) } } } func TestFlatStatementsAddressRowsByPath(t *testing.T) { gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}} key := abstract_sql.EntryRowKey("/dir", "a.txt") insert, args := gen.GetSqlInsert("filemeta", key, []byte("m")) if !strings.Contains(insert, "ON CONFLICT (path)") || args[0] != "/dir/a.txt" || args[1] != "/dir" || args[2] != "a.txt" { t.Fatalf("insert %s %v", insert, args) } update, args := gen.GetSqlUpdate("filemeta", key, []byte("m")) if !strings.Contains(update, "WHERE path=$1") || args[0] != "/dir/a.txt" { t.Fatalf("update %s %v", update, args) } list, args := gen.GetSqlListExclusive("filemeta", "/dir", "b", "%", 10) if !strings.Contains(list, "WHERE path>$2 AND path<$3 AND directory=$1") || !strings.Contains(list, "ORDER BY path ASC") || fmt.Sprint(args) != fmt.Sprint([]interface{}{"/dir", "/dir/b", "/dir0", "%", int64(10)}) { t.Fatalf("list %s %v", list, args) } gen.ForceBinaryCollation = true list, args = gen.GetSqlListInclusive("filemeta", "/", "", "%", 10) if !strings.Contains(list, `WHERE path COLLATE "C">=$2`) || !strings.Contains(list, `ORDER BY path COLLATE "C"`) || args[1] != "/" || args[2] != "0" { t.Fatalf("list without a byte-ordered column: %s %v", list, args) } } func TestFlatRecursiveListing(t *testing.T) { gen := &SqlGenPostgresFlat{} list, args := gen.GetSqlListRecursive("bkt", "/a/b", "/a/", "/a0", 100) if !strings.Contains(list, "WHERE path>$1 AND path>=$2 AND path<$3 ORDER BY path ASC LIMIT $4") || len(args) != 4 || args[0] != "/a/b" || args[2] != "/a0" { t.Fatalf("list %s %v", list, args) } list, args = gen.GetSqlListRecursive("bkt", "", "/", "", 100) if strings.Contains(list, "$4") || len(args) != 3 { t.Fatalf("unbounded list %s %v", list, args) } gen.ForceBinaryCollation = true if list, _ = gen.GetSqlListRecursive("bkt", "", "/", "/0", 1); !strings.Contains(list, `ORDER BY path COLLATE "C"`) { t.Fatalf("list without a byte-ordered column: %s", list) } } func TestFlatPathListing(t *testing.T) { gen := &SqlGenPostgresFlat{} list, args := gen.GetSqlListPath("bkt", "/a", "/a/b", "/a/", "/a0", 100) if !strings.Contains(list, "CASE WHEN directory=$1 THEN meta END") || !strings.Contains(list, "WHERE path>$2 AND path>=$3 AND path<$4 ORDER BY path ASC LIMIT $5") || fmt.Sprint(args) != fmt.Sprint([]interface{}{"/a", "/a/b", "/a/", "/a0", int64(100)}) { t.Fatalf("list %s %v", list, args) } gen.ForceBinaryCollation = true if list, _ = gen.GetSqlListPath("bkt", "/", "", "/", "0", 1); !strings.Contains(list, `ORDER BY path COLLATE "C"`) { t.Fatalf("list without a byte-ordered column: %s", list) } } func TestFlatDeleteFolderChildrenReadsThePathRange(t *testing.T) { gen := &SqlGenPostgresFlat{} for dir, want := range map[string][]interface{}{ "/": {"/", "0", "/"}, "/a/b": {"/a/b/", "/a/b0", "/a/b"}, } { del, args := gen.GetSqlDeleteFolderChildren("filemeta", dir) if !strings.Contains(del, "WHERE path>=$1 AND path<$2 AND directory=$3") || fmt.Sprint(args) != fmt.Sprint(want) { t.Fatalf("delete children of %s: %s %v", dir, del, args) } } } func TestFlatStatementsEscapeTheTableName(t *testing.T) { gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}} for _, statement := range []string{ first(gen.GetSqlInsert(`a"b`, abstract_sql.EntryRowKey("/", "x"), nil)), first(gen.GetSqlFind(`a"b`, abstract_sql.EntryRowKey("/", "x"))), first(gen.GetSqlListExclusive(`a"b`, "/", "", "%", 1)), } { if !strings.Contains(statement, `"a""b"`) { t.Fatalf("table name not escaped: %s", statement) } } } func first(s string, _ []interface{}) string { return s }