github.com/cayleygraph/cayley@v0.7.7/graph/iterator/materialize_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/iterator"
    23  )
    24  
    25  func TestMaterializeIteratorError(t *testing.T) {
    26  	ctx := context.TODO()
    27  	wantErr := errors.New("unique")
    28  	errIt := newTestIterator(false, wantErr)
    29  
    30  	// This tests that we properly return 0 results and the error when the
    31  	// underlying iterator returns an error.
    32  	mIt := NewMaterialize(errIt)
    33  
    34  	if mIt.Next(ctx) != false {
    35  		t.Errorf("Materialize iterator did not pass through underlying 'false'")
    36  	}
    37  	if mIt.Err() != wantErr {
    38  		t.Errorf("Materialize iterator did not pass through underlying Err")
    39  	}
    40  }
    41  
    42  func TestMaterializeIteratorErrorAbort(t *testing.T) {
    43  	ctx := context.TODO()
    44  	wantErr := errors.New("unique")
    45  	errIt := newTestIterator(false, wantErr)
    46  
    47  	// This tests that we properly return 0 results and the error when the
    48  	// underlying iterator is larger than our 'abort at' value, and then
    49  	// returns an error.
    50  	or := NewOr(
    51  		newInt64(1, int64(MaterializeLimit+1), true),
    52  		errIt,
    53  	)
    54  
    55  	mIt := NewMaterialize(or)
    56  
    57  	// We should get all the underlying values...
    58  	for i := 0; i < MaterializeLimit+1; i++ {
    59  		if !mIt.Next(ctx) {
    60  			t.Errorf("Materialize iterator returned spurious 'false' on iteration %d", i)
    61  			return
    62  		}
    63  		if mIt.Err() != nil {
    64  			t.Errorf("Materialize iterator returned non-nil Err on iteration %d", i)
    65  			return
    66  		}
    67  	}
    68  
    69  	// ... and then the error value.
    70  	if mIt.Next(ctx) != false {
    71  		t.Errorf("Materialize iterator did not pass through underlying 'false'")
    72  	}
    73  	if mIt.Err() != wantErr {
    74  		t.Errorf("Materialize iterator did not pass through underlying Err")
    75  	}
    76  }