TestPostgres starts Postgres using testcontainers and runs all other tests in this file as sub-tests (to prevent spawning many clusters).
(t *testing.T)
| 18 | // TestPostgres starts Postgres using testcontainers and runs all other tests in |
| 19 | // this file as sub-tests (to prevent spawning many clusters). |
| 20 | func TestPostgres(t *testing.T) { |
| 21 | ctx := context.Background() |
| 22 | |
| 23 | pg := pgtestcontainer.New(t) |
| 24 | defer pg.Terminate(t) |
| 25 | |
| 26 | encKeyRing, err := database.NewRandomKeyring() |
| 27 | require.NoError(t, err) |
| 28 | conf, err := json.Marshal(encKeyRing) |
| 29 | require.NoError(t, err) |
| 30 | |
| 31 | db, err := database.Open("postgres", pg.DatabaseURL, string(conf)) |
| 32 | require.NoError(t, err) |
| 33 | require.NotNil(t, db) |
| 34 | |
| 35 | require.NoError(t, db.Migrate(ctx)) |
| 36 | defer func() { require.NoError(t, db.Close()) }() |
| 37 | |
| 38 | t.Run("TestOrganizations", func(t *testing.T) { testOrganizations(t, db) }) |
| 39 | t.Run("TestOrgsWithPagination", func(t *testing.T) { testOrgsWithPagination(t, db) }) |
| 40 | t.Run("TestProjects", func(t *testing.T) { testProjects(t, db) }) |
| 41 | t.Run("TestProjectsWithAnnotations", func(t *testing.T) { testProjectsWithAnnotations(t, db) }) |
| 42 | t.Run("TestProjectsWithPagination", func(t *testing.T) { testProjectsWithPagination(t, db) }) |
| 43 | t.Run("TestProjectsForUsersWithPagination", func(t *testing.T) { testProjectsForUserWithPagination(t, db) }) |
| 44 | t.Run("TestMembersWithPagination", func(t *testing.T) { testOrgsMembersPagination(t, db) }) |
| 45 | t.Run("TestUpsertProjectVariable", func(t *testing.T) { testUpsertProjectVariable(t, db) }) |
| 46 | t.Run("TestManagedGitRepos", func(t *testing.T) { testManagedGitRepos(t, db) }) |
| 47 | t.Run("TestOrganizationMemberUserAttributes", func(t *testing.T) { testOrganizationMemberUserAttributes(t, db) }) |
| 48 | t.Run("TestAttributeValidation", func(t *testing.T) { testAttributeValidation(t, db) }) |
| 49 | |
| 50 | t.Run("TestOrgNameValidation", func(t *testing.T) { |
| 51 | cases := []struct { |
| 52 | name string |
| 53 | errorContains string |
| 54 | }{ |
| 55 | {"", "must be at least 2 characters"}, |
| 56 | {"a", "must be at least 2 characters"}, |
| 57 | {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "must be at most 40 characters"}, |
| 58 | {"foo bar", "must use only letters, numbers, underscores and dashes"}, |
| 59 | {"foo@bar", "must use only letters, numbers, underscores and dashes"}, |
| 60 | {"foo_bar!", "must use only letters, numbers, underscores and dashes"}, |
| 61 | {"-foo", "must use only letters, numbers, underscores and dashes"}, |
| 62 | {"aa", ""}, |
| 63 | {"foo-", ""}, |
| 64 | {"_foo", ""}, |
| 65 | {"foo_bar_baz_123", ""}, |
| 66 | {"hello", ""}, |
| 67 | {"foo-bar", ""}, |
| 68 | {"foo-bar-baz", ""}, |
| 69 | {"foo_bar_baz", ""}, |
| 70 | {"foo_bar_baz_123_", ""}, |
| 71 | {"FooBar", ""}, |
| 72 | } |
| 73 | for _, tc := range cases { |
| 74 | t.Run(tc.name, func(t *testing.T) { |
| 75 | _, err := db.InsertOrganization(context.Background(), &database.InsertOrganizationOptions{Name: tc.name}) |
| 76 | if tc.errorContains != "" { |
| 77 | require.ErrorContains(t, err, tc.errorContains) |
nothing calls this directly
no test coverage detected