github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/query/outputnode_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 query
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"runtime"
    23  	"sync"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/dgraph-io/dgraph/types"
    29  	"github.com/dgraph-io/dgraph/x"
    30  )
    31  
    32  func makeFastJsonNode() *fastJsonNode {
    33  	return &fastJsonNode{}
    34  }
    35  
    36  func TestEncodeMemory(t *testing.T) {
    37  	//	if testing.Short() {
    38  	t.Skip("Skipping TestEncodeMemory")
    39  	//	}
    40  	var wg sync.WaitGroup
    41  
    42  	for x := 0; x < runtime.NumCPU(); x++ {
    43  		n := makeFastJsonNode()
    44  		require.NotNil(t, n)
    45  		for i := 0; i < 15000; i++ {
    46  			n.AddValue(fmt.Sprintf("very long attr name %06d", i), types.ValueForType(types.StringID))
    47  			n.AddListChild(fmt.Sprintf("another long child %06d", i), &fastJsonNode{})
    48  		}
    49  		wg.Add(1)
    50  		go func() {
    51  			defer wg.Done()
    52  			for i := 0; i < 1000; i++ {
    53  				var buf bytes.Buffer
    54  				n.encode(&buf)
    55  			}
    56  		}()
    57  	}
    58  
    59  	wg.Wait()
    60  }
    61  
    62  func TestNormalizeJSONLimit(t *testing.T) {
    63  	// Set default normalize limit.
    64  	x.Config.NormalizeNodeLimit = 1e4
    65  
    66  	if testing.Short() {
    67  		t.Skip("Skipping TestNormalizeJSONLimit")
    68  	}
    69  
    70  	n := (&fastJsonNode{}).New("root")
    71  	require.NotNil(t, n)
    72  	for i := 0; i < 1000; i++ {
    73  		n.AddValue(fmt.Sprintf("very long attr name %06d", i),
    74  			types.ValueForType(types.StringID))
    75  		child1 := n.New("child1")
    76  		n.AddListChild("child1", child1)
    77  		for j := 0; j < 100; j++ {
    78  			child1.AddValue(fmt.Sprintf("long child1 attr %06d", j),
    79  				types.ValueForType(types.StringID))
    80  		}
    81  		child2 := n.New("child2")
    82  		n.AddListChild("child2", child2)
    83  		for j := 0; j < 100; j++ {
    84  			child2.AddValue(fmt.Sprintf("long child2 attr %06d", j),
    85  				types.ValueForType(types.StringID))
    86  		}
    87  		child3 := n.New("child3")
    88  		n.AddListChild("child3", child3)
    89  		for j := 0; j < 100; j++ {
    90  			child3.AddValue(fmt.Sprintf("long child3 attr %06d", j),
    91  				types.ValueForType(types.StringID))
    92  		}
    93  	}
    94  	_, err := n.(*fastJsonNode).normalize()
    95  	require.Error(t, err, "Couldn't evaluate @normalize directive - too many results")
    96  }
    97  
    98  func TestNormalizeJSONUid1(t *testing.T) {
    99  	// Set default normalize limit.
   100  	x.Config.NormalizeNodeLimit = 1e4
   101  
   102  	n := (&fastJsonNode{}).New("root")
   103  	require.NotNil(t, n)
   104  	child1 := n.New("child1")
   105  	child1.SetUID(uint64(1), "uid")
   106  	child1.AddValue("attr1", types.ValueForType(types.StringID))
   107  	n.AddListChild("child1", child1)
   108  
   109  	child2 := n.New("child2")
   110  	child2.SetUID(uint64(2), "uid")
   111  	child2.AddValue("attr2", types.ValueForType(types.StringID))
   112  	child1.AddListChild("child2", child2)
   113  
   114  	child3 := n.New("child3")
   115  	child3.SetUID(uint64(3), "uid")
   116  	child3.AddValue("attr3", types.ValueForType(types.StringID))
   117  	child2.AddListChild("child3", child3)
   118  
   119  	normalized, err := n.(*fastJsonNode).normalize()
   120  	require.NoError(t, err)
   121  	require.NotNil(t, normalized)
   122  	nn := (&fastJsonNode{}).New("root")
   123  	for _, c := range normalized {
   124  		nn.AddListChild("alias", &fastJsonNode{attrs: c})
   125  	}
   126  
   127  	var b bytes.Buffer
   128  	nn.(*fastJsonNode).encode(&b)
   129  	require.JSONEq(t, `{"alias":[{"uid":"0x3","attr1":"","attr2":"","attr3":""}]}`, b.String())
   130  }
   131  
   132  func TestNormalizeJSONUid2(t *testing.T) {
   133  	n := (&fastJsonNode{}).New("root")
   134  	require.NotNil(t, n)
   135  	child1 := n.New("child1")
   136  	child1.SetUID(uint64(1), "uid")
   137  	child1.AddValue("___attr1", types.ValueForType(types.StringID))
   138  	n.AddListChild("child1", child1)
   139  
   140  	child2 := n.New("child2")
   141  	child2.SetUID(uint64(2), "uid")
   142  	child2.AddValue("___attr2", types.ValueForType(types.StringID))
   143  	child1.AddListChild("child2", child2)
   144  
   145  	child3 := n.New("child3")
   146  	child3.SetUID(uint64(3), "uid")
   147  	child3.AddValue(fmt.Sprintf("attr3"), types.ValueForType(types.StringID))
   148  	child2.AddListChild("child3", child3)
   149  
   150  	normalized, err := n.(*fastJsonNode).normalize()
   151  	require.NoError(t, err)
   152  	require.NotNil(t, normalized)
   153  	nn := (&fastJsonNode{}).New("root")
   154  	for _, c := range normalized {
   155  		nn.AddListChild("alias", &fastJsonNode{attrs: c})
   156  	}
   157  
   158  	var b bytes.Buffer
   159  	nn.(*fastJsonNode).encode(&b)
   160  	require.JSONEq(t, `{"alias":[{"___attr1":"","___attr2":"","uid":"0x3","attr3":""}]}`, b.String())
   161  }