github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/systest/21million/run_test.go (about)

     1  // +build standalone
     2  
     3  /*
     4   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"flag"
    24  	"io/ioutil"
    25  	"path"
    26  	"runtime"
    27  	"strings"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/dgraph-io/dgraph/chunker"
    32  	"github.com/dgraph-io/dgraph/testutil"
    33  	"github.com/dgraph-io/dgraph/x"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  // JSON output can be hundreds of lines and diffs can scroll off the terminal before you
    39  // can look at them. This option allows saving the JSON to a specified directory instead
    40  // for easier reviewing after the test completes.
    41  var savedir = flag.String("savedir", "",
    42  	"directory to save json from test failures in")
    43  var quiet = flag.Bool("quiet", false,
    44  	"just output whether json differs, not a diff")
    45  
    46  func TestQueries(t *testing.T) {
    47  	_, thisFile, _, _ := runtime.Caller(0)
    48  	queryDir := path.Join(path.Dir(thisFile), "queries")
    49  
    50  	// For this test we DON'T want to start with an empty database.
    51  	dg := testutil.DgraphClient(testutil.SockAddr)
    52  
    53  	files, err := ioutil.ReadDir(queryDir)
    54  	x.CheckfNoTrace(err)
    55  
    56  	savepath := ""
    57  	diffs := 0
    58  	for _, file := range files {
    59  		if !strings.HasPrefix(file.Name(), "query-") {
    60  			continue
    61  		}
    62  		t.Run(file.Name(), func(t *testing.T) {
    63  			filename := path.Join(queryDir, file.Name())
    64  			reader, cleanup := chunker.FileReader(filename)
    65  			bytes, err := ioutil.ReadAll(reader)
    66  			x.CheckfNoTrace(err)
    67  			contents := string(bytes[:])
    68  			cleanup()
    69  
    70  			// The test query and expected result are separated by a delimiter.
    71  			bodies := strings.SplitN(contents, "\n---\n", 2)
    72  
    73  			// If a query takes too long to run, it probably means dgraph is stuck and there's
    74  			// no point in waiting longer or trying more tests.
    75  			ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    76  			resp, err := dg.NewTxn().Query(ctx, bodies[0])
    77  			cancel()
    78  			if ctx.Err() == context.DeadlineExceeded {
    79  				t.Fatal("aborting test due to query timeout")
    80  			}
    81  			require.NoError(t, err)
    82  
    83  			t.Logf("running %s", file.Name())
    84  			if *savedir != "" {
    85  				savepath = path.Join(*savedir, file.Name())
    86  			}
    87  
    88  			if !testutil.EqualJSON(t, bodies[1], string(resp.GetJson()), savepath, *quiet) {
    89  				diffs++
    90  			}
    91  		})
    92  	}
    93  
    94  	if *savedir != "" && diffs > 0 {
    95  		t.Logf("test json saved in directory: %s", *savedir)
    96  	}
    97  }