예제 #1
0
파일: sparql_test.go 프로젝트: knakk/sparql
func TestSolutions(t *testing.T) {
	b := bytes.NewBufferString(testResults)
	r, err := ParseJSON(b)
	if err != nil {
		t.Fatal(err)
	}

	s := r.Solutions()

	blankR1, _ := rdf.NewBlank("r1")
	iriAlice, _ := rdf.NewIRI("http://work.example.org/alice/")
	litAlice, _ := rdf.NewLiteral("Alice")
	litBob, _ := rdf.NewLangLiteral("Bob", "en")
	xsdInteger, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#integer")
	xsdFloat, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#float")
	xsdBoolean, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#boolean")
	xsdDateTime, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#dateTime")

	var tests = []struct {
		got  rdf.Term
		want rdf.Term
	}{
		{s[0]["x"], blankR1},
		{s[0]["hpage"], iriAlice},
		{s[0]["name"], litAlice},
		{s[1]["name"], litBob},
		{s[0]["age"], rdf.NewTypedLiteral("17", xsdInteger)},
		{s[0]["score"], rdf.NewTypedLiteral("0.2", xsdFloat)},
		{s[0]["z"], rdf.NewTypedLiteral("true", xsdBoolean)},
		{s[1]["z"], rdf.NewTypedLiteral("false", xsdBoolean)},
		{s[0]["updated"], rdf.NewTypedLiteral("2014-07-21T04:00:40+02:00", xsdDateTime)},
	}

	for _, tt := range tests {
		if !rdf.TermsEqual(tt.got, tt.want) {
			t.Errorf("Got \"%v\", want \"%v\"", tt.got, tt.want)
		}
	}
}
예제 #2
0
파일: sparql.go 프로젝트: knakk/sparql
// termFromJSON converts a SPARQL json result binding into a rdf.Term. Any
// parsing errors on typed-literal will result in a xsd:string-typed RDF term.
// TODO move this functionality to package rdf?
func termFromJSON(b binding) (rdf.Term, error) {
	switch b.Type {
	case "bnode":
		return rdf.NewBlank(b.Value)
	case "uri":
		return rdf.NewIRI(b.Value)
	case "literal":
		// Untyped literals are typed as xsd:string
		if b.Lang != "" {
			return rdf.NewLangLiteral(b.Value, b.Lang)
		}
		return rdf.NewTypedLiteral(b.Value, xsdString), nil
	case "typed-literal":
		iri, err := rdf.NewIRI(b.DataType)
		if err != nil {
			return nil, err
		}
		return rdf.NewTypedLiteral(b.Value, iri), nil
	default:
		return nil, errors.New("unknown term type")
	}
}