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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package f3
import (
"path/filepath"
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/stretchr/testify/assert"
)
func TestStoreLoad(t *testing.T) {
tmpDir := t.TempDir()
type S struct {
A int
B string
}
original := S{A: 1, B: "B"}
p := filepath.Join(tmpDir, "s.json")
assert.NoError(t, Store(p, original))
var loaded S
assert.NoError(t, Load(p, &loaded, false))
assert.EqualValues(t, original, loaded)
}
func TestF3_CI(t *testing.T) {
var ci CI
err := Load("file_format_testdata/ci/good.json", &ci, true)
assert.NoError(t, err)
err = Load("file_format_testdata/ci/bad.json", &ci, true)
assert.ErrorContains(t, err, "'/index': value must be one of")
}
func TestF3_User(t *testing.T) {
var user User
err := Load("file_format_testdata/user/good.json", &user, true)
assert.NoError(t, err)
err = Load("file_format_testdata/user/bad.json", &user, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Organization(t *testing.T) {
var organization Organization
err := Load("file_format_testdata/organization/good.json", &organization, true)
assert.NoError(t, err)
err = Load("file_format_testdata/organization/bad.json", &organization, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Project(t *testing.T) {
var project Project
err := Load("file_format_testdata/project/good.json", &project, true)
assert.NoError(t, err)
err = Load("file_format_testdata/project/bad.json", &project, true)
assert.ErrorContains(t, err, "'/stars': got string, want number")
}
func TestF3_Issue(t *testing.T) {
var issue Issue
err := Load("file_format_testdata/issue/good.json", &issue, true)
assert.NoError(t, err)
err = Load("file_format_testdata/issue/bad.json", &issue, true)
assert.ErrorContains(t, err, "missing property 'index'")
}
func TestF3_PullRequest(t *testing.T) {
var pullRequest PullRequest
err := Load("file_format_testdata/pullrequest/good.json", &pullRequest, true)
assert.NoError(t, err)
err = Load("file_format_testdata/pullrequest/bad.json", &pullRequest, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Release(t *testing.T) {
var release Release
err := Load("file_format_testdata/release/good.json", &release, true)
assert.NoError(t, err)
err = Load("file_format_testdata/release/bad.json", &release, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Attachment(t *testing.T) {
var attachment Attachment
err := Load("file_format_testdata/attachment/good.json", &attachment, true)
assert.NoError(t, err)
err = Load("file_format_testdata/attachment/bad.json", &attachment, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Comment(t *testing.T) {
var comment Comment
err := Load("file_format_testdata/comment/good.json", &comment, true)
assert.NoError(t, err)
err = Load("file_format_testdata/comment/bad.json", &comment, true)
assert.ErrorContains(t, err, "'/created': 'AAAAAAAAA' is not valid date-time")
}
func TestF3_Label(t *testing.T) {
var label Label
err := Load("file_format_testdata/label/good.json", &label, true)
assert.NoError(t, err)
err = Load("file_format_testdata/label/bad.json", &label, true)
assert.ErrorContains(t, err, "'/exclusive': got string, want boolean")
}
func TestF3_Milestone(t *testing.T) {
var milestone Milestone
err := Load("file_format_testdata/milestone/good.json", &milestone, true)
assert.NoError(t, err)
err = Load("file_format_testdata/milestone/bad.json", &milestone, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Review(t *testing.T) {
var review Review
err := Load("file_format_testdata/review/good.json", &review, true)
assert.NoError(t, err)
err = Load("file_format_testdata/review/bad.json", &review, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Reaction(t *testing.T) {
var reaction Reaction
err := Load("file_format_testdata/reaction/good.json", &reaction, true)
assert.NoError(t, err)
err = Load("file_format_testdata/reaction/bad.json", &reaction, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Repository(t *testing.T) {
var repository Repository
err := Load("file_format_testdata/repository/good.json", &repository, true)
assert.NoError(t, err)
err = Load("file_format_testdata/repository/bad.json", &repository, true)
assert.ErrorContains(t, err, "missing property 'name'")
}
func TestF3_ReviewComment(t *testing.T) {
var reviewComment ReviewComment
err := Load("file_format_testdata/reviewcomment/good.json", &reviewComment, true)
assert.NoError(t, err)
err = Load("file_format_testdata/reviewcomment/bad.json", &reviewComment, true)
assert.ErrorContains(t, err, "missing properties 'index'")
}
func TestF3_Topic(t *testing.T) {
var topic Topic
err := Load("file_format_testdata/topic.json", &topic, true)
assert.NoError(t, err)
}
func TestF3_ValidationFail(t *testing.T) {
var issue Issue
err := Load("file_format_testdata/issue/bad.json", &issue, true)
if _, ok := err.(*jsonschema.ValidationError); ok {
errors := strings.Split(err.(*jsonschema.ValidationError).GoString(), "\n")
assert.Contains(t, errors[1], "missing property")
} else {
t.Fatalf("got: type %T with value %s, want: *jsonschema.ValidationError", err, err)
}
}
|