go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/datastore/testable.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 datastore
    16  
    17  // TestingSnapshot is an opaque implementation-defined snapshot type.
    18  type TestingSnapshot interface {
    19  	ImATestingSnapshot()
    20  }
    21  
    22  // Testable is the testable interface for fake datastore implementations.
    23  type Testable interface {
    24  	// AddIndex adds the provided index.
    25  	// Blocks all datastore access while the index is built.
    26  	// Panics if any of the IndexDefinition objects are not Compound()
    27  	AddIndexes(...*IndexDefinition)
    28  
    29  	// TakeIndexSnapshot allows you to take a snapshot of the current index
    30  	// tables, which can be used later with SetIndexSnapshot.
    31  	TakeIndexSnapshot() TestingSnapshot
    32  
    33  	// SetIndexSnapshot allows you to set the state of the current index tables.
    34  	// Note that this would allow you to create 'non-lienarities' in the precieved
    35  	// index results (e.g. you could force the indexes to go back in time).
    36  	//
    37  	// SetIndexSnapshot takes a reference of the given TestingSnapshot. You're
    38  	// still responsible for closing the snapshot after this call.
    39  	SetIndexSnapshot(TestingSnapshot)
    40  
    41  	// CatchupIndexes catches the index table up to the current state of the
    42  	// datastore. This is equivalent to:
    43  	//   idxSnap := TakeIndexSnapshot()
    44  	//   SetIndexSnapshot(idxSnap)
    45  	//
    46  	// But depending on the implementation it may implemented with an atomic
    47  	// operation.
    48  	CatchupIndexes()
    49  
    50  	// SetTransactionRetryCount set how many times RunInTransaction will retry
    51  	// transaction body pretending transaction conflicts happens. 0 (default)
    52  	// means commit succeeds on the first attempt (no retries).
    53  	SetTransactionRetryCount(int)
    54  
    55  	// Consistent controls the eventual consistency behavior of the testing
    56  	// implementation. If it is called with true, then this datastore
    57  	// implementation will be always-consistent, instead of eventually-consistent.
    58  	//
    59  	// By default the datastore is eventually consistent, and you must call
    60  	// CatchupIndexes or use Take/SetIndexSnapshot to manipulate the index state.
    61  	Consistent(always bool)
    62  
    63  	// AutoIndex controls the index creation behavior. If it is set to true, then
    64  	// any time the datastore encounters a missing index, it will silently create
    65  	// one and allow the query to succeed. If it's false, then the query will
    66  	// return an error describing the index which could be added with AddIndexes.
    67  	//
    68  	// By default this is false.
    69  	AutoIndex(bool)
    70  
    71  	// DisableSpecialEntities turns off maintenance of special __entity_group__
    72  	// type entities. By default this mainenance is enabled, but it can be
    73  	// disabled by calling this with true.
    74  	//
    75  	// If it's true:
    76  	//   - AllocateIDs returns an error.
    77  	//   - Put'ing incomplete Keys returns an error.
    78  	//   - Transactions are disabled and will return an error.
    79  	//
    80  	// This is mainly only useful when using an embedded in-memory datastore as
    81  	// a fully-consistent 'datastore-lite'. In particular, this is useful for the
    82  	// txnBuf filter which uses it to fulfil queries in a buffered transaction,
    83  	// but never wants the in-memory versions of these entities to bleed through
    84  	// to the user code.
    85  	DisableSpecialEntities(bool)
    86  
    87  	// ShowSpecialProperties disables stripping of special properties added by
    88  	// the datastore internally (like __scatter__) from result of Get calls.
    89  	//
    90  	// Normally such properties are used internally by the datastore or only for
    91  	// queries. Returning them explicitly is useful for assertions in tests that
    92  	// rely on queries over special properties.
    93  	ShowSpecialProperties(bool)
    94  
    95  	// SetConstraints sets this instance's constraints. If the supplied
    96  	// constraints are invalid, an error will be returned.
    97  	//
    98  	// If c is nil, default constraints will be set.
    99  	SetConstraints(c *Constraints) error
   100  }