Skip to content

Commit

Permalink
Merge pull request #211 from twitchdev/mock-api-jan-23
Browse files Browse the repository at this point in the history
Updating Mock API to changes as of end of January 2023
  • Loading branch information
Xemdo authored Feb 15, 2023
2 parents 28810f9 + 2957aa8 commit 65b3af7
Show file tree
Hide file tree
Showing 64 changed files with 3,450 additions and 432 deletions.
5 changes: 3 additions & 2 deletions internal/database/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Category struct {
Name string `db:"category_name" json:"name"`
BoxartURL string `json:"box_art_url"`
ViewerCount int `db:"vc" json:"-"`
IGDB string `db:"igdb_id" json:"igdb_id"`
}

func (q *Query) GetCategories(cat Category) (*DBResponse, error) {
Expand Down Expand Up @@ -45,7 +46,7 @@ func (q *Query) GetCategories(cat Category) (*DBResponse, error) {
}

func (q *Query) InsertCategory(category Category, upsert bool) error {
_, err := q.DB.NamedExec(`insert into categories values(:id, :category_name)`, category)
_, err := q.DB.NamedExec(`insert into categories values(:id, :category_name, :igdb_id)`, category)
return err
}

Expand Down Expand Up @@ -73,7 +74,7 @@ func (q *Query) SearchCategories(query string) (*DBResponse, error) {
func (q *Query) GetTopGames() (*DBResponse, error) {
r := []Category{}

err := q.DB.Select(&r, "select c.id, c.category_name, IFNULL(SUM(s.viewer_count),0) as vc from categories c left join users u on c.id = u.category_id left join streams s on s.broadcaster_id = u.id group by c.id, c.category_name order by vc desc")
err := q.DB.Select(&r, "select c.id, c.category_name, c.igdb_id, IFNULL(SUM(s.viewer_count),0) as vc from categories c left join users u on c.id = u.category_id left join streams s on s.broadcaster_id = u.id group by c.id, c.category_name order by vc desc"+q.SQL)
if err != nil {
return nil, err
}
Expand Down
54 changes: 54 additions & 0 deletions internal/database/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package database

type ChatSettings struct {
BroadcasterID string `db:"broadcaster_id" json:"broadcaster_id"`
SlowMode *bool `db:"slow_mode" json:"slow_mode"`
SlowModeWaitTime *int `db:"slow_mode_wait_time" json:"slow_mode_wait_time"`
FollowerMode *bool `db:"follower_mode" json:"follower_mode"`
FollowerModeDuration *int `db:"follower_mode_duration" json:"follower_mode_duration"`
SubscriberMode *bool `db:"subscriber_mode" json:"subscriber_mode"`
EmoteMode *bool `db:"emote_mode" json:"emote_mode"`
UniqueChatMode *bool `db:"unique_chat_mode" json:"unique_chat_mode"`
NonModeratorChatDelay *bool `db:"non_moderator_chat_delay" json:"non_moderator_chat_delay"`
NonModeratorChatDelayDuration *int `db:"non_moderator_chat_delay_duration" json:"non_moderator_chat_delay_duration"`

// Shield mode
ShieldModeIsActive bool `db:"shieldmode_is_active" json:"-"`
ShieldModeModeratorID string `db:"shieldmode_moderator_id" json:"-"`
ShieldModeModeratorLogin string `db:"shieldmode_moderator_login" json:"-"`
ShieldModeModeratorName string `db:"shieldmode_moderator_name" json:"-"`
ShieldModeLastActivated string `db:"shieldmode_last_activated" json:"-"`
}

func (q *Query) GetChatSettingsByBroadcaster(broadcaster string) (*DBResponse, error) {
var r []ChatSettings

err := q.DB.Select(&r, "SELECT * FROM chat_settings WHERE broadcaster_id = $1", broadcaster)
if err != nil {
return nil, err
}

dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
}

// No cursor because there should only ever be one result

return &dbr, err
}

func (q *Query) InsertChatSettings(s ChatSettings) error {
stmt := generateInsertSQL("chat_settings", "broadcaster_id", s, true)
_, err := q.DB.NamedExec(stmt, s)
return err
}

func (q *Query) UpdateChatSettings(s ChatSettings) error {
sql := generateUpdateSQL("chat_settings", []string{"broadcaster_id"}, s)
_, err := q.DB.NamedExec(sql, s)
return err
}
36 changes: 29 additions & 7 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const TEST_USER_ID = "1"
const TEST_USER_LOGIN = "testing_user1"
const TEST_USER_ID_2 = "2"
const TEST_USER_LOGIN_2 = "second_user"
const TEST_USER_ID_99 = "99"
const TEST_USER_LOGIN_99 = "non_mod_user"
const CATEGORY_ID = "1"
const IGDB_ID = "123"

var db CLIDatabase
var q *Query
Expand All @@ -49,7 +52,7 @@ func TestMain(m *testing.M) {
}
q = db.NewQuery(nil, 100)

err = q.InsertCategory(Category{Name: "test", ID: CATEGORY_ID, ViewerCount: 0, BoxartURL: ""}, false)
err = q.InsertCategory(Category{Name: "test", ID: CATEGORY_ID, IGDB: IGDB_ID, ViewerCount: 0, BoxartURL: ""}, false)
log.Print(err)

err = q.InsertUser(User{
Expand Down Expand Up @@ -86,6 +89,23 @@ func TestMain(m *testing.M) {
}, false)
log.Print(err)

err = q.InsertUser(User{
ID: TEST_USER_ID_99,
UserLogin: TEST_USER_LOGIN_99,
DisplayName: TEST_USER_LOGIN_99,
Email: "",
BroadcasterType: "partner",
UserType: "testing",
UserDescription: "hi mom",
CreatedAt: util.GetTimestamp().Format(time.RFC3339),
ModifiedAt: util.GetTimestamp().Format(time.RFC3339),
CategoryID: sql.NullString{String: "", Valid: false},
Title: "hello",
Language: "en",
Delay: 0,
}, false)
log.Print(err)

os.Exit(m.Run())
db.DB.Close()
}
Expand Down Expand Up @@ -170,7 +190,7 @@ func TestAPI(t *testing.T) {
func TestCategories(t *testing.T) {
a := test_setup.SetupTestEnv(t)

c := Category{Name: "test", ID: CATEGORY_ID}
c := Category{Name: "test", ID: CATEGORY_ID, IGDB: IGDB_ID}
err := q.InsertCategory(c, false)
a.NotNil(err)

Expand All @@ -180,6 +200,7 @@ func TestCategories(t *testing.T) {
categories := dbr.Data.([]Category)
a.Len(categories, 1)
a.Equal(c.ID, categories[0].ID)
a.Equal(c.IGDB, categories[0].IGDB)

// search
dbr, err = q.SearchCategories("es")
Expand Down Expand Up @@ -213,7 +234,7 @@ func TestUsers(t *testing.T) {
Title: "hello",
Language: "en",
Delay: 0,
}, false)
}, true)
a.Nil(err)

err = q.InsertUser(User{
Expand All @@ -230,7 +251,7 @@ func TestUsers(t *testing.T) {
Title: "hello",
Language: "en",
Delay: 0,
}, false)
}, true)
a.Nil(err)

u, err := q.GetUser(User{ID: TEST_USER_ID})
Expand Down Expand Up @@ -406,7 +427,7 @@ func TestModeration(t *testing.T) {
moderators := dbr.Data.([]Moderator)
a.GreaterOrEqual(len(moderators), 1)

dbr, err = q.GetModeratorsForBroadcaster(TEST_USER_ID, "2")
dbr, err = q.GetModeratorsForBroadcaster(TEST_USER_ID)
a.Nil(err)
moderators = dbr.Data.([]Moderator)
a.GreaterOrEqual(len(moderators), 1)
Expand Down Expand Up @@ -604,7 +625,7 @@ func TestStreams(t *testing.T) {
dbr, err = q.GetStreamTags(TEST_USER_ID)
a.Nil(err)
tags = dbr.Data.([]Tag)
a.GreaterOrEqual(len(tags), 1)
a.GreaterOrEqual(len(tags), 0)

dbr, err = q.GetFollowedStreams(s.UserID)
a.Nil(err)
Expand All @@ -624,7 +645,7 @@ func TestStreams(t *testing.T) {
streams = dbr.Data.([]Stream)
a.GreaterOrEqual(len(streams), 1)
stream := streams[0]
a.GreaterOrEqual(len(stream.TagIDs), 1)
a.GreaterOrEqual(len(stream.TagIDs), 0)

err = q.DeleteAllStreamTags(s.UserID)
a.Nil(err)
Expand Down Expand Up @@ -771,6 +792,7 @@ func TestVideos(t *testing.T) {
ViewCount: 100,
Duration: 1234.5,
CreatedAt: util.GetTimestamp().Format(time.RFC3339),
VodOffset: int(util.RandomInt(3000)),
}

err = q.InsertClip(c)
Expand Down
13 changes: 7 additions & 6 deletions internal/database/drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package database
import "log"

type DropsEntitlement struct {
ID string `db:"id" json:"id" dbs:"de.id"`
UserID string `db:"user_id" json:"user_id"`
BenefitID string `db:"benefit_id" json:"benefit_id"`
GameID string `db:"game_id" json:"game_id"`
Timestamp string `db:"timestamp" json:"timestamp"`
Status string `db:"status" json:"fulfillment_status"`
ID string `db:"id" json:"id" dbs:"de.id"`
UserID string `db:"user_id" json:"user_id"`
BenefitID string `db:"benefit_id" json:"benefit_id"`
GameID string `db:"game_id" json:"game_id"`
Timestamp string `db:"timestamp" json:"timestamp"`
Status string `db:"status" json:"fulfillment_status"`
LastUpdated string `db:"last_updated" json:"last_updated"`
}

func (q *Query) GetDropsEntitlements(de DropsEntitlement) (*DBResponse, error) {
Expand Down
Loading

0 comments on commit 65b3af7

Please sign in to comment.