aboutsummaryrefslogtreecommitdiffstats
path: root/skate/schema_test.go
blob: c1cec350196d325c57ff868fe0b9f1db9ac73f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package skate

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

	"github.com/nsf/jsondiff"
)

// XXX: Work on JSON directly, as structs can get unwieldy.
func TestOpenLibraryToRelease(t *testing.T) {
	var cases = []struct {
		work    OpenLibraryWork
		release Release
		err     error
	}{
		{
			work: OpenLibraryWork{},
			release: Release{
				ExtIDs: struct {
					Arxiv       string   `json:"arxiv,omitempty"`
					Core        string   `json:"core,omitempty"`
					DOI         string   `json:"doi,omitempty"`
					ISBN        []string `json:"isbn,omitempty"`
					Jstor       string   `json:"jstor,omitempty"`
					OLID        string   `json:"olid,omitempty"`
					PMCID       string   `json:"pmcid,omitempty"`
					PMID        string   `json:"pmid,omitempty"`
					WikidataQID string   `json:"wikidata_qid,omitempty"`
				}{},
			},
			err: nil,
		},
		{
			work: OpenLibraryWork{
				Title: "Hello World",
				Isbn: []string{
					"2844273386",
					"9782844273383",
				},
			},
			release: Release{
				ExtIDs: struct {
					Arxiv       string   `json:"arxiv,omitempty"`
					Core        string   `json:"core,omitempty"`
					DOI         string   `json:"doi,omitempty"`
					ISBN        []string `json:"isbn,omitempty"`
					Jstor       string   `json:"jstor,omitempty"`
					OLID        string   `json:"olid,omitempty"`
					PMCID       string   `json:"pmcid,omitempty"`
					PMID        string   `json:"pmid,omitempty"`
					WikidataQID string   `json:"wikidata_qid,omitempty"`
				}{
					ISBN: []string{"9782844273383"},
				},
				Title: "Hello World",
			},
			err: nil,
		},
		{
			work: OpenLibraryWork{
				Key:   "/works/OL10000003W",
				Title: "Hello World",
				Isbn: []string{
					"2844273386",
					"9782844273383",
				},
				HasFulltext: false,
			},
			release: Release{
				ExtIDs: struct {
					Arxiv       string   `json:"arxiv,omitempty"`
					Core        string   `json:"core,omitempty"`
					DOI         string   `json:"doi,omitempty"`
					ISBN        []string `json:"isbn,omitempty"`
					Jstor       string   `json:"jstor,omitempty"`
					OLID        string   `json:"olid,omitempty"`
					PMCID       string   `json:"pmcid,omitempty"`
					PMID        string   `json:"pmid,omitempty"`
					WikidataQID string   `json:"wikidata_qid,omitempty"`
				}{
					ISBN: []string{"9782844273383"},
					OLID: "OL10000003W",
				},
				Title: "Hello World",
				Extra: struct {
					ContainerName string      `json:"container_name,omitempty"`
					SubtitleValue interface{} `json:"subtitle,omitempty"` // []str or str
					Crossref      struct {
						Type string `json:"type,omitempty"`
					} `json:"crossref,omitempty"`
					DataCite struct {
						MetadataVersion int                `json:"metadataVersion,omitempty"`
						Relations       []DataCiteRelation `json:"relations,omitempty"`
					} `json:"datacite,omitempty"`
					Skate struct {
						// Mark as converted (e.g. by setting status to "ref")
						Status string `json:"status,omitempty"`
						// Carry the ref index and key around.
						Ref struct {
							Index   int64  `json:"index,omitempty"`
							Key     string `json:"key,omitempty"`
							Locator string `json:"locator,omitempty"`
						} `json:"ref,omitempty"`
						ResearchGate struct {
							URL string `json:"url,omitempty"`
						} `json:"rg,omitempty"`
					} `json:"skate,omitempty"`
					OpenLibrary struct {
						HasFulltext bool `json:"has_fulltext,omitempty"`
					} `json:"ol,omitempty"`
				}{
					OpenLibrary: struct {
						HasFulltext bool `json:"has_fulltext,omitempty"`
					}{
						HasFulltext: false,
					},
				},
			},
			err: nil,
		},
	}
	for _, c := range cases {
		r, err := OpenLibraryToRelease(&c.work)
		if err != nil {
			t.Fatalf("got %v, want %v", err, c.err)
		}
		if !reflect.DeepEqual(r, &c.release) {
			t.Fatalf(prettyStructDiff(r, &c.release))
		}
	}
}

func prettyStructDiff(s, t interface{}) string {
	b, err := json.MarshalIndent(s, "", "    ")
	if err != nil {
		return fmt.Sprintf("diff failed: %v", err)
	}
	c, err := json.MarshalIndent(t, "", "    ")
	if err != nil {
		return fmt.Sprintf("diff failed: %v", err)
	}
	opts := jsondiff.DefaultConsoleOptions()
	_, d := jsondiff.Compare(b, c, &opts)
	return d
}