Beispiel #1
0
func TestOpen(t *testing.T) {
	db, err := csvdriver.Conn{
		File:    "testdata/simple.csv",
		Comment: '#',
		Comma:   ';',
	}.Open()

	if err != nil {
		t.Errorf("error opening CSV file: %v\n", err)
		return
	}
	defer db.Close()

	tx, err := db.Begin()
	if err != nil {
		t.Errorf("error starting tx: %v\n", err)
		return
	}
	defer tx.Commit()

	/*
		err = db.Ping()
		if err != nil {
			t.Fatalf("error pinging db: %v\n", err)
		}
	*/

	rows, err := tx.Query("select var1, var2, var3 from csv order by id();")
	if err != nil {
		t.Errorf("error querying db: %v\n", err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var (
			i int64
			f float64
			s string
		)
		err = rows.Scan(&i, &f, &s)
		if err != nil {
			t.Errorf("error scanning db: %v\n", err)
			return
		}
		fmt.Printf("i=%v f=%v s=%q\n", i, f, s)
	}

	err = rows.Close()
	if err != nil {
		t.Errorf("error closing rows: %v\n", err)
		return
	}

	err = db.Close()
	if err != nil {
		t.Errorf("error closing db: %v\n", err)
		return
	}
}
Beispiel #2
0
func TestNTupleScanH1DFromCSV(t *testing.T) {
	db, err := csvdriver.Conn{
		File:    "testdata/simple.csv",
		Comma:   ';',
		Comment: '#',
	}.Open()
	if err != nil {
		t.Fatalf("error opening CSV db: %v\n", err)
	}
	defer db.Close()

	nt, err := hbook.OpenNTuple(db, "csv")
	if err != nil {
		t.Fatalf("error opening ntuple: %v\n", err)
	}

	h := hbook.NewH1D(10, 0, 10)
	h, err = nt.ScanH1D("var2", h)
	if err != nil {
		t.Errorf("error running query: %v\n", err)
	}
	want := struct {
		entries int64
		len     int
		mean    float64
		rms     float64
	}{
		entries: 10,
		len:     10,
		mean:    4.5,
		rms:     2.8722813232690143,
	}

	if h.Entries() != want.entries {
		t.Errorf("error. got %v entries. want=%v\n", h.Entries(), want.entries)
	}
	if h.Len() != want.len {
		t.Errorf("error. got %v bins. want=%d\n", h.Len(), want.len)
	}

	for i := 0; i < h.Len(); i++ {
		v := h.Value(i)
		if v != 1 {
			t.Errorf("error bin(%d)=%v. want=1\n", i, v)
		}
	}

	if mean := h.Mean(); mean != want.mean {
		t.Errorf("error: mean=%v. want=%v\n", mean, want.mean)
	}
	if rms := h.RMS(); rms != want.rms {
		t.Errorf("error: rms=%v. want=%v\n", rms, want.rms)
	}
}