Пример #1
0
func TestScanNullTime(t *testing.T) {
	var rawTime = time.Now()
	var mysqlTime = "2010-07-03 13:24:33"
	var byteTime = []byte(mysqlTime)
	var notTime = 3

	var n Time
	var err error
	err = n.Scan(rawTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.NotEmpty(t, n.Time.Format("2006-01-02 15:04:05"))

	err = n.Scan(mysqlTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), mysqlTime)

	err = n.Scan(byteTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), mysqlTime)

	err = n.Scan(notTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Пример #2
0
func TestUnmarshalNullDate(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var stringDate string = `"2010-07-03"`
	var stringTime string = `"2010-07-03T13:24:33"`
	var stringBogus string = `"bogus"`

	var n Date
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringDate), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = json.Unmarshal([]byte(stringTime), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringBogus), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Пример #3
0
func TestUnmarshalNullTime(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var stringTime string = `"2010-07-03T13:24:33Z"`
	var stringBogus string = `"bogus"`

	var n Time
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringTime), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), "2010-07-03 13:24:33")

	err = json.Unmarshal([]byte(stringBogus), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Пример #4
0
func TestUnmarshalNullString(t *testing.T) {
	var jsonNull string = `null`
	var jsonNumber string = `3`
	var jsonEmpty string = `""`
	var validString string = `"foo"`

	var n String
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonNumber), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.String, "")

	err = json.Unmarshal([]byte(validString), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.String, "foo")

}
Пример #5
0
func TestUnmarshalNullBool(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var bogusString string = `"bogus"`
	var validTrue string = `true`
	var validFalse string = `false`

	var n Bool
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(bogusString), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(validTrue), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)

	err = json.Unmarshal([]byte(validFalse), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
}
Пример #6
0
func TestMarshalJSON(t *testing.T) {
	b1, err1 := json.Marshal(Version{1, 0, 0})
	expect.Nil(t, err1)
	expect.Equal(t, string(b1), `"1.0.0"`)
	b2, err2 := json.Marshal(Version{2, 0, 30})
	expect.Nil(t, err2)
	expect.Equal(t, string(b2), `"2.0.30"`)
}
Пример #7
0
func TestScansQuotedIdentifier(t *testing.T) {
	scan, err := scanOnce(`"simple"`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.QUOTED_IDENT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `simple`)

	scan, err = scanOnceWith("`simple`", Ruleset{BacktickIsQuotemark: true})
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.QUOTED_IDENT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `simple`)
}
Пример #8
0
func TestParseErrors(t *testing.T) {
	examples := []struct {
		Input string
		Error string
	}{
		{Input: `mytable`,
			Error: `sql:1:8: expected 'SELECT, INSERT, or UPDATE' but received 'Identifier'`},
		{Input: `SELECT * WHERE`,
			Error: `sql:1:15: expected 'FROM' but received 'WHERE'`},
		{Input: `SELECT * FROM *`,
			Error: `sql:1:16: expected 'a table name' but received '*'`},
		{Input: "SELECT `mycolumn` FROM mytable", // backticks (w/ ansi ruleset)
			Error: "sql:1:8: unexpected character U+0060 '`'"},
		{Input: `SELECT DISTINCTROW * FROM mytable`, // distinctrow (w/ ansi ruleset)
			Error: `sql:1:19: statement includes SELECT "DISTINCTROW", but CanSelectDistinctRow is false`},
		{Input: `~`,
			Error: `sql:1:1: unexpected character U+007E '~'`},
		{Input: `SELECT * FROM foos; SELECT * FROM bars;`,
			Error: `sql:1:27: statement does not end at semicolon`},
		{Input: `SELECT * FROM mytable PROCEDURE compute(foo)`, // with HasLiteral
			Error: `sql:1:32: cannot parse statement; reached unimplemented clause at 'PROCEDURE'`},
		{Input: `SELECT * FROM mytable +`, // without HasLiteral
			Error: `sql:1:24: cannot parse statement; reached unimplemented clause at '+'`},
	}

	for _, example := range examples {
		parser := New([]byte(example.Input), Ruleset{})
		stmt, err := parser.ParseStatement()
		expect.Nil(t, stmt)
		if expect.NotNil(t, err, "expected a parsing error") {
			expect.Equal(t, err.Error(), example.Error)
		}
	}
}
Пример #9
0
func TestTraceParser(t *testing.T) {
	output := bytes.Buffer{}
	parser := New([]byte(`SELECT * FROM table_with_long_name WHERE ♫`), Ruleset{})
	parser.Trace = &output
	stmt, err := parser.ParseStatement()
	expect.NotNil(t, err, "expected a parsing error")
	expect.Nil(t, stmt)

	expected := []string{
		regexp.QuoteMeta(`  SELECT : SELECT         @ Parser.parseSelect:`) + "[0-9]+",
		regexp.QuoteMeta(`         : *              @ Parser.parseSelect:`) + "[0-9]+",
		regexp.QuoteMeta(`    FROM : FROM           @ Parser.parseSelect:`) + "[0-9]+",
		regexp.QuoteMeta(` table_~ : Identifier     @ Parser.parseSelect:`) + "[0-9]+",
		regexp.QuoteMeta(`   WHERE : WHERE          @ Parser.parseSelect:`) + "[0-9]+",
		regexp.QuoteMeta(` (error) sql:1:42: unexpected character U+266B '♫'`),
		"$", // string ends with newline
	}

	// compare trace output, ignoring the source line numbers
	lines := strings.Split(output.String(), "\n")
	if expect.Equal(t, len(lines), len(expected)) {
		maxSafe := utils.MinInt(len(expected), len(lines))
		for i := 0; i < maxSafe; i++ {
			expect.Regexp(t, lines[i], "^"+expected[i])
		}
	} else {
		t.Log("Error:", err)
		t.Log("Full trace output:\n", output.String())
	}

}
Пример #10
0
func TestUnmarshalJSON(t *testing.T) {
	examples := []struct {
		Json    string
		Version Version
	}{
		{`"5.0.0"`, Version{5, 0, 0}},
		{`"v2.4.12"`, Version{2, 4, 12}},
		{`"3.5.0ab"`, Version{3, 5, 0}},
		{`"8.22"`, Version{8, 22, 0}},
	}

	var v Version
	for _, ex := range examples {
		err := json.Unmarshal([]byte(ex.Json), &v)
		expect.Nil(t, err, ex.Json)
		expect.Equal(t, v, ex.Version, ex.Json)
	}

	badExamples := []struct {
		Json  string
		Error string
	}{
		{`null`, "semver: cannot parse version from non-string JSON value"},
		{`""`, "semver: json string is not a valid version"},
		{`"bogus"`, "semver: json string is not a valid version"},
	}

	for _, ex := range badExamples {
		err := json.Unmarshal([]byte(ex.Json), &v)
		if expect.NotNil(t, err, ex.Json) {
			expect.Equal(t, err.Error(), ex.Error, ex.Json)
		}
	}
}
Пример #11
0
func TestDriverUsage(t *testing.T) {
	db, err := sql.Open("sqltest", "")
	expect.Nil(t, err)

	rows, err := db.Query("SELECT * FROM examples")
	expect.Nil(t, err)

	total := 0
	for rows.Next() {
		err := rows.Scan()
		expect.Nil(t, err)
		total += 1
	}
	err = rows.Close()
	expect.Nil(t, err)
	expect.Equal(t, total, 0)
}
Пример #12
0
func TestParseUpdate(t *testing.T) {
	parser := New([]byte(`UPDATE mytable SET a = 1`), Ruleset{})
	stmt, err := parser.ParseStatement()
	expect.Nil(t, stmt)
	if expect.NotNil(t, err) {
		expect.Equal(t, err.Error(), `sql:1:15: cannot parse statement; reached unimplemented clause at 'mytable'`)
	}
}
Пример #13
0
func TestParseInsert(t *testing.T) {
	parser := New([]byte(`INSERT INTO mytable`), Ruleset{})
	stmt, err := parser.ParseStatement()
	expect.Nil(t, stmt)
	if expect.NotNil(t, err) {
		expect.Equal(t, err.Error(), `sql:1:20: cannot parse statement; reached unimplemented clause at 'mytable'`)
	}
}
Пример #14
0
func TestSqlParseError(t *testing.T) {
	db, err := sql.Open("sqltest", "")
	expect.Nil(t, err)

	_, err = db.Query("SELECT * FROM")
	expect.NotNil(t, err)
	expect.Equal(t, err.Error(), "sql:1:14: expected 'a table name' but received 'End of statement'")
}
Пример #15
0
func TestScanNullDate(t *testing.T) {
	var rawTime = time.Date(2010, time.July, 3, 13, 24, 33, 999, time.UTC)
	var mysqlTime = "2010-07-03 13:24:33"
	var mysqlDate = "2010-07-03"
	var byteTime = []byte(mysqlTime)
	var byteDate = []byte(mysqlDate)
	var notTime = 3

	var n Date
	var err error
	err = n.Scan(rawTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(mysqlTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = n.Scan(mysqlDate)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(byteTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = n.Scan(byteDate)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(notTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Пример #16
0
func TestScanPos(t *testing.T) {
	var err *scanError
	handleError := func(pos token.Position, msg string) {
		err = &scanError{pos, msg}
	}

	var scan scanToken
	s := Scanner{}
	s.Init([]byte("CREATE TABLE\n  candies\n()"), handleError, Ruleset{})
	expect.Equal(t, s.Pos(), token.Position{"sql", 0, 1, 1})
	expect.Nil(t, err)

	scan.pos, scan.tok, scan.lit = s.Scan()
	expect.Equal(t, s.Pos(), token.Position{"sql", 6, 1, 7})
	expect.Nil(t, err)

	scan.pos, scan.tok, scan.lit = s.Scan()
	expect.Equal(t, s.Pos(), token.Position{"sql", 12, 1, 13})
	expect.Nil(t, err)

	scan.pos, scan.tok, scan.lit = s.Scan()
	expect.Equal(t, s.Pos(), token.Position{"sql", 22, 2, 10})
	expect.Nil(t, err)

	scan.pos, scan.tok, scan.lit = s.Scan()
	expect.Equal(t, s.Pos(), token.Position{"sql", 24, 3, 2})
	expect.Nil(t, err)

	scan.pos, scan.tok, scan.lit = s.Scan()
	expect.Equal(t, s.Pos(), token.Position{"sql", 25, 3, 3})
	expect.Nil(t, err)
}
Пример #17
0
func TestScanNullUUID(t *testing.T) {
	// start with a null UUID and scan a typical UUID
	{
		expectedUUID := uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
		stringUUID := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

		n := UUID{}
		err := n.Scan(stringUUID)
		expect.Nil(t, err, "error unmarshaling null.UUID")
		expect.True(t, n.Valid, "null.UUID should be valid")
		expect.Equal(t, n.UUID, expectedUUID, "UUIDs should be equal")
	}

	// start with some UUID, and scan nil
	{
		n := SomeUUID(uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8})
		err := n.Scan(nil)
		expect.Nil(t, err, "error unmarshaling null.UUID")
		expect.False(t, n.Valid, "null.UUID should not be valid")
		expect.Equal(t, n.UUID, uuid.Nil, "null.UUID value should be equal to uuid.Nil")
	}
}
Пример #18
0
func TestScannerNextCharacter(t *testing.T) {
	var err *scanError

	err = scanAll("SELECT * FROM candies\r\n  WHERE sweetness = 11\n\r\r")
	expect.Nil(t, err)

	err = scanAll(string([]byte{0x00, 0xFF}))
	if expect.NotNil(t, err) {
		expect.Equal(t, err.pos.Offset, 0)
		expect.Equal(t, err.pos.Line, 1)
		expect.Equal(t, err.pos.Column, 1)
		expect.Equal(t, err.msg, `unexpected character U+0000`)
	}
}
Пример #19
0
func TestUnmarshalNullInt(t *testing.T) {
	var jsonNull string = `null`
	var rationalFloat string = `12.22`
	var roundedFloat string = `16.0`
	var validZero string = `0`
	var validNegative string = `-300`
	var validPositive string = `1602525`

	var n Int
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(rationalFloat), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(roundedFloat), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(validZero), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, 0)

	err = json.Unmarshal([]byte(validNegative), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, -300)

	err = json.Unmarshal([]byte(validPositive), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, 1602525)
}
Пример #20
0
func TestScansIdentifier(t *testing.T) {
	scan, err := scanOnce(`simple`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.IDENT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `simple`)

	scan, err = scanOnce("sim$ple")
	expect.Equal(t, scan.tok, token.IDENT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `sim`)

	scan, err = scanOnceWith("sim$ple", Ruleset{DollarIsLetter: true})
	expect.Equal(t, scan.tok, token.IDENT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `sim$ple`)
}
Пример #21
0
func TestSkipsWhitesace(t *testing.T) {
	scan, err := scanOnce("\n    SELECT\n")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.SELECT)
	expect.Equal(t, scan.pos, 5)
	expect.Equal(t, scan.lit, "SELECT")

	// scan, err = scanOnce("\n    --comment\n    SELECT--comment\n")
	// expect.Nil(t, err)
	// expect.Equal(t, scan.tok, token.SELECT)
	// expect.Equal(t, scan.pos, 18)
	// expect.Equal(t, scan.lit, "SELECT")
	//
	// scan, err = scanOnce("\n    --comment\r\n    SELECT--comment\n")
	// expect.Nil(t, err)
	// expect.Equal(t, scan.tok, token.SELECT)
	// expect.Equal(t, scan.pos, 19)
	// expect.Equal(t, scan.lit, "SELECT")
}
Пример #22
0
func TestScansStrings(t *testing.T) {
	scan, err := scanOnce(`'simple'`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `'simple'`)

	scan, err = scanOnce(`' white space '`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `' white space '`)

	scan, err = scanOnce(`'quote\''`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `'quote\''`)

	scan, err = scanOnce(`'escaped \n\r\b\t\f'`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `'escaped \n\r\b\t\f'`)

	scan, err = scanOnce(`'slashes \\ \/'`)
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `'slashes \\ \/'`)

	scan, err = scanOnceWith(`"simple"`, Ruleset{DoubleQuoteIsString: true})
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.STRING)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, `"simple"`)
}
Пример #23
0
func TestValueNullUUID(t *testing.T) {
	u := UUID{}
	val, err := u.Value()
	expect.Nil(t, err, "error getting null.UUID value")
	expect.Nil(t, val, "wrong value returned, should be nil")
}
Пример #24
0
func TestParseSelect(t *testing.T) {
	examples := []struct {
		Input  string
		Rules  Ruleset
		Result ast.Stmt
		Trace  bool // for debugging
	}{
		{Input: `SELECT * FROM mytable`, // basics
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Name("mytable"),
				Star: true,
			}},
		{Input: `SELECT * FROM mytable;`, // with semicolon
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Name("mytable"),
				Star: true,
			}},
		{Input: `SELECT * FROM "mytable"`, // doublequotes (ansi)
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Quoted("mytable"),
				Star: true,
			}},
		{Input: "SELECT * FROM `mytable`", // backticks (mysql)
			Rules: MysqlRuleset,
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Quoted("mytable"),
				Star: true,
			}},
		{Input: `SELECT foo, bar FROM mytable`, // with columns
			Result: &ast.SelectStmt{
				Type:   ast.SELECT_ALL,
				From:   ast.Name("mytable"),
				Select: []ast.Expr{ast.Name("foo"), ast.Name("bar")},
			}},
		{Input: `SELECT "foo", "bar" FROM mytable`, // with quoted columns
			Result: &ast.SelectStmt{
				Type:   ast.SELECT_ALL,
				From:   ast.Name("mytable"),
				Select: []ast.Expr{ast.Quoted("foo"), ast.Quoted("bar")},
			}},
		{Input: `SELECT ALL * FROM mytable`, // ALL
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Name("mytable"),
				Star: true,
			}},
		{Input: `SELECT DISTINCT * FROM mytable`, // DISTINCT
			Result: &ast.SelectStmt{
				Type: ast.DISTINCT,
				From: ast.Name("mytable"),
				Star: true,
			}},
		{Input: `SELECT DISTINCTROW * FROM mytable`, // DISTINCT ROW (mysql)
			Rules: MysqlRuleset,
			Result: &ast.SelectStmt{
				Type: ast.DISTINCT_ROW,
				From: ast.Name("mytable"),
				Star: true,
			}},
		{Input: `SELECT * FROM mytable WHERE id = 3`, // simple WHERE clause
			Rules: AnsiRuleset,
			Result: &ast.SelectStmt{
				Type:  ast.SELECT_ALL,
				Star:  true,
				From:  ast.Name("mytable"),
				Where: ast.Binary(ast.Name("id"), ast.EQUAL, ast.Lit("3")),
			}},

		// a WHERE clause with nested expressions (binary and unary)
		{Input: `SELECT * FROM mytable WHERE kind != "muppet" AND 5 < -size`,
			Rules: MysqlRuleset,
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				Star: true,
				From: ast.Name("mytable"),
				Where: ast.Binary(
					ast.Binary(ast.Name("kind"), ast.NOT_EQUAL, ast.Lit(`"muppet"`)),
					ast.AND,
					ast.Binary(ast.Lit("5"), ast.LESS, ast.Unary(ast.NEGATE, ast.Name("size"))),
				),
			}},

		// allow table-less select if someone says its ok
		{Input: `SELECT *`, // TODO: eventually I'd like this to be `SELECT 1+1;`
			Rules:  Ruleset{CanSelectWithoutFrom: true},
			Result: &ast.SelectStmt{Type: ast.SELECT_ALL, Star: true}},

		// allow unimplmented clauses if someone says its ok
		{Input: `SELECT * FROM mytable PROCEDURE compute(foo)`,
			Rules: Ruleset{AllowNotImplemented: true},
			Result: &ast.SelectStmt{
				Type: ast.SELECT_ALL,
				From: ast.Name("mytable"),
				Star: true,
			}},
	}

	for _, example := range examples {
		parser := New([]byte(example.Input), example.Rules)
		if example.Trace {
			parser.Trace = os.Stdout
		}
		stmt, err := parser.ParseStatement()
		expect.Nil(t, err, "Error for `"+example.Input+"`")
		expect.Equal(t, stmt, example.Result, example.Input)
	}
}
Пример #25
0
func TestScansPunctuation(t *testing.T) {
	scan, err := scanOnce("$")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.DOLLAR)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("(")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.LEFT_PAREN)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce(")")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.RIGHT_PAREN)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce(";")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.SEMICOLON)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce(":")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.COLON)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("=")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.EQUALS)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("@")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.AT)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("+")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.PLUS)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("-")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.MINUS)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("/")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.SLASH)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce(",")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.COMMA)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce(".")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.PERIOD)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("*")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.ASTERISK)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("?")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.QUESTION)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("[")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.LEFT_BRACKET)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")

	scan, err = scanOnce("]")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.RIGHT_BRACKET)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "")
}
Пример #26
0
func TestScansNumbers(t *testing.T) {
	scan, err := scanOnce("4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "4")

	scan, err = scanOnce("4.123")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "4.123")

	scan, err = scanOnce(".4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".4")

	scan, err = scanOnce(".123")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".123")

	scan, err = scanOnce("9")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "9")

	scan, err = scanOnce("0")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "0")

	scan, err = scanOnce("0.123")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "0.123")

	scan, err = scanOnce("123e4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "123e4")

	scan, err = scanOnce("123e-4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "123e-4")

	scan, err = scanOnce("123e+4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, "123e+4")

	scan, err = scanOnce(".123e4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".123e4")

	scan, err = scanOnce(".123e-4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".123e-4")

	scan, err = scanOnce(".123e+4")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".123e+4")

	scan, err = scanOnce(".123e4567")
	expect.Nil(t, err)
	expect.Equal(t, scan.tok, token.NUMBER)
	expect.Equal(t, scan.pos, 0)
	expect.Equal(t, scan.lit, ".123e4567")
}
Пример #27
0
func TestGobEncodeDecode(t *testing.T) {
	var buf bytes.Buffer
	// FIXME: preserve date timezone (or use UTC by default? see time.Time)
	var destDate, srcDate Date
	srcDate.Set(date.At(2033, 10, 24, nil))
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcDate))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destDate))
	expect.Equal(t, destDate, srcDate)
	buf.Reset()

	var destTime, srcTime Time
	srcTime.Set(time.Now())
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcTime))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destTime))
	expect.Equal(t, destTime, srcTime)
	buf.Reset()

	var destString, srcString String
	srcString.Set("gobify me")
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcString))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destString))
	expect.Equal(t, destString, srcString)
	buf.Reset()

	var destInt, srcInt Int
	srcInt.Set(-154)
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcInt))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destInt))
	expect.Equal(t, destInt, srcInt)
	buf.Reset()

	var destBool, srcBool Bool
	srcBool.Set(true)
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcBool))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destBool))
	expect.Equal(t, destBool, srcBool)
	buf.Reset()

	var destUUID, srcUUID UUID
	srcUUID.Set(uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8})
	expect.Nil(t, gob.NewEncoder(&buf).Encode(srcUUID))
	expect.Nil(t, gob.NewDecoder(&buf).Decode(&destUUID))
	expect.Equal(t, destUUID, srcUUID)
	buf.Reset()
}