Blob tests
This commit is contained in:
		
							parent
							
								
									cc7f29afbe
								
							
						
					
					
						commit
						ad3be07ad1
					
				@ -112,7 +112,9 @@ func (e *RowsEvent) decodeRows(buf *tools.Buffer, td TableDescription, bm []byte
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		row[i] = e.decodeValue(buf, mysql.ColumnType(td.ColumnTypes[i]), td.ColumnMeta[i])
 | 
			
		||||
		// pretty.Println("PARSED", mysql.ColumnType(td.ColumnTypes[i]).String(), td.ColumnMeta[i], row[i])
 | 
			
		||||
		// fmt.Printf("Parsed %s, meta %x, value %++v\n",
 | 
			
		||||
		// 	mysql.ColumnType(td.ColumnTypes[i]).String(), td.ColumnMeta[i], row[i],
 | 
			
		||||
		// )
 | 
			
		||||
	}
 | 
			
		||||
	return row, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										61
									
								
								tests/blob_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								tests/blob_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
package tests
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/localhots/bocadillo/mysql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestTinyblob(t *testing.T) {
 | 
			
		||||
	tbl := suite.createTable(mysql.ColumnTypeTinyblob, "", attrNone)
 | 
			
		||||
	defer tbl.drop(t)
 | 
			
		||||
 | 
			
		||||
	for _, v := range nRandBytes(0, 1, 100, 255) {
 | 
			
		||||
		t.Run(strconv.Itoa(len(v)), func(t *testing.T) {
 | 
			
		||||
			suite.insertAndCompare(t, tbl, v)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestBlob(t *testing.T) {
 | 
			
		||||
	tbl := suite.createTable(mysql.ColumnTypeBlob, "", attrNone)
 | 
			
		||||
	defer tbl.drop(t)
 | 
			
		||||
 | 
			
		||||
	for _, v := range nRandBytes(0, 1, 10000, 65535) {
 | 
			
		||||
		t.Run(strconv.Itoa(len(v)), func(t *testing.T) {
 | 
			
		||||
			suite.insertAndCompare(t, tbl, v)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func TestText(t *testing.T)
 | 
			
		||||
//
 | 
			
		||||
// Blob and Text values are encoded identically. It's best to handle it in the
 | 
			
		||||
// reader where schema is available and could tell how to encode these.
 | 
			
		||||
// Currently reader doesn't maintain schema and there's no way to test it.
 | 
			
		||||
 | 
			
		||||
// func TestMediumblob(t *testing.T)
 | 
			
		||||
// func TestLongblob(t *testing.T)
 | 
			
		||||
//
 | 
			
		||||
// Tried testing Mediumblob and Longblob the same way, got this error:
 | 
			
		||||
// Error 1105: Parameter of prepared statement which is set through
 | 
			
		||||
// mysql_send_long_data() is longer than 'max_allowed_packet' bytes
 | 
			
		||||
//
 | 
			
		||||
// That is from the client trying to insert a massive blob. I guess that's good
 | 
			
		||||
// enough and I hope these types work too.
 | 
			
		||||
 | 
			
		||||
func nRandBytes(ns ...int) [][]byte {
 | 
			
		||||
	nns := make([][]byte, len(ns))
 | 
			
		||||
	for i, n := range ns {
 | 
			
		||||
		nns[i] = randBytes(n)
 | 
			
		||||
	}
 | 
			
		||||
	return nns
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func randBytes(n int) []byte {
 | 
			
		||||
	s := make([]byte, n)
 | 
			
		||||
	rand.Read(s)
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
package tests
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
@ -143,6 +144,15 @@ func colTypeSyntax(ct mysql.ColumnType) (typName, attrs string) {
 | 
			
		||||
		return "CHAR", "CHARACTER SET utf8mb4"
 | 
			
		||||
	case mysql.ColumnTypeVarchar:
 | 
			
		||||
		return "VARCHAR", "CHARACTER SET utf8mb4"
 | 
			
		||||
	case mysql.ColumnTypeTinyblob:
 | 
			
		||||
		return "TINYBLOB", ""
 | 
			
		||||
	case mysql.ColumnTypeBlob:
 | 
			
		||||
		return "BLOB", ""
 | 
			
		||||
	case mysql.ColumnTypeMediumblob:
 | 
			
		||||
		return "MEDIUMBLOB", ""
 | 
			
		||||
	case mysql.ColumnTypeLongblob:
 | 
			
		||||
		return "LONGBLOB", ""
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
		panic(fmt.Errorf("Syntax not defined for %s", ct.String()))
 | 
			
		||||
	}
 | 
			
		||||
@ -197,8 +207,15 @@ func (s *testSuite) compare(t *testing.T, tbl *table, exp, res interface{}) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// fmt.Printf("VALUE RECEIVED: %T(%+v), EXPECTED: %T(%+v)\n", res, res, exp, exp)
 | 
			
		||||
	if exp != res {
 | 
			
		||||
		t.Errorf("Expected %T(%+v), got %T(%+v)", exp, exp, res, res)
 | 
			
		||||
	switch texp := exp.(type) {
 | 
			
		||||
	case []byte:
 | 
			
		||||
		if !bytes.Equal(texp, res.([]byte)) {
 | 
			
		||||
			t.Errorf("Expected %T(%+v), got %T(%+v)", exp, exp, res, res)
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		if exp != res {
 | 
			
		||||
			t.Errorf("Expected %T(%+v), got %T(%+v)", exp, exp, res, res)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user