func TestParseCommand_History(t *testing.T) { t.Parallel() c := cli.CommandLine{Line: liner.NewLiner()} defer c.Line.Close() // append one entry to history c.Line.AppendHistory("abc") tests := []struct { cmd string }{ {cmd: "history"}, {cmd: " history"}, {cmd: "history "}, {cmd: "History "}, } for _, test := range tests { if err := c.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) } } // buf size should be at least 1 var buf bytes.Buffer c.Line.WriteHistory(&buf) if buf.Len() < 1 { t.Fatal("History is borked") } }
func TestParseCommand_Insert(t *testing.T) { t.Parallel() ts := emptyTestServer() defer ts.Close() u, _ := url.Parse(ts.URL) config := client.Config{URL: *u} c, err := client.NewClient(config) if err != nil { t.Fatalf("unexpected error. expected %v, actual %v", nil, err) } m := cli.CommandLine{Client: c} tests := []struct { cmd string }{ {cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"}, {cmd: " INSERT cpu,host=serverA,region=us-west value=1.0"}, {cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"}, {cmd: "insert cpu,host=serverA,region=us-west value=1.0 "}, {cmd: "insert"}, {cmd: "Insert "}, {cmd: "insert c"}, {cmd: "insert int"}, } for _, test := range tests { if err := m.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) } } }
func TestParseCommand_InsertInto(t *testing.T) { t.Parallel() ts := emptyTestServer() defer ts.Close() u, _ := url.Parse(ts.URL) config := client.Config{URL: *u} c, err := client.NewClient(config) if err != nil { t.Fatalf("unexpected error. expected %v, actual %v", nil, err) } m := cli.CommandLine{Client: c} tests := []struct { cmd, db, rp string }{ { cmd: `INSERT INTO test cpu,host=serverA,region=us-west value=1.0`, db: "", rp: "test", }, { cmd: ` INSERT INTO .test cpu,host=serverA,region=us-west value=1.0`, db: "", rp: "test", }, { cmd: `INSERT INTO "test test" cpu,host=serverA,region=us-west value=1.0`, db: "", rp: "test test", }, { cmd: `Insert iNTO test.test cpu,host=serverA,region=us-west value=1.0`, db: "test", rp: "test", }, { cmd: `insert into "test test" cpu,host=serverA,region=us-west value=1.0`, db: "test", rp: "test test", }, { cmd: `insert into "d b"."test test" cpu,host=serverA,region=us-west value=1.0`, db: "d b", rp: "test test", }, } for _, test := range tests { if err := m.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) } if m.Database != test.db { t.Fatalf(`Command "insert into" db parsing failed, expected: %q, actual: %q`, test.db, m.Database) } if m.RetentionPolicy != test.rp { t.Fatalf(`Command "insert into" rp parsing failed, expected: %q, actual: %q`, test.rp, m.RetentionPolicy) } } }
func TestParseCommand_Use(t *testing.T) { t.Parallel() ts := emptyTestServer() defer ts.Close() u, _ := url.Parse(ts.URL) config := client.Config{URL: *u} c, err := client.NewClient(config) if err != nil { t.Fatalf("unexpected error. expected %v, actual %v", nil, err) } tests := []struct { cmd string }{ {cmd: "use db"}, {cmd: " use db"}, {cmd: "use db "}, {cmd: "use db;"}, {cmd: "use db; "}, {cmd: "Use db"}, } for _, test := range tests { m := cli.CommandLine{Client: c} if err := m.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) } if m.Database != "db" { t.Fatalf(`Command "use" changed database to %q. Expected db`, m.Database) } } }
func TestParseCommand_UseAuth(t *testing.T) { t.Parallel() ts := emptyTestServer() defer ts.Close() u, _ := url.Parse(ts.URL) tests := []struct { cmd string user string database string }{ { cmd: "use db", user: "******", database: "db", }, { cmd: "use blank", user: "******", database: "", }, { cmd: "use db", user: "******", database: "db", }, { cmd: "use blank", user: "******", database: "blank", }, } for i, tt := range tests { config := client.Config{URL: *u, Username: tt.user} fmt.Println("using auth:", tt.user) c, err := client.NewClient(config) if err != nil { t.Errorf("%d. unexpected error. expected %v, actual %v", i, nil, err) continue } m := cli.CommandLine{Client: c} m.ClientConfig.Username = tt.user if err := m.ParseCommand(tt.cmd); err != nil { t.Fatalf(`%d. Got error %v for command %q, expected nil.`, i, err, tt.cmd) } if m.Database != tt.database { t.Fatalf(`%d. Command "use" changed database to %q. Expected %q`, i, m.Database, tt.database) } } }
func TestParseCommand_TogglePretty(t *testing.T) { t.Parallel() c := cli.CommandLine{} if c.Pretty { t.Fatalf(`Pretty should be false.`) } c.ParseCommand("pretty") if !c.Pretty { t.Fatalf(`Pretty should be true.`) } c.ParseCommand("pretty") if c.Pretty { t.Fatalf(`Pretty should be false.`) } }
func TestParseCommand_Connect(t *testing.T) { t.Parallel() ts := emptyTestServer() defer ts.Close() u, _ := url.Parse(ts.URL) cmd := "connect " + u.Host c := cli.CommandLine{} // assert connection is established if err := c.ParseCommand(cmd); err != nil { t.Fatalf("There was an error while connecting to %v: %v", u.Path, err) } // assert server version is populated if c.ServerVersion != SERVER_VERSION { t.Fatalf("Server version is %s but should be %s.", c.ServerVersion, SERVER_VERSION) } }
func TestParseCommand_Quit(t *testing.T) { t.Parallel() tests := []struct { cmd string }{ {cmd: "quit"}, {cmd: " quit"}, {cmd: "quit "}, {cmd: "Quit "}, } for _, test := range tests { c := cli.CommandLine{Quit: make(chan struct{}, 1)} c.ParseCommand(test.cmd) // channel should be closed if _, ok := <-c.Quit; ok { t.Fatalf(`Command "quit" failed for %q.`, test.cmd) } } }
func TestParseCommand_HistoryWithBlankCommand(t *testing.T) { t.Parallel() c := cli.CommandLine{Line: liner.NewLiner()} defer c.Line.Close() // append one entry to history c.Line.AppendHistory("x") tests := []struct { cmd string err error }{ {cmd: "history"}, {cmd: " history"}, {cmd: "history "}, {cmd: "", err: cli.ErrBlankCommand}, // shouldn't be persisted in history {cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history {cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history } // a blank command will return cli.ErrBlankCommand. for _, test := range tests { if err := c.ParseCommand(test.cmd); err != test.err { t.Errorf(`Got error %v for command %q, expected %v`, err, test.cmd, test.err) } } // buf shall not contain empty commands var buf bytes.Buffer c.Line.WriteHistory(&buf) scanner := bufio.NewScanner(&buf) for scanner.Scan() { if strings.TrimSpace(scanner.Text()) == "" { t.Fatal("Empty commands should not be persisted in history.") } } }
func TestParseCommand_Consistency(t *testing.T) { t.Parallel() c := cli.CommandLine{} tests := []struct { cmd string }{ {cmd: "consistency one"}, {cmd: " consistency one"}, {cmd: "consistency one "}, {cmd: "consistency one;"}, {cmd: "consistency one; "}, {cmd: "Consistency one"}, } for _, test := range tests { if err := c.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) } if c.WriteConsistency != "one" { t.Fatalf(`Command "consistency" changed consistency to %q. Expected one`, c.WriteConsistency) } } }
func TestParseCommand_CommandsExist(t *testing.T) { t.Parallel() c, err := client.NewClient(client.Config{}) if err != nil { t.Fatalf("unexpected error %v", err) } m := cli.CommandLine{Client: c, Line: liner.NewLiner()} tests := []struct { cmd string }{ {cmd: "gopher"}, {cmd: "auth"}, {cmd: "help"}, {cmd: "format"}, {cmd: "precision"}, {cmd: "settings"}, } for _, test := range tests { if err := m.ParseCommand(test.cmd); err != nil { t.Fatalf(`Got error %v for command %q, expected nil`, err, test.cmd) } } }