perkeep/lib/go/schema/schema_test.go

86 lines
1.8 KiB
Go
Raw Normal View History

2010-12-30 18:17:47 +00:00
package schema
import (
"strings"
"testing"
)
type isUtf8Test struct {
s string
e bool
}
func TestIsUtf8(t *testing.T) {
tests := []isUtf8Test{
{"foo", true},
{"Straße", true},
{string([]uint8{65, 234, 234, 192, 23, 123}), false},
{string([]uint8{65, 97}), true},
}
for idx, test := range tests {
if isValidUtf8(test.s) != test.e {
t.Errorf("expected isutf8==%d for test index %d", test.e, idx)
}
}
}
const kExpectedHeader = `{"camliVersion"`
func TestJson(t *testing.T) {
2010-12-31 06:37:46 +00:00
m := newMapForFileName("schema_test.go")
2010-12-30 18:17:47 +00:00
json, err := MapToCamliJson(m)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Logf("Got json: [%s]\n", json)
// TODO: test it parses back
if !strings.HasPrefix(json, kExpectedHeader) {
t.Errorf("JSON does't start with expected header.")
}
2010-12-31 06:37:46 +00:00
}
2010-12-31 20:13:33 +00:00
type rfc3339NanoTest struct {
nanos int64
e string
}
func TestRfc3339FromNanos(t *testing.T) {
tests := []rfc3339NanoTest{
{0, "1970-01-01T00:00:00Z"},
{1, "1970-01-01T00:00:00.000000001Z"},
{10, "1970-01-01T00:00:00.00000001Z"},
{1000, "1970-01-01T00:00:00.000001Z"},
}
for idx, test := range tests {
got := rfc3339FromNanos(test.nanos)
if got != test.e {
t.Errorf("On test %d got %q; expected %q", idx, got, test.e)
}
}
}
2010-12-31 06:37:46 +00:00
func TestRegularFile(t *testing.T) {
m, err := NewFileMap("schema_test.go", nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
json, err := MapToCamliJson(m)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Logf("Got json for regular file: [%s]\n", json)
2011-01-01 21:12:45 +00:00
}
func TestSymlink(t *testing.T) {
m, err := NewFileMap("testdata/test-symlink", nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
json, err := MapToCamliJson(m)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Logf("Got json for symlink file: [%s]\n", json)
2010-12-30 18:17:47 +00:00
}