github.com/altipla-consulting/ravendb-go-client@v0.1.3/tests/documents_load_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  // Note: client/documents/LoadTest.java
    10  
    11  type Foo struct {
    12  	Name string
    13  }
    14  
    15  type Bar struct {
    16  	FooId  string
    17  	FooIDs []string
    18  	Name   string
    19  }
    20  
    21  func documentsLoadTestLoadWithIncludes(t *testing.T, driver *RavenTestDriver) {
    22  	var err error
    23  	store := driver.getDocumentStoreMust(t)
    24  	defer store.Close()
    25  
    26  	barId := ""
    27  	{
    28  		session := openSessionMust(t, store)
    29  		foo := &Foo{}
    30  		foo.Name = "Beginning"
    31  		err = session.Store(foo)
    32  		assert.NoError(t, err)
    33  
    34  		fooId := session.Advanced().GetDocumentID(foo)
    35  		bar := &Bar{}
    36  		bar.Name = "End"
    37  		bar.FooId = fooId
    38  
    39  		session.Store(bar)
    40  
    41  		barId = session.Advanced().GetDocumentID(bar)
    42  		err = session.SaveChanges()
    43  		assert.NoError(t, err)
    44  		session.Close()
    45  	}
    46  
    47  	{
    48  		newSession := openSessionMust(t, store)
    49  		// Note: in Java it's fooId, we must match Go naming with FooId
    50  		bars := map[string]*Bar{}
    51  		err = newSession.Include("FooId").LoadMulti(bars, []string{barId})
    52  		assert.NoError(t, err)
    53  
    54  		assert.NotNil(t, bars)
    55  		assert.Equal(t, len(bars), 1)
    56  		for _, v := range bars {
    57  			assert.NotNil(t, v)
    58  		}
    59  
    60  		numOfRequests := newSession.Advanced().GetNumberOfRequests()
    61  
    62  		bar := bars[barId]
    63  		var foo *Foo
    64  		err = newSession.Load(&foo, bar.FooId)
    65  		assert.NoError(t, err)
    66  		assert.NotNil(t, foo)
    67  		assert.Equal(t, foo.Name, "Beginning")
    68  
    69  		assert.Equal(t, newSession.Advanced().GetNumberOfRequests(), numOfRequests)
    70  		newSession.Close()
    71  	}
    72  }
    73  
    74  func documentsLoadTestLoadWithIncludesAndMissingDocument(t *testing.T, driver *RavenTestDriver) {
    75  	// TODO: is @Disabled
    76  }
    77  
    78  func TestDocumentsLoad(t *testing.T) {
    79  	driver := createTestDriver(t)
    80  	destroy := func() { destroyDriver(t, driver) }
    81  	defer recoverTest(t, destroy)
    82  
    83  	// matches order of Java tests
    84  	documentsLoadTestLoadWithIncludes(t, driver)
    85  	documentsLoadTestLoadWithIncludesAndMissingDocument(t, driver)
    86  }