go-hep.org/x/hep@v0.38.1/groot/rsrv/db_test.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rsrv
     6  
     7  import (
     8  	"os"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/groot/riofs"
    13  )
    14  
    15  func TestDB(t *testing.T) {
    16  	dir, err := os.MkdirTemp("", "groot-rsrv-db-")
    17  	if err != nil {
    18  		t.Fatalf("%+v", err)
    19  	}
    20  	os.RemoveAll(dir)
    21  
    22  	db := NewDB(dir)
    23  	if got, want := len(db.Files()), 0; got != want {
    24  		t.Fatalf("invalid number of files. got=%d, want=%d", got, want)
    25  	}
    26  
    27  	f, err := riofs.Open("../testdata/simple.root")
    28  	if err != nil {
    29  		t.Fatalf("could not open ROOT file: %v", err)
    30  	}
    31  	defer f.Close()
    32  
    33  	const uri = "upload-store:///simple.root"
    34  	db.set(uri, f)
    35  
    36  	if got, want := db.Files(), []string{uri}; !reflect.DeepEqual(got, want) {
    37  		t.Fatalf("invalid list of files. got=%v, want=%v", got, want)
    38  	}
    39  
    40  	wantFname := f.Name()
    41  	err = db.Tx(uri, func(f *riofs.File) error {
    42  		got := f.Name()
    43  		if got != wantFname {
    44  			t.Fatalf("invalid filename in transaction. got=%q, want=%q", got, wantFname)
    45  		}
    46  		return nil
    47  	})
    48  	if err != nil {
    49  		t.Fatalf("%+v", err)
    50  	}
    51  
    52  	err = db.Tx("not-there", func(f *riofs.File) error { return nil })
    53  	if err == nil {
    54  		t.Fatalf("expected an error")
    55  	}
    56  
    57  	db.Close()
    58  }