Ejemplo n.º 1
0
func TestCopySingleFile(t *testing.T) {
	const (
		srcPath   = "src"
		destPath  = "dest"
		file1Path = "/d1/d2/f1.ex"
	)
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	//Create data
	testData1 := []byte("Content of file 1")
	// create test model
	fs.WriteFile(srcPath+file1Path, testData1, 0777)
	// copy
	fs.Copy(srcPath+file1Path, destPath+file1Path)
	// test
	readData1, err := fs.ReadFile(destPath + file1Path)
	if err != nil {
		t.Error("can not read file1 after write data ", err)
	} else {
		if !testbase.ByteArrayEq(testData1, readData1) {
			t.Error("read1 and test1 data are diffrent ", testData1, readData1)
		}
	}
}
Ejemplo n.º 2
0
func TestFilespaceConverter_FromMultipart(t *testing.T) {
	memFilespace, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
		return
	}
	converter := NewFilespaceConverter(memFilespace)
	fh := mockmultipart.NewFileHeader([]byte(TestFileContent))
	iInterface, err := converter.FromMultipart(fh)
	if err != nil {
		t.Error(err)
		return
	}
	file, ok := iInterface.(types.File)
	if !ok {
		t.Errorf("converter.FromString result is not types.File instance %v", file)
		return
	}
	fileContent, err := file.Filespace().ReadFile(file.Path())
	if err != nil {
		t.Error(err)
		return
	}
	if !testbase.ByteArrayEq(fileContent, []byte(TestFileContent)) {
		t.Errorf("converter.FromString result is not types.File instance \n%v \n !=\n %v", string(fileContent), TestFileContent)
		return
	}
}
Ejemplo n.º 3
0
func TestFilespaceConverter_ToString(t *testing.T) {
	memFilespace, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
		return
	}
	file := NewFile(memFilespace, TestFilePath)
	converter := NewFilespaceConverter(memFilespace)
	str, err := converter.ToString(file)
	if err != nil {
		t.Error(err)
		return
	}
	if str != TestFilePath {
		t.Errorf("converter.ToString result is incorrect: %v", str)
		return
	}
}
Ejemplo n.º 4
0
func TestWriteAndRead(t *testing.T) {
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	//Create data
	testData := []byte("There is test data")

	// create directories
	path := "/mydir1/mydir2/mydir3/myfile.ex"
	fs.WriteFile(path, testData, 0777)
	readData, err := fs.ReadFile(path)
	if err != nil {
		t.Error("can not read file after write data ", err)
	}
	if !testbase.ByteArrayEq(readData, testData) {
		t.Error("read data are diffrent ", readData, testData)
	}
}
Ejemplo n.º 5
0
func TestWriteAndReader(t *testing.T) {
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	//Create data
	testData := []byte("There is test data")

	// create directories
	path := "/mydir1/mydir2/mydir3/myfile.ex"
	err = fs.WriteFile(path, testData, 0777)
	if err != nil {
		t.Error(err)
		return
	}
	reader, err := fs.Reader(path)
	if err != nil {
		t.Error(err)
		return
	}
	buf := make([]byte, 222)
	n, err := reader.Read(buf)
	if err != nil {
		t.Error(err)
		return
	}
	err = reader.Close()
	if err != nil {
		t.Error(err)
		return
	}
	if n != len(testData) {
		t.Errorf("return length should be equal to data size %v %v", n, len(testData))
		return
	}
	if !testbase.ByteArrayEq(buf[:n], testData) {
		t.Error("read data are diffrent ", buf, testData)
	}
}
Ejemplo n.º 6
0
func newTestScope() (*testScope, error) {
	bdtx, err := sqlx.Open("sqlite3", ":memory:")
	if err != nil {
		return nil, err
	}
	fs, err := memfs.NewFilespace()
	if err != nil {
		return nil, err
	}
	types := NewTestTypes(fs)
	table := NewBaseTable(testTable, types)
	dao := NewBaseDAO(table)
	dao.CreateTable(bdtx)
	if err != nil {
		return nil, err
	}
	return &testScope{
		table: table,
		dao:   dao,
		bdtx:  bdtx,
	}, nil
}
Ejemplo n.º 7
0
func TestFilespaceConverter_FromString(t *testing.T) {
	memFilespace, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
		return
	}
	converter := NewFilespaceConverter(memFilespace)
	iInterface, err := converter.FromString(TestFilePath)
	if err != nil {
		t.Error(err)
		return
	}
	file, ok := iInterface.(types.File)
	if !ok {
		t.Errorf("converter.FromString result is not types.File instance %v", file)
		return
	}
	if file.Path() != TestFilePath {
		t.Errorf("Path of file must be equels %v != %v", TestFilePath, file.Path())
		return
	}
}
Ejemplo n.º 8
0
func TestMkdir(t *testing.T) {
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	// create directories
	path := "/mydir1/mydir2/mydir3"
	if err := fs.MkdirAll(path, 0777); err != nil {
		t.Error("Fail when create directories", err)
	}
	// test node type
	if !fs.IsDir("/mydir1/mydir2") {
		t.Error("node is not a directory or not exists")
	}
	if !fs.IsDir(path) {
		t.Error("node is not a directory or not exists")
	}
	if fs.IsDir("/noExistPath") {
		t.Error("node is not a directory or not exists")
	}
}
Ejemplo n.º 9
0
func TestWriteStreamAndRead(t *testing.T) {
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	//Create data
	testData := []byte("There is test data")
	// create directories
	path := "/mydir1/mydir2/mydir3/myfile.ex"
	writer, err := fs.Writer(path)
	if err != nil {
		t.Error(err)
		return
	}
	n, err := writer.Write(testData)
	if err != nil {
		t.Error(err)
		return
	}
	if n != len(testData) {
		t.Errorf("return length should be equal to data size %v %v", n, len(testData))
		return
	}
	err = writer.Close()
	if err != nil {
		t.Error(err)
		return
	}
	readData, err := fs.ReadFile(path)
	if err != nil {
		t.Error(err)
	}
	if !testbase.ByteArrayEq(readData, testData) {
		t.Error("read data are diffrent ", readData, testData)
	}
}
Ejemplo n.º 10
0
func TestWriteAndRead(t *testing.T) {
	var writeObject, readObject TestObject
	// init
	fs, err := memfs.NewFilespace()
	if err != nil {
		t.Error(err)
	}
	// create test data
	path := "fyfile.json"
	writeObject = TestObject{
		Value1: "str1",
		Value2: "str2",
	}
	// write & read
	json.WriteJSON(fs, path, writeObject)
	json.ReadJSON(fs, path, &readObject)
	// test node type
	if !fs.IsFile(path) {
		t.Error("filesystem not conatin file adter write")
	}
	if writeObject.Value1 != readObject.Value1 || writeObject.Value2 != readObject.Value2 {
		t.Error("read date is wrong", writeObject, readObject)
	}
}