github.com/cayleygraph/cayley@v0.7.7/graph/iterator/and_test.go (about)

     1  // Copyright 2014 The Cayley Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package iterator_test
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"github.com/cayleygraph/cayley/graph"
    23  	. "github.com/cayleygraph/cayley/graph/iterator"
    24  )
    25  
    26  // Make sure that tags work on the And.
    27  func TestAndTag(t *testing.T) {
    28  	ctx := context.TODO()
    29  	fix1 := NewFixed(Int64Node(234))
    30  	fix2 := NewFixed(Int64Node(234))
    31  	var and graph.Iterator = NewAnd(Tag(fix1, "foo")).AddOptionalIterator(Tag(fix2, "baz"))
    32  	and = Tag(and, "bar")
    33  
    34  	if !and.Next(ctx) {
    35  		t.Errorf("And did not next")
    36  	}
    37  	val := and.Result()
    38  	if val.(Int64Node) != 234 {
    39  		t.Errorf("Unexpected value")
    40  	}
    41  	tags := make(map[string]graph.Ref)
    42  	and.TagResults(tags)
    43  	if tags["bar"].(Int64Node) != 234 {
    44  		t.Errorf("no bar tag")
    45  	}
    46  	if tags["foo"].(Int64Node) != 234 {
    47  		t.Errorf("no foo tag")
    48  	}
    49  	if tags["baz"].(Int64Node) != 234 {
    50  		t.Errorf("no baz tag")
    51  	}
    52  }
    53  
    54  // Do a simple itersection of fixed values.
    55  func TestAndAndFixedIterators(t *testing.T) {
    56  	ctx := context.TODO()
    57  	fix1 := NewFixed(
    58  		Int64Node(1),
    59  		Int64Node(2),
    60  		Int64Node(3),
    61  		Int64Node(4),
    62  	)
    63  	fix2 := NewFixed(
    64  		Int64Node(3),
    65  		Int64Node(4),
    66  		Int64Node(5),
    67  	)
    68  	and := NewAnd(fix1, fix2)
    69  	// Should be as big as smallest subiterator
    70  	size, accurate := and.Size()
    71  	if size != 3 {
    72  		t.Error("Incorrect size:", size)
    73  	}
    74  	if !accurate {
    75  		t.Error("not accurate")
    76  	}
    77  
    78  	if !and.Next(ctx) || and.Result().(Int64Node) != 3 {
    79  		t.Error("Incorrect first value")
    80  	}
    81  
    82  	if !and.Next(ctx) || and.Result().(Int64Node) != 4 {
    83  		t.Error("Incorrect second value")
    84  	}
    85  
    86  	if and.Next(ctx) {
    87  		t.Error("Too many values")
    88  	}
    89  
    90  }
    91  
    92  // If there's no intersection, the size should still report the same,
    93  // but there should be nothing to Next()
    94  func TestNonOverlappingFixedIterators(t *testing.T) {
    95  	ctx := context.TODO()
    96  	fix1 := NewFixed(
    97  		Int64Node(1),
    98  		Int64Node(2),
    99  		Int64Node(3),
   100  		Int64Node(4),
   101  	)
   102  	fix2 := NewFixed(
   103  		Int64Node(5),
   104  		Int64Node(6),
   105  		Int64Node(7),
   106  	)
   107  	and := NewAnd(fix1, fix2)
   108  	// Should be as big as smallest subiterator
   109  	size, accurate := and.Size()
   110  	if size != 3 {
   111  		t.Error("Incorrect size")
   112  	}
   113  	if !accurate {
   114  		t.Error("not accurate")
   115  	}
   116  
   117  	if and.Next(ctx) {
   118  		t.Error("Too many values")
   119  	}
   120  
   121  }
   122  
   123  func TestAllIterators(t *testing.T) {
   124  	ctx := context.TODO()
   125  	all1 := newInt64(1, 5, true)
   126  	all2 := newInt64(4, 10, true)
   127  	and := NewAnd(all2, all1)
   128  
   129  	if !and.Next(ctx) || and.Result().(Int64Node) != Int64Node(4) {
   130  		t.Error("Incorrect first value")
   131  	}
   132  
   133  	if !and.Next(ctx) || and.Result().(Int64Node) != Int64Node(5) {
   134  		t.Error("Incorrect second value")
   135  	}
   136  
   137  	if and.Next(ctx) {
   138  		t.Error("Too many values")
   139  	}
   140  }
   141  
   142  func TestAndIteratorErr(t *testing.T) {
   143  	ctx := context.TODO()
   144  	wantErr := errors.New("unique")
   145  	allErr := newTestIterator(false, wantErr)
   146  
   147  	and := NewAnd(
   148  		allErr,
   149  		newInt64(1, 5, true),
   150  	)
   151  
   152  	if and.Next(ctx) != false {
   153  		t.Errorf("And iterator did not pass through initial 'false'")
   154  	}
   155  	if and.Err() != wantErr {
   156  		t.Errorf("And iterator did not pass through underlying Err: %v", and.Err())
   157  	}
   158  }