Skip to content

Commit

Permalink
fix(domain): handle multi-seasons from mal agent (#34)
Browse files Browse the repository at this point in the history
* fix(domain): handle multi-seasons from mal agent

* test: update test for multi-season mal
  • Loading branch information
varoOP committed Oct 13, 2023
1 parent 1ab0d3a commit 1d06346
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 50 deletions.
4 changes: 2 additions & 2 deletions internal/domain/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (a *AnimeUpdate) processScrobble(ctx context.Context) error {
return nil
}

if a.Media.Season == 1 {
if a.Media.Season == 1 || a.Media.IdSource == "mal" {
a.Malid, err = a.Media.GetMalID(ctx, a.DB)
if err != nil {
return err
Expand Down Expand Up @@ -165,7 +165,7 @@ func (a *AnimeUpdate) processRate(ctx context.Context) error {
return nil
}

if a.Media.Season == 1 {
if a.Media.Season == 1 || a.Media.IdSource == "mal" {
a.Malid, err = a.Media.GetMalID(ctx, a.DB)
if err != nil {
return err
Expand Down
130 changes: 82 additions & 48 deletions internal/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package server

import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/nstratos/go-myanimelist/mal"
"github.com/rs/zerolog"
"github.com/varoOP/shinkro/internal/database"
"github.com/varoOP/shinkro/internal/domain"
"github.com/varoOP/shinkro/pkg/plex"
"golang.org/x/oauth2"
)

Expand All @@ -36,7 +35,6 @@ func TestPlex(t *testing.T) {
tests := []struct {
name string
have have
want *mal.AnimeListStatus
}{
{
name: "HAMA_Episode_DB_Rate_1",
Expand All @@ -50,6 +48,8 @@ func TestPlex(t *testing.T) {
"Metadata": {
"guid": "com.plexapp.agents.hama://anidb-17494/1/7?lang=en",
"type": "episode",
"parentIndex": 1,
"index": 7,
"grandparentTitle": "Tomo-chan wa Onnanoko!"
}
}`,
Expand All @@ -59,9 +59,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 52305),
},
want: &mal.AnimeListStatus{
Score: 8,
},
},
{
name: "HAMA_Episode_DB_Scrobble_1",
Expand All @@ -74,6 +71,8 @@ func TestPlex(t *testing.T) {
"Metadata": {
"guid": "com.plexapp.agents.hama://anidb-17290/1/9?lang=en",
"type": "episode",
"parentIndex": 1,
"index": 9,
"grandparentTitle": "Isekai Nonbiri Nouka"
}
}`,
Expand All @@ -83,9 +82,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 51462),
},
want: &mal.AnimeListStatus{
NumEpisodesWatched: 9,
},
},
{
name: "HAMA_Episode_Mapping_Scrobble_1",
Expand All @@ -98,6 +94,8 @@ func TestPlex(t *testing.T) {
"Metadata": {
"guid": "com.plexapp.agents.hama://tvdb-289882/4/22?lang=en",
"type": "episode",
"parentIndex": 4,
"index": 22,
"grandparentTitle": "Dungeon ni Deai o Motomeru no wa Machigatte Iru Darouka: Familia Myth"
}
}`,
Expand All @@ -107,9 +105,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
NumEpisodesWatched: 11,
},
},
{
name: "HAMA_Episode_Mapping_Scrobble_2",
Expand All @@ -122,6 +117,8 @@ func TestPlex(t *testing.T) {
"Metadata": {
"guid": "com.plexapp.agents.hama://tvdb-316842/0/38?lang=en",
"type": "episode",
"parentIndex": 0,
"index": 38,
"grandparentTitle": "Mahou Tsukai no Yome"
}
}`,
Expand All @@ -131,9 +128,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
NumEpisodesWatched: 3,
},
},
{
name: "MAL_Movie_Scrobble_1",
Expand All @@ -145,7 +139,10 @@ func TestPlex(t *testing.T) {
},
"Metadata": {
"guid": "net.fribbtastic.coding.plex.myanimelist://28805?lang=en",
"type": "movie"
"type": "movie",
"parentIndex": 1,
"index": 1,
"title":"Mal_Movie"
}
}`,
event: scrobbleEvent,
Expand All @@ -154,9 +151,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
NumEpisodesWatched: 1,
},
},
{
name: "MAL_Movie_Rate_1",
Expand All @@ -169,7 +163,10 @@ func TestPlex(t *testing.T) {
},
"Metadata": {
"guid": "net.fribbtastic.coding.plex.myanimelist://32281?lang=en",
"type": "movie"
"type": "movie",
"parentIndex": 1,
"index": 1,
"title": "Mal_Movie"
}
}`,
event: rateEvent,
Expand All @@ -178,9 +175,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
Score: 8,
},
},
{
name: "MAL_Episode_Scrobble_1",
Expand All @@ -192,7 +186,10 @@ func TestPlex(t *testing.T) {
},
"Metadata": {
"guid": "net.fribbtastic.coding.plex.myanimelist://52173/1/5?lang=en",
"type": "episode"
"type": "episode",
"parentIndex": 1,
"index": 5,
"grandparentTitle": "Mal_Episode"
}
}`,
event: scrobbleEvent,
Expand All @@ -201,8 +198,28 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
NumEpisodesWatched: 5,
},
{
name: "MAL_Episode_Scrobble_2",
have: have{
data: `{
"event": "media.scrobble",
"Account": {
"title": "TestPlexUser"
},
"Metadata": {
"guid": "net.fribbtastic.coding.plex.myanimelist://47160/2/2?lang=en",
"type": "episode",
"parentIndex": 2,
"index": 2,
"grandparentTitle": "Goblin Slayer II"
}
}`,
event: scrobbleEvent,
cfg: &domain.Config{
PlexUser: "TestPlexUser",
},
db: createMockDB(t, 0),
},
},
{
Expand All @@ -216,7 +233,10 @@ func TestPlex(t *testing.T) {
},
"Metadata": {
"guid": "net.fribbtastic.coding.plex.myanimelist://52305/1/7?lang=en",
"type": "episode"
"type": "episode",
"parentIndex": 1,
"index": 7,
"grandparentTitle": "Mal_Episode"
}
}`,
event: rateEvent,
Expand All @@ -225,9 +245,6 @@ func TestPlex(t *testing.T) {
},
db: createMockDB(t, 0),
},
want: &mal.AnimeListStatus{
Score: 7,
},
},
}

