sosedoff / pgweb

Cross-platform client for PostgreSQL databases
https://sosedoff.github.io/pgweb
MIT License
8.63k stars 732 forks source link

test assertion assumes a clean postgres db #161

Closed akarki15 closed 8 years ago

akarki15 commented 8 years ago

i am running make test on master and it is failing because the expected tables returned by statements.PG_OBJECTS does not exactly match the tables that the running postgres has. statements.PG_OBJECTS returns tables from all databases, not just the one that the client.TestAll() creates. however, the test at line 203 asserts equality- which fails as the following output demonstrates

▶ make test
godep go test -cover ./...
?       github.com/sosedoff/pgweb   [no test files]
ok      github.com/sosedoff/pgweb/pkg/api   0.026s  coverage: 8.3% of statements
ok      github.com/sosedoff/pgweb/pkg/bookmarks 0.015s  coverage: 100.0% of statements
Teardown error: exit status 1
--- FAIL: TestAll (1.34s)
        Error Trace:    client_test.go:203
            client_test.go:340
    Error:      Not equal: []string{"alternate_stock", "authors", "book_backup", "book_queue", "books", "customers", "daily_inventory", "distinguished_authors", "editions", "employees", "favorite_authors", "favorite_books", "money_example", "my_list", "numeric_values", "publishers", "schedules", "shipments", "states", "stock", "stock_backup", "subjects", "text_sorting"} (expected)
                    != []string{"alternate_stock", "authors", "book_backup", "book_queue", "books", "customers", "daily_inventory", "distinguished_authors", "editions", "employees", "favorite_authors", "favorite_books", "money_example", "my_list", "numeric_values", "persons", "publishers", "schedules", "shipments", "states", "stock", "stock_backup", "subjects", "text_sorting"} (actual)

            Diff:
            --- Expected
            +++ Actual
            @@ -1,2 +1,2 @@
            -([]string) (len=23 cap=23) {
            +([]string) (len=24 cap=32) {
              (string) (len=15) "alternate_stock",
            @@ -16,2 +16,3 @@
              (string) (len=14) "numeric_values",
            + (string) (len=7) "persons",
              (string) (len=10) "publishers",

FAIL
coverage: 46.5% of statements
FAIL    github.com/sosedoff/pgweb/pkg/client    1.362s
ok      github.com/sosedoff/pgweb/pkg/command   0.014s  coverage: 80.0% of statements
ok      github.com/sosedoff/pgweb/pkg/connection    0.178s  coverage: 90.0% of statements
?       github.com/sosedoff/pgweb/pkg/data  [no test files]
?       github.com/sosedoff/pgweb/pkg/history   [no test files]
?       github.com/sosedoff/pgweb/pkg/shared    [no test files]
?       github.com/sosedoff/pgweb/pkg/statements    [no test files]
?       github.com/sosedoff/pgweb/pkg/util  [no test files]
godep: go exit status 1
make: *** [test] Error 1

lets change the assert.Equal in line 208 to something that checks asserts that objects["public"].Tables contains all the expected tables. something like :


    for table := range tables {
        assert.True(t, stringSliceContains(objects["public"].Tables, table))
    }
    // stringSliceContains(a,b) returns whether slice a contains string b
sosedoff commented 8 years ago

How did you get extra tables in the test database?

akarki15 commented 8 years ago

i didn't add table to the test database. statements.PG_OBJECTS returns tables from all databases

sosedoff commented 8 years ago

PG_OBJECTS statement only returns objects from the current database. Also, if you look at client tests, it drops and then recreates the database on every run so you should not have any extra tables in your database. Would need more info on your setup since i cant reproduce your issue.

sosedoff commented 8 years ago

You should not have any tables while running make test since it will drop and recreate the database on every run, see https://github.com/sosedoff/pgweb/blob/master/pkg/client/client_test.go#L332 Closing for now since its an edge case and has something to do with your local setup. I tried to reproduce with no luck.

akarki15 commented 8 years ago

sorry for replying late @sosedoff. i found out that for some reason whenever i create a new pg database, it was automatically creating a table named "person". i think there is some default schema being applied to every new table and that was polluting the test db. since this is an issue very specific to my machine setup, im going to close the pull request as well. thanks for getting back though!

cbandy commented 8 years ago

@akarki15 check the template1 database.

https://www.postgresql.org/docs/current/static/sql-createdatabase.html

akarki15 commented 8 years ago

holy shit that was it. thanks a lot @cbandy!