package seriessetmaster import ( "errors" "testing" "time" ) func TestService_GetAllSeries_Success(t *testing.T) { repo := &mockRepository{ findAllSeriesFunc: func(search string) ([]SeriesSetMaster, error) { return []SeriesSetMaster{ {ID: 1, SeriesName: "Evolusi Mega"}, {ID: 2, SeriesName: "Scarlet & Violet"}, }, nil }, } svc := NewService(repo) list, err := svc.GetAllSeries(ListSeriesQuery{Search: ""}) if err != nil { t.Fatalf("expected no error, got: %v", err) } if len(list) != 2 { t.Fatalf("expected 2 items, got: %d", len(list)) } if list[0].SeriesName != "Evolusi Mega" { t.Errorf("expected 'Evolusi Mega', got: %s", list[0].SeriesName) } } func TestService_GetAllSets_Success(t *testing.T) { parentID := uint(1) repo := &mockRepository{ findAllSetsFunc: func(search string, pID uint) ([]SeriesSetMaster, error) { return []SeriesSetMaster{ {ID: 10, ParentID: &parentID, SetCode: "MA1", SetName: "Evolusi Mega"}, }, nil }, } svc := NewService(repo) list, err := svc.GetAllSets(ListSetQuery{Search: "MA1", ParentID: 1}) if err != nil { t.Fatalf("expected no error, got: %v", err) } if len(list) != 1 { t.Fatalf("expected 1 item, got: %d", len(list)) } if list[0].SetCode != "MA1" { t.Errorf("expected 'MA1', got: %s", list[0].SetCode) } } func TestService_GetByID_Success(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return &SeriesSetMaster{ID: id, SeriesName: "Pedang & Perisai"}, nil }, } svc := NewService(repo) result, err := svc.GetByID(1) if err != nil { t.Fatalf("expected no error, got: %v", err) } if result.ID != 1 { t.Errorf("expected ID 1, got: %d", result.ID) } if result.SeriesName != "Pedang & Perisai" { t.Errorf("expected 'Pedang & Perisai', got: %s", result.SeriesName) } } func TestService_GetSetByCode_Success(t *testing.T) { parentID := uint(1) repo := &mockRepository{ findBySetCodeFunc: func(code string) (*SeriesSetMaster, error) { return &SeriesSetMaster{ID: 10, ParentID: &parentID, SetCode: code, SetName: "Evolusi Mega M-P"}, nil }, } svc := NewService(repo) result, err := svc.GetSetByCode("M-P") if err != nil { t.Fatalf("expected no error, got: %v", err) } if result.SetCode != "M-P" { t.Errorf("expected 'M-P', got: %s", result.SetCode) } } func TestService_GetByID_NotFound(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return nil, errors.New("record not found") }, } svc := NewService(repo) _, err := svc.GetByID(999) if !errors.Is(err, ErrSeriesSetNotFound) { t.Fatalf("expected ErrSeriesSetNotFound, got: %v", err) } } func TestService_UpdateSeries_Success(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return &SeriesSetMaster{ID: id, ParentID: nil, SeriesName: "Old Name"}, nil }, updateFunc: func(m *SeriesSetMaster) error { return nil }, } svc := NewService(repo) req := UpdateSeriesRequest{ Name: "New Series Name", Image: "/images/series/new.png", } updated, err := svc.UpdateSeries(1, req) if err != nil { t.Fatalf("expected no error, got: %v", err) } if updated.SeriesName != "New Series Name" { t.Errorf("expected 'New Series Name', got: %s", updated.SeriesName) } if updated.Image != "/images/series/new.png" { t.Errorf("expected Image '/images/series/new.png', got: %s", updated.Image) } } func TestService_UpdateSeries_NotFound(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return nil, errors.New("record not found") }, } svc := NewService(repo) _, err := svc.UpdateSeries(999, UpdateSeriesRequest{Name: "Test"}) if !errors.Is(err, ErrSeriesSetNotFound) { t.Fatalf("expected ErrSeriesSetNotFound, got: %v", err) } } func TestService_UpdateSeries_InvalidRecordType(t *testing.T) { parentID := uint(1) repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return &SeriesSetMaster{ID: id, ParentID: &parentID, SetCode: "MA1"}, nil }, } svc := NewService(repo) _, err := svc.UpdateSeries(1, UpdateSeriesRequest{Name: "Test"}) if err == nil { t.Fatal("expected error when trying to update a Set using UpdateSeries, got nil") } } func TestService_UpdateSet_Success(t *testing.T) { parentID := uint(1) repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { if id == 1 { return &SeriesSetMaster{ID: 1, ParentID: nil, SeriesName: "Evolusi Mega"}, nil } return &SeriesSetMaster{ID: id, ParentID: &parentID, SetCode: "OLD", SetName: "Old Set", SeriesName: "Evolusi Mega"}, nil }, updateFunc: func(m *SeriesSetMaster) error { return nil }, } svc := NewService(repo) now := time.Now() newParentID := uint(1) req := UpdateSetRequest{ ParentID: &newParentID, SetCode: "NEW1", SetName: "New Set Name", ReleaseDate: &now, } updated, err := svc.UpdateSet(10, req) if err != nil { t.Fatalf("expected no error, got: %v", err) } if updated.SetCode != "NEW1" { t.Errorf("expected 'NEW1', got: %s", updated.SetCode) } if updated.SetName != "New Set Name" { t.Errorf("expected 'New Set Name', got: %s", updated.SetName) } } func TestService_UpdateSet_NotFound(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return nil, errors.New("record not found") }, } svc := NewService(repo) _, err := svc.UpdateSet(999, UpdateSetRequest{SetName: "Test"}) if !errors.Is(err, ErrSeriesSetNotFound) { t.Fatalf("expected ErrSeriesSetNotFound, got: %v", err) } } func TestService_UpdateSet_InvalidRecordType(t *testing.T) { repo := &mockRepository{ findByIDFunc: func(id uint) (*SeriesSetMaster, error) { return &SeriesSetMaster{ID: id, ParentID: nil, SeriesName: "Evolusi Mega"}, nil }, } svc := NewService(repo) _, err := svc.UpdateSet(1, UpdateSetRequest{SetName: "Test"}) if err == nil { t.Fatal("expected error when trying to update a Series using UpdateSet, got nil") } }