2018-09-18 13:49:16 +03:00
|
|
|
package _select
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-16 21:34:52 +03:00
|
|
|
"time"
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-27 22:14:55 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-18 13:49:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelect(t *testing.T) {
|
2022-06-16 16:02:52 +03:00
|
|
|
repo := repository.CreateGoGitTestRepo(t, false)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
backend, err := cache.NewRepoCacheNoEvents(repo)
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
const typename = "foo"
|
|
|
|
const namespace = "foos"
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
resolve := func(args []string) (*cache.BugCache, []string, error) {
|
|
|
|
return Resolve[*cache.BugCache](backend, typename, namespace, backend.Bugs(), args)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = resolve([]string{})
|
|
|
|
require.True(t, IsErrNoValidId(err))
|
|
|
|
|
|
|
|
err = Select(backend, namespace, "invalid")
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Resolve without a pattern should fail when no bug is selected
|
2022-12-27 21:40:40 +03:00
|
|
|
_, _, err = resolve([]string{})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.Error(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// generate a bunch of bugs
|
2019-02-16 21:34:52 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
rene, err := backend.Identities().New("René Descartes", "rene@descartes.fr")
|
2019-02-17 19:59:54 +03:00
|
|
|
require.NoError(t, err)
|
2019-02-16 21:34:52 +03:00
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
for i := 0; i < 10; i++ {
|
2022-12-27 21:40:40 +03:00
|
|
|
_, _, err := backend.Bugs().NewRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
// and two more for testing
|
2022-12-27 21:40:40 +03:00
|
|
|
b1, _, err := backend.Bugs().NewRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2022-12-27 21:40:40 +03:00
|
|
|
b2, _, err := backend.Bugs().NewRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
err = Select(backend, namespace, b1.Id())
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// normal select without args
|
2022-12-27 21:40:40 +03:00
|
|
|
b3, _, err := resolve([]string{})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.Id(), b3.Id())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// override selection with same id
|
2022-12-27 21:40:40 +03:00
|
|
|
b4, _, err := resolve([]string{b1.Id().String()})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.Id(), b4.Id())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// override selection with a prefix
|
2022-12-27 21:40:40 +03:00
|
|
|
b5, _, err := resolve([]string{b1.Id().Human()})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.Id(), b5.Id())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// args that shouldn't override
|
2022-12-27 21:40:40 +03:00
|
|
|
b6, _, err := resolve([]string{"arg"})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.Id(), b6.Id())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// override with a different id
|
2022-12-27 21:40:40 +03:00
|
|
|
b7, _, err := resolve([]string{b2.Id().String()})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b2.Id(), b7.Id())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2022-12-27 21:40:40 +03:00
|
|
|
err = Clear(backend, namespace)
|
2019-02-16 19:32:30 +03:00
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
// Resolve without a pattern should error again after clearing the selected bug
|
2022-12-27 21:40:40 +03:00
|
|
|
_, _, err = resolve([]string{})
|
2019-02-16 19:32:30 +03:00
|
|
|
require.Error(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
}
|