atom_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // Copyright (C) 2017-2019 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. func ta(tags ...string) map[string]struct{} {
  31. ret := make(map[string]struct{}, len(tags))
  32. for _, tag := range tags {
  33. ret[tag] = struct{}{}
  34. }
  35. return ret
  36. }
  37. func TestTagsFromString(t *testing.T) {
  38. t.Parallel()
  39. assert.Equal(t, ta("ha"), tagsFromString("#ha, 1.2 foo#nein"), "aha")
  40. assert.Equal(t, ta("🐳"), tagsFromString("🐳, foo#nein"), "aha")
  41. assert.Equal(t, ta("§", "$", "†"), tagsFromString("#§, #$ #† foo#nein"), "aha")
  42. assert.Equal(t, ta("🐳"), tagsFromString("#🐳, foo#nein #"), "aha")
  43. assert.Equal(t, ta("ipsum", "opensource", "🐳"), tagsFromString(`Lorem #ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
  44. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  45. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. #opensource #🐳`), "ja, genau")
  46. assert.Equal(t, ta("⭐️"), tagsFromString("a single ⭐️ is also a tag"), "aha")
  47. }
  48. func TestEntryCategoriesMerged(t *testing.T) {
  49. t.Parallel()
  50. e := Entry{
  51. Title: HumanText{Body: "#ha, foo#nein"},
  52. Content: &HumanText{Body: "B #so, bar"},
  53. Categories: []Category{{Term: "so"}, {Term: "da"}},
  54. }
  55. assert.NotNil(t, e, "genau")
  56. assert.Equal(t, []Category{{Term: "da"}, {Term: "ha"}, {Term: "so"}}, e.CategoriesMerged(), "genau")
  57. }
  58. var b64Tob24Tests = []struct {
  59. n string
  60. expected string
  61. }{
  62. {"FxW3Ow", "77tk489"},
  63. {"Zt3wHA", "4ezde68"},
  64. {"oHordQ", "c8x2syk"}, // http://sebsauvage.net/links/?oHordQ
  65. {"McLIuQ", "k9cr7c3"}, // http://sebsauvage.net/links/?McLIuQ
  66. }
  67. func TestFeedIdLowerFromIdMixed(t *testing.T) {
  68. // https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go
  69. for _, tt := range b64Tob24Tests {
  70. actual, _ := base64ToBase24x7(tt.n)
  71. if actual != tt.expected {
  72. t.Errorf("base64ToBase24x7(%s): expected %s, actual %s", tt.n, tt.expected, actual)
  73. }
  74. }
  75. }
  76. func TestFeedNewEntry(t *testing.T) {
  77. f := Feed{}
  78. ent := f.newEntry(time.Time{})
  79. assert.True(t, ent.Published.IsZero(), "oha")
  80. assert.Equal(t, Id("dzcz8k2"), ent.Id, "soso")
  81. }
  82. func TestFeedFromFileName_Atom(t *testing.T) {
  83. t.Parallel()
  84. feed, err := FeedFromFileName("testdata/links.atom")
  85. assert.Nil(t, err, "soso")
  86. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  87. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  88. assert.Equal(t, 0, len(feed.Links), "soso")
  89. assert.Equal(t, "m", feed.Authors[0].Name, "soso")
  90. assert.Equal(t, Iri(""), feed.Authors[0].Uri, "soso")
  91. assert.Equal(t, Id(""), feed.Id, "soso")
  92. assert.Equal(t, TextType("html"), feed.Entries[0].Content.Type, "soso")
  93. 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 />
  94. <br />
  95. Ach was, wen gibt's denn dann da noch so?<br>(<a href="https://links.mro.name/?aTh_gA">Permalink</a>)`
  96. assert.Equal(t, txt, feed.Entries[0].Content.Body, "soso")
  97. 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. …"
  98. Ach was, wen gibt's denn dann da noch so?`, cleanLegacyContent(txt), "soso")
  99. assert.Equal(t, 3618, len(feed.Entries), "soso")
  100. }
  101. func _TestFeedFromFileName_AtomLarge(t *testing.T) {
  102. if testing.Short() {
  103. t.Skip("long running")
  104. }
  105. t.Parallel()
  106. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  107. assert.Nil(t, err, "soso")
  108. assert.Equal(t, 21900, len(feed.Entries), "soso")
  109. }
  110. func TestFeedFromFileName_PhotosAtom(t *testing.T) {
  111. t.Parallel()
  112. feed, err := FeedFromFileName("testdata/photos.atom")
  113. assert.Nil(t, err, "soso")
  114. assert.Equal(t, "Demo Album", feed.Title.Body, "soso")
  115. assert.Equal(t, "2016-11-27T12:32:57+01:00", feed.Updated.Format(time.RFC3339), "soso")
  116. assert.Equal(t, 2, len(feed.Links), "soso")
  117. assert.Equal(t, relSelf, feed.Links[0].Rel, "soso")
  118. assert.Equal(t, "https://lager.mro.name/galleries/demo/", feed.Links[0].Href, "soso")
  119. assert.Equal(t, relAlternate, feed.Links[1].Rel, "soso")
  120. assert.Equal(t, "https://lager.mro.name/galleries/demo/", feed.Links[1].Href, "soso")
  121. assert.Equal(t, "Marcus Rohrmoser", feed.Authors[0].Name, "soso")
  122. assert.Equal(t, Iri("http://mro.name/me"), feed.Authors[0].Uri, "soso")
  123. assert.Equal(t, Id("https://lager.mro.name/galleries/demo/"), feed.Id, "soso")
  124. assert.Equal(t, 127, len(feed.Entries), "soso")
  125. assert.Equal(t, Iri("https://lager.mro.name/galleries/demo/200p/fbb6669a533054da3747fb71790dc515bbf76da2.jpeg"), feed.Entries[0].MediaThumbnail.Url, "soso")
  126. assert.Equal(t, Latitude(48.047504), feed.Entries[0].GeoRssPoint.Lat, "soso")
  127. assert.Equal(t, Longitude(10.871933), feed.Entries[0].GeoRssPoint.Lon, "soso")
  128. }
  129. func _TestFeedLargeToGob(t *testing.T) {
  130. if testing.Short() {
  131. t.Skip("long running")
  132. }
  133. t.Parallel()
  134. file, err := os.Create("testdata/sebsauvage.gob~")
  135. assert.Nil(t, err, "soso")
  136. defer file.Close()
  137. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  138. assert.Nil(t, err, "soso")
  139. err = gob.NewEncoder(file).Encode(feed)
  140. assert.Nil(t, err, "soso")
  141. }
  142. func _TestFeedLargeToAtomClean(t *testing.T) {
  143. if testing.Short() {
  144. t.Skip("long running")
  145. }
  146. t.Parallel()
  147. feed, err := FeedFromFileName("testdata/sebsauvage.atom")
  148. assert.Nil(t, err, "soso")
  149. for _, entry := range feed.Entries {
  150. entry.Content.Body = cleanLegacyContent(entry.Content.Body)
  151. entry.Content.Type = "text"
  152. if entry.Published.IsZero() {
  153. entry.Published = entry.Updated
  154. }
  155. }
  156. err = feed.SaveToFile("testdata/sebsauvage.atom~")
  157. assert.Nil(t, err, "soso")
  158. }
  159. func _TestFeedFromFileName_GobLarge(t *testing.T) {
  160. t.Parallel()
  161. file, err := os.Open("testdata/sebsauvage.gob")
  162. assert.Nil(t, err, "soso")
  163. defer file.Close()
  164. feed := Feed{}
  165. err = gob.NewDecoder(file).Decode(&feed)
  166. assert.Nil(t, err, "soso")
  167. assert.Equal(t, 21900, len(feed.Entries), "soso")
  168. }
  169. func _TestFeedFromFileName_Gob(t *testing.T) {
  170. t.Parallel()
  171. file, err := os.Open("testdata/links.gob")
  172. assert.Nil(t, err, "soso")
  173. defer file.Close()
  174. feed := Feed{}
  175. err = gob.NewDecoder(file).Decode(&feed)
  176. assert.Nil(t, err, "soso")
  177. assert.Nil(t, err, "soso")
  178. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  179. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  180. assert.Equal(t, 2, len(feed.Links), "soso")
  181. assert.Equal(t, "self", feed.Links[0].Rel, "soso")
  182. assert.Equal(t, "https://links.mro.name/?do=atom&nb=all", feed.Links[0].Href, "soso")
  183. assert.Equal(t, "hub", feed.Links[1].Rel, "soso")
  184. assert.Equal(t, "http://blog.mro.name/?pushpress=hub", feed.Links[1].Href, "soso")
  185. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Name, "soso")
  186. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Uri, "soso")
  187. assert.Equal(t, Id("https://links.mro.name/"), feed.Id, "soso")
  188. assert.Equal(t, 3618, len(feed.Entries), "soso")
  189. }
  190. func _TestFeedFromFileName_GobGz(t *testing.T) {
  191. t.Parallel()
  192. file, err := os.Open("testdata/links.gob.gz")
  193. assert.Nil(t, err, "soso")
  194. defer file.Close()
  195. gunzip, err := gzip.NewReader(file)
  196. assert.Nil(t, err, "soso")
  197. defer gunzip.Close()
  198. feed := Feed{}
  199. err = gob.NewDecoder(gunzip).Decode(&feed)
  200. assert.Nil(t, err, "soso")
  201. assert.Nil(t, err, "soso")
  202. assert.Equal(t, "🔗 mro", feed.Title.Body, "soso")
  203. assert.Equal(t, "2017-02-09T22:44:52+01:00", feed.Updated.Format(time.RFC3339), "soso")
  204. assert.Equal(t, 2, len(feed.Links), "soso")
  205. assert.Equal(t, "self", feed.Links[0].Rel, "soso")
  206. assert.Equal(t, "https://links.mro.name/?do=atom&nb=all", feed.Links[0].Href, "soso")
  207. assert.Equal(t, "hub", feed.Links[1].Rel, "soso")
  208. assert.Equal(t, "http://blog.mro.name/?pushpress=hub", feed.Links[1].Href, "soso")
  209. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Name, "soso")
  210. assert.Equal(t, "https://links.mro.name/", feed.Authors[0].Uri, "soso")
  211. assert.Equal(t, Id("https://links.mro.name/"), feed.Id, "soso")
  212. assert.Equal(t, 3618, len(feed.Entries), "soso")
  213. }