diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..18ff30b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,32 @@ +name: Test +on: + push: + branches: + - main + pull_request: + branches: + - main +jobs: + go: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.18.x, 1.19.x] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + cache: true + - run: make test + golangci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: 1.x + cache: true + - uses: golangci/golangci-lint-action@v3 + with: + version: latest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6aa8b3a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.makefile diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b888a14..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: go -go: - - 1.12.x - - 1.13.x -env: - - GO111MODULE=on -cache: - directories: - - $GOPATH/pkg/mod diff --git a/Makefile b/Makefile index 7e42be8..bdffdab 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,6 @@ -default: vet test +default: test -test: - go test -a ./... +.minimal.makefile: + curl -fsSL -o $@ https://gitlab.com/bsm/misc/raw/master/make/go/minimal.makefile -bench: - go test ./... -bench=. -run=NONE - -vet: - go vet ./... +include .minimal.makefile diff --git a/audio_test.go b/audio_test.go index 85e9dfd..1c7c9a8 100644 --- a/audio_test.go +++ b/audio_test.go @@ -1,57 +1,62 @@ -package openrtb +package openrtb_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "errors" + "reflect" + "testing" + + . "github.com/bsm/openrtb/v3" ) -var _ = Describe("Audio", func() { +func TestAudio(t *testing.T) { var subject *Audio + if err := fixture("audio", &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } - BeforeEach(func() { - Expect(fixture("audio", &subject)).To(Succeed()) - }) - - It("should parse correctly", func() { - Expect(subject).To(Equal(&Audio{ - MIMEs: []string{ - "audio/mp4", - }, - MinDuration: 5, - MaxDuration: 30, - Protocols: []Protocol{ProtocolDAAST1, ProtocolDAAST1Wrapper}, - Sequence: 1, - BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, - MaxExtended: 30, - MinBitrate: 300, - MaxBitrate: 1500, - Delivery: []ContentDelivery{ContentDeliveryProgressive}, - CompanionAds: []Banner{ - {Width: 300, Height: 250, ID: "1234567893-1", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, ExpDirs: []ExpDir{ExpDirRight, ExpDirDown}}, - {Width: 728, Height: 90, ID: "1234567893-2", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}}, - }, - APIs: []APIFramework{APIFrameworkVPAID1, APIFrameworkVPAID2}, - CompanionTypes: []CompanionType{CompanionTypeStatic, CompanionTypeHTML}, - })) - }) - - It("should validate", func() { - Expect((&Audio{ - MinDuration: 5, - MaxDuration: 30, - Protocols: []Protocol{ProtocolDAAST1, ProtocolDAAST1Wrapper}, - Sequence: 1, - BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, - MaxExtended: 30, - MinBitrate: 300, - MaxBitrate: 1500, - Delivery: []ContentDelivery{ContentDeliveryProgressive}, - CompanionAds: []Banner{ - {Width: 300, Height: 250, ID: "1234567893-1", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, ExpDirs: []ExpDir{ExpDirRight, ExpDirDown}}, - {Width: 728, Height: 90, ID: "1234567893-2", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}}, - }, - CompanionTypes: []CompanionType{CompanionTypeStatic, CompanionTypeHTML}, - }).Validate()).To(Equal(ErrInvalidAudioNoMIMEs)) - }) + exp := &Audio{ + MIMEs: []string{ + "audio/mp4", + }, + MinDuration: 5, + MaxDuration: 30, + Protocols: []Protocol{ProtocolDAAST1, ProtocolDAAST1Wrapper}, + Sequence: 1, + BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, + MaxExtended: 30, + MinBitrate: 300, + MaxBitrate: 1500, + Delivery: []ContentDelivery{ContentDeliveryProgressive}, + CompanionAds: []Banner{ + {Width: 300, Height: 250, ID: "1234567893-1", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, ExpDirs: []ExpDir{ExpDirRight, ExpDirDown}}, + {Width: 728, Height: 90, ID: "1234567893-2", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}}, + }, + APIs: []APIFramework{APIFrameworkVPAID1, APIFrameworkVPAID2}, + CompanionTypes: []CompanionType{CompanionTypeStatic, CompanionTypeHTML}, + } + if got := subject; !reflect.DeepEqual(exp, got) { + t.Errorf("expected %+v, got %+v", exp, got) + } +} -}) +func TestAudio_Validate(t *testing.T) { + subject := &Audio{ + MinDuration: 5, + MaxDuration: 30, + Protocols: []Protocol{ProtocolDAAST1, ProtocolDAAST1Wrapper}, + Sequence: 1, + BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, + MaxExtended: 30, + MinBitrate: 300, + MaxBitrate: 1500, + Delivery: []ContentDelivery{ContentDeliveryProgressive}, + CompanionAds: []Banner{ + {Width: 300, Height: 250, ID: "1234567893-1", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}, ExpDirs: []ExpDir{ExpDirRight, ExpDirDown}}, + {Width: 728, Height: 90, ID: "1234567893-2", Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated, CreativeAttributeWindowsDialogOrAlert}}, + }, + CompanionTypes: []CompanionType{CompanionTypeStatic, CompanionTypeHTML}, + } + if exp, got := ErrInvalidAudioNoMIMEs, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } +} diff --git a/banner_test.go b/banner_test.go index 26e3f0d..669d80f 100644 --- a/banner_test.go +++ b/banner_test.go @@ -1,27 +1,28 @@ -package openrtb +package openrtb_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "reflect" + "testing" + + . "github.com/bsm/openrtb/v3" ) -var _ = Describe("Banner", func() { +func TestBanner(t *testing.T) { var subject *Banner + if err := fixture("banner", &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } - BeforeEach(func() { - Expect(fixture("banner", &subject)).To(Succeed()) - }) - - It("should parse correctly", func() { - Expect(subject).To(Equal(&Banner{ - Width: 728, - Height: 90, - Position: AdPositionAboveFold, - BlockedTypes: []BannerType{BannerTypeFrame}, - BlockedAttrs: []CreativeAttribute{CreativeAttributeWindowsDialogOrAlert}, - APIs: []APIFramework{APIFrameworkMRAID1}, - VCM: 1, - })) - }) - -}) + exp := &Banner{ + Width: 728, + Height: 90, + Position: AdPositionAboveFold, + BlockedTypes: []BannerType{BannerTypeFrame}, + BlockedAttrs: []CreativeAttribute{CreativeAttributeWindowsDialogOrAlert}, + APIs: []APIFramework{APIFrameworkMRAID1}, + VCM: 1, + } + if got := subject; !reflect.DeepEqual(exp, got) { + t.Errorf("expected %+v, got %+v", exp, got) + } +} diff --git a/bench_test.go b/bench_test.go index c90b2dd..4f20fb2 100644 --- a/bench_test.go +++ b/bench_test.go @@ -2,13 +2,13 @@ package openrtb import ( "encoding/json" - "io/ioutil" + "os" "path/filepath" "testing" ) func BenchmarkBidRequest_Unmarshal(b *testing.B) { - data, err := ioutil.ReadFile(filepath.Join("testdata", "breq.video.json")) + data, err := os.ReadFile(filepath.Join("testdata", "breq.video.json")) if err != nil { b.Fatal(err.Error()) } @@ -23,7 +23,7 @@ func BenchmarkBidRequest_Unmarshal(b *testing.B) { } func BenchmarkBidRequest_Marshal(b *testing.B) { - data, err := ioutil.ReadFile(filepath.Join("testdata", "breq.video.json")) + data, err := os.ReadFile(filepath.Join("testdata", "breq.video.json")) if err != nil { b.Fatal(err.Error()) } diff --git a/bid_test.go b/bid_test.go index 44899b3..9b1d0f9 100644 --- a/bid_test.go +++ b/bid_test.go @@ -1,38 +1,45 @@ -package openrtb +package openrtb_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "errors" + "reflect" + "testing" + + . "github.com/bsm/openrtb/v3" ) -var _ = Describe("Bid", func() { +func TestBid(t *testing.T) { var subject *Bid + if err := fixture("bid", &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } - BeforeEach(func() { - Expect(fixture("bid", &subject)).To(Succeed()) - }) - - It("should parse correctly", func() { - Expect(subject).To(Equal(&Bid{ - ID: "1", - ImpID: "1", - Price: 0.751371, - AdID: "52a5516d29e435137c6f6e74", - NoticeURL: "http://ads.com/win/112770_1386565997?won=${AUCTION_PRICE}", - AdMarkup: "", - AdvDomains: []string{"ads.com"}, - ImageURL: "http://ads.com/112770_1386565997.jpeg", - CampaignID: "52a5516d29e435137c6f6e74", - CreativeID: "52a5516d29e435137c6f6e74_1386565997", - DealID: "example_deal", - Attrs: []CreativeAttribute{}, - })) - }) - - It("should validate", func() { - Expect((&Bid{}).Validate()).To(Equal(ErrInvalidBidNoID)) - Expect((&Bid{ID: "BIDID"}).Validate()).To(Equal(ErrInvalidBidNoImpID)) - Expect(subject.Validate()).NotTo(HaveOccurred()) - }) + exp := &Bid{ + ID: "1", + ImpID: "1", + Price: 0.751371, + AdID: "52a5516d29e435137c6f6e74", + NoticeURL: "http://ads.com/win/112770_1386565997?won=${AUCTION_PRICE}", + AdMarkup: "", + AdvDomains: []string{"ads.com"}, + ImageURL: "http://ads.com/112770_1386565997.jpeg", + CampaignID: "52a5516d29e435137c6f6e74", + CreativeID: "52a5516d29e435137c6f6e74_1386565997", + DealID: "example_deal", + Attrs: []CreativeAttribute{}, + } + if got := subject; !reflect.DeepEqual(exp, got) { + t.Errorf("expected %+v, got %+v", exp, got) + } +} -}) +func TestBid_Validate(t *testing.T) { + subject := &Bid{} + if exp, got := ErrInvalidBidNoID, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } + subject = &Bid{ID: "BIDID"} + if exp, got := ErrInvalidBidNoImpID, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } +} diff --git a/bidrequest_test.go b/bidrequest_test.go index 88131c3..33ff7a4 100644 --- a/bidrequest_test.go +++ b/bidrequest_test.go @@ -1,79 +1,88 @@ -package openrtb +package openrtb_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "errors" + "reflect" + "testing" + + . "github.com/bsm/openrtb/v3" ) -var _ = Describe("BidRequest", func() { +func TestBidRequest(t *testing.T) { var subject *BidRequest - privacyPolicy := 1 - - BeforeEach(func() { - Expect(fixture("breq.banner", &subject)).To(Succeed()) - }) + if err := fixture("breq.banner", &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } - It("should parse complex requests", func() { - for _, kind := range []string{"exp", "video", "native"} { - var req *BidRequest - err := fixture("breq."+kind, &req) - Expect(err).NotTo(HaveOccurred(), "for %s", kind) - Expect(req.Validate()).NotTo(HaveOccurred(), "for %s", kind) - } - }) - - It("should parse correctly", func() { - Expect(subject).To(Equal(&BidRequest{ - ID: "1234534625254", - Impressions: []Impression{ - { - ID: "1", - Secure: 1, - Banner: &Banner{Width: 300, Height: 250, Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated}}, - }, + privacyPolicy := 1 + exp := &BidRequest{ + ID: "1234534625254", + Impressions: []Impression{ + { + ID: "1", + Secure: 1, + Banner: &Banner{Width: 300, Height: 250, Position: AdPositionAboveFold, BlockedAttrs: []CreativeAttribute{CreativeAttributeUserInitiated}}, }, - Site: &Site{ - Inventory: Inventory{ - ID: "234563", - Name: "Site ABCD", - Domain: "siteabcd.com", - Categories: []ContentCategory{ContentCategoryAutoParts, ContentCategoryAutoRepair}, - Publisher: &Publisher{ID: "pub12345", Name: "Publisher A"}, - PrivacyPolicy: &privacyPolicy, - Content: &Content{ - Keywords: "keyword a,keyword b,keyword c", - }, + }, + Site: &Site{ + Inventory: Inventory{ + ID: "234563", + Name: "Site ABCD", + Domain: "siteabcd.com", + Categories: []ContentCategory{ContentCategoryAutoParts, ContentCategoryAutoRepair}, + Publisher: &Publisher{ID: "pub12345", Name: "Publisher A"}, + PrivacyPolicy: &privacyPolicy, + Content: &Content{ + Keywords: "keyword a,keyword b,keyword c", }, - Page: "http://siteabcd.com/page.htm", - Referrer: "http://referringsite.com/referringpage.htm", - }, - Device: &Device{ - UA: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16", - IP: "64.124.253.1", - OS: "OS X", - JS: 1, - FlashVersion: "10.1", - }, - User: &User{ - ID: "45asdf987656789adfad4678rew656789", - BuyerUID: "5df678asd8987656asdf78987654", }, - Test: 1, - AuctionType: 2, - TimeMax: 120, - BlockedAdvDomains: []string{"company1.com", "company2.com"}, - })) - }) + Page: "http://siteabcd.com/page.htm", + Referrer: "http://referringsite.com/referringpage.htm", + }, + Device: &Device{ + UA: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16", + IP: "64.124.253.1", + OS: "OS X", + JS: 1, + FlashVersion: "10.1", + }, + User: &User{ + ID: "45asdf987656789adfad4678rew656789", + BuyerUID: "5df678asd8987656asdf78987654", + }, + Test: 1, + AuctionType: 2, + TimeMax: 120, + BlockedAdvDomains: []string{"company1.com", "company2.com"}, + } + if got := subject; !reflect.DeepEqual(exp, got) { + t.Errorf("expected %+v, got %+v", exp, got) + } +} - It("should validate", func() { - Expect((&BidRequest{}).Validate()).To(Equal(ErrInvalidReqNoID)) - Expect((&BidRequest{ID: "A"}).Validate()).To(Equal(ErrInvalidReqNoImps)) - Expect((&BidRequest{ID: "A", Impressions: []Impression{{ID: "1"}}, Site: &Site{}, App: &App{}}).Validate()).To(Equal(ErrInvalidReqMultiInv)) - - Expect((&BidRequest{ID: "A", Impressions: []Impression{{ID: "1", Banner: &Banner{}}}}).Validate()).NotTo(HaveOccurred()) - Expect((&BidRequest{ID: "A", Impressions: []Impression{{ID: "1", Banner: &Banner{}}}, Site: &Site{}}).Validate()).NotTo(HaveOccurred()) - Expect((&BidRequest{ID: "A", Impressions: []Impression{{ID: "1", Banner: &Banner{}}}, App: &App{}}).Validate()).NotTo(HaveOccurred()) - Expect(subject.Validate()).NotTo(HaveOccurred()) - }) +func TestBidRequest_complex(t *testing.T) { + for _, kind := range []string{"exp", "video", "native"} { + var subject *BidRequest + if err := fixture("breq."+kind, &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } + if err := subject.Validate(); err != nil { + t.Fatalf("expected no error, got %v", err) + } + } +} -}) +func TestBidRequest_Validate(t *testing.T) { + subject := &BidRequest{} + if exp, got := ErrInvalidReqNoID, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } + subject = &BidRequest{ID: "RID"} + if exp, got := ErrInvalidReqNoImps, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } + subject = &BidRequest{ID: "A", Impressions: []Impression{{ID: "1"}}, Site: &Site{}, App: &App{}} + if exp, got := ErrInvalidReqMultiInv, subject.Validate(); !errors.Is(exp, got) { + t.Fatalf("expected %v, got %v", exp, got) + } +} diff --git a/bidresponse_test.go b/bidresponse_test.go index 3b94222..39fb075 100644 --- a/bidresponse_test.go +++ b/bidresponse_test.go @@ -1,54 +1,66 @@ -package openrtb +package openrtb_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "errors" + "reflect" + "testing" + + . "github.com/bsm/openrtb/v3" ) -var _ = Describe("BidResponse", func() { +func TestBidResponse(t *testing.T) { var subject *BidResponse + if err := fixture("bres.single", &subject); err != nil { + t.Fatalf("expected no error, got %v", err) + } - BeforeEach(func() { - Expect(fixture("bres.single", &subject)).To(Succeed()) - }) - - It("should parse complex responses", func() { - for _, kind := range []string{"multi", "pmp", "vast"} { - var req *BidResponse - Expect(fixture("bres."+kind, &req)).To(Succeed(), "for %s", kind) - Expect(req.Validate()).NotTo(HaveOccurred(), "for %s", kind) - } - }) - - It("should parse responses", func() { - Expect(subject).To(Equal(&BidResponse{ - ID: "BID-4-ZIMP-4b309eae-504a-4252-a8a8-4c8ceee9791a", - SeatBids: []SeatBid{ - { - Bids: []Bid{ - { - ID: "32a69c6ba388f110487f9d1e63f77b22d86e916b", - ImpID: "32a69c6ba388f110487f9d1e63f77b22d86e916b", - Price: 0.065445, - AdID: "529833ce55314b19e8796116", - NoticeURL: "http://ads.com/win/529833ce55314b19e8796116?won=${auction_price}", - AdMarkup: "