2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
2018-07-12 22:31:41 +03:00
|
|
|
|
|
|
|
import (
|
2019-01-19 21:23:31 +03:00
|
|
|
"encoding/json"
|
2018-07-12 22:31:41 +03:00
|
|
|
"testing"
|
2018-09-25 18:56:58 +03:00
|
|
|
"time"
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2020-07-01 21:00:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2019-02-25 01:05:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/timestamp"
|
2019-01-19 21:23:31 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-07-01 21:00:53 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-12 22:31:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
2018-09-28 21:39:39 +03:00
|
|
|
snapshot := Snapshot{}
|
2018-07-12 22:31:41 +03:00
|
|
|
|
2020-07-01 21:00:53 +03:00
|
|
|
rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
|
2018-09-25 18:56:58 +03:00
|
|
|
unix := time.Now().Unix()
|
|
|
|
|
|
|
|
create := NewCreateOp(rene, unix, "title", "message", nil)
|
2018-07-12 22:31:41 +03:00
|
|
|
|
2018-09-29 12:28:18 +03:00
|
|
|
create.Apply(&snapshot)
|
2018-07-12 22:31:41 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id := create.Id()
|
|
|
|
assert.NoError(t, id.Validate())
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2019-03-28 03:21:41 +03:00
|
|
|
comment := Comment{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: id,
|
2019-03-28 03:21:41 +03:00
|
|
|
Author: rene,
|
|
|
|
Message: "message",
|
|
|
|
UnixTime: timestamp.Timestamp(create.UnixTime),
|
|
|
|
}
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
expected := Snapshot{
|
2018-07-12 22:31:41 +03:00
|
|
|
Title: "title",
|
2018-09-28 21:39:39 +03:00
|
|
|
Comments: []Comment{
|
2018-09-29 21:41:19 +03:00
|
|
|
comment,
|
2018-07-12 22:31:41 +03:00
|
|
|
},
|
2019-03-31 23:32:35 +03:00
|
|
|
Author: rene,
|
|
|
|
Participants: []identity.Interface{rene},
|
|
|
|
Actors: []identity.Interface{rene},
|
2020-06-26 00:18:17 +03:00
|
|
|
CreateTime: create.Time(),
|
2018-09-29 21:41:19 +03:00
|
|
|
Timeline: []TimelineItem{
|
2018-09-30 18:15:54 +03:00
|
|
|
&CreateTimelineItem{
|
2019-08-07 16:31:38 +03:00
|
|
|
CommentTimelineItem: NewCommentTimelineItem(id, comment),
|
2018-09-30 18:15:54 +03:00
|
|
|
},
|
2018-09-29 21:41:19 +03:00
|
|
|
},
|
2018-07-12 22:31:41 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
assert.Equal(t, expected, snapshot)
|
2018-07-12 22:31:41 +03:00
|
|
|
}
|
2019-01-19 21:23:31 +03:00
|
|
|
|
|
|
|
func TestCreateSerialize(t *testing.T) {
|
2020-11-08 19:54:28 +03:00
|
|
|
repo := repository.NewMockRepo()
|
2020-07-01 21:00:53 +03:00
|
|
|
rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
|
|
|
|
err := rene.Commit(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
unix := time.Now().Unix()
|
|
|
|
before := NewCreateOp(rene, unix, "title", "message", nil)
|
|
|
|
|
|
|
|
data, err := json.Marshal(before)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var after CreateOperation
|
|
|
|
err = json.Unmarshal(data, &after)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-07-01 21:00:53 +03:00
|
|
|
// enforce creating the ID
|
2019-08-11 15:08:03 +03:00
|
|
|
before.Id()
|
2020-07-01 21:00:53 +03:00
|
|
|
|
|
|
|
// Replace the identity stub with the real thing
|
|
|
|
assert.Equal(t, rene.Id(), after.base().Author.Id())
|
|
|
|
after.Author = rene
|
2019-08-07 16:31:38 +03:00
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
assert.Equal(t, before, &after)
|
|
|
|
}
|