github.phpd.cn/dgraph-io/dgraph@v1.1.0/dgraph/cmd/live/load-uids/load_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 main
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"os"
    23  	"path"
    24  	"runtime"
    25  	"testing"
    26  
    27  	"github.com/dgraph-io/dgo"
    28  	"github.com/dgraph-io/dgo/protos/api"
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"github.com/dgraph-io/dgraph/testutil"
    32  	"github.com/dgraph-io/dgraph/x"
    33  )
    34  
    35  var alphaService = testutil.SockAddr
    36  var zeroService = testutil.SockAddrZero
    37  
    38  var (
    39  	testDataDir string
    40  	dg          *dgo.Dgraph
    41  	tmpDir      string
    42  )
    43  
    44  func checkDifferentUid(t *testing.T, wantMap, gotMap map[string]interface{}) {
    45  	require.NotEqual(t, gotMap["q"].([]interface{})[0].(map[string]interface{})["uid"],
    46  		wantMap["q"].([]interface{})[0].(map[string]interface{})["uid"],
    47  		"new uid was assigned")
    48  
    49  	gotMap["q"].([]interface{})[0].(map[string]interface{})["uid"] = -1
    50  	wantMap["q"].([]interface{})[0].(map[string]interface{})["uid"] = -1
    51  	testutil.CompareJSONMaps(t, wantMap, gotMap)
    52  }
    53  
    54  func checkLoadedData(t *testing.T, newUids bool) {
    55  	resp, err := dg.NewTxn().Query(context.Background(), `
    56  		{
    57  			q(func: anyofterms(name, "Homer")) {
    58  				uid
    59  				name
    60  				age
    61  				role
    62  			}
    63  		}
    64  	`)
    65  	require.NoError(t, err)
    66  
    67  	gotMap := testutil.UnmarshalJSON(t, string(resp.GetJson()))
    68  	wantMap := testutil.UnmarshalJSON(t, `
    69  		{
    70  		    "q": [
    71  					{
    72  					"uid": "0x2001",
    73  					"name": "Homer",
    74  					"age": 38,
    75  					"role": "father"
    76  			    }
    77  			]
    78  		}
    79  	`)
    80  	if newUids {
    81  		checkDifferentUid(t, wantMap, gotMap)
    82  	} else {
    83  		testutil.CompareJSONMaps(t, wantMap, gotMap)
    84  	}
    85  
    86  	resp, err = dg.NewTxn().Query(context.Background(), `
    87  		{
    88  			q(func: anyofterms(name, "Maggie")) {
    89  				uid
    90  				name
    91  				role
    92  				carries
    93  			}
    94  		}
    95  	`)
    96  	require.NoError(t, err)
    97  
    98  	gotMap = testutil.UnmarshalJSON(t, string(resp.GetJson()))
    99  	wantMap = testutil.UnmarshalJSON(t, `
   100  		{
   101  		    "q": [
   102  				{
   103  					"uid": "0x3003",
   104  					"name": "Maggie",
   105  					"role": "daughter",
   106  					"carries": "pacifier"
   107  			    }
   108  			]
   109  		}
   110  	`)
   111  	if newUids {
   112  		checkDifferentUid(t, wantMap, gotMap)
   113  	} else {
   114  		testutil.CompareJSONMaps(t, wantMap, gotMap)
   115  	}
   116  }
   117  
   118  func TestLiveLoadJsonUidKeep(t *testing.T) {
   119  	testutil.DropAll(t, dg)
   120  
   121  	pipeline := [][]string{
   122  		{os.ExpandEnv("$GOPATH/bin/dgraph"), "live",
   123  			"--schema", testDataDir + "/family.schema", "--files", testDataDir + "/family.json",
   124  			"--alpha", alphaService, "--zero", zeroService},
   125  	}
   126  	err := testutil.Pipeline(pipeline)
   127  	require.NoError(t, err, "live loading JSON file exited with error")
   128  
   129  	checkLoadedData(t, false)
   130  }
   131  
   132  func TestLiveLoadJsonUidDiscard(t *testing.T) {
   133  	testutil.DropAll(t, dg)
   134  
   135  	pipeline := [][]string{
   136  		{os.ExpandEnv("$GOPATH/bin/dgraph"), "live", "--new_uids",
   137  			"--schema", testDataDir + "/family.schema", "--files", testDataDir + "/family.json",
   138  			"--alpha", alphaService, "--zero", zeroService},
   139  	}
   140  	err := testutil.Pipeline(pipeline)
   141  	require.NoError(t, err, "live loading JSON file exited with error")
   142  
   143  	checkLoadedData(t, true)
   144  }
   145  
   146  func TestLiveLoadRdfUidKeep(t *testing.T) {
   147  	testutil.DropAll(t, dg)
   148  
   149  	pipeline := [][]string{
   150  		{os.ExpandEnv("$GOPATH/bin/dgraph"), "live",
   151  			"--schema", testDataDir + "/family.schema", "--files", testDataDir + "/family.rdf",
   152  			"--alpha", alphaService, "--zero", zeroService},
   153  	}
   154  	err := testutil.Pipeline(pipeline)
   155  	require.NoError(t, err, "live loading JSON file exited with error")
   156  
   157  	checkLoadedData(t, false)
   158  }
   159  
   160  func TestLiveLoadRdfUidDiscard(t *testing.T) {
   161  	testutil.DropAll(t, dg)
   162  
   163  	pipeline := [][]string{
   164  		{os.ExpandEnv("$GOPATH/bin/dgraph"), "live", "--new_uids",
   165  			"--schema", testDataDir + "/family.schema", "--files", testDataDir + "/family.rdf",
   166  			"--alpha", alphaService, "--zero", zeroService},
   167  	}
   168  	err := testutil.Pipeline(pipeline)
   169  	require.NoError(t, err, "live loading JSON file exited with error")
   170  
   171  	checkLoadedData(t, true)
   172  }
   173  
   174  func TestMain(m *testing.M) {
   175  	_, thisFile, _, _ := runtime.Caller(0)
   176  	testDataDir = path.Dir(thisFile)
   177  
   178  	dg = testutil.DgraphClientWithGroot(testutil.SockAddr)
   179  	x.Check(dg.Alter(
   180  		context.Background(), &api.Operation{DropAll: true}))
   181  
   182  	// Try to create any files in a dedicated temp directory that gets cleaned up
   183  	// instead of all over /tmp or the working directory.
   184  	tmpDir, err := ioutil.TempDir("", "test.tmp-")
   185  	x.Check(err)
   186  	os.Chdir(tmpDir)
   187  	defer os.RemoveAll(tmpDir)
   188  
   189  	os.Exit(m.Run())
   190  }