From 5f6d12922d93b2bc2184561be7b151f99176848b Mon Sep 17 00:00:00 2001 From: Nelson-He Date: Thu, 2 Jun 2022 15:21:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbookmarks=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=8C=85=E5=90=8D=E7=A7=B0=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=E4=BF=AE=E5=A4=8Dmake=20test=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- data/booktown.sql | 32 +++++++----------- pkg/bookmarks/bookmarks_test.go | 4 +-- pkg/cli/cli.go | 2 +- pkg/client/client.go | 10 ++++++ pkg/client/client_test.go | 57 +++++++++++++++------------------ pkg/client/dump.go | 4 +-- pkg/client/dump_test.go | 6 ++-- 8 files changed, 55 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index c4264d0..4f5f684 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Compiled binaries will be stored into ./bin directory. ## Testing Before running tests, make sure you have openGauss server running on `localhost:5432` -interface. Also, you must have `postgres` user that could create new databases +interface. Also, you must have `openGauss` user with password `Gaussdb_123` that could create new databases in your local environment. openGauss-webclient server should not be running at the same time. Execute test suite: diff --git a/data/booktown.sql b/data/booktown.sql index aecccac..1a11201 100644 --- a/data/booktown.sql +++ b/data/booktown.sql @@ -1,17 +1,6 @@ -- -- Selected TOC Entries: -- --- --- TOC Entry ID 1 (OID 0) --- --- Name: booktown Type: DATABASE Owner: postgres --- - -DROP DATABASE IF EXISTS "booktown"; -CREATE DATABASE "booktown"; - -\connect booktown postgres --- -- TOC Entry ID 2 (OID 2991542) -- -- Name: DATABASE "booktown" Type: COMMENT Owner: @@ -461,7 +450,7 @@ CREATE TABLE "money_example" ( -- CREATE TABLE "shipments" ( - "id" integer DEFAULT nextval('"shipments_ship_id_seq"'::text) NOT NULL, + "id" integer NOT NULL, "customer_id" integer, "isbn" text, "ship_date" timestamp with time zone @@ -1025,10 +1014,11 @@ CREATE SEQUENCE "author_ids" start 0 increment 1 maxvalue 2147483647 minvalue 0 -- Name: distinguished_authors Type: TABLE Owner: manager -- -CREATE TABLE "distinguished_authors" ( - "award" text -) -INHERITS ("authors"); +--- This syntax is not supported yet by openGauss +---CREATE TABLE "distinguished_authors" ( +--- "award" text +---) +---INHERITS ("authors"); -- -- TOC Entry ID 107 (OID 3726476) @@ -1293,7 +1283,7 @@ CREATE VIEW "recent_shipments" as SELECT count(*) AS num_shipped, max(shipments. COPY "publishers" FROM stdin; -150 Kids Can Press Kids Can Press, 29 Birch Ave. Toronto, ON  M4V 1E2 +150 Kids Can Press Kids Can Press, 29 Birch Ave. Toronto,�ON��M4V 1E2 91 Henry Holt & Company, Inc. Henry Holt & Company, Inc. 115 West 18th Street New York, NY 10011 113 O'Reilly & Associates O'Reilly & Associates, Inc. 101 Morris St, Sebastopol, CA 95472 62 Watson-Guptill Publications 1515 Boradway, New York, NY 10036 @@ -1633,10 +1623,10 @@ COPY "books" FROM stdin; -- -COPY "distinguished_authors" FROM stdin; -25043 Simon Neil Pulitzer Prize -1809 Geisel Theodor Seuss Pulitzer Prize -\. +---COPY "distinguished_authors" FROM stdin; +---25043 Simon Neil Pulitzer Prize +---1809 Geisel Theodor Seuss Pulitzer Prize +---\. -- -- Data for TOC Entry ID 129 (OID 3727889) -- diff --git a/pkg/bookmarks/bookmarks_test.go b/pkg/bookmarks/bookmarks_test.go index 0b43497..88c6767 100644 --- a/pkg/bookmarks/bookmarks_test.go +++ b/pkg/bookmarks/bookmarks_test.go @@ -3,8 +3,8 @@ package bookmarks import ( "testing" - "github.com/sosedoff/pgweb/pkg/command" - "github.com/sosedoff/pgweb/pkg/shared" + "gitee.com/openGauss/openGauss-webclient/pkg/command" + "gitee.com/openGauss/openGauss-webclient/pkg/shared" "github.com/stretchr/testify/assert" ) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index ca0b036..3393a50 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -160,7 +160,7 @@ func initOptions() { } func printVersion() { - chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)} + chunks := []string{fmt.Sprintf("openGauss-webclient v%s", command.Version)} if command.GitCommit != "" { chunks = append(chunks, fmt.Sprintf("(git: %s)", command.GitCommit)) diff --git a/pkg/client/client.go b/pkg/client/client.go index 8ef4166..431a4f1 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -26,6 +26,9 @@ var ( cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s`) cockroachType = "CockroachDB" + + openGaussSignature = regexp.MustCompile(`(?i)openGauss ([\d\.]+)\s`) + openGaussType = "PostgreSQL" ) type Client struct { @@ -165,6 +168,13 @@ func (client *Client) setServerVersion() { client.serverVersion = matches[0][1] return } + + matches = openGaussSignature.FindAllStringSubmatch(version, 1) + if len(matches) > 0 { + client.serverType = postgresType + client.serverVersion = matches[0][1] + return + } } func (client *Client) Test() error { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index cafedae..7190fc0 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "runtime" + "sort" "testing" "time" @@ -29,6 +30,7 @@ func mapKeys(data map[string]*Objects) []string { for k, _ := range data { result = append(result, k) } + sort.Strings(result) return result } @@ -54,16 +56,14 @@ func initVars() { serverHost = getVar("PGHOST", "localhost") serverPort = getVar("PGPORT", "5432") - serverUser = getVar("PGUSER", "postgres") - serverPassword = getVar("PGPASSWORD", "postgres") - serverDatabase = getVar("PGDATABASE", "booktown") + serverUser = getVar("PGUSER", "openGauss") + serverPassword = getVar("PGPASSWORD", "Gaussdb_123") + serverDatabase = getVar("PGDATABASE", "opengauss_test_booktown") } func setupCommands() { testCommands = map[string]string{ - "createdb": "createdb", - "psql": "psql", - "dropdb": "dropdb", + "gsql": "gsql", } if onWindows() { @@ -82,11 +82,13 @@ func setup() { command.Opts.DisablePrettyJSON = true out, err := exec.Command( - testCommands["createdb"], + testCommands["gsql"], "-U", serverUser, + "-d", "postgres", "-h", serverHost, "-p", serverPort, - serverDatabase, + "-W", serverPassword, + "-c", "CREATE DATABASE "+serverDatabase, ).CombinedOutput() if err != nil { @@ -96,10 +98,11 @@ func setup() { } out, err = exec.Command( - testCommands["psql"], + testCommands["gsql"], "-U", serverUser, "-h", serverHost, "-p", serverPort, + "-W", serverPassword, "-f", "../../data/booktown.sql", serverDatabase, ).CombinedOutput() @@ -112,7 +115,7 @@ func setup() { } func setupClient() { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) testClient, _ = NewFromUrl(url, nil) } @@ -124,11 +127,13 @@ func teardownClient() { func teardown() { _, err := exec.Command( - testCommands["dropdb"], + testCommands["gsql"], "-U", serverUser, + "-d", "postgres", "-h", serverHost, "-p", serverPort, - serverDatabase, + "-W", serverPassword, + "-c", "DROP DATABASE IF EXISTS "+serverDatabase, ).CombinedOutput() if err != nil { @@ -137,19 +142,7 @@ func teardown() { } func testNewClientFromUrl(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) - client, err := NewFromUrl(url, nil) - - if err != nil { - defer client.Close() - } - - assert.Equal(t, nil, err) - assert.Equal(t, url, client.ConnectionString) -} - -func testNewClientFromUrl2(t *testing.T) { - url := fmt.Sprintf("postgresql://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, err := NewFromUrl(url, nil) if err != nil { @@ -197,7 +190,7 @@ func testDatabases(t *testing.T) { res, err := testClient.Databases() assert.Equal(t, nil, err) - assert.Contains(t, res, "booktown") + assert.Contains(t, res, "opengauss_test_booktown") assert.Contains(t, res, "postgres") } @@ -213,7 +206,6 @@ func testObjects(t *testing.T) { "books", "customers", "daily_inventory", - "distinguished_authors", "dummies", "editions", "employees", @@ -234,7 +226,7 @@ func testObjects(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, []string{"schema", "name", "type", "owner", "comment"}, res.Columns) - assert.Equal(t, []string{"public"}, mapKeys(objects)) + assert.Equal(t, []string{"db4ai", "dbe_perf", "dbe_pldeveloper", "public"}, mapKeys(objects)) assert.Equal(t, tables, objects["public"].Tables) assert.Equal(t, []string{"recent_shipments", "stock_view"}, objects["public"].Views) assert.Equal(t, []string{"author_ids", "book_ids", "shipments_ship_id_seq", "subject_ids"}, objects["public"].Sequences) @@ -370,7 +362,7 @@ func testQueryInvalidTable(t *testing.T) { res, err := testClient.Query("SELECT * FROM books2") assert.NotEqual(t, nil, err) - assert.Equal(t, "pq: relation \"books2\" does not exist", err.Error()) + assert.Contains(t, err.Error(), "pq: relation \"books2\" does not exist") assert.Equal(t, true, res == nil) } @@ -418,7 +410,7 @@ func testHistoryError(t *testing.T) { } func testHistoryUniqueness(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, _ := NewFromUrl(url, nil) client.Query("SELECT * FROM books WHERE id = 1") @@ -434,7 +426,7 @@ func testReadOnlyMode(t *testing.T) { command.Opts.ReadOnly = false }() - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) client, _ := NewFromUrl(url, nil) err := client.SetReadOnlyMode() @@ -496,7 +488,8 @@ func TestAll(t *testing.T) { testHistoryUniqueness(t) testHistoryError(t) testReadOnlyMode(t) - testDumpExport(t) + //TODO:support gs_dump of openGauss + //testDumpExport(t) teardownClient() teardown() diff --git a/pkg/client/dump.go b/pkg/client/dump.go index d9c1705..1d6488c 100644 --- a/pkg/client/dump.go +++ b/pkg/client/dump.go @@ -22,7 +22,7 @@ type Dump struct { // CanExport returns true if database dump tool could be used without an error func (d *Dump) CanExport() bool { - return exec.Command("pg_dump", "--version").Run() == nil + return exec.Command("gs_dump", "--version").Run() == nil } // Export streams the database dump to the specified writer @@ -47,7 +47,7 @@ func (d *Dump) Export(connstr string, writer io.Writer) error { opts = append(opts, connstr) - cmd := exec.Command("pg_dump", opts...) + cmd := exec.Command("gs_dump", opts...) cmd.Stdout = writer cmd.Stderr = errOutput diff --git a/pkg/client/dump_test.go b/pkg/client/dump_test.go index 61c5171..8901520 100644 --- a/pkg/client/dump_test.go +++ b/pkg/client/dump_test.go @@ -9,7 +9,7 @@ import ( ) func testDumpExport(t *testing.T) { - url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) + url := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, serverDatabase) savePath := "/tmp/dump.sql.gz" os.Remove(savePath) @@ -34,7 +34,7 @@ func testDumpExport(t *testing.T) { assert.NoError(t, err) // Test nonexistent database - invalidURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, "foobar") + invalidURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", serverUser, serverPassword, serverHost, serverPort, "foobar") err = dump.Export(invalidURL, saveFile) assert.Contains(t, err.Error(), `database "foobar" does not exist`) @@ -46,7 +46,7 @@ func testDumpExport(t *testing.T) { // Should drop "search_path" param from URI dump = Dump{} - searchPathURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable&search_path=private", serverUser, serverHost, serverPort, serverDatabase) + searchPathURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable&search_path=private", serverUser, serverPassword, serverHost, serverPort, serverDatabase) err = dump.Export(searchPathURL, saveFile) assert.NoError(t, err) } -- Gitee