parser_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package wordpress
  2. import (
  3. "commerce-crwaler/util"
  4. "github.com/stretchr/testify/assert"
  5. "os"
  6. "testing"
  7. )
  8. func TestGetImages(t *testing.T) {
  9. body, err := util.Get("https://seoulknit.com/shop/collar-half-sleeved-top_blue/")
  10. assert.Equal(t, nil, err)
  11. imgs := getImages(body)
  12. assert.Equal(t, "https://seoulknit.com/wp-content/uploads/2021/05/abito_0594-scaled-454x681.jpg", imgs[0])
  13. assert.Equal(t, "https://seoulknit.com/wp-content/uploads/2021/05/abito_0594-scaled-100x100.jpg", imgs[1])
  14. body, err = util.Get("https://www.dollshecraft.com/product/amanda-beauty-26f-classic-maxi-65cm-special-package-2/")
  15. assert.Equal(t, nil, err)
  16. imgs = getImages(body)
  17. assert.Equal(t, "https://cdn.dollshecraft.com/wp-content/uploads/2018/08/Amanda-Beauty_558743_01.jpg", imgs[0])
  18. body, err = util.Get("https://www.shoprootscience.com/shop/arctic-c-vitamin-c-serum")
  19. assert.Equal(t, nil, err)
  20. imgs = getImages(body)
  21. assert.Equal(t, "https://www.shoprootscience.com/wp-content/uploads/2020/10/Arctic-C-product-511x634.jpg", imgs[0])
  22. assert.Equal(t, "https://www.shoprootscience.com/wp-content/uploads/2020/10/Arctic-C-product.jpg", imgs[1])
  23. body, err = util.Get("https://strandbergguitars.com/product/boden-6-dr-titanium/")
  24. assert.Equal(t, nil, err)
  25. imgs = getImages(body)
  26. assert.Equal(t, "https://strandbergguitars.com/cdn-cgi/image/onerror=redirect,quality=85,format=auto,metadata=copyright,width=600,height=901,fit=cover/wp-content/uploads/2021/04/bd6ct-20dr-k-p-ti_6.png", imgs[0])
  27. body, err = util.Get("https://lanbelle.com/product/%EB%9E%91%EB%B2%A8-%EC%88%98%ED%8D%BC%EB%82%B4%EC%B8%84%EB%9F%B4-%ED%81%AC%EB%A6%BC/")
  28. assert.Equal(t, nil, err)
  29. imgs = getImages(body)
  30. assert.Equal(t, "https://strandbergguitars.com/cdn-cgi/image/onerror=redirect,quality=85,format=auto,metadata=copyright,width=600,height=901,fit=cover/wp-content/uploads/2021/04/bd6ct-20dr-k-p-ti_6.png", imgs[0])
  31. }
  32. func TestGetPrice(t *testing.T) {
  33. body, err := util.Get("https://seoulknit.com/shop/collar-half-sleeved-top_blue/")
  34. assert.Equal(t, nil, err)
  35. ret := getPrice(body)
  36. assert.Equal(t, "₩ 59,000", ret)
  37. body, err = util.Get("https://www.dollshecraft.com/product/amanda-beauty-26f-classic-maxi-65cm-special-package-2/")
  38. assert.Equal(t, nil, err)
  39. ret = getPrice(body)
  40. assert.Equal(t, "$1,450.00", ret)
  41. body, err = util.Get("https://www.shoprootscience.com/shop/arctic-c-vitamin-c-serum")
  42. assert.Equal(t, nil, err)
  43. ret = getPrice(body)
  44. assert.Equal(t, "$110.00", ret)
  45. body, err = util.Get("https://strandbergguitars.com/product/boden-6-dr-titanium/")
  46. assert.Equal(t, nil, err)
  47. ret = getPrice(body)
  48. assert.Equal(t, "$2,995.00", ret)
  49. }
  50. func TestGetWordPressProduct(t *testing.T) {
  51. body, err := util.Get("https://seoulknit.com/shop/collar-half-sleeved-top_blue/")
  52. assert.Equal(t, nil, err)
  53. ret := getProduct(body)
  54. assert.Equal(t, ret.Sku, float64(5677))
  55. body, err = util.Get("https://www.shoprootscience.com/shop/arctic-c-vitamin-c-serum")
  56. assert.Equal(t, nil, err)
  57. ret = getProduct(body)
  58. assert.Equal(t, ret.Sku, "FS-ARCC-30")
  59. body, err = util.Get("https://strandbergguitars.com/product/boden-6-dr-titanium/")
  60. assert.Equal(t, nil, err)
  61. ret = getProduct(body)
  62. assert.Equal(t, "BD6CT-20DRB-K-P-TI", ret.Sku)
  63. t.Run("상품설명", func(t *testing.T) {
  64. body, err := util.Get("https://www.mehisox.com/shop/sn388/")
  65. assert.Equal(t, nil, err)
  66. html := getProductDescription(body)
  67. os.WriteFile("c:\\temp\\test.html", []byte(html), 0644)
  68. assert.Equal(t, ``, html)
  69. })
  70. }
  71. func TestGetCategories(t *testing.T) {
  72. body, err := util.Get("https://seoulknit.com/shop/collar-half-sleeved-top_blue/")
  73. assert.Equal(t, nil, err)
  74. ret := getCategories(body)
  75. assert.Equal(t, ret, "2021SS, Abito angelico, All, New arrivals, Top")
  76. body, err = util.Get("https://www.shoprootscience.com/shop/arctic-c-vitamin-c-serum")
  77. assert.Equal(t, nil, err)
  78. ret = getCategories(body)
  79. assert.Equal(t, ret, "Treatments")
  80. body, err = util.Get("https://shopkeeper.wp-theme.design/shop/hoodies/oversize-drawstring-sweatshirt/")
  81. assert.Equal(t, nil, err)
  82. ret = getCategories(body)
  83. assert.Equal(t, ret, "Hoodies, Jackets, Trousers")
  84. }
  85. func TestGetOptions(t *testing.T) {
  86. body, err := util.Get("https://seoulknit.com/shop/collar-half-sleeved-top_blue/")
  87. assert.Equal(t, nil, err)
  88. ret := getOptions(body)
  89. assert.EqualValues(t, ret, []string{"옵션을 선택하세요", "Small", "Midium"})
  90. body, err = util.Get("https://www.shoprootscience.com/shop/arctic-c-vitamin-c-serum")
  91. assert.Equal(t, nil, err)
  92. ret = getOptions(body)
  93. assert.EqualValues(t, ret, []string(nil))
  94. body, err = util.Get("https://shopkeeper.wp-theme.design/shop/hoodies/oversize-drawstring-sweatshirt/")
  95. assert.Equal(t, nil, err)
  96. ret = getOptions(body)
  97. assert.EqualValues(t, ret, []string{"Choose an option", "Camel", "Green", "Black", "White", "Choose an option", "XS", "S", "M", "L", "XL"})
  98. }