github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/integration/share/share_test.go (about)

     1  /*
     2   * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package testing
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  type Res struct {
    31  	Code    string            `json:"code"`
    32  	Message string            `json:"message"`
    33  	Uids    map[string]string `json:"uids"`
    34  }
    35  
    36  type Share struct {
    37  	Share     string `json:"_share_"`
    38  	ShareHash string `json:"_share_hash_"`
    39  }
    40  
    41  type Res2 struct {
    42  	Root []Share `json:"me"`
    43  }
    44  
    45  type Res3 struct {
    46  	Root Res2 `json:"data"`
    47  }
    48  
    49  func DONOTRUNTestShare(t *testing.T) {
    50  	dgraphServer := "http://localhost:8081/share?debug=true"
    51  	client := new(http.Client)
    52  	q := `%7B%0A%20%20me(func:%20eq(name,%20%22Steven%20Spielberg%22))%20%7B%0A%09%09name%0A%09%09director.film%20%7B%0A%09%09%09name%0A%09%09%7D%0A%20%20%7D%0A%7D`
    53  	req, err := http.NewRequest("POST", dgraphServer, strings.NewReader(q))
    54  	require.NoError(t, err)
    55  	resp, err := client.Do(req)
    56  	require.NoError(t, err)
    57  	b, err := ioutil.ReadAll(resp.Body)
    58  	require.NoError(t, err)
    59  
    60  	var r Res
    61  	json.Unmarshal(b, &r)
    62  	require.NotNil(t, r.Uids["share"])
    63  
    64  	q2 := fmt.Sprintf(`
    65  	{
    66  		me(func: uid(%s)) {
    67  			_share_
    68  			_share_hash_
    69  		}
    70  	}
    71  	`, r.Uids["share"])
    72  
    73  	dgraphServer = "http://localhost:8081/query"
    74  	req, err = http.NewRequest("POST", dgraphServer, strings.NewReader(q2))
    75  	require.NoError(t, err)
    76  	resp, err = client.Do(req)
    77  	require.NoError(t, err)
    78  	b, err = ioutil.ReadAll(resp.Body)
    79  	require.NoError(t, err)
    80  
    81  	var r3 Res3
    82  	json.Unmarshal(b, &r3)
    83  	r2 := r3.Root
    84  	require.Equal(t, 1, len(r2.Root))
    85  	require.Equal(t, q, r2.Root[0].Share)
    86  	require.NotNil(t, r2.Root[0].ShareHash)
    87  }