github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/dgraph/cmd/live/load-json/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 live 18 19 import ( 20 "context" 21 "io/ioutil" 22 "os" 23 "path" 24 "runtime" 25 "strings" 26 "testing" 27 28 "github.com/dgraph-io/dgo" 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 // Just check the first and last entries and assumes everything in between is okay. 45 func checkLoadedData(t *testing.T) { 46 resp, err := dg.NewTxn().Query(context.Background(), ` 47 { 48 q(func: anyofterms(name, "Homer")) { 49 name 50 age 51 role @facets(gender,generation) 52 role@es 53 } 54 } 55 `) 56 require.NoError(t, err) 57 testutil.CompareJSON(t, ` 58 { 59 "q": [ 60 { 61 "name": "Homer", 62 "age": 38, 63 "role": "father", 64 "role@es": "padre", 65 "role|gender": "male" 66 } 67 ] 68 } 69 `, string(resp.GetJson())) 70 71 resp, err = dg.NewTxn().Query(context.Background(), ` 72 { 73 q(func: anyofterms(name, "Maggie")) { 74 name 75 role @facets(gender,generation) 76 role@es 77 carries 78 } 79 } 80 `) 81 require.NoError(t, err) 82 testutil.CompareJSON(t, ` 83 { 84 "q": [ 85 { 86 "name": "Maggie", 87 "role": "daughter", 88 "role@es": "hija", 89 "carries": "pacifier", 90 "role|gender": "female", 91 "role|generation": 3 92 } 93 ] 94 } 95 `, string(resp.GetJson())) 96 } 97 98 func TestLiveLoadJSONFileEmpty(t *testing.T) { 99 testutil.DropAll(t, dg) 100 101 pipeline := [][]string{ 102 {"echo", "[]"}, 103 {os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 104 "--schema", testDataDir + "/family.schema", "--files", "/dev/stdin", 105 "--alpha", alphaService, "--zero", zeroService}, 106 } 107 err := testutil.Pipeline(pipeline) 108 require.NoError(t, err, "live loading JSON file ran successfully") 109 } 110 111 func TestLiveLoadJSONFile(t *testing.T) { 112 testutil.DropAll(t, dg) 113 114 pipeline := [][]string{ 115 {os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 116 "--schema", testDataDir + "/family.schema", "--files", testDataDir + "/family.json", 117 "--alpha", alphaService, "--zero", zeroService}, 118 } 119 err := testutil.Pipeline(pipeline) 120 require.NoError(t, err, "live loading JSON file exited with error") 121 122 checkLoadedData(t) 123 } 124 125 func TestLiveLoadJSONCompressedStream(t *testing.T) { 126 testutil.DropAll(t, dg) 127 128 pipeline := [][]string{ 129 {"gzip", "-c", testDataDir + "/family.json"}, 130 {os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 131 "--schema", testDataDir + "/family.schema", "--files", "/dev/stdin", 132 "--alpha", alphaService, "--zero", zeroService}, 133 } 134 err := testutil.Pipeline(pipeline) 135 require.NoError(t, err, "live loading JSON stream exited with error") 136 137 checkLoadedData(t) 138 } 139 140 func TestLiveLoadJSONMultipleFiles(t *testing.T) { 141 testutil.DropAll(t, dg) 142 143 files := []string{ 144 testDataDir + "/family1.json", 145 testDataDir + "/family2.json", 146 testDataDir + "/family3.json", 147 } 148 fileList := strings.Join(files, ",") 149 150 pipeline := [][]string{ 151 {os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 152 "--schema", testDataDir + "/family.schema", "--files", fileList, 153 "--alpha", alphaService, "--zero", zeroService}, 154 } 155 err := testutil.Pipeline(pipeline) 156 require.NoError(t, err, "live loading multiple JSON files exited with error") 157 158 checkLoadedData(t) 159 } 160 161 func TestMain(m *testing.M) { 162 _, thisFile, _, _ := runtime.Caller(0) 163 testDataDir = path.Dir(thisFile) 164 165 dg = testutil.DgraphClientWithGroot(testutil.SockAddr) 166 167 // Try to create any files in a dedicated temp directory that gets cleaned up 168 // instead of all over /tmp or the working directory. 169 tmpDir, err := ioutil.TempDir("", "test.tmp-") 170 x.Check(err) 171 os.Chdir(tmpDir) 172 defer os.RemoveAll(tmpDir) 173 174 os.Exit(m.Run()) 175 }