github.com/andrew00x/gomovies@v0.1.0/pkg/service/service_torrent_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 6 "github.com/andrew00x/gomovies/pkg/api" 7 "github.com/andrew00x/gomovies/pkg/config" 8 "github.com/andrew00x/gomovies/pkg/torrent" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var tr *torrentMock 13 14 func TestAddFile(t *testing.T) { 15 setup() 16 srv := &TorrentService{tr: tr} 17 18 err := srv.AddFile([]byte("torrent")) 19 20 assert.Nil(t, err) 21 assert.Equal(t, []interface{}{[]byte("torrent")}, tr.added) 22 } 23 24 func TestAddUrl(t *testing.T) { 25 setup() 26 srv := &TorrentService{tr: tr} 27 28 err := srv.AddUrl("torrent") 29 30 assert.Nil(t, err) 31 assert.Equal(t, []interface{}{"torrent"}, tr.added) 32 } 33 34 func TestGetTorrents(t *testing.T) { 35 setup() 36 tr.downloads = []api.TorrentDownload{{Name: "foo", Completed: true}, {Name: "bar", Completed: true}} 37 srv := &TorrentService{tr: tr} 38 39 res, err := srv.Torrents() 40 assert.Nil(t, err) 41 assert.Equal(t, []api.TorrentDownload{{Name: "foo", Completed: true}, {Name: "bar", Completed: true}}, res) 42 } 43 44 func TestStopTorrent(t *testing.T) { 45 setup() 46 srv := &TorrentService{tr: tr} 47 48 err := srv.Stop(api.TorrentDownload{Name: "foo"}) 49 assert.Nil(t, err) 50 assert.Equal(t, []api.TorrentDownload{{Name: "foo"}}, tr.stopped) 51 } 52 53 func TestStartTorrent(t *testing.T) { 54 setup() 55 srv := &TorrentService{tr: tr} 56 57 err := srv.Start(api.TorrentDownload{Name: "foo"}) 58 assert.Nil(t, err) 59 assert.Equal(t, []api.TorrentDownload{{Name: "foo"}}, tr.started) 60 } 61 62 func TestDeleteTorrent(t *testing.T) { 63 setup() 64 srv := &TorrentService{tr: tr} 65 66 err := srv.Delete(api.TorrentDownload{Name: "foo"}) 67 assert.Nil(t, err) 68 assert.Equal(t, []api.TorrentDownload{{Name: "foo"}}, tr.deleted) 69 } 70 71 func setup() { 72 tr = &torrentMock{} 73 torrent.Factory = func(*config.Config) torrent.Torrent { 74 return tr 75 } 76 } 77 78 type torrentMock struct { 79 err error 80 downloads []api.TorrentDownload 81 added []interface{} 82 deleted []api.TorrentDownload 83 started []api.TorrentDownload 84 stopped []api.TorrentDownload 85 } 86 87 func (t *torrentMock) AddFile(b []byte) error { 88 t.added = append(t.added, b) 89 return t.err 90 } 91 92 func (t *torrentMock) AddUrl(u string) error { 93 t.added = append(t.added, u) 94 return t.err 95 } 96 97 func (t *torrentMock) Torrents() ([]api.TorrentDownload, error) { 98 return t.downloads, t.err 99 } 100 101 func (t *torrentMock) Stop(d api.TorrentDownload) error { 102 t.stopped = append(t.stopped, d) 103 return t.err 104 } 105 106 func (t *torrentMock) Start(d api.TorrentDownload) error { 107 t.started = append(t.started, d) 108 return t.err 109 } 110 111 func (t *torrentMock) Delete(d api.TorrentDownload) error { 112 t.deleted = append(t.deleted, d) 113 return t.err 114 }