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