diff --git a/album_csv_reader_test.go b/album_csv_reader_test.go index a7ee35a..5978f2f 100644 --- a/album_csv_reader_test.go +++ b/album_csv_reader_test.go @@ -1,209 +1,18 @@ package main -import ( - "strings" - "time" - - . "gopkg.in/check.v1" -) +import . "gopkg.in/check.v1" type AlbumCsvReaderSuite struct{} var _ = Suite(&AlbumCsvReaderSuite{}) func (s *AlbumCsvReaderSuite) TestCanReadCsv(c *C) { - csvData := `IdAlbum;ISBN;Serie;Num;NumA;Titre;Editeur;Collection;EO;DL;AI;Cote;Etat;DateAchat;PrixAchat;Note;Scenariste;Dessinateur;Wishlist;AVendre;Perso1;Perso2;Perso3;Perso4;Format;Suivi;Commentaire;Table -95235;978-2-84865-325-9;"... et autres racontars";1;"";"La vierge froide et autres racontars";"Sarbacane";"";1;10/2009;;;1;03/09/2013;;;"De Bonneval, Gwen";"Tanquerelle, Hervé";0;0;"RACO";"1";"";0;3;0;"";ALBUM -120785;978-2-84865-429-4;"... et autres racontars";2;"";"Le roi Oscar et autres racontars";"Sarbacane";"";1;01/2011;;;2;03/09/2013;;;"De Bonneval, Gwen";"Tanquerelle, Hervé";0;0;"RACO";"2";"";0;3;0;"";ALBUM -138799;978-2-205-06746-0;"''Atar Gull'' ou le destin d'un esclave modèle";;"";"''Atar Gull'' ou le destin d'un esclave modèle";"Dargaud";"Long Courrier";1;10/2011;;;3;04/09/2013;16.95;;"Nury, Fabien";"Brüno";0;0;"LONG";"11";"";0;1;0;"";ALBUM -57203;;"(AUT) Liberatore";;"";"Tanino Liberatore";"Kesselring";"";1;12/1985;;;4;07/12/2013;;;"Liberatore";"Liberatore";0;0;"LIB";"00";"";0;0;0;"";ALBUM -58595;;"(AUT) Plantu";;"1989";"Des fourmis dans les jambes";"Le Monde";"";1;;;;0;07/12/2013;;;"Plantu";"Plantu";0;0;"HUMO";"17";"";0;1;0;"";ALBUM -88273;;"(AUT) Serre";1;"";"Humour noir et hommes en blanc";"Éditions du Grésivaudan";"";1;11/1972;;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"1";"";0;3;0;"";ALBUM -46296;2-7234-0071-9;"(AUT) Serre";2;"";"Le Sport";"Glénat";"";1;10/1977;11/1977;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"2";"";0;2;0;"";ALBUM -46010;2-7234-0100-6;"(AUT) Serre";3;"";"L'automobile";"Glénat";"";1;12/1978;12/1978;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"3";"";0;2;0;"";ALBUM -46295;2-7234-0130-8;"(AUT) Serre";4;"";"...vice compris";"Glénat";"";1;10/1979;;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"7";"";0;1;0;"";ALBUM -7096;2-7234-0247-9;"(AUT) Serre";5;"";"Savoir vivre";"Glénat";"";1;10/1981;;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"5";"";0;2;0;"";ALBUM -7088;2-7234-0326-2;"(AUT) Serre";6;"";"La Bouffe";"Glénat";"";1;10/1982;;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"4";"";0;2;0;"";ALBUM -7094;2-7234-0409-9;"(AUT) Serre";7;"";"Le bricolage";"Glénat";"";1;12/1983;;;0;03/09/2013;;;"Serre, Claude";"Serre, Claude";0;0;"SERR";"6";"";0;2;0;"";ALBUM -` - - expected := []Album{ - Album{ - ID: 95235, - ISBN: "978-2-84865-325-9", - Series: "... et autres racontars", - Num: 1, - NumA: "", - Title: "La vierge froide et autres racontars", - Editor: "Sarbacane", - Collection: "", - State: 1, - LegalDeposit: time.Date(2009, 10, 1, 0, 0, 0, 0, time.UTC), - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "RACO-1", - }, - Album{ID: 120785, - ISBN: "978-2-84865-429-4", - Series: "... et autres racontars", - Num: 2, - NumA: "", - Title: "Le roi Oscar et autres racontars", - Editor: "Sarbacane", - Collection: "", - LegalDeposit: time.Date(2011, 01, 1, 0, 0, 0, 0, time.UTC), - State: 2, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "RACO-2", - }, - Album{ - ID: 138799, - ISBN: "978-2-205-06746-0", - Series: "''Atar Gull'' ou le destin d'un esclave modèle", - Num: -1, - NumA: "", - Title: "''Atar Gull'' ou le destin d'un esclave modèle", - Editor: "Dargaud", - Collection: "Long Courrier", - LegalDeposit: time.Date(2011, 10, 1, 0, 0, 0, 0, time.UTC), - State: 3, - PurchaseDate: time.Date(2013, 9, 4, 0, 0, 0, 0, time.UTC), - SatID: "LONG-11", - }, - Album{ - ID: 57203, - ISBN: "", - Series: "(AUT) Liberatore", - Num: -1, - NumA: "", - Title: "Tanino Liberatore", - Editor: "Kesselring", - Collection: "", - LegalDeposit: time.Date(1985, 12, 1, 0, 0, 0, 0, time.UTC), - State: 4, - PurchaseDate: time.Date(2013, 12, 7, 0, 0, 0, 0, time.UTC), - SatID: "LIB-00", - }, - Album{ - ID: 58595, - ISBN: "", - Series: "(AUT) Plantu", - Num: -1, - NumA: "1989", - Title: "Des fourmis dans les jambes", - Editor: "Le Monde", - Collection: "", - State: 0, - PurchaseDate: time.Date(2013, 12, 7, 0, 0, 0, 0, time.UTC), - SatID: "HUMO-17", - }, - Album{ - ID: 88273, - ISBN: "", - Series: "(AUT) Serre", - Num: 1, - NumA: "", - Title: "Humour noir et hommes en blanc", - Editor: "Éditions du Grésivaudan", - Collection: "", - LegalDeposit: time.Date(1972, 11, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-1", - }, - Album{ - ID: 46296, - ISBN: "2-7234-0071-9", - Series: "(AUT) Serre", - Num: 2, - NumA: "", - Title: "Le Sport", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1977, 10, 1, 0, 0, 0, 0, time.UTC), - PrintDate: time.Date(1977, 11, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-2", - }, - Album{ - ID: 46010, - ISBN: "2-7234-0100-6", - Series: "(AUT) Serre", - Num: 3, - NumA: "", - Title: "L'automobile", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1978, 12, 1, 0, 0, 0, 0, time.UTC), - PrintDate: time.Date(1978, 12, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-3", - }, - Album{ - ID: 46295, - ISBN: "2-7234-0130-8", - Series: "(AUT) Serre", - Num: 4, - NumA: "", - Title: "...vice compris", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1979, 10, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-7", - }, - Album{ - ID: 7096, - ISBN: "2-7234-0247-9", - Series: "(AUT) Serre", - Num: 5, - NumA: "", - Title: "Savoir vivre", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1981, 10, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-5", - }, - Album{ - ID: 7088, - ISBN: "2-7234-0326-2", - Series: "(AUT) Serre", - Num: 6, - NumA: "", - Title: "La Bouffe", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1982, 10, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-4", - }, - Album{ - ID: 7094, - ISBN: "2-7234-0409-9", - Series: "(AUT) Serre", - Num: 7, - NumA: "", - Title: "Le bricolage", - Editor: "Glénat", - Collection: "", - LegalDeposit: time.Date(1983, 12, 1, 0, 0, 0, 0, time.UTC), - State: 0, - PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), - SatID: "SERR-6", - }, - } - - r, err := NewAlbumCsvReader(strings.NewReader(csvData)) + r, err := NewAlbumCsvReader(GetCsvData()) c.Assert(err, IsNil) - for _, e := range expected { + for _, e := range albumsDataTest { res, err := r.Read() if c.Check(err, IsNil) == true { - c.Check(*res, DeepEquals, e, Commentf("expected: %+v actual: %+v", e, res)) + c.Check(*res, DeepEquals, StripNonCsvField(e), Commentf("expected: %+v actual: %+v", e, res)) } } diff --git a/data_test.go b/data_test.go new file mode 100644 index 0000000..839fd56 --- /dev/null +++ b/data_test.go @@ -0,0 +1,92 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + "regexp" + "time" +) + +var albumsDataTest []Album + +// StripNonCsvField strips all the fields which are not in the Csv +func StripNonCsvField(a Album) Album { + return Album{ + ID: a.ID, + ISBN: a.ISBN, + Series: a.Series, + Title: a.Title, + Num: a.Num, + NumA: a.NumA, + State: a.State, + Author: a.Author, + Editor: a.Editor, + Collection: a.Collection, + SatID: a.SatID, + LegalDeposit: a.LegalDeposit, + PrintDate: a.PrintDate, + PurchaseDate: a.PurchaseDate, + } +} + +func formatDate(w io.Writer, t time.Time, fakeError *bool) { + if t == time.Unix(0, 0) { + if *fakeError == true { + fmt.Fprintf(w, "00/0000;") + *fakeError = false + } else { + fmt.Fprintf(w, ";") + } + } else { + fmt.Fprintf(w, "%s;", t.Format("01/2006")) + } +} + +var rxSatID = regexp.MustCompile(`\A([a-zA-Z]+)-([a-zA-Z0-9]+)\z`) + +func GetCsvData() *bytes.Buffer { + res := &bytes.Buffer{} + + fmt.Fprintf(res, "%s\n", "IdAlbum;ISBN;Serie;Num;NumA;Titre;Editeur;Collection;EO;DL;AI;Cote;Etat;DateAchat;PrixAchat;Note;Scenariste;Dessinateur;Wishlist;AVendre;Perso1;Perso2;Perso3;Perso4;Format;Suivi;Commentaire;Table") + + fakeWrongTime := true + + for _, a := range albumsDataTest { + fmt.Fprintf(res, "%d;%s;\"%s\";", a.ID, a.ISBN, a.Series) + + if a.Num < 0 { + fmt.Fprintf(res, ";") + } else { + fmt.Fprintf(res, "%d;", a.Num) + } + + fmt.Fprintf(res, "\"%s\";\"%s\";\"%s\";\"%s\";1;", a.NumA, a.Title, a.Editor, a.Collection) + formatDate(res, a.LegalDeposit, &fakeWrongTime) + formatDate(res, a.PrintDate, &fakeWrongTime) + + m := rxSatID.FindStringSubmatch(a.SatID) + if m == nil { + panic(fmt.Sprintf("should not happen with %#v", a)) + } + fmt.Fprintf(res, ";%d;%s;;;%s;;;;%s;%s;;;\"foo\";;;ALBUM\n", a.State, a.PurchaseDate.Format("02/01/2006"), a.Author, m[1], m[2]) + } + return res +} + +func init() { + albumsPath := filepath.Join("testdata", "albums.json") + f, err := os.Open(albumsPath) + if err != nil { + panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err)) + } + defer CloseOrPanic(f, albumsPath) + + dec := json.NewDecoder(f) + if err = dec.Decode(&albumsDataTest); err != nil { + panic(fmt.Sprintf("Could not parse '%s': %s", albumsPath, err)) + } +}