Exemple #1
0
func Test_getHeaders(t *testing.T) {
	// Prepare test
	p := &person{}
	fields, structName := getFields(p)
	assert.Equal(t, "csvutil.person", structName)

	// Start test
	headers := getHeaders(fields)
	assert.Equal(t, CsvHeader{"Name": 0, "Age": 1, "Balance": 2, "LowBalance": 3}, headers)
}
Exemple #2
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)
}
Exemple #3
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)
}
Exemple #4
0
func Test_GetTableNames(t *testing.T) {
	dbcon.Close()
	tableNames, err := GetTableNames()
	if err == nil {
		t.Fatal("Expected database error.")
	}
	dbcon.Connect()

	createSql := `
	CREATE TABLE %s (
	  Id int(11) unsigned NOT NULL,
	  Field int(11) DEFAULT NULL,
	  PRIMARY KEY (id)
	) ENGINE=%s`

	for x := 0; x < 4; x++ {
		_, _, err = dbcon.Query(fmt.Sprintf(createSql, "test"+strconv.FormatInt(int64(x), 10), TEST_DB_ENGINE))
		if err != nil {
			t.Fatal("Did not expect database error: " + err.Error())
		}
	}

	expTables := []string{"test0", "test1", "test2", "test3"}
	tableNames, err = GetTableNames()
	if err != nil {
		t.Fatal("Did not expect database error: " + err.Error())
	}

	assert.Equal(t, expTables, tableNames)
}
Exemple #5
0
func Test_ToCsvQoute(t *testing.T) {
	// Prepare test
	p := &person{"Tom", 45, 111.22, "aaa", true}

	// Start test
	gotCsv := ToCsv(p, "|", "YY", "NN", true)
	assert.Equal(t, "\"Tom\"|\"45\"|\"111.22\"|\"YY\"", gotCsv)
}
Exemple #6
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())
}
Exemple #7
0
func Test_ToCsv(t *testing.T) {
	// Prepare test
	p := &person{"Tom", 45, 111.22, "aaa", true}

	// Start test
	gotCsv := ToCsv(p, "|", "YY", "NN")
	assert.Equal(t, "Tom|45|111.22|YY", gotCsv)
}
Exemple #8
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)
}
Exemple #9
0
func Test_Header(t *testing.T) {
	// Prepare test
	c := NewCsvUtil(nil)

	// Start test
	exp := CsvHeader{"Name": 0, "Age": 1, "Balance": 2, "LowBalance": 3}
	c.Header(exp)
	assert.Equal(t, exp, c.header)
}
Exemple #10
0
func Test_embededToCsv(t *testing.T) {
	// Prepare test
	b := new(B)
	b.Field1 = "F1"
	b.Field2 = "F2"
	b.Field3 = "F3"

	// Start test
	assert.Equal(t, "F1,F2,F3", ToCsv(b, ",", "Y", "N"))
}
Exemple #11
0
func Test_embededToCsvQuote(t *testing.T) {
	// Prepare test
	b := new(B)
	b.Field1 = "F1"
	b.Field2 = "F2"
	b.Field3 = "F3"

	// Start test
	assert.Equal(t, "\"F1\",\"F2\",\"F3\"", ToCsv(b, ",", "Y", "N", true))
}
Exemple #12
0
func Test_getFields(t *testing.T) {
	// Prepare test
	p := &person{}

	// Start test
	fields, structName := getFields(p)
	assert.Equal(t, 4, len(fields))
	assert.Equal(t, "csvutil.person", structName)

	assert.Equal(t, "Name", fields[0].name)
	assert.Equal(t, reflect.String, fields[0].typ.Kind())

	assert.Equal(t, "Age", fields[1].name)
	assert.Equal(t, reflect.Int, fields[1].typ.Kind())

	assert.Equal(t, "Balance", fields[2].name)
	assert.Equal(t, reflect.Float32, fields[2].typ.Kind())

	assert.Equal(t, "LowBalance", fields[3].name)
	assert.Equal(t, reflect.Bool, fields[3].typ.Kind())
}
Exemple #13
0
// ErrorCode asserts err is *mysql.Error and has code.
// Applicable only to github.com/ziutek/mymysql/mysql package.
func ErrorCode(t assert.Tester, err error, errorCode uint16) {
	assert.Error(t, err, assert.NESTING_2, assert.FAIL_SLOW, "Expected error but got nil.")

	merr := GetMySqlErorr(err)
	fn := func() {
		t.Error("Expected MySQL error but got some other error.")
		if err == nil {
			t.Errorf("Got error: nil")
		} else {
			t.Errorf("Got error: %s", err.Error())
		}
	}
	_, ok := err.(*mysql.Error)
	assert.Assert(t, ok, assert.NESTING_2, assert.FAIL_FAST, fn)
	assert.Equal(t, errorCode, merr.Code, assert.NESTING_2, "Expected error code %d got %d.", errorCode, merr.Code)
}
Exemple #14
0
func Test_TrailingComma(t *testing.T) {
	csvu := NewCsvUtil(nil)
	assert.Equal(t, false, csvu.csvr.TrailingComma)
	csvu.TrailingComma(true)
	assert.Equal(t, true, csvu.csvr.TrailingComma)
}
Exemple #15
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)
}
func Test_AssertEqual(t *testing.T) {
	p1 := Point{1, 1}
	p2 := Point{2, 1}
	assert.Equal(t, p1, p2)
}
Exemple #17
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)
}
Exemple #18
0
func Test_Comma(t *testing.T) {
	csvu := NewCsvUtil(nil)
	assert.Equal(t, ',', csvu.csvr.Comma)
	csvu.Comma('|')
	assert.Equal(t, '|', csvu.csvr.Comma)
}
Exemple #19
0
// TableCount asserts number of tables in the database.
func TableCount(t assert.Tester, expectedCount int) {
	sqlTables, _ := GetTableNames()
	tablesCount := len(sqlTables)
	assert.Equal(t, expectedCount, tablesCount, assert.NESTING_2, "Expected %d tables but got %d.", expectedCount, tablesCount)
}
Exemple #20
0
func Test_Comment(t *testing.T) {
	csvu := NewCsvUtil(nil)
	assert.Equal(t, '\000', csvu.csvr.Comment)
	csvu.Comment('#')
	assert.Equal(t, '#', csvu.csvr.Comment)
}
Exemple #21
0
func Test_FieldsPerRecord(t *testing.T) {
	csvu := NewCsvUtil(nil)
	assert.Equal(t, 0, csvu.csvr.FieldsPerRecord)
	csvu.FieldsPerRecord(-1)
	assert.Equal(t, -1, csvu.csvr.FieldsPerRecord)
}