From 652ae13e2bfc743bf0e003a80a70b98d41200fe0 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 5 Jan 2025 22:33:27 +0100 Subject: [PATCH] improve testing --- crossref/reader_test.go | 2 +- csl/writer_test.go | 2 +- datacite/reader_test.go | 10 +- .../testdata/writer/10.5061_dryad.8515.json | 130 ++++++++++++++++++ datacite/writer_test.go | 69 ++++++++++ inveniordm/writer_test.go | 2 +- jsonfeed/reader_test.go | 2 +- schemaorg/reader.go | 10 +- schemaorg/reader_test.go | 5 +- .../testdata/writer/10.5061_dryad.8515.json | 2 +- schemaorg/writer_test.go | 9 +- 11 files changed, 227 insertions(+), 16 deletions(-) create mode 100644 datacite/testdata/writer/10.5061_dryad.8515.json create mode 100644 datacite/writer_test.go diff --git a/crossref/reader_test.go b/crossref/reader_test.go index 391e187..3adb56e 100644 --- a/crossref/reader_test.go +++ b/crossref/reader_test.go @@ -84,7 +84,7 @@ func TestFetch(t *testing.T) { t.Fatal(err) } - want := commonmeta.Data{} + var want commonmeta.Data err = json.Unmarshal(bytes, &want) if err != nil { t.Fatal(err) diff --git a/csl/writer_test.go b/csl/writer_test.go index bda1acf..ea01d0a 100644 --- a/csl/writer_test.go +++ b/csl/writer_test.go @@ -55,7 +55,7 @@ func TestWrite(t *testing.T) { t.Fatal(err) } - want := csl.CSL{} + var want csl.CSL err = json.Unmarshal(bytes, &want) if err != nil { t.Fatal(err) diff --git a/datacite/reader_test.go b/datacite/reader_test.go index 245f6ba..11fa7da 100644 --- a/datacite/reader_test.go +++ b/datacite/reader_test.go @@ -80,11 +80,11 @@ func TestFetch(t *testing.T) { if err != nil { t.Fatal(err) } - want := commonmeta.Data{} - _ = json.Unmarshal(content, &want) - // if err != nil { - // t.Fatal(err) - // } + var want commonmeta.Data + err = json.Unmarshal(content, &want) + if err != nil { + t.Fatal(err) + } if diff := cmp.Diff(want, got); diff != "" { t.Errorf("FetchDatacite(%s) mismatch (-want +got):\n%s", tc.id, diff) } diff --git a/datacite/testdata/writer/10.5061_dryad.8515.json b/datacite/testdata/writer/10.5061_dryad.8515.json new file mode 100644 index 0000000..25653ce --- /dev/null +++ b/datacite/testdata/writer/10.5061_dryad.8515.json @@ -0,0 +1,130 @@ +{ + "doi": "10.5061/dryad.8515", + "creators": [ + { + "name": "Benjamin, Ollomo", + "givenName": "Benjamin", + "familyName": "Ollomo", + "nameType": "Personal", + "affiliation": [ + "Centre International de Recherches Médicales de Franceville" + ] + }, + { + "name": "Patrick, Durand", + "givenName": "Patrick", + "familyName": "Durand", + "nameType": "Personal", + "affiliation": ["French National Centre for Scientific Research"] + }, + { + "name": "Franck, Prugnolle", + "givenName": "Franck", + "familyName": "Prugnolle", + "nameType": "Personal", + "affiliation": ["French National Centre for Scientific Research"] + }, + { + "name": "Emmanuel J. P., Douzery", + "givenName": "Emmanuel J. P.", + "familyName": "Douzery", + "nameType": "Personal" + }, + { + "name": "Céline, Arnathau", + "givenName": "Céline", + "familyName": "Arnathau", + "nameType": "Personal", + "affiliation": ["French National Centre for Scientific Research"] + }, + { + "name": "Dieudonné, Nkoghe", + "givenName": "Dieudonné", + "familyName": "Nkoghe", + "nameType": "Personal", + "affiliation": [ + "Centre International de Recherches Médicales de Franceville" + ] + }, + { + "name": "Eric, Leroy", + "givenName": "Eric", + "familyName": "Leroy", + "nameType": "Personal", + "affiliation": [ + "Centre International de Recherches Médicales de Franceville" + ] + }, + { + "name": "François, Renaud", + "givenName": "François", + "familyName": "Renaud", + "nameType": "Personal", + "affiliation": ["French National Centre for Scientific Research"] + } + ], + "publisher": { + "name": "Dryad" + }, + "container": {}, + "publicationYear": 2011, + "titles": [ + { + "title": "Data from: A new malaria agent in African hominids." + } + ], + "url": "https://datadryad.org/stash/dataset/doi:10.5061/dryad.8515", + "subjects": [ + { + "subject": "Plasmodium" + }, + { + "subject": "Malaria" + }, + { + "subject": "mitochondrial genome" + }, + { + "subject": "Parasites" + } + ], + "dates": [ + { + "date": "2011-02-01T17:22:41Z", + "dateType": "Issued" + } + ], + "language": "en", + "types": { + "resourceTypeGeneral": "Dataset", + "ris": "DATA", + "citeproc": "dataset", + "schemaOrg": "Dataset" + }, + "version": "1", + "rightsList": [ + { + "rightsUri": "https://creativecommons.org/publicdomain/zero/1.0/legalcode", + "schemeUri": "https://spdx.org/licenses/", + "rightsIdentifier": "cc0-1.0", + "rightsIdentifierScheme": "SPDX" + } + ], + "descriptions": [ + { + "description": "Plasmodium falciparum is the major human malaria agent responsible for 200\n to 300 million infections and one to three million deaths annually, mainly\n among African infants. The origin and evolution of this pathogen within\n the human lineage is still unresolved. A single species, P. reichenowi,\n which infects chimpanzees, is known to be a close sister lineage of P.\n falciparum. Here we report the discovery of a new Plasmodium species\n infecting Hominids. This new species has been isolated in two chimpanzees\n (Pan troglodytes) kept as pets by villagers in Gabon (Africa). Analysis of\n its complete mitochondrial genome (5529 nucleotides including Cyt b, Cox I\n and Cox III genes) reveals an older divergence of this lineage from the\n clade that includes P. falciparum and P. reichenowi (approximately 21+/-9\n Myrs ago using Bayesian methods and considering that the divergence\n between P. falciparum and P. reichenowi occurred 4 to 7 million years ago\n as generally considered in the literature). This time frame would be\n congruent with the radiation of hominoids, suggesting that this Plasmodium\n lineage might have been present in early hominoids and that they may both\n have experienced a simultaneous diversification. Investigation of the\n nuclear genome of this new species will further the understanding of the\n genetic adaptations of P. falciparum to humans. The risk of transfer and\n emergence of this new species in humans must be now seriously considered\n given that it was found in two chimpanzees living in contact with humans\n and its close relatedness to the most virulent agent of malaria.", + "descriptionType": "Abstract" + }, + { + "description": "Ollomo_PLoSPathog_2009Nucleotide alignment concatenation of 4\n mitochondrial genes for 17 Plasmodium species and one\n outgroup.Ollomo_PLoSPathog_2009_PHYMLMaximum likelihood tree inferred from\n the 4-gene concatenation using PHYML.", + "descriptionType": "Other" + } + ], + "geoLocations": [ + { + "geoLocationPoint": {}, + "geoLocationBox": {}, + "geoLocationPlace": "Africa" + } + ] +} diff --git a/datacite/writer_test.go b/datacite/writer_test.go new file mode 100644 index 0000000..d9c9f3a --- /dev/null +++ b/datacite/writer_test.go @@ -0,0 +1,69 @@ +package datacite_test + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/front-matter/commonmeta/commonmeta" + "github.com/front-matter/commonmeta/crossref" + "github.com/front-matter/commonmeta/datacite" + "github.com/front-matter/commonmeta/doiutils" + "github.com/google/go-cmp/cmp" +) + +func TestWrite(t *testing.T) { + t.Parallel() + + type testCase struct { + name string + id string + from string + } + + testCases := []testCase{ + //{name: "journal article", id: "https://doi.org/10.7554/elife.01567", from: "crossref"}, + //{name: "preprint", id: "https://doi.org/10.1101/097196", from: "crossref"}, + {name: "dataset", id: "https://doi.org/10.5061/dryad.8515", from: "datacite"}, + } + + for _, tc := range testCases { + var data commonmeta.Data + var err error + if tc.from == "crossref" { + data, err = crossref.Fetch(tc.id) + } else if tc.from == "datacite" { + data, err = datacite.Fetch(tc.id) + } + if err != nil { + t.Errorf("Crossref Fetch (%v): error %v", tc.id, err) + } + + got, err := datacite.Write(data) + if err != nil { + t.Errorf("Datacite Write (%v): error %v", tc.id, err) + } + // read json file from testdata folder and convert to Data struct + doi, ok := doiutils.ValidateDOI(tc.id) + if !ok { + t.Fatal(errors.New("invalid doi")) + } + filename := strings.ReplaceAll(doi, "/", "_") + ".json" + filepath := filepath.Join("testdata/writer", filename) + content, err := os.ReadFile(filepath) + if err != nil { + t.Fatal(err) + } + var want datacite.Datacite + err = json.Unmarshal(content, &want) + if err != nil { + t.Fatal(err) + } + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Schemaorg Fetch (%v): -want +got %s", tc.id, diff) + } + } +} diff --git a/inveniordm/writer_test.go b/inveniordm/writer_test.go index e88f344..b4ad58f 100644 --- a/inveniordm/writer_test.go +++ b/inveniordm/writer_test.go @@ -55,7 +55,7 @@ func TestWrite(t *testing.T) { t.Fatal(err) } - want := inveniordm.Inveniordm{} + var want inveniordm.Inveniordm err = json.Unmarshal(bytes, &want) if err != nil { t.Fatal(err) diff --git a/jsonfeed/reader_test.go b/jsonfeed/reader_test.go index 78c57ce..f3539a4 100644 --- a/jsonfeed/reader_test.go +++ b/jsonfeed/reader_test.go @@ -72,7 +72,7 @@ func TestFetch(t *testing.T) { t.Fatal(err) } - want := commonmeta.Data{} + var want commonmeta.Data err = json.Unmarshal(bytes, &want) if err != nil { t.Fatal(err) diff --git a/schemaorg/reader.go b/schemaorg/reader.go index bba42f6..bac1383 100644 --- a/schemaorg/reader.go +++ b/schemaorg/reader.go @@ -376,7 +376,10 @@ func Read(content Content) (commonmeta.Data, error) { var contributors []Contributor err := json.Unmarshal(content.Author, &contributor) if err != nil { - _ = json.Unmarshal(content.Author, &contributors) + err = json.Unmarshal(content.Author, &contributors) + if err != nil { + fmt.Println(err) + } } if len(contributors) == 0 { contributors = append(contributors, contributor) @@ -414,7 +417,10 @@ func Read(content Content) (commonmeta.Data, error) { var identifiers []string err = json.Unmarshal(content.Identifier, &identifier) if err != nil { - _ = json.Unmarshal(content.Identifier, &identifiers) + err = json.Unmarshal(content.Identifier, &identifiers) + if err != nil { + fmt.Println(err) + } } if identifier != "" { identifiers = append(identifiers, identifier) diff --git a/schemaorg/reader_test.go b/schemaorg/reader_test.go index d30840b..5420d21 100644 --- a/schemaorg/reader_test.go +++ b/schemaorg/reader_test.go @@ -77,7 +77,10 @@ func TestFetch(t *testing.T) { t.Fatal(err) } var want commonmeta.Data - _ = json.Unmarshal(content, &want) + err = json.Unmarshal(content, &want) + if err != nil { + t.Fatal(err) + } if diff := cmp.Diff(want, got); diff != "" { t.Errorf("Schemaorg Fetch (%v): -want +got %s", tc.id, diff) } diff --git a/schemaorg/testdata/writer/10.5061_dryad.8515.json b/schemaorg/testdata/writer/10.5061_dryad.8515.json index 343898c..bd4b416 100644 --- a/schemaorg/testdata/writer/10.5061_dryad.8515.json +++ b/schemaorg/testdata/writer/10.5061_dryad.8515.json @@ -87,7 +87,7 @@ "description": "Plasmodium falciparum is the major human malaria agent responsible for 200\n to 300 million infections and one to three million deaths annually, mainly\n among African infants. The origin and evolution of this pathogen within\n the human lineage is still unresolved. A single species, P. reichenowi,\n which infects chimpanzees, is known to be a close sister lineage of P.\n falciparum. Here we report the discovery of a new Plasmodium species\n infecting Hominids. This new species has been isolated in two chimpanzees\n (Pan troglodytes) kept as pets by villagers in Gabon (Africa). Analysis of\n its complete mitochondrial genome (5529 nucleotides including Cyt b, Cox I\n and Cox III genes) reveals an older divergence of this lineage from the\n clade that includes P. falciparum and P. reichenowi (approximately 21+/-9\n Myrs ago using Bayesian methods and considering that the divergence\n between P. falciparum and P. reichenowi occurred 4 to 7 million years ago\n as generally considered in the literature). This time frame would be\n congruent with the radiation of hominoids, suggesting that this Plasmodium\n lineage might have been present in early hominoids and that they may both\n have experienced a simultaneous diversification. Investigation of the\n nuclear genome of this new species will further the understanding of the\n genetic adaptations of P. falciparum to humans. The risk of transfer and\n emergence of this new species in humans must be now seriously considered\n given that it was found in two chimpanzees living in contact with humans\n and its close relatedness to the most virulent agent of malaria.", "license": "https://creativecommons.org/publicdomain/zero/1.0/legalcode", "version": "1", - "keywords": ["Plasmodium", "Malaria", "mitochondrial genome", "Parasites"], + "keywords": "Plasmodium, Malaria, mitochondrial genome, Parasites", "inLanguage": "English", "datePublished": "2011-02-01T17:22:41Z", "publisher": { "@type": "Organization", "name": "Dryad" }, diff --git a/schemaorg/writer_test.go b/schemaorg/writer_test.go index 4a2be62..7328e07 100644 --- a/schemaorg/writer_test.go +++ b/schemaorg/writer_test.go @@ -26,9 +26,9 @@ func TestWrite(t *testing.T) { } testCases := []testCase{ - {name: "journal article", id: "https://doi.org/10.7554/elife.01567", from: "crossref"}, + //{name: "journal article", id: "https://doi.org/10.7554/elife.01567", from: "crossref"}, //{name: "preprint", id: "https://doi.org/10.1101/097196", from: "crossref"}, - //{name: "dataset", id: "https://doi.org/10.5061/dryad.8515", from: "datacite"}, + {name: "dataset", id: "https://doi.org/10.5061/dryad.8515", from: "datacite"}, } for _, tc := range testCases { @@ -59,7 +59,10 @@ func TestWrite(t *testing.T) { t.Fatal(err) } var want schemaorg.Schemaorg - _ = json.Unmarshal(content, &want) + err = json.Unmarshal(content, &want) + if err != nil { + t.Fatal(err) + } if diff := cmp.Diff(want, got); diff != "" { t.Errorf("Schemaorg Fetch (%v): -want +got %s", tc.id, diff) }