Skip to content

Commit

Permalink
Added methods to save and fetch documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
starJammer committed Dec 12, 2014
1 parent 1420468 commit 4ff242a
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 30 deletions.
74 changes: 65 additions & 9 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arango

import (
"fmt"
"strings"
)

//Collection types
Expand Down Expand Up @@ -52,7 +53,7 @@ type CollectionCreationOptions struct {

//KeyOptions stores information about how a collection's key is configured.
//It is used during collection creation to specify how the new collection's
//key should be setup.
//key should be setup.
//
//It is also used for existing collections so you know how the collection's
//key is configured.
Expand Down Expand Up @@ -111,11 +112,11 @@ func (c *Collection) Name() string {
//The result is cached. Call Properties() to refresh.
//The status can be any of the constants defined above.
//const (
//NEW_BORN_STATUS = 1
//UNLOADED_STATUS = 2
//LOADED_STATUS = 3
//BEING_UNLOADED_STATUS = 4
//DELETED_STATUS = 5
//NEW_BORN_STATUS = 1
//UNLOADED_STATUS = 2
//LOADED_STATUS = 3
//BEING_UNLOADED_STATUS = 4
//DELETED_STATUS = 5
//)
func (c *Collection) Status() int {
return c.json.Status
Expand Down Expand Up @@ -168,7 +169,6 @@ func (c *Collection) KeyOptions() *KeyOptions {
return c.json.KeyOptions
}


//Properties fetches additional properties of the collection.
//It queries the /_api/collection/{collection-name}/properties
//endpoint and causes the collection to switch to the loaded
Expand Down Expand Up @@ -200,8 +200,64 @@ func (c *Collection) Properties() error {

//Drop deletes the collection from the database
//DO NOT expect the collection to work after dropping it.
//Calling any further methods on it will result in
//Calling any further methods on it will result in
//unexpected behavior
func (c *Collection) Drop() error {
return c.db.DropCollection( c.Name() )
return c.db.DropCollection(c.Name())
}

//Save creates a document in the collection.
//Uses the POST /_api/document endpoint
//If your document embeds the DocumentImplementation type
//or it has fields to hold the Id, Rev, and Key fields
//from arango, then it will be populated with the Id, Rev, Key
//fields during the json.Unmarshal call
func (c *Collection) Save(document interface{}) error {
return c.db.SaveDocumentWithOptions(document, &SaveOptions{
Collection: c.Name(),
CreateCollection: false,
WaitForSync: false,
})
}

//SaveWithOptions lets you save a document but lets you specify some options
//See the POST /_api/document endpoint for more info.
func (c *Collection) SaveWithOptions(document interface{}, options *SaveOptions) error {

options.Collection = c.Name()
options.CreateCollection = false

return c.db.SaveDocumentWithOptions(document, options)
}

//Document will fetch the document associated with the documentHandle.
//An error will be returned if anything goes wrong. Otherwise, document
//be populated with the document values from arango. json.Unmarhshal
//is used under the hood so make sure you understand how string literals
//tags work with the encoding/json package.
//This uses the GET /_api/document/{document-handle} endpoint.
//The method provides a extra bit of functionality in that it won't let
//you fetch an document NOT from this collection. For example,
//you can't request 'users/123413' when the collection name is 'places'.
//If you want to look up an arbitrary document then use db.Document()
func (c *Collection) Document(documentHandle interface{},
document interface{}) error {
return c.DocumentWithOptions(documentHandle, document, nil)
}

func (c *Collection) DocumentWithOptions(documentHandle interface{},
document interface{},
options *FetchDocumentOptions) error {
switch id := documentHandle.(type) {
case string:
idParts := strings.Split(id, "/")
if len(idParts) == 1 {
return c.db.DocumentWithOptions(c.Name()+"/"+idParts[0], document, options)
} else if len(idParts) == 2 {
if idParts[0] != c.Name() {
return newError(fmt.Sprintf("The id %s won't be found in the collection named %s.", id, c.Name()))
}
}
}
return c.db.DocumentWithOptions(documentHandle, document, options)
}
148 changes: 148 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,151 @@
package arango

import (

"testing"
)

var (
db *Database
)

func setup() {
var err error
db, err = Conn( "http://root@localhost:8529" )
if err != nil {
panic( err )
}

db.DropDatabase( "testing" )
err = db.CreateDatabase( "testing", nil, users )

if err != nil {
panic( err )
}
db, err = db.UseDatabase( "testing" )
if err != nil {
panic( err )
}
}

func teardown(){
var err error
db, err = db.UseDatabase( "_system" )
if err != nil {
panic( err )
}
err = db.DropDatabase( "testing" )
if err != nil {
panic( err )
}
}

type DummyDocument struct{
Hi string
}

type DummyFullDocument struct {
DocumentImplementation
Hi string
}

func TestSavingAndRetrievingDocument( t *testing.T ){

setup()
defer teardown()

c, err := db.CreateDocumentCollection( "testing" )

err = c.Save( &DummyDocument{ Hi : "Hello World" } )

if err != nil {
t.Fatal( err )
}

fulld := &DummyFullDocument{
Hi : "Hello World",
}

err = c.Save( fulld )

if err != nil {
t.Fatal( err )
}

if fulld.Id() == "" {
t.Fatal( "Expected id to be populated in document after a save." )
}

//Find it via full id
ret := &DummyFullDocument{}
err = c.Document( fulld.Id(), ret )

if err != nil {
t.Fatal( err )
}

if ret.Hi != "Hello World" {
t.Fatal( "Expected to have the value for the document correctly fetched.")
}

if ret.Id() != fulld.Id() {
t.Fatal( "Expected to have the ids for documents be equal since they are the same document.")
}

if ret.Key() != fulld.Key() {
t.Fatal( "Expected to have the keys for documents be equal since they are the same document.")
}

if ret.Rev() != fulld.Rev() {
t.Fatal( "Expected to have the revs for documents be equal since they are the same document.")
}

//Find it using a Document struct
ret = &DummyFullDocument{}
err = c.Document( fulld, ret )

if err != nil {
t.Fatal( err )
}

if ret.Hi != "Hello World" {
t.Fatal( "Expected to have the value for the document correctly fetched.")
}

if ret.Id() != fulld.Id() {
t.Fatal( "Expected to have the ids for documents be equal since they are the same document.")
}

if ret.Key() != fulld.Key() {
t.Fatal( "Expected to have the keys for documents be equal since they are the same document.")
}

if ret.Rev() != fulld.Rev() {
t.Fatal( "Expected to have the revs for documents be equal since they are the same document.")
}

//Find it by key
ret = &DummyFullDocument{}
err = c.Document( fulld.Key(), ret )

if err != nil {
t.Fatal( err )
}

if ret.Hi != "Hello World" {
t.Fatal( "Expected to have the value for the document correctly fetched.")
}

if ret.Id() != fulld.Id() {
t.Fatal( "Expected to have the ids for documents be equal since they are the same document.")
}

if ret.Key() != fulld.Key() {
t.Fatal( "Expected to have the keys for documents be equal since they are the same document.")
}

if ret.Rev() != fulld.Rev() {
t.Fatal( "Expected to have the revs for documents be equal since they are the same document.")
}

}
7 changes: 4 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ func ConnDbUserPassword(host, databaseName, user, password string) (*Database, e
}

switch response.Status() {
case 200:
case 200:
return db, nil
case 401:
return nil, newError( "401 Unauthorized: check user password." )
default:
return nil, e
}

return db, nil

}
Loading

0 comments on commit 4ff242a

Please sign in to comment.