Пример #1
0
func TestRecords(t *testing.T) {
	cases := map[string]struct {
		args             []string
		code             int
		in, err, out     []byte
		prior, posterior data.State
	}{

		// elos records kinds {{{
		"elos records kinds": {
			args: []string{"kinds"},
			out:  []byte(strings.Join(apply(s(models.Kinds), func(s string) string { return "* " + s }), "\n") + "\n"),
			prior: data.State{
				models.Kind_USER: []*data.Record{
					&data.Record{
						Kind: models.Kind_USER,
						User: &models.User{
							Id: "1",
						},
					},
				},
				models.Kind_CREDENTIAL: []*data.Record{
					&data.Record{
						Kind: models.Kind_CREDENTIAL,
						Credential: &models.Credential{
							Id:      "2",
							Type:    models.Credential_PASSWORD,
							Public:  "pu",
							Private: "pr",
							OwnerId: "1",
						},
					},
				},
			},
		},
		// }}}

		// elos records count {{{
		"elos records count": {
			args: []string{"count"},
			in:   []byte("SESSION\n"),
			out:  []byte("Which kind? [string]:2\n"),
			prior: data.State{
				models.Kind_USER: []*data.Record{
					&data.Record{
						Kind: models.Kind_USER,
						User: &models.User{
							Id: "1",
						},
					},
					&data.Record{
						Kind: models.Kind_USER,
						User: &models.User{
							Id: "2",
						},
					},
				},
				models.Kind_CREDENTIAL: []*data.Record{
					&data.Record{
						Kind: models.Kind_CREDENTIAL,
						Credential: &models.Credential{
							Id:      "3",
							Type:    models.Credential_PASSWORD,
							Public:  "pu",
							Private: "pr",
							OwnerId: "1",
						},
					},
					&data.Record{
						Kind: models.Kind_CREDENTIAL,
						Credential: &models.Credential{
							Id:      "4",
							Type:    models.Credential_PASSWORD,
							Public:  "2pu",
							Private: "pr",
							OwnerId: "2",
						},
					},
				},
				models.Kind_SESSION: []*data.Record{
					&data.Record{
						Kind: models.Kind_SESSION,
						Session: &models.Session{
							Id:           "5",
							AccessToken:  "non-empty",
							ExpiresAt:    models.TimestampFrom(time.Now().Add(5 * time.Minute)).WithoutNanos(),
							OwnerId:      "1",
							CredentialId: "3",
						},
					},
					&data.Record{
						Kind: models.Kind_SESSION,
						Session: &models.Session{
							Id:           "4",
							AccessToken:  "non-empty",
							ExpiresAt:    models.TimestampFrom(time.Now().Add(5 * time.Minute)).WithoutNanos(),
							OwnerId:      "1",
							CredentialId: "3",
						},
					},
					&data.Record{
						Kind: models.Kind_SESSION,
						Session: &models.Session{
							Id:           "4",
							AccessToken:  "non-empty",
							ExpiresAt:    models.TimestampFrom(time.Now().Add(5 * time.Minute)).WithoutNanos(),
							OwnerId:      "2",
							CredentialId: "4",
						},
					},
				},
			},
		},
		// }}}

		// TODO(nclandolfi) test query and changes

	}

	for n, c := range cases {
		t.Run(n, func(t *testing.T) {
			db := mem.NewDB()
			dbc, conn, err := data.DBBothLocal(db)
			if err != nil {
				t.Fatalf("data.DBBothLocal error: %v", err)
			}
			defer conn.Close()
			if err := data.Seed(context.Background(), dbc, c.prior); err != nil {
				t.Fatalf("data.Seed error: %v", err)
			}

			ui := &cli.MockUi{
				InputReader: bytes.NewBuffer(c.in),
			}
			cmd := &RecordsCommand{
				UI:       ui,
				UserID:   c.prior[models.Kind_USER][0].User.Id,
				DBClient: dbc,
			}

			if got, want := cmd.Run(c.args), c.code; got != want {
				t.Log(ui.ErrorWriter.String())
				t.Fatalf("cmd.Run(%v): got %d, want %d", c.args, got, want)
			}

			if got, want := ui.ErrorWriter.String(), string(c.err); got != want {
				t.Fatalf("ui.ErrorWriter.String(): got %q, want %q", got, want)
			}

			if got, want := ui.OutputWriter.String(), string(c.out); got != want {
				t.Fatalf("ui.OutputWriter.String(): got %q, want %q", got, want)
			}

			finalState := c.prior
			if c.posterior != nil {
				finalState = c.posterior
			}

			if got, want := data.CompareState(context.Background(), dbc, finalState), error(nil); got != want {
				t.Fatalf("data.CompareState: got %v, want %v", got, want)
			}
		})
	}
}
Пример #2
0
func TestCal(t *testing.T) {
	//TODO(nclandolfi): fix test
	t.Skip()
	NOW := time.Now()
	cases := map[string]struct {
		prior  data.State
		userID string
		args   []string
		input  io.Reader
		code   int
		errput string
		output string
		// IF posterior is nil, prior will be used
		posterior data.State
	}{
		"simple cal week": {
			prior: data.State{
				models.Kind_USER: []*data.Record{
					&data.Record{
						Kind: models.Kind_USER,
						User: &models.User{
							Id: "1",
						},
					},
				},
				models.Kind_CREDENTIAL: []*data.Record{
					&data.Record{
						Kind: models.Kind_CREDENTIAL,
						Credential: &models.Credential{
							Id:      "2",
							OwnerId: "1",
							Type:    models.Credential_PASSWORD,
							Public:  "pu",
							Private: "pr",
						},
					},
				},
				models.Kind_FIXTURE: []*data.Record{
					&data.Record{
						Kind: models.Kind_FIXTURE,
						Fixture: &models.Fixture{
							Id:        "3",
							OwnerId:   "1",
							StartTime: models.TimestampFrom(NOW.Add(1 * time.Hour)),
							EndTime:   models.TimestampFrom(NOW.Add(2 * time.Hour)),
						},
					},
				},
			},
			input:  new(bytes.Buffer),
			userID: "1",
			args:   []string{"week"},
		},
	}

	for n, c := range cases {
		t.Run(n, func(t *testing.T) {
			db := mem.NewDB()
			dbc, conn, err := data.DBBothLocal(db)
			if err != nil {
				t.Fatalf("data.DBBothLocal error: %v", err)
			}
			defer conn.Close()
			if err := data.Seed(context.Background(), dbc, c.prior); err != nil {
				t.Fatalf("data.Seed error: %v", err)
			}

			if c.input == nil {
				t.Fatal("c.input must be non-nil")
			}
			ui := &cli.MockUi{
				InputReader: c.input,
			}
			cmd := &CalCommand{
				UI:     ui,
				UserID: c.userID,
				DB:     data.DB(dbc),
			}

			if got, want := cmd.Run(c.args), c.code; got != want {
				t.Log(ui.ErrorWriter.String())
				t.Fatalf("cmd.Run(%v): got %d, want %d", c.args, got, want)
			}

			if got, want := ui.ErrorWriter.String(), c.errput; got != want {
				t.Fatalf("ui.ErrorWriter.String(): got %q, want %q", got, want)
			}

			if got, want := ui.OutputWriter.String(), c.output; got != want {
				t.Fatalf("ui.OutputWriter.String(): got %q, want %q", got, want)
			}

			finalState := c.prior
			if c.posterior != nil {
				finalState = c.posterior
			}

			if got, want := data.CompareState(context.Background(), dbc, finalState), error(nil); got != want {
				t.Fatalf("data.CompareState: got %v, want %v", got, want)
			}
		})
	}
}