|
17 | 17 | package main |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "bytes" |
21 | | - "encoding/json" |
22 | | - "fmt" |
| 20 | + "context" |
23 | 21 | "io/ioutil" |
24 | | - "net/http" |
25 | 22 | "os" |
26 | 23 | "os/exec" |
27 | 24 | "path/filepath" |
28 | | - "strings" |
29 | 25 | "testing" |
30 | 26 |
|
31 | 27 | "github.com/stretchr/testify/require" |
32 | 28 |
|
| 29 | + "github.com/dgraph-io/dgo/v200/protos/api" |
33 | 30 | "github.com/dgraph-io/dgraph/testutil" |
| 31 | + "github.com/dgraph-io/dgraph/x" |
34 | 32 | ) |
35 | 33 |
|
| 34 | +// TestLoaderXidmap checks that live loader re-uses xidmap on loading data from two different files |
36 | 35 | func TestLoaderXidmap(t *testing.T) { |
| 36 | + dg, err := testutil.DgraphClient(testutil.SockAddr) |
| 37 | + require.NoError(t, err) |
| 38 | + ctx := context.Background() |
| 39 | + testutil.DropAll(t, dg) |
37 | 40 | tmpDir, err := ioutil.TempDir("", "loader_test") |
38 | 41 | require.NoError(t, err) |
39 | 42 | defer os.RemoveAll(tmpDir) |
@@ -63,80 +66,23 @@ func TestLoaderXidmap(t *testing.T) { |
63 | 66 | liveCmd.Stderr = os.Stdout |
64 | 67 | require.NoError(t, liveCmd.Run()) |
65 | 68 |
|
66 | | - exportRequest := `mutation { |
67 | | - export(input: {format: "rdf"}) { |
68 | | - response { |
69 | | - code |
70 | | - message |
| 69 | + op := api.Operation{Schema: "name: string @index(exact) ."} |
| 70 | + x.Check(dg.Alter(ctx, &op)) |
| 71 | + |
| 72 | + query := ` |
| 73 | + { |
| 74 | + q(func: eq(name, "Alice")) { |
| 75 | + age |
| 76 | + location |
| 77 | + friend{ |
| 78 | + name |
71 | 79 | } |
72 | 80 | } |
73 | 81 | }` |
| 82 | + expected := `{"q":[{"age":"13","location":"Wonderland","friend":[{"name":"Bob"}]}]}` |
74 | 83 |
|
75 | | - adminUrl := "http://" + testutil.SockAddrHttp + "/admin" |
76 | | - params := testutil.GraphQLParams{ |
77 | | - Query: exportRequest, |
78 | | - Variables: map[string]interface{}{"format": "json"}, |
79 | | - } |
80 | | - b, err := json.Marshal(params) |
81 | | - require.NoError(t, err) |
82 | | - resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) |
83 | | - require.NoError(t, err) |
84 | | - defer resp.Body.Close() |
85 | | - b, err = ioutil.ReadAll(resp.Body) |
86 | | - require.NoError(t, err) |
87 | | - expected := `{ |
88 | | - "export": { |
89 | | - "response": { |
90 | | - "code": "Success", |
91 | | - "message": "Export completed." |
92 | | - } |
93 | | - } |
94 | | - }` |
95 | | - res1 := &testutil.GraphQLResponse{} |
96 | | - err = json.Unmarshal(b, res1) |
97 | | - require.NoError(t, err) |
98 | | - require.JSONEq(t, expected, string(res1.Data)) |
99 | | - |
100 | | - require.NoError(t, copyExportFiles(tmpDir)) |
101 | | - |
102 | | - dataFile, err := findFile(filepath.Join(tmpDir, "export"), ".rdf.gz") |
| 84 | + resp, err := dg.NewReadOnlyTxn().Query(ctx, query) |
103 | 85 | require.NoError(t, err) |
| 86 | + testutil.CompareJSON(t, expected, string(resp.GetJson())) |
104 | 87 |
|
105 | | - cmd := fmt.Sprintf("gunzip -c %s | sort", dataFile) |
106 | | - out, err := exec.Command("sh", "-c", cmd).Output() |
107 | | - require.NoError(t, err) |
108 | | - |
109 | | - expected = `<0x1> <age> "13" . |
110 | | -<0x1> <friend> <0x2711> . |
111 | | -<0x1> <location> "Wonderland" . |
112 | | -<0x1> <name> "Alice" . |
113 | | -<0x2711> <name> "Bob" . |
114 | | -` |
115 | | - require.Equal(t, expected, string(out)) |
116 | | -} |
117 | | - |
118 | | -func copyExportFiles(tmpDir string) error { |
119 | | - exportPath := filepath.Join(tmpDir, "export") |
120 | | - if err := os.MkdirAll(exportPath, 0755); err != nil { |
121 | | - return err |
122 | | - } |
123 | | - |
124 | | - srcPath := "alpha1:/data/alpha1/export" |
125 | | - dstPath := filepath.Join(tmpDir, "export") |
126 | | - return testutil.DockerCp(srcPath, dstPath) |
127 | | -} |
128 | | - |
129 | | -func findFile(dir string, ext string) (string, error) { |
130 | | - var fp string |
131 | | - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
132 | | - if err != nil { |
133 | | - return err |
134 | | - } |
135 | | - if strings.HasSuffix(path, ext) { |
136 | | - fp = path |
137 | | - return nil |
138 | | - } |
139 | | - return nil |
140 | | - }) |
141 | | - return fp, err |
142 | 88 | } |
0 commit comments