PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/app/manager/channel/manager_test.go

https://gitlab.com/epicglue/epicglue
Go | 396 lines | 75 code | 12 blank | 309 comment | 8 complexity | 182d2a60f8a33f2a6f74a855559a8939 MD5 | raw file
  1. package channel_test
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "gitlab.com/epicglue/epicglue/app/manager/channel"
  5. "gitlab.com/epicglue/epicglue/app/model"
  6. r "gopkg.in/dancannon/gorethink.v2"
  7. "testing"
  8. "time"
  9. )
  10. func TestManager_Get(t *testing.T) {
  11. now := time.Now()
  12. user := model.User{
  13. ID: 1,
  14. CreatedAt: time.Now(),
  15. }
  16. mock := r.NewMock()
  17. mock.On(r.Table("channel").Get(int64(1))).Return([]interface{}{
  18. map[string]interface{}{
  19. "id": 1,
  20. "cmd": "reddit --first 10 --sub images",
  21. "group_name": "reddit",
  22. "is_active": true,
  23. "is_hidden": false,
  24. "min_refresh": 200,
  25. "description": "some desc",
  26. "created_at": now,
  27. "updated_at": now,
  28. },
  29. }, nil)
  30. mock.On(r.Table("channel").Get(int64(2))).Return([]interface{}{
  31. map[string]interface{}{
  32. "id": 2,
  33. "cmd": "reddit --first 10 --sub images",
  34. "group_name": "reddit",
  35. "is_active": false,
  36. "is_hidden": true,
  37. "created_at": now,
  38. },
  39. }, nil)
  40. mock.On(r.Table("channel").Get(int64(3))).Return(nil, nil)
  41. ch, err := channel.NewChannelManager(mock, &user)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. item, err := ch.Get(1)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. assert.EqualValues(t, 1, item.ID)
  50. assert.Equal(t, "reddit --first 10 --sub images", item.Cmd)
  51. assert.Equal(t, "reddit", item.GroupName)
  52. assert.Equal(t, "some desc", *item.Description)
  53. assert.True(t, item.IsActive)
  54. assert.False(t, item.IsHidden)
  55. assert.Equal(t, now, item.CreatedAt)
  56. assert.Equal(t, now, *item.UpdatedAt)
  57. item, err = ch.Get(2)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. assert.EqualValues(t, 2, item.ID)
  62. assert.Equal(t, "reddit --first 10 --sub images", item.Cmd)
  63. assert.Equal(t, "reddit", item.GroupName)
  64. assert.False(t, item.IsActive)
  65. assert.True(t, item.IsHidden)
  66. assert.Nil(t, item.Description)
  67. assert.Nil(t, item.UpdatedAt)
  68. item, err = ch.Get(3)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. assert.Nil(t, item)
  73. }
  74. func TestManager_All(t *testing.T) {
  75. //now := time.Now()
  76. //user := model.User{
  77. // ID: 1,
  78. // CreatedAt: time.Now(),
  79. //}
  80. //
  81. //mock := r.NewMock()
  82. //mock.On(r.Table("channel").Get(int64(1))).Return([]interface{}{
  83. // map[string]interface{}{
  84. // "id": 1,
  85. // "cmd": "reddit --first 10 --sub images",
  86. // "group_name": "reddit",
  87. // "is_active": true,
  88. // "is_hidden": false,
  89. // "min_refresh": 200,
  90. // "description": "some desc",
  91. // "created_at": now,
  92. // "updated_at": now,
  93. // },
  94. //}, nil)
  95. //mock.On(r.Table("channel").Get(int64(2))).Return([]interface{}{
  96. // map[string]interface{}{
  97. // "id": 2,
  98. // "cmd": "reddit --first 10 --sub images",
  99. // "group_name": "reddit",
  100. // "is_active": false,
  101. // "is_hidden": true,
  102. // "created_at": now,
  103. // },
  104. //}, nil)
  105. //mock.On(r.Table("channel").Get(int64(3))).Return(nil, nil)
  106. //
  107. //ch, err := channel.NewChannelManager(mock, &user)
  108. //if err != nil {
  109. // t.Fatal(err)
  110. //}
  111. //
  112. //item, err := ch.Get(1)
  113. //if err != nil {
  114. // t.Fatal(err)
  115. //}
  116. //
  117. //assert.EqualValues(t, 1, item.ID)
  118. //assert.Equal(t, "reddit --first 10 --sub images", item.Cmd)
  119. //assert.Equal(t, "reddit", item.GroupName)
  120. //assert.Equal(t, "some desc", *item.Description)
  121. //assert.True(t, item.IsActive)
  122. //assert.False(t, item.IsHidden)
  123. //assert.Equal(t, now, item.CreatedAt)
  124. //assert.Equal(t, now, *item.UpdatedAt)
  125. //
  126. //item, err = ch.Get(2)
  127. //if err != nil {
  128. // t.Fatal(err)
  129. //}
  130. //
  131. //assert.EqualValues(t, 2, item.ID)
  132. //assert.Equal(t, "reddit --first 10 --sub images", item.Cmd)
  133. //assert.Equal(t, "reddit", item.GroupName)
  134. //assert.False(t, item.IsActive)
  135. //assert.True(t, item.IsHidden)
  136. //assert.Nil(t, item.Description)
  137. //assert.Nil(t, item.UpdatedAt)
  138. //
  139. //item, err = ch.Get(3)
  140. //if err != nil {
  141. // t.Fatal(err)
  142. //}
  143. //
  144. //assert.Nil(t, item)
  145. }
  146. //func TestDefaultDataManager_Channels(t *testing.T) {
  147. // //suffix := helpers.RandomString(8)
  148. // //setup(suffix)
  149. // //defer tearDown(suffix)
  150. //
  151. // var (
  152. // dm manager.DataManager = manager.NewManagerWithConnectors(DB, nil).Data()
  153. // channels []model.Channel
  154. // err error
  155. // )
  156. //
  157. // // Insert some test data
  158. // u1 := model.User{Username: "test_user"}
  159. // u2 := model.User{Username: "test_user_2"}
  160. // DB.Save(&u1).Save(&u2)
  161. //
  162. // ch1 := model.Channel{Cmd: null.StringFrom("reddit --first 10 --sub images"), GroupName: "reddit", Name: "/r/images", IsActive: true, MinRefresh: 300}
  163. // ch2 := model.Channel{Cmd: null.StringFrom("reddit --first 10 --sub wtf"), GroupName: "reddit", Name: "/r/wtf", IsActive: true, MinRefresh: 300}
  164. // ch3 := model.Channel{UserId: null.IntFrom(u1.Id), GroupName: "own", Name: "/r/wtf", IsActive: true, MinRefresh: 300}
  165. // ch4 := model.Channel{GroupName: "hidden", Name: "hidden channel", IsActive: true, IsHidden: true, MinRefresh: 300}
  166. // ch5 := model.Channel{UserId: null.IntFrom(u2.Id), GroupName: "not_active", Name: "not active channel", IsActive: false, MinRefresh: 300}
  167. // DB.Save(&ch1).Save(&ch2).Save(&ch3).Save(&ch4).Save(&ch5)
  168. //
  169. // // Tests for two users, second should see only public channels
  170. // channels, err = dm.Channels(&u1)
  171. // assert.Nil(t, err, err)
  172. // assert.Len(t, channels, 3)
  173. // ChannelsAreEqual(t, ch1, channels[0])
  174. // ChannelsAreEqual(t, ch2, channels[1])
  175. // ChannelsAreEqual(t, ch3, channels[2])
  176. //
  177. // channels, err = dm.Channels(&u2)
  178. // assert.Nil(t, err, err)
  179. // assert.Len(t, channels, 2)
  180. // ChannelsAreEqual(t, ch1, channels[0])
  181. // ChannelsAreEqual(t, ch2, channels[1])
  182. //}
  183. //
  184. //func TestDefaultDataManager_Channel(t *testing.T) {
  185. // suffix := helpers.RandomString(8)
  186. // setup(suffix)
  187. // defer tearDown(suffix)
  188. //
  189. // var (
  190. // dm manager.DataManager = manager.NewManagerWithConnectors(DB, nil).Data()
  191. // channels []model.Channel
  192. // err error
  193. // )
  194. //
  195. // // Insert some test data
  196. // u1 := model.User{Username: "test_user"}
  197. // DB.Save(&u1)
  198. //
  199. // ch1 := model.Channel{Cmd: null.StringFrom("reddit --first 10 --sub images"), GroupName: "reddit", Name: "/r/images", IsActive: true, MinRefresh: 300}
  200. // ch2 := model.Channel{UserId: null.IntFrom(u1.Id), GroupName: "own", Name: "/r/wtf", IsActive: true, MinRefresh: 300}
  201. // DB.Save(&ch1).Save(&ch2)
  202. //
  203. // // Test
  204. // channels, err = dm.Channels(&u1)
  205. // assert.Nil(t, err, err)
  206. // assert.Len(t, channels, 2)
  207. // ChannelsAreEqual(t, ch1, channels[0])
  208. // ChannelsAreEqual(t, ch2, channels[1])
  209. //
  210. // getCh1, err := dm.Channel(&u1, ch1.Id)
  211. // assert.Nil(t, err, err)
  212. // assert.NotNil(t, getCh1)
  213. // ChannelsAreEqual(t, ch1, *getCh1)
  214. //}
  215. //
  216. //func TestDefaultDataManager_CreateChannel(t *testing.T) {
  217. // suffix := helpers.RandomString(8)
  218. // setup(suffix)
  219. // defer tearDown(suffix)
  220. //
  221. // var (
  222. // dm manager.DataManager = manager.NewManagerWithConnectors(DB, nil).Data()
  223. // channel *model.Channel
  224. // channels []model.Channel
  225. // err error
  226. // )
  227. //
  228. // // Insert some test data
  229. // u1 := model.User{Username: "test_user"}
  230. // DB.Save(&u1)
  231. //
  232. // // Test
  233. // channel, err = dm.CreateChannel(&u1, "group", "name", nil)
  234. // channels, err = dm.Channels(&u1)
  235. //
  236. // assert.Nil(t, err, err)
  237. // assert.Len(t, channels, 1)
  238. // assert.True(t, channel.UserId.Valid)
  239. // assert.Equal(t, channel.UserId.Int64, u1.Id)
  240. // assert.False(t, channel.ServiceProfileId.Valid)
  241. // assert.Equal(t, "group", channel.GroupName)
  242. // assert.Equal(t, "name", channel.Name)
  243. // assert.False(t, channel.Description.Valid)
  244. // assert.True(t, channel.IsActive)
  245. // assert.False(t, channel.IsHidden)
  246. //
  247. // desc := "description"
  248. // channel, err = dm.CreateChannel(&u1, "group", "name2", &desc)
  249. // channels, err = dm.Channels(&u1)
  250. //
  251. // assert.Nil(t, err, err)
  252. // assert.Len(t, channels, 2)
  253. // assert.True(t, channel.Description.Valid)
  254. // assert.Equal(t, "description", channel.Description.String)
  255. //}
  256. //
  257. //func TestDefaultDataManager_UpdateChannel(t *testing.T) {
  258. // suffix := helpers.RandomString(8)
  259. // setup(suffix)
  260. // defer tearDown(suffix)
  261. //
  262. // var (
  263. // dm manager.DataManager = manager.NewManagerWithConnectors(DB, nil).Data()
  264. // ch1 int64
  265. // ch2 int64
  266. // channel *model.Channel
  267. // channels []model.Channel
  268. // err error
  269. // desc string = "description"
  270. // )
  271. //
  272. // // Insert some test data
  273. // u1 := model.User{Username: "test_user"}
  274. // u2 := model.User{Username: "test_user_2"}
  275. // DB.Save(&u1).Save(&u2)
  276. //
  277. // // Validate test data
  278. // channel, err = dm.CreateChannel(&u1, "group", "name", nil)
  279. // ch1 = channel.Id
  280. // assert.Nil(t, err, err)
  281. // assert.NotNil(t, channel)
  282. //
  283. // channel, err = dm.CreateChannel(&u2, "group2", "name2", nil)
  284. // ch2 = channel.Id
  285. // assert.Nil(t, err, err)
  286. // assert.NotNil(t, channel)
  287. //
  288. // channels, err = dm.Channels(&u1)
  289. // assert.Len(t, channels, 1)
  290. // channels, err = dm.Channels(&u2)
  291. // assert.Len(t, channels, 1)
  292. //
  293. // // Tests
  294. //
  295. // // Try to update other user's channel
  296. // channel, err = dm.UpdateChannel(&u2, ch1, "group33", "name33", &desc)
  297. // assert.NotNil(t, err)
  298. // assert.Nil(t, channel)
  299. //
  300. // // Update channel with description and do fresh GET then compare values
  301. // channel, err = dm.UpdateChannel(&u1, ch1, "group33", "name33", &desc)
  302. // freshChannel, err2 := dm.Channel(&u1, ch1)
  303. //
  304. // assert.Nil(t, err, err)
  305. // assert.Nil(t, err2)
  306. // assert.Equal(t, "group33", channel.GroupName)
  307. // assert.Equal(t, "group33", freshChannel.GroupName)
  308. // assert.Equal(t, "name33", channel.Name)
  309. // assert.Equal(t, "name33", freshChannel.Name)
  310. // assert.True(t, channel.Description.Valid)
  311. // assert.True(t, freshChannel.Description.Valid)
  312. // assert.Equal(t, desc, channel.Description.String)
  313. // assert.Equal(t, desc, freshChannel.Description.String)
  314. //
  315. // // Update channel without description and do fresh GET then compare values
  316. // channel, err = dm.UpdateChannel(&u2, ch2, "group33", "name33", nil)
  317. // freshChannel, err2 = dm.Channel(&u2, ch2)
  318. //
  319. // assert.Nil(t, err, err)
  320. // assert.Nil(t, err2)
  321. // assert.Equal(t, "group33", channel.GroupName)
  322. // assert.Equal(t, "group33", freshChannel.GroupName)
  323. // assert.Equal(t, "name33", channel.Name)
  324. // assert.Equal(t, "name33", freshChannel.Name)
  325. // assert.False(t, channel.Description.Valid)
  326. // assert.False(t, freshChannel.Description.Valid)
  327. //}
  328. //
  329. //func TestDefaultDataManager_DeleteChannel(t *testing.T) {
  330. // suffix := helpers.RandomString(8)
  331. // setup(suffix)
  332. // defer tearDown(suffix)
  333. //
  334. // var (
  335. // dm manager.DataManager = manager.NewManagerWithConnectors(DB, nil).Data()
  336. // ch1 int64
  337. // channel *model.Channel
  338. // channels []model.Channel
  339. // err error
  340. // )
  341. //
  342. // // Insert some test data
  343. // u1 := model.User{Username: "test_user"}
  344. // u2 := model.User{Username: "test_user_2"}
  345. // DB.Save(&u1).Save(&u2)
  346. //
  347. // // Validate test data
  348. // channel, err = dm.CreateChannel(&u1, "group", "name", nil)
  349. // ch1 = channel.Id
  350. // assert.Nil(t, err, err)
  351. // assert.NotNil(t, channel)
  352. //
  353. // channel, err = dm.CreateChannel(&u2, "group2", "name2", nil)
  354. // assert.Nil(t, err, err)
  355. // assert.NotNil(t, channel)
  356. //
  357. // channels, err = dm.Channels(&u1)
  358. // assert.Len(t, channels, 1)
  359. // channels, err = dm.Channels(&u2)
  360. // assert.Len(t, channels, 1)
  361. //
  362. // // Tests
  363. //
  364. // // Try to delete other user's channel
  365. // err = dm.DeleteChannel(&u2, ch1)
  366. // assert.NotNil(t, err)
  367. //
  368. // // Try to delete unexisting channel
  369. // err = dm.DeleteChannel(&u2, 1000)
  370. // assert.NotNil(t, err)
  371. //
  372. // // Delete channel and check if it's gone
  373. // err = dm.DeleteChannel(&u1, ch1)
  374. // freshChannel, err2 := dm.Channel(&u1, ch1)
  375. //
  376. // assert.Nil(t, err, err)
  377. // assert.Nil(t, freshChannel)
  378. // assert.NotNil(t, err2)
  379. //
  380. // channels, err = dm.Channels(&u1)
  381. // assert.Len(t, channels, 0)
  382. // channels, err = dm.Channels(&u2)
  383. // assert.Len(t, channels, 1)
  384. //}