Exemplo n.º 1
0
func TestWriteCSVFile(t *testing.T) {
	testName := utils.GetAppName() + "-test"
	tempDir, _ := ioutil.TempDir("", testName)

	tempFile, err := ioutil.TempFile(tempDir, utils.GetAppName()+"-test")
	if err != nil {
		t.Errorf("failed to create the temp file: %s", err.Error())
	}
	fStat, _ := tempFile.Stat()
	fName := tempFile.Name()

	tempFile.Sync()
	tempFile.Close()
	defer os.RemoveAll(tempDir)

	db, _ := sql.Open("testdb", "")
	defer db.Close()

	sql := "select id, name, age from users"
	columns := []string{"id", "name", "age", "created"}
	result := `
1,tim,20,2012-10-01 01:00:01
2,joe,25,2012-10-02 02:00:02
3,bob,30,2012-10-03 03:00:03
`
	testdb.StubQuery(sql, testdb.RowsFromCSVString(columns, result))

	res, err := db.Query(sql)
	args := &writeCSVFileArgs{
		Rows:     res,
		FileName: fStat.Name(),
		Path:     tempDir,
		Bom:      false,
	}

	csvState := writeCSVFile(args)

	file, err := os.OpenFile(fName, os.O_RDONLY, 0777)
	defer file.Close()

	fStat, _ = file.Stat()

	if !csvState {
		t.Errorf("[writeCSVFile] result error: %s", err.Error())
	}
	if fStat.Size() <= 0 {
		t.Errorf("csv file not out put: %d", fStat.Size())
	}
}
Exemplo n.º 2
0
func TestCompatibilityMode(t *testing.T) {
	DB, _ := gorm.Open("testdb", "")
	testdb.SetQueryFunc(func(query string) (driver.Rows, error) {
		columns := []string{"id", "name", "age"}
		result := `
		1,Tim,20
		2,Joe,25
		3,Bob,30
		`
		return testdb.RowsFromCSVString(columns, result), nil
	})

	var users []User
	DB.Find(&users)
	if (users[0].Name != "Tim") || len(users) != 3 {
		t.Errorf("Unexcepted result returned")
	}
}