(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestDialService_CreateDial(t *testing.T) { |
| 15 | // Ensure a dial can be created by a user & a membership for the user is automatically created. |
| 16 | t.Run("OK", func(t *testing.T) { |
| 17 | db := MustOpenDB(t) |
| 18 | defer MustCloseDB(t, db) |
| 19 | |
| 20 | ctx := context.Background() |
| 21 | _, ctx0 := MustCreateUser(t, ctx, db, &wtf.User{Name: "jane", Email: "jane@gmail.com"}) |
| 22 | |
| 23 | s := sqlite.NewDialService(db) |
| 24 | dial := &wtf.Dial{Name: "mydial"} |
| 25 | |
| 26 | // Create new dial. Ensure the current user is the owner & an invite code is generated. |
| 27 | if err := s.CreateDial(ctx0, dial); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } else if got, want := dial.ID, 1; got != want { |
| 30 | t.Fatalf("ID=%v, want %v", got, want) |
| 31 | } else if got, want := dial.UserID, 1; got != want { |
| 32 | t.Fatalf("UserID=%v, want %v", got, want) |
| 33 | } else if dial.InviteCode == "" { |
| 34 | t.Fatal("expected invite code generation") |
| 35 | } else if dial.CreatedAt.IsZero() { |
| 36 | t.Fatal("expected created at") |
| 37 | } else if dial.UpdatedAt.IsZero() { |
| 38 | t.Fatal("expected updated at") |
| 39 | } else if dial.User == nil { |
| 40 | t.Fatal("expected user") |
| 41 | } |
| 42 | |
| 43 | // Fetch dial from database & compare. |
| 44 | if other, err := s.FindDialByID(ctx0, 1); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } else if !reflect.DeepEqual(dial, other) { |
| 47 | t.Fatalf("mismatch: %#v != %#v", dial, other) |
| 48 | } |
| 49 | |
| 50 | // Ensure membership for owner automatically created. |
| 51 | if _, n, err := sqlite.NewDialMembershipService(db).FindDialMemberships(ctx0, wtf.DialMembershipFilter{DialID: &dial.ID}); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } else if n != 1 { |
| 54 | t.Fatal("expected owner membership auto-creation") |
| 55 | } |
| 56 | }) |
| 57 | |
| 58 | // Ensure that creating a nameless dial returns an error. |
| 59 | t.Run("ErrNameRequired", func(t *testing.T) { |
| 60 | db := MustOpenDB(t) |
| 61 | defer MustCloseDB(t, db) |
| 62 | _, ctx0 := MustCreateUser(t, context.Background(), db, &wtf.User{Name: "jane", Email: "jane@gmail.com"}) |
| 63 | |
| 64 | if err := sqlite.NewDialService(db).CreateDial(ctx0, &wtf.Dial{}); err == nil { |
| 65 | t.Fatal("expected error") |
| 66 | } else if wtf.ErrorCode(err) != wtf.EINVALID || wtf.ErrorMessage(err) != "Dial name required." { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | // Ensure that creating a dial with a long name returns an error. |
nothing calls this directly
no test coverage detected