This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"io" | |
"io/ioutil" | |
"log" | |
"github.com/aws/aws-sdk-go/aws" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
results, err := s3Client.GetObject(&s3.GetObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type S3Fetcher interface { | |
GetObject(*s3.GetObjectInput) (*s3.GetObjectOutput, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type fileFetcher struct { | |
data []byte | |
file string | |
} | |
func NewFileFetcher(filename string) (S3Fetcher, error) { | |
// read contents of file | |
jsonBuff, err := ioutil.ReadFile(filename) | |
if err != nil { | |
return nil, err |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (fetcher *fileFetcher) GetObject(s3Input *s3.GetObjectInput) (*s3.GetObjectOutput, error) { | |
// completely ignore the input, just return an object wrapping the fetcher.data | |
log.Printf("reading offers from file '%s'", fetcher.file) | |
return &s3.GetObjectOutput{ | |
Body: *(NewMockBody(fetcher.data)), | |
}, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MockBody struct { | |
buf *bytes.Buffer | |
} | |
func (m MockBody) Close() error { | |
return nil | |
} | |
func (m MockBody) Read(p []byte) (n int, err error) { | |
return m.buf.Read(p) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func downloadS3Data(fetcher S3Fetcher, bucket string, key string) ([]byte, error) { | |
results, err := fetcher.GetObject(&s3.GetObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
}) | |
if err != nil { | |
return nil, err | |
} | |
defer results.Body.Close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package person_test | |
import ( | |
. "github.com/tapjoy/mocks3fetcher/person" | |
. "github.com/onsi/ginkgo" | |
. "github.com/onsi/gomega" | |
) | |
var _ = Describe("person", func() { | |
goodJson := ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var svc S3Fetcher.S3Fetcher | |
var err error | |
var jsonData string = goodJson | |
JustBeforeEach(func() { | |
svc, err = S3Fetcher.NewStringFetcher(jsonData) | |
}) | |
Context("ReadPersonFromS3", func() { | |
It("successfully reads data from S3", func() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
… | |
"github.com/tapjoy/gomockblog/mocks/mock_s3fetcher" | |
… | |
} |
OlderNewer