collection_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package seo
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. _ "github.com/lib/pq"
  8. )
  9. func TestRender(t *testing.T) {
  10. u, _ := url.Parse("http://dev.qor5.com/product/1")
  11. defaultRequest := &http.Request{
  12. Method: "GET",
  13. URL: u,
  14. }
  15. globlalSeoSetting := TestQorSEOSetting{
  16. QorSEOSetting: QorSEOSetting{
  17. Name: GlobalSEO,
  18. Setting: Setting{
  19. Title: "global | {{SiteName}}",
  20. },
  21. Variables: map[string]string{"SiteName": "Qor5 dev"},
  22. },
  23. }
  24. tests := []struct {
  25. name string
  26. prepareDB func()
  27. collection *Collection
  28. obj interface{}
  29. want string
  30. }{
  31. {
  32. name: "Render Golabl SEO with setting variables and default context variables",
  33. prepareDB: func() { GlobalDB.Save(&globlalSeoSetting) },
  34. collection: NewCollection().SetSettingModel(&TestQorSEOSetting{}),
  35. obj: GlobalSEO,
  36. want: `
  37. <title>global | Qor5 dev</title>
  38. <meta property='og:url' name='og:url' content='http://dev.qor5.com/product/1'>
  39. `,
  40. },
  41. {
  42. name: "Render seo setting with global setting variables",
  43. prepareDB: func() {
  44. GlobalDB.Save(&globlalSeoSetting)
  45. product := TestQorSEOSetting{
  46. QorSEOSetting: QorSEOSetting{
  47. Name: "Product",
  48. Setting: Setting{
  49. Title: "product | {{SiteName}}",
  50. },
  51. },
  52. }
  53. GlobalDB.Save(&product)
  54. },
  55. collection: NewCollection().SetSettingModel(&TestQorSEOSetting{}).RegisterSEOByNames("Product"),
  56. obj: "Product",
  57. want: `<title>product | Qor5 dev</title>`,
  58. },
  59. {
  60. name: "Render seo setting with setting and context variables",
  61. prepareDB: func() {
  62. GlobalDB.Save(&globlalSeoSetting)
  63. product := TestQorSEOSetting{
  64. QorSEOSetting: QorSEOSetting{
  65. Name: "Product",
  66. Setting: Setting{
  67. Title: "product {{ProductTag}} | {{SiteName}}",
  68. },
  69. Variables: map[string]string{"ProductTag": "Men"},
  70. },
  71. }
  72. GlobalDB.Save(&product)
  73. },
  74. collection: func() *Collection {
  75. collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
  76. collection.RegisterSEO("Product").
  77. RegisterSettingVaribles(struct{ ProductTag string }{}).
  78. RegisterContextVariables("og:image", func(_ interface{}, _ *Setting, _ *http.Request) string {
  79. return "http://dev.qor5.com/images/logo.png"
  80. })
  81. return collection
  82. }(),
  83. obj: "Product",
  84. want: `
  85. <title>product Men | Qor5 dev</title>
  86. <meta property='og:image' name='og:image' content='http://dev.qor5.com/images/logo.png'>`,
  87. },
  88. {
  89. name: "Render model setting with global and seo setting variables",
  90. prepareDB: func() {
  91. GlobalDB.Save(&globlalSeoSetting)
  92. product := TestQorSEOSetting{
  93. QorSEOSetting: QorSEOSetting{
  94. Name: "Product",
  95. Variables: map[string]string{"ProductTag": "Men"},
  96. },
  97. }
  98. GlobalDB.Save(&product)
  99. },
  100. collection: func() *Collection {
  101. collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
  102. collection.RegisterSEO(&Product{})
  103. return collection
  104. }(),
  105. obj: Product{
  106. Name: "product 1",
  107. SEO: Setting{
  108. Title: "product1 | {{ProductTag}} | {{SiteName}}",
  109. EnabledCustomize: true,
  110. },
  111. },
  112. want: `<title>product1 | Men | Qor5 dev</title>`,
  113. },
  114. {
  115. name: "Render model setting with default seo setting",
  116. prepareDB: func() {
  117. GlobalDB.Save(&globlalSeoSetting)
  118. product := TestQorSEOSetting{
  119. QorSEOSetting: QorSEOSetting{
  120. Name: "Product",
  121. Setting: Setting{
  122. Title: "product | Qor5 dev",
  123. },
  124. Variables: map[string]string{"ProductTag": "Men"},
  125. },
  126. }
  127. GlobalDB.Save(&product)
  128. },
  129. collection: func() *Collection {
  130. collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
  131. collection.RegisterSEO(&Product{})
  132. return collection
  133. }(),
  134. obj: Product{
  135. Name: "product 1",
  136. SEO: Setting{
  137. Title: "product1 | {{ProductTag}} | {{SiteName}}",
  138. EnabledCustomize: false,
  139. },
  140. },
  141. want: `<title>product | Qor5 dev</title>`,
  142. },
  143. {
  144. name: "Render model setting with inherite gloabl and seo setting",
  145. prepareDB: func() {
  146. GlobalDB.Save(&globlalSeoSetting)
  147. product := TestQorSEOSetting{
  148. QorSEOSetting: QorSEOSetting{
  149. Name: "Product",
  150. Setting: Setting{
  151. Description: "product description",
  152. },
  153. Variables: map[string]string{"ProductTag": "Men"},
  154. },
  155. }
  156. GlobalDB.Save(&product)
  157. },
  158. collection: func() *Collection {
  159. collection := NewCollection().SetSettingModel(&TestQorSEOSetting{})
  160. collection.RegisterSEO(&Product{})
  161. return collection
  162. }(),
  163. obj: Product{
  164. Name: "product 1",
  165. SEO: Setting{
  166. Keywords: "shoes, {{ProductTag}}",
  167. EnabledCustomize: true,
  168. },
  169. },
  170. want: `
  171. <title>global | Qor5 dev</title>
  172. <meta name='description' content='product description'>
  173. <meta name='keywords' content='shoes, Men'>
  174. `,
  175. },
  176. {
  177. name: "Render model setting without inherite gloabl and seo setting",
  178. prepareDB: func() {
  179. GlobalDB.Save(&globlalSeoSetting)
  180. product := TestQorSEOSetting{
  181. QorSEOSetting: QorSEOSetting{
  182. Name: "Product",
  183. Setting: Setting{
  184. Description: "product description",
  185. },
  186. Variables: map[string]string{"ProductTag": "Men"},
  187. },
  188. }
  189. GlobalDB.Save(&product)
  190. },
  191. collection: func() *Collection {
  192. collection := NewCollection().SetInherited(false).SetSettingModel(&TestQorSEOSetting{})
  193. collection.RegisterSEO(&Product{})
  194. return collection
  195. }(),
  196. obj: Product{
  197. Name: "product 1",
  198. SEO: Setting{
  199. Keywords: "shoes, {{ProductTag}}",
  200. EnabledCustomize: true,
  201. },
  202. },
  203. want: `
  204. <title></title>
  205. <meta name='description'>
  206. <meta name='keywords' content='shoes, Men'>
  207. `,
  208. },
  209. }
  210. for _, tt := range tests {
  211. t.Run(tt.name, func(t *testing.T) {
  212. resetDB()
  213. tt.prepareDB()
  214. if got, _ := tt.collection.Render(tt.obj, defaultRequest).MarshalHTML(context.TODO()); !metaEqual(string(got), tt.want) {
  215. t.Errorf("Render = %v, want %v", string(got), tt.want)
  216. }
  217. })
  218. }
  219. }