// Copyright 2014 Tamás Gulácsi. All rights reserved. // Use of this source code is governed by an Apache 2.0 // license that can be found in the LICENSE file. package picago import ( "encoding/xml" "os" "reflect" "testing" "time" ) const albumsXML = ` https://picasaweb.google.com/data/feed/api/user/liz 2009-03-12T01:19:14.876Z liz https://iconPath/liz.jpg Liz http://picasaweb.google.com/liz Picasaweb 1 1 1000 liz Liz https://thumbnailPath/liz.jpg 1073741824 32716 500 https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID 2005-06-17T07:09:42.000Z 2009-03-12T01:19:14.000Z 2009-03-12T01:19:14.000Z lolcats Hilarious Felines public Liz http://picasaweb.google.com/liz albumID Mountain View, CA public 1118992182000 1 499 23044 liz Liz lolcats Hilarious Felines Liz ` const photosXML = ` https://picasaweb.google.com/data/feed/user/liz/albumid/albumID 2008-12-08T01:24:16.000Z lolcats Hilarious Felines public https://iconPath/Lolcats.jpg Liz http://picasaweb.google.com/liz Picasaweb 1 1 1000 albumID Mountain View, CA, USA public 1150527600000 1 499 23044 liz Liz 37.38911780598221 -122.08638668060303 37.38482151758655 -122.0958924293518 37.39341409437787 -122.07688093185425 true true http://picasaweb.google.com/data/entry/user/liz/albumid/albumID/photoid/photoID 2008-08-15T18:58:44.000Z 2008-12-08T01:11:03.000Z 2008-12-08T01:11:03.000Z invisible_bike.jpg Bike photoID 1.66002086E9 albumID public 410 295 23044 1218826724000 0 0657130896bace739a44ce90a7d5b451 Liz invisible, bike invisible_bike.jpg 37.427399548633325 -122.1703290939331 37.42054944692195 -122.1825385093689 37.4342496503447 -122.15811967849731 ` func TestAtom(t *testing.T) { for _, text := range []string{albumsXML, photosXML} { var result Atom if err := xml.Unmarshal([]byte(text), &result); err != nil { t.Errorf("Unmarshal error: %v", err) } t.Logf("result: %#v", result) } } func mustParseAtom(t *testing.T, file string) *Atom { f, err := os.Open(file) if err != nil { t.Fatal(err) } defer f.Close() a := new(Atom) if err := xml.NewDecoder(f).Decode(a); err != nil { t.Fatal(err) } return a } func TestVideoInGallery(t *testing.T) { atom := mustParseAtom(t, "testdata/gallery-with-a-video.xml") if len(atom.Entries) != 3 { t.Fatalf("num entries = %d; want 3", len(atom.Entries)) } p, err := atom.Entries[2].photo() if err != nil { t.Fatal(err) } if p.Type != "video/mpeg4" { t.Errorf("type = %q; want video/mpeg4", p.Type) } if got, want := p.URL, "https://foo.googlevideo.com/bar.mp4"; got != want { t.Errorf("URL = %q; want %q", got, want) } wantKW := []string{"keyboard", "stuff"} if !reflect.DeepEqual(p.Keywords, wantKW) { t.Errorf("Keywords = %q; want %q", p.Keywords, wantKW) } } func TestAlbumFromEntry(t *testing.T) { atom := mustParseAtom(t, "testdata/album-list.xml") if len(atom.Entries) != 3 { t.Fatalf("num entries = %d; want 3", len(atom.Entries)) } var got []Album for _, ent := range atom.Entries { got = append(got, ent.album()) } tm := func(s string) time.Time { ret, err := time.Parse(time.RFC3339, s) if err != nil { t.Fatal(err) } return ret } want := []Album{ Album{ID: "6040139514831220113", Name: "BikingWithBlake", Title: "Biking with Blake", Description: "Description is biking up San Bruno mountain.\n\nAnd a newline.", Location: "San Bruno Mt, CA", AuthorName: "Gast Erson", AuthorURI: "https://picasaweb.google.com/114403741484702971746", Published: tm("2014-07-22T07:00:00.000Z"), Updated: tm("2014-07-28T22:22:25.577Z"), URL: "https://picasaweb.google.com/114403741484702971746/BikingWithBlake"}, Album{ID: "6041693388376552305", Name: "Mexico", Title: "Mexico", Description: "", Location: "", AuthorName: "Gast Erson", AuthorURI: "https://picasaweb.google.com/114403741484702971746", Published: tm("2014-07-30T03:36:00.000Z"), Updated: tm("2014-07-30T19:46:05.346Z"), URL: "https://picasaweb.google.com/114403741484702971746/Mexico"}, Album{ID: "6041709940397032273", Name: "TestingOver2048", Title: "testing over 2048", Description: "", Location: "", AuthorName: "Gast Erson", AuthorURI: "https://picasaweb.google.com/114403741484702971746", Published: tm("2014-07-30T04:40:14.000Z"), Updated: tm("2014-07-30T05:01:02.919Z"), URL: "https://picasaweb.google.com/114403741484702971746/TestingOver2048"}, } if len(got) != len(want) { t.Fatalf("got %d entries, want %d", len(got), len(want)) } if reflect.DeepEqual(got, want) { return } for i := range got { if !reflect.DeepEqual(got[i], want[i]) { t.Errorf("index %d doesn't match:\n got: %+v\nwant: %+v\n", i, got[i], want[i]) } } }