Skip to content

Commit

Permalink
Merge 531d521 into d3e51a9
Browse files Browse the repository at this point in the history
  • Loading branch information
andrehp committed May 22, 2018
2 parents d3e51a9 + 531d521 commit 195872a
Show file tree
Hide file tree
Showing 21 changed files with 580 additions and 516 deletions.
6 changes: 4 additions & 2 deletions api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ var _ = BeforeEach(func() {
})

var _ = AfterEach(func() {
err := app.DB.(*runner.Tx).Rollback()
Expect(err).NotTo(HaveOccurred())
if !app.DB.(*runner.Tx).IsRollbacked {
err := app.DB.(*runner.Tx).Rollback()
Expect(err).NotTo(HaveOccurred())
}
app.DB = db
app.Clock = oTesting.MockClock{
CurrentTime: 1486678000,
Expand Down
2 changes: 2 additions & 0 deletions api/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ var _ = Describe("Game Handler", func() {
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("offer_instances").Exec()
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("offer_versions").Exec()
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("offers").Exec()
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("games").Exec()
Expand Down
2 changes: 1 addition & 1 deletion api/offer_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (h *OfferRequestHandler) offerInfo(w http.ResponseWriter, r *http.Request)
var err error
var offer *models.OfferToReturn
err = mr.WithSegment(models.SegmentModel, func() error {
offer, err = models.GetOfferInfo(r.Context(), h.App.DB, gameID, playerID, offerInstanceID, h.App.OffersCacheMaxAge, mr)
offer, err = models.GetOfferInfo(r.Context(), h.App.DB, gameID, offerInstanceID, h.App.OffersCacheMaxAge, mr)
return err
})

Expand Down
32 changes: 7 additions & 25 deletions api/offer_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ var _ = Describe("Offer Handler", func() {
// Create Offer by requesting it
gameID := "offers-game"
playerID := "player-1"
id := "6c4a79f2-24b8-4be9-93d4-12413b789823"
id := "35df52e7-3161-446f-975b-92f32871e37c"
var body map[string]int64

// View the Offer
Expand All @@ -535,7 +535,7 @@ var _ = Describe("Offer Handler", func() {
var offersToReturn map[string][]models.OfferToReturn
json.Unmarshal(recorder.Body.Bytes(), &offersToReturn)
offer := models.OfferToReturn{
ID: "6c4a79f2-24b8-4be9-93d4-12413b789823",
ID: "35df52e7-3161-446f-975b-92f32871e37c",
ProductID: "com.tfg.sample.3",
Contents: dat.JSON([]byte(`{"gems":5,"gold":100}`)),
Metadata: dat.JSON([]byte("{}")),
Expand All @@ -549,7 +549,7 @@ var _ = Describe("Offer Handler", func() {
// Create Offer by requesting it
gameID := "offers-game"
playerID := "player-1"
id := "6c4a79f2-24b8-4be9-93d4-12413b789823"
id := "35df52e7-3161-446f-975b-92f32871e37c"
var body map[string]int64

// View the Offer for the first time
Expand Down Expand Up @@ -597,24 +597,6 @@ var _ = Describe("Offer Handler", func() {
}
Expect(offersToReturn["store"]).NotTo(ContainElement(offer))
})

It("should return available offers from cache on second request", func() {
playerID := "player-1"
gameID := "offers-game"
url := fmt.Sprintf("/available-offers?player-id=%s&game-id=%s", playerID, gameID)
request, _ := http.NewRequest("GET", url, nil)

start := time.Now().UnixNano()
app.Router.ServeHTTP(recorder, request)
dbElapsedTime := time.Now().UnixNano() - start

recorder = httptest.NewRecorder()
start = time.Now().UnixNano()
app.Router.ServeHTTP(recorder, request)
cacheElapsedTime := time.Now().UnixNano() - start

Expect(dbElapsedTime).To(BeNumerically(">", cacheElapsedTime))
})
})

Describe("PUT /offers/claim", func() {
Expand Down Expand Up @@ -927,8 +909,8 @@ var _ = Describe("Offer Handler", func() {
var jsonBody map[string]string
json.Unmarshal(recorder.Body.Bytes(), &jsonBody)
Expect(jsonBody["code"]).To(Equal("OFF-001"))
Expect(jsonBody["description"]).To(Equal("offerInstance was not found with specified filters."))
Expect(jsonBody["error"]).To(Equal("offerInstanceNotFoundError"))
Expect(jsonBody["description"]).To(Equal("OfferInstance was not found with specified filters."))
Expect(jsonBody["error"]).To(Equal("OfferInstanceNotFoundError"))
})
})

Expand Down Expand Up @@ -1117,8 +1099,8 @@ var _ = Describe("Offer Handler", func() {
})

It("should not increment when is a retry request and rechead max view", func() {
offerInstanceID := "56fc0477-39f1-485c-898e-4909e9155eb1"
offerID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
offerInstanceID := "38cf3ed6-b999-4ee8-9e21-8f700532b37c"
offerID := "d5114990-77d7-45c4-ba5f-462fc86b213f"
gameID := "offers-game"
playerID := "player-1"
impressionID := uuid.NewV4().String()
Expand Down
4 changes: 2 additions & 2 deletions api/offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ var _ = Describe("Offer Template Handler", func() {
})

It("should update offer", func() {
id := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
id := "a411fbcf-dddc-4153-b42b-3f9b2684c965"
request, _ := http.NewRequest("PUT", fmt.Sprintf("/offers/%s", id), offerReader)

app.Router.ServeHTTP(recorder, request)
Expand Down Expand Up @@ -1029,7 +1029,7 @@ var _ = Describe("Offer Template Handler", func() {
"placement": placement,
"filters": dat.JSON([]byte(filters)),
})
id := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
id := "a411fbcf-dddc-4153-b42b-3f9b2684c965"
request, _ := http.NewRequest("PUT", fmt.Sprintf("/offers/%s", id), offerReader)

app.Router.ServeHTTP(recorder, request)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Offers API
- **period**: Enable player to buy offer every x times, at most y times. <ul><li>every: decimal number with unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"</li><li>max: maximum number of times this offer can be bought by the player</li></ul>If "every" is an empty string, then the offer can be bought max times with no time restriction. If "max" is 0, then the offer can be bought infinite times with time restriction. They can't be "" and 0 at the same time.
- **frequency**: Enable player to see offer on UI x/unit of time, at most y times. <ul><li>every: decimal number with unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"</li><li>max: maximum number of times this offer can be seen by the player</li></ul>If "every" is an empty string, then the offer can be seen max times with no time restriction. If "max" is 0, then the offer can be seen infinite times with time restriction. They can't be "" and 0 at the same time.
- **trigger**: Time when the offer is available.
- **filters**: The filters for the offer, they can be of three different types for a given attribute: <ul><li>interval: the attribute must define the beginning and/or end of the interval with "geq" and "lt", the interval includes the beginning but not the end</li><li>equality: the attribute must define the "eq", the value that the filter expects the attribute to be equal to, it should be a string</li><li>difference: the attribute must define "neq", the value that the filter expects the attribute to be different from, it should be a string</ul>An example: "{ "intervalValue": { "geq": 0.0, "lt": 10.0 }, "equalValue": { "eq": "John" } }". Please note that interval filters are only enabled if the game has the `allowInefficientQueries` property set to `true`.
- **filters**: The filters for the offer, they can be of three different types for a given attribute: <ul><li>interval: the attribute must define the beginning and/or end of the interval with "geq" and "lt", the interval includes the beginning but not the end</li><li>equality: the attribute must define the "eq", the value that the filter expects the attribute to be equal to, it should be a string</li><li>difference: the attribute must define "neq", the value that the filter expects the attribute to be different from, it should be a string</ul>An example: "{ "intervalValue": { "geq": 0.0, "lt": 10.0 }, "equalValue": { "eq": "John" } }". Please note that interval filters and differences are only enabled if the game has the `allowInefficientQueries` property set to `true`.
- **enabled**: True if the offer is enabled.
- **placement**: Where the offer is shown in the UI.
- **version**: Offer current version.
Expand Down Expand Up @@ -282,7 +282,7 @@ Offers API
- **period**: Enable player to buy offer every x times, at most y times. <ul><li>every: decimal number with unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"</li><li>max: maximum number of times this offer can be bought by the player</li></ul>If "every" is an empty string, then the offer can be bought max times with no time restriction. If "max" is 0, then the offer can be bought infinite times with time restriction. They can't be "" and 0 at the same time.
- **frequency**: Enable player to see offer on UI x/unit of time, at most y times. <ul><li>every: decimal number with unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"</li><li>max: maximum number of times this offer can be seen by the player</li></ul>If "every" is an empty string, then the offer can be seen max times with no time restriction. If "max" is 0, then the offer can be seen infinite times with time restriction. They can't be "" and 0 at the same time.
- **trigger**: Time when the offer is available.
- **filters**: The filters for the offer, they can be of three different types for a given attribute: <ul><li>interval: the attribute must define the beginning and/or end of the interval with "geq" and "lt", the interval includes the beginning but not the end</li><li>equality: the attribute must define the "eq", the value that the filter expects the attribute to be equal to, it should be a string</li><li>difference: the attribute must define "neq", the value that the filter expects the attribute to be different from, it should be a string</ul>An example: "{ "intervalValue": { "geq": 0.0, "lt": 10.0 }, "equalValue": { "eq": "John" } }".
- **filters**: The filters for the offer, they can be of three different types for a given attribute: <ul><li>interval: the attribute must define the beginning and/or end of the interval with "geq" and "lt", the interval includes the beginning but not the end</li><li>equality: the attribute must define the "eq", the value that the filter expects the attribute to be equal to, it should be a string</li><li>difference: the attribute must define "neq", the value that the filter expects the attribute to be different from, it should be a string</ul>An example: "{ "intervalValue": { "geq": 0.0, "lt": 10.0 }, "equalValue": { "eq": "John" } }". Please note that interval filters and differences are only enabled if the game has the `allowInefficientQueries` property set to `true`.
- **enabled**: True if the offer is enabled.
- **placement**: Where the offer is shown in the UI.
- **version**: Offer current version.
Expand Down
148 changes: 1 addition & 147 deletions fixtures/offer_instances.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#Copyright © 2017 Top Free Games <backend@tfgco.com>

-
id: eb7e8d2a-2739-4da3-aa31-7970b63bdad7
id: abcd8d2a-2739-4da3-aa31-8970b63bdad7
game_id: offers-game
player_id: player-1
offer_id: dd21ec96-2890-4ba0-b8e2-40ea67196990
Expand All @@ -15,149 +15,3 @@
cost: '{"gems": 500}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:36:12
-
id: 9cae0065-0ee1-4e67-b802-c29d1af0e8b5
game_id: offers-game
player_id: player-1
offer_id: dd21ec96-2890-4ba0-b8e2-40ea67196990
offer_version: 2
contents: '{"gems": 5, "gold": 100}'
cost: '{"gems": 500}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:33:12
-
id: 56fc0477-39f1-485c-898e-4909e9155eb1
game_id: offers-game
player_id: player-1
offer_id: dd21ec96-2890-4ba0-b8e2-40ea67196990
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
cost: '{"gems": 500}'
created_at: 2016-01-01 12:30:12
-
id: 35df52e7-3161-446f-975b-92f32871e37c
game_id: offers-game
player_id: player-2
offer_id: a411fbcf-dddc-4153-b42b-3f9b2684c965
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample.3
created_at: 2016-01-01 12:30:12
-
id: fe528bb0-dab6-4f5a-b6cd-347422fd9817
game_id: offers-game-max-freq
player_id: player-1
offer_id: 9456f6c4-f9f1-4dd9-8841-9e5770c8e62c
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: e716af67-42a3-47e9-b862-f3582e59ddf6
game_id: offers-game-max-period
player_id: player-1
offer_id: ed6147c1-1714-4da1-a072-bea600c8a6a2
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 51689ae5-eac4-4cb6-82bc-f91806305e99
game_id: offers-game-invalid-every-freq
player_id: player-1
offer_id: 5a2d15bc-8e4d-481e-a717-fcd2b67ea7e5
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: a16c2615-ea2f-47a3-9981-aae82df21c1e
game_id: offers-game-invalid-every-period
player_id: player-1
offer_id: cb06428b-71e1-47c5-a0e2-05e7c322f6fd
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 5ba8848f-1df0-45b3-b8b1-27a7d5eedd6a
game_id: limited-offers-game
player_id: player-1
offer_id: aa65a3f2-7cf8-4d76-957f-0a23a1bbbd32
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 6c4a79f2-24b8-4be9-93d4-12413b789823
game_id: offers-game
player_id: player-1
offer_id: a411fbcf-dddc-4153-b42b-3f9b2684c965
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample.3
created_at: 2016-01-01 12:30:12
-
id: 4407b770-5b24-4ffa-8563-0694d1a10156
game_id: offers-game
player_id: player-11
offer_id: 5fed76ab-1fd7-4a91-972d-bca228ce80c4
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 29593e1d-792a-4849-8236-9d7b80fc6f6c
game_id: another-game
player_id: player-11
offer_id: a2539a8c-55f2-4539-a8c0-929b240d8c80
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 0a90073e-a798-46a8-a4f2-6b32182672ff
game_id: another-game-v8
player_id: player-11
offer_id: 17e42a40-da28-44dc-abd1-0cef8c2dff42
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: f0e3ce8c-bf9b-4da2-886b-e1b5a62a18cb
game_id: another-game
player_id: player-12
offer_id: f1f74fcd-17ae-4ccd-a248-f77c60e78c8c
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 33f9bbc1-5e9e-4a80-ae95-8d74d8774629
game_id: another-game-with-filters
player_id: player-13
offer_id: 26baac0a-4a1a-42ba-a561-7fd3735b1799
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: 0eebb309-753c-4736-98f1-5be851e1ac4d
game_id: another-game-with-filters
player_id: player-13
offer_id: 03408436-f7d4-4ba0-ad0d-93e03f1c4267
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
-
id: b0bffdd6-5cb8-4b54-b250-349b18c07638
game_id: offers-game
player_id: player-14
offer_id: 27b0370f-bd61-4346-a10d-50ec052ae125
offer_version: 1
contents: '{"gems": 5, "gold": 100}'
product_id: com.tfg.sample
created_at: 2016-01-01 12:30:12
17 changes: 17 additions & 0 deletions fixtures/offer_players.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#offers api
#https://github.com/topfreegames/offers

#Licensed under the MIT license:
#http://www.opensource.org/licenses/mit-license
#Copyright © 2018 Top Free Games <backend@tfgco.com>

-
id: eb7e8d2a-2739-4da3-aa31-7970b63bdad7
game_id: offers-game
player_id: player-1
offer_id: dd21ec96-2890-4ba0-b8e2-40ea67196990
view_timestamp: 2017-02-09T22:06:35Z
transactions: '[]'
impressions: '[]'
claim_counter: 0
view_counter: 0
Loading

0 comments on commit 195872a

Please sign in to comment.