Skip to content

Commit

Permalink
improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Jan 5, 2025
1 parent 2947ff9 commit 652ae13
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crossref/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion csl/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions datacite/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
130 changes: 130 additions & 0 deletions datacite/testdata/writer/10.5061_dryad.8515.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
69 changes: 69 additions & 0 deletions datacite/writer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion inveniordm/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion jsonfeed/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions schemaorg/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion schemaorg/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion schemaorg/testdata/writer/10.5061_dryad.8515.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
9 changes: 6 additions & 3 deletions schemaorg/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 652ae13

Please sign in to comment.