Susumu Yata
null+****@clear*****
Tue Sep 12 10:58:05 JST 2017
Susumu Yata 2017-09-12 10:58:05 +0900 (Tue, 12 Sep 2017) New Revision: ba77300b79ced6af0c644be035b5b77e36ad86f5 https://github.com/groonga/grnci/commit/ba77300b79ced6af0c644be035b5b77e36ad86f5 Message: Simplify DB.ColumnList. Ref: #42 Modified files: v2/db.go v2/libgrn/db_test.go Modified: v2/db.go (+12 -9) =================================================================== --- v2/db.go 2017-09-12 10:38:55 +0900 (d80ede9) +++ v2/db.go 2017-09-12 10:58:05 +0900 (2a3b538) @@ -175,29 +175,32 @@ type DBColumn struct { } // ColumnList executes column_list. -func (db *DB) ColumnList(tbl string) ([]DBColumn, Response, error) { +func (db *DB) ColumnList(tbl string) ([]DBColumn, error) { resp, err := db.Invoke("column_list", map[string]interface{}{ "table": tbl, }, nil) if err != nil { - return nil, nil, err + return nil, err } defer resp.Close() jsonData, err := ioutil.ReadAll(resp) if err != nil { - return nil, resp, err + return nil, err + } + if len(jsonData) == 0 { + return nil, resp.Err() } var result [][]interface{} if err := json.Unmarshal(jsonData, &result); err != nil { - if resp.Err() != nil { - return nil, resp, nil - } - return nil, resp, NewError(ResponseError, "json.Unmarshal failed.", map[string]interface{}{ + return nil, NewError(ResponseError, "json.Unmarshal failed.", map[string]interface{}{ "error": err.Error(), }) } if len(result) == 0 { - return nil, resp, NewError(ResponseError, "The result is empty.", nil) + if resp.Err() != nil { + return nil, resp.Err() + } + return nil, NewError(ResponseError, "The result is empty.", nil) } var fields []string for _, meta := range result[0] { @@ -252,7 +255,7 @@ func (db *DB) ColumnList(tbl string) ([]DBColumn, Response, error) { } columns = append(columns, column) } - return columns, resp, nil + return columns, resp.Err() } // ColumnRemove executes column_remove. Modified: v2/libgrn/db_test.go (+27 -34) =================================================================== --- v2/libgrn/db_test.go 2017-09-12 10:38:55 +0900 (b5c1e08) +++ v2/libgrn/db_test.go 2017-09-12 10:58:05 +0900 (72402a2) @@ -34,40 +34,6 @@ func removeDB(db *grnci.DB, dir string) { os.RemoveAll(dir) } -func TestDBColumnList(t *testing.T) { - db, dir := makeDB(t) - defer removeDB(db, dir) - - dump := `table_create Users TABLE_PAT_KEY ShortText -column_create Users name COLUMN_SCALAR ShortText` - if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { - t.Fatalf("db.Restore failed: %v", err) - } - result, resp, err := db.ColumnList("Users") - if err == nil { - err = resp.Err() - } - if err != nil { - t.Fatalf("db.ColumnList failed: %v", err) - } - if len(result) != 2 { - t.Fatalf("db.ColumnList failed: result = %#v", result) - } -} - -func TestDBColumnListInvalidTable(t *testing.T) { - db, dir := makeDB(t) - defer removeDB(db, dir) - - _, resp, err := db.ColumnList("no_such_table") - if err != nil { - t.Fatalf("db.ColumnList failed: %v", err) - } - if resp.Err() == nil { - t.Fatalf("db.ColumnList wrongly succeeded") - } -} - // func TestDBColumnCopy(t *testing.T) { // client, err := NewHTTPClient("", nil) // if err != nil { @@ -113,6 +79,33 @@ func TestDBColumnCreateInvalidTable(t *testing.T) { } } +func TestDBColumnList(t *testing.T) { + db, dir := makeDB(t) + defer removeDB(db, dir) + + dump := `table_create Users TABLE_PAT_KEY ShortText +column_create Users name COLUMN_SCALAR ShortText` + if _, err := db.Restore(strings.NewReader(dump), nil, true); err != nil { + t.Fatalf("db.Restore failed: %v", err) + } + result, err := db.ColumnList("Users") + if err != nil { + t.Fatalf("db.ColumnList failed: %v", err) + } + if len(result) != 2 { + t.Fatalf("db.ColumnList failed: result = %#v", result) + } +} + +func TestDBColumnListInvalidTable(t *testing.T) { + db, dir := makeDB(t) + defer removeDB(db, dir) + + if _, err := db.ColumnList("no_such_table"); err == nil { + t.Fatalf("db.ColumnList wrongly succeeded") + } +} + func TestDBColumnRemove(t *testing.T) { db, dir := makeDB(t) defer removeDB(db, dir) -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20170912/9e5f9a67/attachment-0001.htm