Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,105 @@
|
||||
package row
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func vals(raw ...any) Row {
|
||||
r := Row{}
|
||||
for _, v := range raw {
|
||||
switch x := v.(type) {
|
||||
case nil:
|
||||
r.Values = append(r.Values, Value{})
|
||||
case string:
|
||||
r.Values = append(r.Values, Value{Raw: []byte(x)})
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func cols(names ...string) []Column {
|
||||
out := make([]Column, len(names))
|
||||
for i, n := range names {
|
||||
out[i] = Column{Name: n, Type: "varchar"}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestDumpEncoding(t *testing.T) {
|
||||
enc, err := NewEncoder("dump", cols("a", "b", "c"), nil, Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := vals("x\ty", nil, `a\nb`)
|
||||
got, err := enc.Encode(nil, r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "x\\ty\t\\N\ta\\\\nb\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDumpEscapesControlBytes(t *testing.T) {
|
||||
enc, _ := NewEncoder("dump", cols("a"), nil, Options{})
|
||||
r := Row{Values: []Value{{Raw: []byte{'a', 0, '\b', 0x1a, '\r'}}}}
|
||||
got, _ := enc.Encode(nil, r)
|
||||
want := "a\\0\\b\\Z\\r\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVEncoding(t *testing.T) {
|
||||
enc, err := NewEncoder("csv", cols("a", "b", "c", "d"), nil, Options{CSVNull: `\N`})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if h := string(enc.Header()); h != "a,b,c,d\r\n" {
|
||||
t.Fatalf("header %q", h)
|
||||
}
|
||||
r := vals(`a,"b"`, "", nil, "plain")
|
||||
got, _ := enc.Encode(nil, r)
|
||||
want := "\"a,\"\"b\"\"\",,\\N,plain\r\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVNullToken(t *testing.T) {
|
||||
enc, _ := NewEncoder("csv", cols("a"), nil, Options{CSVNull: "NULL"})
|
||||
got, _ := enc.Encode(nil, vals(nil))
|
||||
if string(got) != "NULL\r\n" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONLTypes(t *testing.T) {
|
||||
columns := []Column{
|
||||
{Name: "id", Type: "bigint"},
|
||||
{Name: "price", Type: "decimal"},
|
||||
{Name: "created", Type: "datetime"},
|
||||
{Name: "day", Type: "date"},
|
||||
{Name: "blob", Type: "blob"},
|
||||
{Name: "note", Type: "varchar"},
|
||||
{Name: "missing", Type: "varchar"},
|
||||
}
|
||||
enc, _ := NewEncoder("jsonl", columns, nil, Options{})
|
||||
r := vals("42", "12.30", "2024-01-02 03:04:05", "2024-01-02", "hi", `line "one"`, nil)
|
||||
got, _ := enc.Encode(nil, r)
|
||||
want := `{"id":42,"price":"12.30","created":"2024-01-02T03:04:05Z","day":"2024-01-02","blob":"aGk=","note":"line \"one\"","missing":null}` + "\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("got %s\nwant %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONLColumnProjection(t *testing.T) {
|
||||
columns := []Column{{Name: "a", Type: "int"}, {Name: "b", Type: "int"}, {Name: "c", Type: "int"}}
|
||||
enc, _ := NewEncoder("jsonl", columns, []int{2, 0}, Options{})
|
||||
got, _ := enc.Encode(nil, vals("1", "2", "3"))
|
||||
if strings.TrimSpace(string(got)) != `{"c":3,"a":1}` {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user