sub_format_changer_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package sub_formatter
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/dao"
  4. "github.com/allanpk716/ChineseSubFinder/internal/models"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
  7. "path/filepath"
  8. "testing"
  9. )
  10. func TestSubFormatChanger_AutoDetectThenChangeTo(t *testing.T) {
  11. testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_format_changer"}, 4, true)
  12. movie_name := "AAA"
  13. series_name := "Loki"
  14. // Emby 的信息
  15. movieDir_org_emby := filepath.Join(testRootDir, "movie_org_emby")
  16. seriesDir_org_emby := filepath.Join(testRootDir, "series_org_emby")
  17. movieOneDir_org_emby := filepath.Join(movieDir_org_emby, movie_name)
  18. seriesOneDir_org_emby := filepath.Join(seriesDir_org_emby, series_name, "Season 1")
  19. // Normal 的信息
  20. movieDir_org_normal := filepath.Join(testRootDir, "movie_org_normal")
  21. seriesDir_org_normal := filepath.Join(testRootDir, "series_org_normal")
  22. movieOneDir_org_normal := filepath.Join(movieDir_org_normal, movie_name)
  23. seriesOneDir_org_normal := filepath.Join(seriesDir_org_normal, series_name, "Season 1")
  24. // emby 转 emby 理论上不应该改文件
  25. movieDir_emby_2_emby := filepath.Join(testRootDir, "movie_emby_2_emby")
  26. seriesDir_emby_2_emby := filepath.Join(testRootDir, "series_emby_2_emby")
  27. type fields struct {
  28. movieRootDir string
  29. seriesRootDir string
  30. }
  31. type args struct {
  32. desFormatter common.FormatterName
  33. }
  34. tests := []struct {
  35. name string
  36. fields fields
  37. args args
  38. want RenameResults
  39. wantErr bool
  40. }{
  41. {name: "emby 2 normal",
  42. fields: fields{movieRootDir: movieDir_org_emby, seriesRootDir: seriesDir_org_emby},
  43. args: args{desFormatter: common.Normal},
  44. want: RenameResults{
  45. RenamedFiles: map[string]int{
  46. filepath.Join(movieOneDir_org_emby, "AAA.zh.ass"): 2,
  47. filepath.Join(movieOneDir_org_emby, "AAA.zh.default.ass"): 1,
  48. filepath.Join(movieOneDir_org_emby, "AAA.zh.srt"): 1,
  49. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.ass"): 5,
  50. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.default.ass"): 1,
  51. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.srt"): 1,
  52. },
  53. }, wantErr: false},
  54. {name: "normal 2 emby",
  55. fields: fields{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal},
  56. args: args{desFormatter: common.Emby},
  57. want: RenameResults{
  58. RenamedFiles: map[string]int{
  59. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).ass"): 1,
  60. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).default.ass"): 1,
  61. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).srt"): 1,
  62. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(繁英).ass"): 1,
  63. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).default.ass"): 1,
  64. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).srt"): 1,
  65. },
  66. }, wantErr: false},
  67. {name: "emby 2 emby",
  68. fields: fields{movieRootDir: movieDir_emby_2_emby, seriesRootDir: seriesDir_emby_2_emby},
  69. args: args{desFormatter: common.Emby},
  70. want: RenameResults{},
  71. wantErr: false},
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.name, func(t *testing.T) {
  75. s := NewSubFormatChanger([]string{tt.fields.movieRootDir}, []string{tt.fields.seriesRootDir})
  76. got, err := s.AutoDetectThenChangeTo(tt.args.desFormatter)
  77. if (err != nil) != tt.wantErr {
  78. t.Errorf("AutoDetectThenChangeTo() error = %v, wantErr %v", err, tt.wantErr)
  79. return
  80. }
  81. if len(got.ErrFiles) > 0 {
  82. t.Errorf("AutoDetectThenChangeTo() got.ErrFiles len > 0")
  83. return
  84. }
  85. //for s2, i := range tt.want.RenamedFiles {
  86. // println(s2, i)
  87. //}
  88. //println("-------------------------------")
  89. //for s2, i := range got.RenamedFiles {
  90. // println(s2, i)
  91. //}
  92. for fileName, counter := range got.RenamedFiles {
  93. if tt.want.RenamedFiles[filepath.FromSlash(fileName)] != counter {
  94. //println(fileName)
  95. //println(filepath.FromSlash(fileName))
  96. t.Errorf("AutoDetectThenChangeTo() RenamedFiles %v got = %v, want %v", fileName, counter, tt.want.RenamedFiles[fileName])
  97. return
  98. }
  99. }
  100. })
  101. }
  102. }
  103. func TestSubFormatChangerProcess(t *testing.T) {
  104. // 先删除 db
  105. err := dao.DeleteDbFile()
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_format_changer"}, 4, true)
  110. movie_name := "AAA"
  111. series_name := "Loki"
  112. // Emby 的信息
  113. movieDir_org_emby := filepath.Join(testRootDir, "movie_org_emby")
  114. seriesDir_org_emby := filepath.Join(testRootDir, "series_org_emby")
  115. movieOneDir_org_emby := filepath.Join(movieDir_org_emby, movie_name)
  116. seriesOneDir_org_emby := filepath.Join(seriesDir_org_emby, series_name, "Season 1")
  117. // Normal 的信息
  118. movieDir_org_normal := filepath.Join(testRootDir, "movie_org_normal")
  119. seriesDir_org_normal := filepath.Join(testRootDir, "series_org_normal")
  120. movieOneDir_org_normal := filepath.Join(movieDir_org_normal, movie_name)
  121. seriesOneDir_org_normal := filepath.Join(seriesDir_org_normal, series_name, "Season 1")
  122. // emby 转 emby 理论上不应该改文件
  123. movieDir_emby_2_emby := filepath.Join(testRootDir, "movie_emby_2_emby")
  124. seriesDir_emby_2_emby := filepath.Join(testRootDir, "series_emby_2_emby")
  125. type args struct {
  126. movieRootDir string
  127. seriesRootDir string
  128. nowDesFormatter common.FormatterName
  129. }
  130. tests := []struct {
  131. name string
  132. args args
  133. want RenameResults
  134. wantErr bool
  135. }{
  136. // 先从 emby 2 normal
  137. {name: "emby 2 normal",
  138. args: args{movieRootDir: movieDir_org_emby, seriesRootDir: seriesDir_org_emby, nowDesFormatter: common.Normal},
  139. want: RenameResults{
  140. RenamedFiles: map[string]int{
  141. filepath.Join(movieOneDir_org_emby, "AAA.zh.ass"): 2,
  142. filepath.Join(movieOneDir_org_emby, "AAA.zh.default.ass"): 1,
  143. filepath.Join(movieOneDir_org_emby, "AAA.zh.srt"): 1,
  144. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.ass"): 5,
  145. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.default.ass"): 1,
  146. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.srt"): 1,
  147. },
  148. }, wantErr: false},
  149. // 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
  150. {name: "normal 2 emby",
  151. args: args{movieRootDir: movieDir_org_emby, seriesRootDir: movieDir_org_emby, nowDesFormatter: common.Emby},
  152. want: RenameResults{
  153. RenamedFiles: map[string]int{
  154. filepath.Join(movieOneDir_org_emby, "AAA.chinese(简英).ass"): 1,
  155. filepath.Join(movieOneDir_org_emby, "AAA.chinese(简英).default.ass"): 1,
  156. filepath.Join(movieOneDir_org_emby, "AAA.chinese(简英).srt"): 1,
  157. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(繁英).ass"): 1,
  158. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(简英).default.ass"): 1,
  159. filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(简英).srt"): 1,
  160. },
  161. }, wantErr: false},
  162. {name: "emby 2 emby",
  163. args: args{movieRootDir: movieDir_emby_2_emby, seriesRootDir: seriesDir_emby_2_emby, nowDesFormatter: common.Emby},
  164. want: RenameResults{},
  165. wantErr: false},
  166. // 重新评估 normal 2 emby ,需要清理数据库
  167. {name: "normal 2 emby new",
  168. args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Emby},
  169. want: RenameResults{
  170. RenamedFiles: map[string]int{
  171. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).ass"): 1,
  172. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).default.ass"): 1,
  173. filepath.Join(movieOneDir_org_normal, "AAA.chinese(简英).srt"): 1,
  174. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(繁英).ass"): 1,
  175. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).default.ass"): 1,
  176. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).srt"): 1,
  177. },
  178. }, wantErr: false},
  179. // 然后从上面一个测试用例的文件夹中,继续,转 emby 2 normal
  180. {name: "emby 2 normal new",
  181. args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Normal},
  182. want: RenameResults{
  183. RenamedFiles: map[string]int{
  184. filepath.Join(movieOneDir_org_normal, "AAA.zh.ass"): 1,
  185. filepath.Join(movieOneDir_org_normal, "AAA.zh.default.ass"): 1,
  186. filepath.Join(movieOneDir_org_normal, "AAA.zh.srt"): 1,
  187. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.ass"): 1,
  188. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.default.ass"): 1,
  189. filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.srt"): 1,
  190. },
  191. }, wantErr: false},
  192. }
  193. for i, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. if i == 0 || i == 2 || i == 3 {
  196. // 0 - 1 轮次,测试的是 先从 emby 2 normal
  197. // 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
  198. // 先删除 db
  199. err = dao.DeleteDbFile()
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. err = dao.InitDb()
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. }
  208. got, err := SubFormatChangerProcess([]string{tt.args.movieRootDir}, []string{tt.args.seriesRootDir}, tt.args.nowDesFormatter)
  209. if err != nil != tt.wantErr {
  210. t.Errorf("SubFormatChangerProcess() error = %v, wantErr %v", err, tt.wantErr)
  211. }
  212. if len(got.ErrFiles) > 0 {
  213. t.Errorf("SubFormatChangerProcess() got.ErrFiles len > 0")
  214. return
  215. }
  216. for s2, i := range tt.want.RenamedFiles {
  217. println(s2, i)
  218. }
  219. println("-------------------------------")
  220. for s2, i := range got.RenamedFiles {
  221. println(s2, i)
  222. }
  223. for fileName, counter := range got.RenamedFiles {
  224. if tt.want.RenamedFiles[filepath.FromSlash(fileName)] != counter {
  225. //println(fileName)
  226. //println(filepath.FromSlash(fileName))
  227. t.Errorf("SubFormatChangerProcess() RenamedFiles %v got = %v, want %v", fileName, counter, tt.want.RenamedFiles[fileName])
  228. return
  229. }
  230. }
  231. if i == 0 {
  232. // 这里需要校验一次数据库的赋值是否正确
  233. var subFormatRec models.SubFormatRec
  234. dao.GetDb().First(&subFormatRec)
  235. if subFormatRec.FormatName != int(common.Normal) || subFormatRec.Done == false {
  236. t.Fatal(tt.name, "i == 0 check db result")
  237. }
  238. }
  239. if i == 1 {
  240. // 这里需要校验一次数据库的赋值是否正确
  241. var subFormatRec models.SubFormatRec
  242. dao.GetDb().First(&subFormatRec)
  243. if subFormatRec.FormatName != int(common.Emby) || subFormatRec.Done == false {
  244. t.Fatal(tt.name, "i == 1 check db result")
  245. }
  246. }
  247. if i == 3 {
  248. // 这里需要校验一次数据库的赋值是否正确
  249. var subFormatRec models.SubFormatRec
  250. dao.GetDb().First(&subFormatRec)
  251. if subFormatRec.FormatName != int(common.Emby) || subFormatRec.Done == false {
  252. t.Fatal(tt.name, "i == 3 check db result")
  253. }
  254. }
  255. if i == 4 {
  256. // 这里需要校验一次数据库的赋值是否正确
  257. var subFormatRec models.SubFormatRec
  258. dao.GetDb().First(&subFormatRec)
  259. if subFormatRec.FormatName != int(common.Normal) || subFormatRec.Done == false {
  260. t.Fatal(tt.name, "i == 4 check db result")
  261. }
  262. }
  263. })
  264. }
  265. }