diff options
author | Michael Muré <batolettre@gmail.com> | 2022-08-18 23:34:05 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-08-18 23:44:06 +0200 |
commit | 5511c230b678a181cc596238bf6669428d1b1902 (patch) | |
tree | 8701efc87732439f993eb4f1d00585fc419b87ab /entities/bug/operation_test.go | |
parent | 5ca686b59751e3c87740b84108c54fc675a074cf (diff) | |
download | git-bug-5511c230b678a181cc596238bf6669428d1b1902.tar.gz |
move {bug,identity} to /entities, move input to /commands
Diffstat (limited to 'entities/bug/operation_test.go')
-rw-r--r-- | entities/bug/operation_test.go | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/entities/bug/operation_test.go b/entities/bug/operation_test.go new file mode 100644 index 00000000..fe8080c3 --- /dev/null +++ b/entities/bug/operation_test.go @@ -0,0 +1,131 @@ +package bug + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/entities/identity" + "github.com/MichaelMure/git-bug/entity/dag" + "github.com/MichaelMure/git-bug/repository" +) + +// TODO: move to entity/dag? + +func TestValidate(t *testing.T) { + repo := repository.NewMockRepoClock() + + makeIdentity := func(t *testing.T, name, email string) *identity.Identity { + i, err := identity.NewIdentity(repo, name, email) + require.NoError(t, err) + return i + } + + rene := makeIdentity(t, "René Descartes", "rene@descartes.fr") + + unix := time.Now().Unix() + + good := []Operation{ + NewCreateOp(rene, unix, "title", "message", nil), + NewSetTitleOp(rene, unix, "title2", "title1"), + NewAddCommentOp(rene, unix, "message2", nil), + NewSetStatusOp(rene, unix, ClosedStatus), + NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"}), + } + + for _, op := range good { + if err := op.Validate(); err != nil { + t.Fatal(err) + } + } + + bad := []Operation{ + // opbase + NewSetStatusOp(makeIdentity(t, "", "rene@descartes.fr"), unix, ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes\u001b", "rene@descartes.fr"), unix, ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@descartes.fr\u001b"), unix, ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René \nDescartes", "rene@descartes.fr"), unix, ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@\ndescartes.fr"), unix, ClosedStatus), + &CreateOperation{OpBase: dag.NewOpBase(CreateOp, rene, 0), + Title: "title", + Message: "message", + }, + + NewCreateOp(rene, unix, "multi\nline", "message", nil), + NewCreateOp(rene, unix, "title", "message", []repository.Hash{repository.Hash("invalid")}), + NewCreateOp(rene, unix, "title\u001b", "message", nil), + NewCreateOp(rene, unix, "title", "message\u001b", nil), + NewSetTitleOp(rene, unix, "multi\nline", "title1"), + NewSetTitleOp(rene, unix, "title", "multi\nline"), + NewSetTitleOp(rene, unix, "title\u001b", "title2"), + NewSetTitleOp(rene, unix, "title", "title2\u001b"), + NewAddCommentOp(rene, unix, "message\u001b", nil), + NewAddCommentOp(rene, unix, "message", []repository.Hash{repository.Hash("invalid")}), + NewSetStatusOp(rene, unix, 1000), + NewSetStatusOp(rene, unix, 0), + NewLabelChangeOperation(rene, unix, []Label{}, []Label{}), + NewLabelChangeOperation(rene, unix, []Label{"multi\nline"}, []Label{}), + } + + for i, op := range bad { + if err := op.Validate(); err == nil { + t.Fatal("validation should have failed", i, op) + } + } +} + +func TestMetadata(t *testing.T) { + repo := repository.NewMockRepoClock() + + rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + op := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil) + + op.SetMetadata("key", "value") + + val, ok := op.GetMetadata("key") + require.True(t, ok) + require.Equal(t, val, "value") +} + +func TestID(t *testing.T) { + repo := repository.CreateGoGitTestRepo(t, false) + + repos := []repository.ClockedRepo{ + repository.NewMockRepo(), + repo, + } + + for _, repo := range repos { + rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr") + require.NoError(t, err) + err = rene.Commit(repo) + require.NoError(t, err) + + b, op, err := Create(rene, time.Now().Unix(), "title", "message", nil, nil) + require.NoError(t, err) + + id1 := op.Id() + require.NoError(t, id1.Validate()) + + err = b.Commit(repo) + require.NoError(t, err) + + op2 := b.FirstOp() + + id2 := op2.Id() + require.NoError(t, id2.Validate()) + require.Equal(t, id1, id2) + + b2, err := Read(repo, b.Id()) + require.NoError(t, err) + + op3 := b2.FirstOp() + + id3 := op3.Id() + require.NoError(t, id3.Validate()) + require.Equal(t, id1, id3) + } +} |