Ejemplo n.º 1
0
func TestCache(t *testing.T) {
	db, err := NewSQL("sqlite3test", "sqlite3", "./_test.sqlite3")
	defer os.Remove("./_test.sqlite3")

	mustExec(t, db, "create table tbl(a int, b text, c float, d datetime)")
	mustExec(t, db, "insert into tbl(a, b, c, d) values (1, 'x', 10.1, '2014-01-01'), (2, 'y', 11.2, '2014-02-02'), (3, 'z', 12.3, '2014-03-03')")
	c, err := NewCache(db, "./_cache_test.sqlite3", 0, 0, true)
	defer os.Remove("./_cache_test.sqlite3")
	if err != nil {
		t.Fatalf("cannot open cache db: %s", err)
	}
	df, err := c.Query("select a, b, c, d from tbl")
	if err != nil {
		t.Fatalf("cannot query cache db: %s", err)
	}
	dt, _ := data.NewVector("2014-01-01", "2014-02-02", "2014-03-03").AsTime()
	for _, tc := range []struct {
		s string
		v *data.Vector
	}{
		{"a", data.NewVector(1, 2, 3)},
		{"b", data.NewVector("x", "y", "z")},
		{"c", data.NewVector(10.1, 11.2, 12.3)},
		{"d", dt},
	} {
		if !reflect.DeepEqual(df.Column(tc.s), tc.v) {
			t.Errorf("Not equal: %s and %s", tc.s, tc.v)
		}
	}
}
Ejemplo n.º 2
0
func TestQuery(t *testing.T) {
	_, err := NewSQL("test", "sqlite3", "/this-path-does-not/exist")
	if err == nil {
		t.Errorf("expected an error for a non-existing path ")
	}
	_, err = NewSQL("test", "no-such-driver", "")
	if err == nil {
		t.Errorf("expected an error for a non-existing driver")
	}
	db, err := NewSQL("sqlite3test", "sqlite3", "./_df_test.sqlite3")
	if err != nil {
		t.Fatalf("cannot open test database: %s", err)
	}
	if db.Name() != "sqlite3test" || db.Driver() != "sqlite3" {
		t.Errorf("Wrong name or driver: %s, %s", db.Name(), db.Driver())
	}
	defer os.Remove("./_df_test.sqlite3")
	mustExec(t, db, "create table tbl(a int, b text, c float, d datetime)")
	mustExec(t, db, "insert into tbl(a, b, c, d) values (1, 'x', 10.1, '2014-01-01'), (2, 'y', 11.2, '2014-02-02'), (3, 'z', 12.3, '2014-03-03')")
	df, err := db.Query("select a, b, c, d from tbl")
	if err != nil {
		t.Fatalf("cannot query db: %s", err)
	}
	dt, _ := data.NewVector("2014-01-01", "2014-02-02", "2014-03-03").AsTime()
	for _, tc := range []struct {
		s string
		v *data.Vector
	}{
		{"a", data.NewVector(1, 2, 3)},
		{"b", data.NewVector("x", "y", "z")},
		{"c", data.NewVector(10.1, 11.2, 12.3)},
		{"d", dt},
	} {
		if !reflect.DeepEqual(df.Column(tc.s), tc.v) {
			t.Errorf("Not equal: %s and %s", df.Column(tc.s), tc.v)
		}
	}
}