git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/feeds/consume_test.go (about)

     1  package feeds
     2  
     3  import (
     4  	"encoding/xml"
     5  	"io"
     6  	"os"
     7  	"testing"
     8  	// "github.com/kr/pretty"
     9  )
    10  
    11  var testRssFeedXML = RssFeedXml{
    12  	XMLName:          xml.Name{Space: "", Local: "rss"},
    13  	Version:          "2.0",
    14  	ContentNamespace: "",
    15  	Channel: &RssFeed{
    16  		XMLName:        xml.Name{Space: "", Local: "channel"},
    17  		Title:          "Lorem ipsum feed for an interval of 1 minutes",
    18  		Link:           "http://example.com/",
    19  		Description:    "This is a constantly updating lorem ipsum feed",
    20  		Language:       "",
    21  		Copyright:      "Michael Bertolacci, licensed under a Creative Commons Attribution 3.0 Unported License.",
    22  		ManagingEditor: "",
    23  		WebMaster:      "",
    24  		PubDate:        "Tue, 30 Oct 2018 23:22:00 GMT",
    25  		LastBuildDate:  "Tue, 30 Oct 2018 23:22:37 GMT",
    26  		Category:       "",
    27  		Generator:      "RSS for Node",
    28  		Docs:           "",
    29  		Cloud:          "",
    30  		Ttl:            60,
    31  		Rating:         "",
    32  		SkipHours:      "",
    33  		SkipDays:       "",
    34  		Image:          (*RssImage)(nil),
    35  		TextInput:      (*RssTextInput)(nil),
    36  		Items: []*RssItem{
    37  			{
    38  				XMLName:     xml.Name{Space: "", Local: "item"},
    39  				Title:       "Lorem ipsum 2018-10-30T23:22:00+00:00",
    40  				Link:        "http://example.com/test/1540941720",
    41  				Description: "Exercitation ut Lorem sint proident.",
    42  				Content:     (*RssContent)(nil),
    43  				Author:      "",
    44  				Category:    "",
    45  				Comments:    "",
    46  				Enclosure:   (*RssEnclosure)(nil),
    47  				Guid:        "http://example.com/test/1540941720",
    48  				PubDate:     "Tue, 30 Oct 2018 23:22:00 GMT",
    49  				Source:      "",
    50  			},
    51  			{
    52  				XMLName:     xml.Name{Space: "", Local: "item"},
    53  				Title:       "Lorem ipsum 2018-10-30T23:21:00+00:00",
    54  				Link:        "http://example.com/test/1540941660",
    55  				Description: "Ea est do quis fugiat exercitation.",
    56  				Content:     (*RssContent)(nil),
    57  				Author:      "",
    58  				Category:    "",
    59  				Comments:    "",
    60  				Enclosure:   (*RssEnclosure)(nil),
    61  				Guid:        "http://example.com/test/1540941660",
    62  				PubDate:     "Tue, 30 Oct 2018 23:21:00 GMT",
    63  				Source:      "",
    64  			},
    65  			{
    66  				XMLName:     xml.Name{Space: "", Local: "item"},
    67  				Title:       "Lorem ipsum 2018-10-30T23:20:00+00:00",
    68  				Link:        "http://example.com/test/1540941600",
    69  				Description: "Ipsum velit cillum ad laborum sit nulla exercitation consequat sint veniam culpa veniam voluptate incididunt.",
    70  				Content:     (*RssContent)(nil),
    71  				Author:      "",
    72  				Category:    "",
    73  				Comments:    "",
    74  				Enclosure:   (*RssEnclosure)(nil),
    75  				Guid:        "http://example.com/test/1540941600",
    76  				PubDate:     "Tue, 30 Oct 2018 23:20:00 GMT",
    77  				Source:      "",
    78  			},
    79  			{
    80  				XMLName:     xml.Name{Space: "", Local: "item"},
    81  				Title:       "Lorem ipsum 2018-10-30T23:19:00+00:00",
    82  				Link:        "http://example.com/test/1540941540",
    83  				Description: "Ullamco pariatur aliqua consequat ea veniam id qui incididunt laborum.",
    84  				Content:     (*RssContent)(nil),
    85  				Author:      "",
    86  				Category:    "",
    87  				Comments:    "",
    88  				Enclosure:   (*RssEnclosure)(nil),
    89  				Guid:        "http://example.com/test/1540941540",
    90  				PubDate:     "Tue, 30 Oct 2018 23:19:00 GMT",
    91  				Source:      "",
    92  			},
    93  			{
    94  				XMLName:     xml.Name{Space: "", Local: "item"},
    95  				Title:       "Lorem ipsum 2018-10-30T23:18:00+00:00",
    96  				Link:        "http://example.com/test/1540941480",
    97  				Description: "Velit proident aliquip aliquip anim mollit voluptate laboris voluptate et occaecat occaecat laboris ea nulla.",
    98  				Content:     (*RssContent)(nil),
    99  				Author:      "",
   100  				Category:    "",
   101  				Comments:    "",
   102  				Enclosure:   (*RssEnclosure)(nil),
   103  				Guid:        "http://example.com/test/1540941480",
   104  				PubDate:     "Tue, 30 Oct 2018 23:18:00 GMT",
   105  				Source:      "",
   106  			},
   107  			{
   108  				XMLName:     xml.Name{Space: "", Local: "item"},
   109  				Title:       "Lorem ipsum 2018-10-30T23:17:00+00:00",
   110  				Link:        "http://example.com/test/1540941420",
   111  				Description: "Do in quis mollit consequat id in minim laborum sint exercitation laborum elit officia.",
   112  				Content:     (*RssContent)(nil),
   113  				Author:      "",
   114  				Category:    "",
   115  				Comments:    "",
   116  				Enclosure:   (*RssEnclosure)(nil),
   117  				Guid:        "http://example.com/test/1540941420",
   118  				PubDate:     "Tue, 30 Oct 2018 23:17:00 GMT",
   119  				Source:      "",
   120  			},
   121  			{
   122  				XMLName:     xml.Name{Space: "", Local: "item"},
   123  				Title:       "Lorem ipsum 2018-10-30T23:16:00+00:00",
   124  				Link:        "http://example.com/test/1540941360",
   125  				Description: "Irure id sint ullamco Lorem magna consectetur officia adipisicing duis incididunt.",
   126  				Content:     (*RssContent)(nil),
   127  				Author:      "",
   128  				Category:    "",
   129  				Comments:    "",
   130  				Enclosure:   (*RssEnclosure)(nil),
   131  				Guid:        "http://example.com/test/1540941360",
   132  				PubDate:     "Tue, 30 Oct 2018 23:16:00 GMT",
   133  				Source:      "",
   134  			},
   135  			{
   136  				XMLName:     xml.Name{Space: "", Local: "item"},
   137  				Title:       "Lorem ipsum 2018-10-30T23:15:00+00:00",
   138  				Link:        "http://example.com/test/1540941300",
   139  				Description: "Sunt anim excepteur esse nisi commodo culpa laborum exercitation ad anim ex elit.",
   140  				Content:     (*RssContent)(nil),
   141  				Author:      "",
   142  				Category:    "",
   143  				Comments:    "",
   144  				Enclosure:   (*RssEnclosure)(nil),
   145  				Guid:        "http://example.com/test/1540941300",
   146  				PubDate:     "Tue, 30 Oct 2018 23:15:00 GMT",
   147  				Source:      "",
   148  			},
   149  			{
   150  				XMLName:     xml.Name{Space: "", Local: "item"},
   151  				Title:       "Lorem ipsum 2018-10-30T23:14:00+00:00",
   152  				Link:        "http://example.com/test/1540941240",
   153  				Description: "Excepteur aliquip fugiat ex labore nisi.",
   154  				Content:     (*RssContent)(nil),
   155  				Author:      "",
   156  				Category:    "",
   157  				Comments:    "",
   158  				Enclosure:   (*RssEnclosure)(nil),
   159  				Guid:        "http://example.com/test/1540941240",
   160  				PubDate:     "Tue, 30 Oct 2018 23:14:00 GMT",
   161  				Source:      "",
   162  			},
   163  			{
   164  				XMLName:     xml.Name{Space: "", Local: "item"},
   165  				Title:       "Lorem ipsum 2018-10-30T23:13:00+00:00",
   166  				Link:        "http://example.com/test/1540941180",
   167  				Description: "Id proident adipisicing proident pariatur aute pariatur pariatur dolor dolor in voluptate dolor.",
   168  				Content:     (*RssContent)(nil),
   169  				Author:      "",
   170  				Category:    "",
   171  				Comments:    "",
   172  				Enclosure:   (*RssEnclosure)(nil),
   173  				Guid:        "http://example.com/test/1540941180",
   174  				PubDate:     "Tue, 30 Oct 2018 23:13:00 GMT",
   175  				Source:      "",
   176  			},
   177  		},
   178  	},
   179  }
   180  
   181  var testAtomFeedXML = AtomFeed{
   182  	XMLName:  xml.Name{Space: "", Local: "feed"},
   183  	Xmlns:    "",
   184  	Title:    "Lorem ipsum feed for an interval of 1 minutes",
   185  	Id:       "",
   186  	Updated:  "",
   187  	Category: "",
   188  	Icon:     "",
   189  	Logo:     "",
   190  	Rights:   "",
   191  	Subtitle: "",
   192  	Link: &AtomLink{
   193  		XMLName: xml.Name{Space: "", Local: "link"},
   194  		Href:    "",
   195  		Rel:     "",
   196  		Type:    "",
   197  		Length:  "",
   198  	},
   199  	Author: &AtomAuthor{
   200  		XMLName:    xml.Name{Space: "", Local: "author"},
   201  		AtomPerson: AtomPerson{},
   202  	},
   203  	Contributor: (*AtomContributor)(nil),
   204  	Entries: []*AtomEntry{
   205  		{
   206  			XMLName:     xml.Name{Space: "", Local: "entry"},
   207  			Xmlns:       "",
   208  			Title:       "Lorem ipsum 2018-10-30T23:22:00+00:00",
   209  			Updated:     "",
   210  			Id:          "",
   211  			Category:    "",
   212  			Content:     (*AtomContent)(nil),
   213  			Rights:      "",
   214  			Source:      "",
   215  			Published:   "",
   216  			Contributor: (*AtomContributor)(nil),
   217  			Links:       nil,
   218  			Summary:     (*AtomSummary)(nil),
   219  			Author:      (*AtomAuthor)(nil),
   220  		},
   221  		{
   222  			XMLName:     xml.Name{Space: "", Local: "entry"},
   223  			Xmlns:       "",
   224  			Title:       "Lorem ipsum 2018-10-30T23:21:00+00:00",
   225  			Updated:     "",
   226  			Id:          "",
   227  			Category:    "",
   228  			Content:     (*AtomContent)(nil),
   229  			Rights:      "",
   230  			Source:      "",
   231  			Published:   "",
   232  			Contributor: (*AtomContributor)(nil),
   233  			Links:       nil,
   234  			Summary:     (*AtomSummary)(nil),
   235  			Author:      (*AtomAuthor)(nil),
   236  		},
   237  		{
   238  			XMLName:     xml.Name{Space: "", Local: "entry"},
   239  			Xmlns:       "",
   240  			Title:       "Lorem ipsum 2018-10-30T23:20:00+00:00",
   241  			Updated:     "",
   242  			Id:          "",
   243  			Category:    "",
   244  			Content:     (*AtomContent)(nil),
   245  			Rights:      "",
   246  			Source:      "",
   247  			Published:   "",
   248  			Contributor: (*AtomContributor)(nil),
   249  			Links:       nil,
   250  			Summary:     (*AtomSummary)(nil),
   251  			Author:      (*AtomAuthor)(nil),
   252  		},
   253  		{
   254  			XMLName:     xml.Name{Space: "", Local: "entry"},
   255  			Xmlns:       "",
   256  			Title:       "Lorem ipsum 2018-10-30T23:19:00+00:00",
   257  			Updated:     "",
   258  			Id:          "",
   259  			Category:    "",
   260  			Content:     (*AtomContent)(nil),
   261  			Rights:      "",
   262  			Source:      "",
   263  			Published:   "",
   264  			Contributor: (*AtomContributor)(nil),
   265  			Links:       nil,
   266  			Summary:     (*AtomSummary)(nil),
   267  			Author:      (*AtomAuthor)(nil),
   268  		},
   269  		{
   270  			XMLName:     xml.Name{Space: "", Local: "entry"},
   271  			Xmlns:       "",
   272  			Title:       "Lorem ipsum 2018-10-30T23:18:00+00:00",
   273  			Updated:     "",
   274  			Id:          "",
   275  			Category:    "",
   276  			Content:     (*AtomContent)(nil),
   277  			Rights:      "",
   278  			Source:      "",
   279  			Published:   "",
   280  			Contributor: (*AtomContributor)(nil),
   281  			Links:       nil,
   282  			Summary:     (*AtomSummary)(nil),
   283  			Author:      (*AtomAuthor)(nil),
   284  		},
   285  		{
   286  			XMLName:     xml.Name{Space: "", Local: "entry"},
   287  			Xmlns:       "",
   288  			Title:       "Lorem ipsum 2018-10-30T23:17:00+00:00",
   289  			Updated:     "",
   290  			Id:          "",
   291  			Category:    "",
   292  			Content:     (*AtomContent)(nil),
   293  			Rights:      "",
   294  			Source:      "",
   295  			Published:   "",
   296  			Contributor: (*AtomContributor)(nil),
   297  			Links:       nil,
   298  			Summary:     (*AtomSummary)(nil),
   299  			Author:      (*AtomAuthor)(nil),
   300  		},
   301  		{
   302  			XMLName:     xml.Name{Space: "", Local: "entry"},
   303  			Xmlns:       "",
   304  			Title:       "Lorem ipsum 2018-10-30T23:16:00+00:00",
   305  			Updated:     "",
   306  			Id:          "",
   307  			Category:    "",
   308  			Content:     (*AtomContent)(nil),
   309  			Rights:      "",
   310  			Source:      "",
   311  			Published:   "",
   312  			Contributor: (*AtomContributor)(nil),
   313  			Links:       nil,
   314  			Summary:     (*AtomSummary)(nil),
   315  			Author:      (*AtomAuthor)(nil),
   316  		},
   317  		{
   318  			XMLName:     xml.Name{Space: "", Local: "entry"},
   319  			Xmlns:       "",
   320  			Title:       "Lorem ipsum 2018-10-30T23:15:00+00:00",
   321  			Updated:     "",
   322  			Id:          "",
   323  			Category:    "",
   324  			Content:     (*AtomContent)(nil),
   325  			Rights:      "",
   326  			Source:      "",
   327  			Published:   "",
   328  			Contributor: (*AtomContributor)(nil),
   329  			Links:       nil,
   330  			Summary:     (*AtomSummary)(nil),
   331  			Author:      (*AtomAuthor)(nil),
   332  		},
   333  		{
   334  			XMLName:     xml.Name{Space: "", Local: "entry"},
   335  			Xmlns:       "",
   336  			Title:       "Lorem ipsum 2018-10-30T23:14:00+00:00",
   337  			Updated:     "",
   338  			Id:          "",
   339  			Category:    "",
   340  			Content:     (*AtomContent)(nil),
   341  			Rights:      "",
   342  			Source:      "",
   343  			Published:   "",
   344  			Contributor: (*AtomContributor)(nil),
   345  			Links:       nil,
   346  			Summary:     (*AtomSummary)(nil),
   347  			Author:      (*AtomAuthor)(nil),
   348  		},
   349  		{
   350  			XMLName:     xml.Name{Space: "", Local: "entry"},
   351  			Xmlns:       "",
   352  			Title:       "Lorem ipsum 2018-10-30T23:13:00+00:00",
   353  			Updated:     "",
   354  			Id:          "",
   355  			Category:    "",
   356  			Content:     (*AtomContent)(nil),
   357  			Rights:      "",
   358  			Source:      "",
   359  			Published:   "",
   360  			Contributor: (*AtomContributor)(nil),
   361  			Links:       nil,
   362  			Summary:     (*AtomSummary)(nil),
   363  			Author:      (*AtomAuthor)(nil),
   364  		},
   365  	},
   366  }
   367  
   368  func TestRssUnmarshal(t *testing.T) {
   369  	var xmlFeed RssFeedXml
   370  	xmlFile, err := os.Open("test.rss")
   371  	if err != nil {
   372  		panic("AHH file bad")
   373  	}
   374  	bytes, _ := io.ReadAll(xmlFile)
   375  	_ = xml.Unmarshal(bytes, &xmlFeed)
   376  
   377  	// TODO
   378  	// if !reflect.DeepEqual(testRssFeedXML, xmlFeed) {
   379  	// 	diffs := pretty.Diff(testRssFeedXML, xmlFeed)
   380  	// 	t.Log(pretty.Println(diffs))
   381  	// 	t.Error("object was not unmarshalled correctly")
   382  	// }
   383  
   384  }
   385  
   386  func TestAtomUnmarshal(t *testing.T) {
   387  	var xmlFeed AtomFeed
   388  	xmlFile, err := os.Open("test.atom")
   389  	if err != nil {
   390  		panic("AHH file bad")
   391  	}
   392  	bytes, _ := io.ReadAll(xmlFile)
   393  	_ = xml.Unmarshal(bytes, &xmlFeed)
   394  
   395  	// TODO
   396  	// if !reflect.DeepEqual(testAtomFeedXML, xmlFeed) {
   397  	// 	diffs := pretty.Diff(testAtomFeedXML, xmlFeed)
   398  	// 	t.Log(pretty.Println(diffs))
   399  	// 	t.Error("object was not unmarshalled correctly")
   400  	// }
   401  }