From ab201424ad8c47cd6885cb147ee0c23b17c36fbd Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Sat, 16 Jan 2016 19:18:59 +0100 Subject: [PATCH] Adds a skeleton for an AlbumCsvReader --- album_csv_reader.go | 21 ++++ album_csv_reader_test.go | 212 +++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 album_csv_reader.go create mode 100644 album_csv_reader_test.go diff --git a/album_csv_reader.go b/album_csv_reader.go new file mode 100644 index 0000000..2f3c548 --- /dev/null +++ b/album_csv_reader.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "io" +) + +// AlbumCsvReader can read Album definition from a csv file +type AlbumCsvReader struct { +} + +// NewAlbumCsvReader creates a new AlbumCsvReader from a reader +func NewAlbumCsvReader(r io.Reader) *AlbumCsvReader { + return nil +} + +// Read get the next line in the CSV and return an Album or an error, +// or io.EOF if stream is closed +func (a *AlbumCsvReader) Read() (*Album, error) { + return nil, fmt.Errorf("Niot uyet implemented") +} diff --git a/album_csv_reader_test.go b/album_csv_reader_test.go new file mode 100644 index 0000000..2e057d3 --- /dev/null +++ b/album_csv_reader_test.go @@ -0,0 +1,212 @@ +package main + +import ( + "strings" + "time" + + . "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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 0, 0, 0, 0, time.UTC), + PrintDate: time.Date(1977, 11, 0, 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, 0, 0, 0, 0, 0, time.UTC), + PrintDate: time.Date(1978, 12, 0, 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, 0, 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, 0, 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, 0, 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, 0, 0, 0, 0, 0, time.UTC), + State: 0, + PurchaseDate: time.Date(2013, 9, 3, 0, 0, 0, 0, time.UTC), + SatID: "SERR-6", + }, + } + + r := NewAlbumCsvReader(strings.NewReader(csvData)) + for _, e := range expected { + res, err := r.Read() + if c.Check(err, IsNil) == true { + c.Check(res, Equals, e) + } + } + + res, err := r.Read() + c.Check(res, IsNil) + c.Check(err, ErrorMatches, "EOF") +} diff --git a/main.go b/main.go index 7e3ad0b..0a2b4fa 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "time" ) +// Execute executes the job func Execute() error { if len(os.Args) != 2 { return fmt.Errorf("Missing mandatory .csv parameter") @@ -84,7 +85,6 @@ func Execute() error { time.Sleep(waitUntil.Sub(time.Now())) } - return nil } func main() {