atom_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Copyright (C) 2017-2021 Marcus Rohrmoser, http://purl.mro.name/ShaarliGo
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package main
  18. import (
  19. "compress/gzip"
  20. "encoding/gob"
  21. "os"
  22. "time"
  23. "github.com/stretchr/testify/assert"
  24. "testing"
  25. )
  26. func TestIriAdd(t *testing.T) {
  27. t.Parallel()
  28. assert.Equal(t, Iri("ab"), Iri("a")+Iri("b"), "uh")
  29. }
  30. var b64Tob24Tests = []struct {
  31. n string
  32. expected string
  33. }{
  34. {"FxW3Ow", "77tk489"},
  35. {"Zt3wHA", "4ezde68"},
  36. {"oHordQ", "c8x2syk"}, // http://sebsauvage.net/links/?oHordQ
  37. {"McLIuQ", "k9cr7c3"}, // http://sebsauvage.net/links/?McLIuQ
  38. }
  39. func TestFeedIdLowerFromIdMixed(t *testing.T) {
  40. // https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go
  41. for _, tt := range b64Tob24Tests {
  42. actual, _ := base64ToBase24x7(tt.n)
  43. if actual != tt.expected {
  44. t.Errorf("base64ToBase24x7(%s): expected %s, actual %s", tt.n, tt.expected, actual)
  45. }
  46. }
  47. }
  48. func TestFeedNewEntry(t *testing.T) {
  49. f := Feed{}
  50. ent := f.newEntry(time.Time{})
  51. assert.True(t, ent.Published.IsZero(), "oha")
  52. assert.Equal(t, Id("dzcz8k2"), ent.Id, "soso")
  53. }
  54. func TestFeedFromFileName_Atom(t *testing.T) {
  55. t.Parallel()
  56. feed, err := FeedFromFileName("testdata/links.atom")
  57. assert.Nil(t, err, "soso")
  58. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  59. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  60. assert.Equal(t, 0, len(feed.Links), "soso")
  61. assert.Equal(t, "m", feed.Authors[0].Name, "soso")
  62. assert.Equal(t, Iri(""), feed.Authors[0].Uri, "soso")
  63. assert.Equal(t, Id(""), feed.Id, "soso")
  64. assert.Equal(t, TextType("html"), feed.Entries[0].Content.Type, "soso")
  65. txt := `&quot;… Ein Vertreter der Bundesanwaltschaft (BAW) erklärte vor dem U-Ausschuss des Bundestages, Marschners Akte sei selbst für die BAW gesperrt. …&quot;<br />
  66. <br />
  67. Ach was, wen gibt's denn dann da noch so?<br>(<a href="https://links.mro.name/?aTh_gA">Permalink</a>)`
  68. assert.Equal(t, txt, feed.Entries[0].Content.Body, "soso")
  69. assert.Equal(t, `"… Ein Vertreter der Bundesanwaltschaft (BAW) erklärte vor dem U-Ausschuss des Bundestages, Marschners Akte sei selbst für die BAW gesperrt. …"
  70. Ach was, wen gibt's denn dann da noch so?`, cleanLegacyContent(txt), "soso")
  71. assert.Equal(t, 3618, len(feed.Entries), "soso")
  72. }
  73. func _TestFeedFromFileName_AtomLarge(t *testing.T) {
  74. if testing.Short() {
  75. t.Skip("long running")
  76. }
  77. t.Parallel()
  78. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  79. assert.Nil(t, err, "soso")
  80. assert.Equal(t, 21900, len(feed.Entries), "soso")
  81. }
  82. func TestFeedFromFileName_PhotosAtom(t *testing.T) {
  83. t.Parallel()
  84. feed, err := FeedFromFileName("testdata/photos.atom")
  85. assert.Nil(t, err, "soso")
  86. assert.Equal(t, "Demo Album", feed.Title.Body, "soso")
  87. assert.Equal(t, "2016-11-27T12:32:57+01:00", feed.Updated.Format(time.RFC3339), "soso")
  88. assert.Equal(t, 2, len(feed.Links), "soso")
  89. assert.Equal(t, relSelf, feed.Links[0].Rel, "soso")
  90. assert.Equal(t, "https://lager.mro.name/galleries/demo/", feed.Links[0].Href, "soso")
  91. assert.Equal(t, relAlternate, feed.Links[1].Rel, "soso")
  92. assert.Equal(t, "https://lager.mro.name/galleries/demo/", feed.Links[1].Href, "soso")
  93. assert.Equal(t, "Marcus Rohrmoser", feed.Authors[0].Name, "soso")
  94. assert.Equal(t, Iri("http://mro.name/me"), feed.Authors[0].Uri, "soso")
  95. assert.Equal(t, Id("https://lager.mro.name/galleries/demo/"), feed.Id, "soso")
  96. assert.Equal(t, 127, len(feed.Entries), "soso")
  97. assert.Equal(t, Iri("https://lager.mro.name/galleries/demo/200p/fbb6669a533054da3747fb71790dc515bbf76da2.jpeg"), feed.Entries[0].MediaThumbnail.Url, "soso")
  98. assert.Equal(t, Latitude(48.047504), feed.Entries[0].GeoRssPoint.Lat, "soso")
  99. assert.Equal(t, Longitude(10.871933), feed.Entries[0].GeoRssPoint.Lon, "soso")
  100. }
  101. func _TestFeedLargeToGob(t *testing.T) {
  102. if testing.Short() {
  103. t.Skip("long running")
  104. }
  105. t.Parallel()
  106. file, err := os.Create("testdata/sebsauvage.gob~")
  107. assert.Nil(t, err, "soso")
  108. defer file.Close()
  109. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  110. assert.Nil(t, err, "soso")
  111. err = gob.NewEncoder(file).Encode(feed)
  112. assert.Nil(t, err, "soso")
  113. }
  114. func _TestFeedLargeToAtomClean(t *testing.T) {
  115. if testing.Short() {
  116. t.Skip("long running")
  117. }
  118. t.Parallel()
  119. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  120. assert.Nil(t, err, "soso")
  121. for _, entry := range feed.Entries {
  122. entry.Content.Body = cleanLegacyContent(entry.Content.Body)
  123. entry.Content.Type = "text"
  124. if entry.Published.IsZero() {
  125. entry.Published = entry.Updated
  126. }
  127. }
  128. err = feed.SaveToFile("testdata/sebsauvage.atom~")
  129. assert.Nil(t, err, "soso")
  130. }
  131. func _TestFeedFromFileName_GobLarge(t *testing.T) {
  132. t.Parallel()
  133. file, err := os.Open("testdata/sebsauvage.gob")
  134. assert.Nil(t, err, "soso")
  135. defer file.Close()
  136. feed := Feed{}
  137. err = gob.NewDecoder(file).Decode(&feed)
  138. assert.Nil(t, err, "soso")
  139. assert.Equal(t, 21900, len(feed.Entries), "soso")
  140. }
  141. func _TestFeedFromFileName_Gob(t *testing.T) {
  142. t.Parallel()
  143. file, err := os.Open("testdata/links.gob")
  144. assert.Nil(t, err, "soso")
  145. defer file.Close()
  146. feed := Feed{}
  147. err = gob.NewDecoder(file).Decode(&feed)
  148. assert.Nil(t, err, "soso")
  149. assert.Nil(t, err, "soso")
  150. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  151. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  152. assert.Equal(t, 2, len(feed.Links), "soso")
  153. assert.Equal(t, "self", feed.Links[0].Rel, "soso")
  154. assert.Equal(t, "https://links.mro.name/?do=atom&nb=all", feed.Links[0].Href, "soso")
  155. assert.Equal(t, "hub", feed.Links[1].Rel, "soso")
  156. assert.Equal(t, "http://blog.mro.name/?pushpress=hub", feed.Links[1].Href, "soso")
  157. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Name, "soso")
  158. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Uri, "soso")
  159. assert.Equal(t, Id("https://links.mro.name/"), feed.Id, "soso")
  160. assert.Equal(t, 3618, len(feed.Entries), "soso")
  161. }
  162. func _TestFeedFromFileName_GobGz(t *testing.T) {
  163. t.Parallel()
  164. file, err := os.Open("testdata/links.gob.gz")
  165. assert.Nil(t, err, "soso")
  166. defer file.Close()
  167. gunzip, err := gzip.NewReader(file)
  168. assert.Nil(t, err, "soso")
  169. defer gunzip.Close()
  170. feed := Feed{}
  171. err = gob.NewDecoder(gunzip).Decode(&feed)
  172. assert.Nil(t, err, "soso")
  173. assert.Nil(t, err, "soso")
  174. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  175. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  176. assert.Equal(t, 2, len(feed.Links), "soso")
  177. assert.Equal(t, "self", feed.Links[0].Rel, "soso")
  178. assert.Equal(t, "https://links.mro.name/?do=atom&nb=all", feed.Links[0].Href, "soso")
  179. assert.Equal(t, "hub", feed.Links[1].Rel, "soso")
  180. assert.Equal(t, "http://blog.mro.name/?pushpress=hub", feed.Links[1].Href, "soso")
  181. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Name, "soso")
  182. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Uri, "soso")
  183. assert.Equal(t, Id("https://links.mro.name/"), feed.Id, "soso")
  184. assert.Equal(t, 3618, len(feed.Entries), "soso")
  185. }