示例#1
0
func Test_SetData(t *testing.T) {
	// Prepare test
	sr := NewStringReadCloser(strings.Join(testCsvLines, "\n"))
	c := NewCsvUtil(sr).Comma('|').
		TrailingComma(true).
		FieldsPerRecord(-1).
		CustomBool([]string{"Y"}, []string{"N"})

	// Start test
	p := &person{Skipped: "aaa"}
	err := c.SetData(p)
	assert.NotError(t, err)
	assert.Equal(t, "Tony", p.Name)
	assert.Equal(t, 23, p.Age)
	assert.Equal(t, float32(123.456), p.Balance)
	assert.Equal(t, "aaa", p.Skipped)
	assert.Equal(t, true, p.LowBalance)

	err = c.SetData(p)
	assert.NotError(t, err)
	assert.Equal(t, "John", p.Name)
	assert.Equal(t, 34, p.Age)
	assert.Equal(t, float32(234.567), p.Balance)
	assert.Equal(t, "aaa", p.Skipped)
	assert.Equal(t, false, p.LowBalance)

	err = c.SetData(p)
	assert.Equal(t, io.EOF, err)
	// The previous data stays intact
	assert.Equal(t, "John", p.Name)
	assert.Equal(t, 34, p.Age)
	assert.Equal(t, float32(234.567), p.Balance)
	assert.Equal(t, "aaa", p.Skipped)
	assert.Equal(t, false, p.LowBalance)
}
示例#2
0
func Test_NewReader(t *testing.T) {
	// Prepare test
	sr := NewStringReadCloser(strings.Join(testCsvLines, "\n"))
	c := NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1)

	// Start test
	assert.NotNil(t, c.csvr)
	l, err := c.read()
	assert.NotError(t, err)
	assert.Equal(t, []string{"Tony", "23", "123.456", "Y"}, l)
	assert.Equal(t, "Tony|23|123.456|Y", c.LastCsvLine())

	l, err = c.read()
	assert.NotError(t, err)
	assert.Equal(t, []string{"John", "34", "234.567", "N", ""}, l)
	assert.Equal(t, "John|34|234.567|N|", c.LastCsvLine())
}
示例#3
0
// RowExists asserts row exists in a table.
func RowExists(t assert.Tester, tableName, pkName, selectValue interface{}) ([]mysql.Row, mysql.Result) {
	value := fmt.Sprintf("%v", selectValue)
	if _, ok := selectValue.(string); ok {
		value = "'" + dbcon.Escape(value) + "'"
	}
	rows, res, err := dbcon.Query("SELECT * FROM %s WHERE %s = %s", tableName, pkName, value)
	assert.NotError(t, err, assert.NESTING_2, assert.FAIL_FAST)
	assert.T(t, len(rows) > 0, assert.NESTING_2, assert.FAIL_FAST, "Expected row with %s = %s to exist in the table %s.", pkName, value, tableName)
	return rows, res
}
示例#4
0
// LoadFixture loads fixture to database
func LoadFixture(t assert.Tester, fixturePath string) {
	sql_lines := assert.LoadFixture(t, fixturePath)
	assert.T(t, len(sql_lines) > 0)
	for _, sql := range sql_lines {
		if strings.HasPrefix(sql, "--") || sql == "" {
			continue
		}
		if _, _, err := dbcon.Query(sql); err != nil {
			assert.NotError(t, err, assert.NESTING_2)
		}
	}
}
示例#5
0
func Test_trim(t *testing.T) {
	// Prepare test
	sr := NewStringReadCloser("   Tom |12|123|T")
	c := NewCsvUtil(sr).Comma('|').Trim(" ")

	// Start test
	p := &person{}
	err := c.SetData(p)
	assert.NotError(t, err)

	assert.Equal(t, "Tom", p.Name)
	assert.Equal(t, 12, p.Age)
	assert.Equal(t, float32(123), p.Balance)
	assert.Equal(t, true, p.LowBalance)
}
示例#6
0
func Test_pickingColumns(t *testing.T) {
	// Prepare test
	sr := NewStringReadCloser(strings.Join(testCsvLines, "\n"))
	c := NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1)

	c.Header(map[string]int{"Name": 0, "Balance": 2})

	// Start test
	p := &person2{}
	err := c.SetData(p)
	assert.NotError(t, err)

	assert.Equal(t, "Tony", p.Name)
	assert.Equal(t, float32(123.456), p.Balance)
}
示例#7
0
func Test_customTrueFalse(t *testing.T) {
	// Prepare test
	sr := NewStringReadCloser("YY|NN")
	c := NewCsvUtil(sr).Comma('|').CustomBool([]string{"YY"}, []string{"NN"})

	type YN struct {
		Yes bool
		No  bool
	}

	// Start test
	p := &YN{}
	err := c.SetData(p)
	assert.NotError(t, err)

	assert.Equal(t, true, p.Yes)
	assert.Equal(t, false, p.No)
}
示例#8
0
// TableNotEmpty asserts tableName is not empty.
func TableNotEmpty(t assert.Tester, tableName string) {
	rowCount, err := GetTableRowCount(tableName)
	assert.NotError(t, err, assert.NESTING_2)
	assert.T(t, rowCount > 0, assert.NESTING_2, "Expected table to have data.")
}
示例#9
0
// TableRowCount asserts tableName has expectedRowCount rows.
func TableRowCount(t assert.Tester, tableName string, expectedRowCount int) {
	rowCount, err := GetTableRowCount(tableName)
	assert.NotError(t, err, assert.NESTING_2)
	assert.Equal(t, rowCount, expectedRowCount, assert.NESTING_2, "Expected %d rows in %s table but got %d.", expectedRowCount, tableName, rowCount)
}