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
| Context("simulate an error retrieving data from S3", func() { | |
| var testCall *gomock.Call | |
| BeforeEach(func() { | |
| testCall = mockFetcher.EXPECT().GetObject(gomock.Any()).Return(nil, fmt.Errorf("AWS error")).AnyTimes() | |
| }) | |
| It("and fails", func() { | |
| _, err := ReadPersonFromS3(mockFetcher) | |
| Expect(err).To(HaveOccurred()) | |
| testCall.Times(1) | |
| }) |
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
| Context("ReadPersonFromS3 -- using mock", func() { | |
| bucket := "testbucket" | |
| key := "persondata.txt" | |
| objectInput := &s3.GetObjectInput{ | |
| Bucket: aws.String(bucket), | |
| Key: aws.String(key), | |
| } | |
| goodObjectOutput := &s3.GetObjectOutput{ | |
| Body: S3Fetcher.NewMockBody([]byte(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 _ = Describe("Person", func() { | |
| var ( | |
| mockFetcher *mock_S3Fetcher.MockS3Fetcher | |
| ctrl *gomock.Controller | |
| ) | |
| BeforeEach(func() { | |
| ctrl = gomock.NewController(GinkgoT()) | |
| }) | |
| AfterEach(func() { | |
| ctrl.Finish() |
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" | |
| … | |
| } |
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
| 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
| 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
| 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 (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 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 |
NewerOlder