aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/entity_actions_test.go
blob: 6cc544b669cf612f042df5c19228260057dab7c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package dag

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"
)

func allEntities(t testing.TB, bugs <-chan StreamedEntity) []*Entity {
	var result []*Entity
	for streamed := range bugs {
		if streamed.Err != nil {
			t.Fatal(streamed.Err)
		}
		result = append(result, streamed.Entity)
	}
	return result
}

func TestPushPull(t *testing.T) {
	repoA, repoB, remote, id1, id2, def := makeTestContextRemote()
	defer repository.CleanupTestRepos(repoA, repoB, remote)

	// distribute the identities
	_, err := identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote --> B
	entity := New(def)
	entity.Append(newOp1(id1, "foo"))

	err = entity.Commit(repoA)
	require.NoError(t, err)

	_, err = Push(def, repoA, "origin")
	require.NoError(t, err)

	err = Pull(def, repoB, "origin")
	require.NoError(t, err)

	entities := allEntities(t, ReadAll(def, repoB))
	require.Len(t, entities, 1)

	// B --> remote --> A
	entity = New(def)
	entity.Append(newOp2(id2, "bar"))

	err = entity.Commit(repoB)
	require.NoError(t, err)

	_, err = Push(def, repoB, "origin")
	require.NoError(t, err)

	err = Pull(def, repoA, "origin")
	require.NoError(t, err)

	entities = allEntities(t, ReadAll(def, repoB))
	require.Len(t, entities, 2)
}

func TestListLocalIds(t *testing.T) {
	repoA, repoB, remote, id1, id2, def := makeTestContextRemote()
	defer repository.CleanupTestRepos(repoA, repoB, remote)

	// distribute the identities
	_, err := identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote --> B
	entity := New(def)
	entity.Append(newOp1(id1, "foo"))
	err = entity.Commit(repoA)
	require.NoError(t, err)

	entity = New(def)
	entity.Append(newOp2(id2, "bar"))
	err = entity.Commit(repoA)
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 0)

	_, err = Push(def, repoA, "origin")
	require.NoError(t, err)

	_, err = Fetch(def, repoB, "origin")
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 0)

	err = Pull(def, repoB, "origin")
	require.NoError(t, err)

	listLocalIds(t, def, repoA, 2)
	listLocalIds(t, def, repoB, 2)
}

func listLocalIds(t *testing.T, def Definition, repo repository.RepoData, expectedCount int) {
	ids, err := ListLocalIds(def, repo)
	require.NoError(t, err)
	require.Len(t, ids, expectedCount)
}