Expand All @@ -246,25 +263,34 @@ func TestPlex(t *testing.T) {
}
}

func createMultipartForm(t *testing.T, data string) (*bytes.Buffer, *multipart.Writer) {
// func createMultipartForm(t *testing.T, data string) (*bytes.Buffer, *multipart.Writer) {

body := &bytes.Buffer{}
// body := &bytes.Buffer{}

w := multipart.NewWriter(body)
defer w.Close()
// w := multipart.NewWriter(body)
// defer w.Close()

fw, err := w.CreateFormField("payload")
if err != nil {
t.Fatal(err)
}
// fw, err := w.CreateFormField("payload")
// if err != nil {
// t.Fatal(err)
// }

// _, err = io.Copy(fw, strings.NewReader(data))
// if err != nil {
// t.Fatal(err)
// }

// return body, w

_, err = io.Copy(fw, strings.NewReader(data))
// }

func createPlexPayload(data string) (*plex.PlexWebhook, error) {
p, err := plex.NewPlexWebhook([]byte(data))
if err != nil {
t.Fatal(err)
return nil, err
}

return body, w

return p, nil
}

func createMalclient(t *testing.T) []string {
Expand All @@ -290,7 +316,7 @@ func createMockDB(t *testing.T, malid int) *database.DB {

r := createMalclient(t)
rows := sqlmock.NewRows([]string{"client_id", "client_secret", "access_token"}).AddRow(r[0], r[1], r[2])
mock.ExpectQuery(`SELECT \* from malauth;`).WillReturnRows(rows)
mock.ExpectQuery(`SELECT client_id, client_secret, access_token from malauth;`).WillReturnRows(rows)
rows = sqlmock.NewRows([]string{"mal_id"}).AddRow(malid)
mock.ExpectQuery("SELECT mal_id from anime").WillReturnRows(rows)

Expand Down Expand Up @@ -319,9 +345,17 @@ func unmarshal(t *testing.T, path string, v any) {
}

func createRequest(t *testing.T, data string) *http.Request {
req := httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("")))
req.Header.Set("Content-Type", "application/json")
p, err := createPlexPayload(data)
if err != nil {
t.Errorf("failed to create plex payload. err: %v", err)
}

_, agent := isMetadataAgent(p)

b, w := createMultipartForm(t, data)
req := httptest.NewRequest("POST", "/", b)
req.Header.Set("Content-Type", w.FormDataContentType())
return req
ctx := req.Context()
ctx = context.WithValue(ctx, domain.PlexPayload, p)
ctx = context.WithValue(ctx, domain.Agent, agent)
return req.WithContext(ctx)
}
File renamed without changes.

0 comments on commit 1d06346

Please sign in to comment.