Skip to content

Commit ecc2c00

Browse files
kasarolzzwCoda-bot
andcommitted
test: [Coda] add manage repo snippet relation tests
(LogID: 20251023152033010091104016574B193) Co-Authored-By: Coda <[email protected]>
1 parent 7d59484 commit ecc2c00

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed

backend/modules/prompt/infra/repo/manage_test.go

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,156 @@ func TestManageRepoImpl_SaveDraft(t *testing.T) {
16741674
// },
16751675
// wantErr: errorx.New("Draft's base version is invalid, saving draft's base version = 1.0.0, original draft's base version = 0.9.0 "),
16761676
//},
1677+
{
1678+
name: "create draft with snippets",
1679+
fieldsGetter: func(ctrl *gomock.Controller) fields {
1680+
mockDB := dbmocks.NewMockProvider(ctrl)
1681+
mockDB.EXPECT().Transaction(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, fc func(*gorm.DB) error, opts ...db.Option) error {
1682+
return fc(nil)
1683+
})
1684+
1685+
mockBasicDAO := daomocks.NewMockIPromptBasicDAO(ctrl)
1686+
mockBasicDAO.EXPECT().Get(gomock.Any(), int64(1), gomock.Any()).Return(&model.PromptBasic{
1687+
ID: 1,
1688+
SpaceID: 100,
1689+
}, nil)
1690+
1691+
mockDraftDAO := daomocks.NewMockIPromptUserDraftDAO(ctrl)
1692+
mockDraftDAO.EXPECT().Get(gomock.Any(), int64(1), "test_user", gomock.Any()).Return(nil, nil)
1693+
1694+
mockIDGen := idgenmocks.NewMockIIDGenerator(ctrl)
1695+
mockIDGen.EXPECT().GenID(gomock.Any()).Return(int64(1001), nil)
1696+
1697+
mockDraftDAO.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, draft *model.PromptUserDraft, opts ...db.Option) error {
1698+
assert.Equal(t, int64(100), draft.SpaceID)
1699+
assert.Equal(t, "test_user", draft.UserID)
1700+
assert.True(t, draft.HasSnippets)
1701+
return nil
1702+
})
1703+
1704+
mockDraftDAO.EXPECT().GetByID(gomock.Any(), int64(1001), gomock.Any()).Return(&model.PromptUserDraft{
1705+
ID: 1001,
1706+
UserID: "test_user",
1707+
}, nil)
1708+
1709+
mockRelationDAO := daomocks.NewMockIPromptRelationDAO(ctrl)
1710+
mockRelationDAO.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*model.PromptRelation{}, nil)
1711+
1712+
mockIDGen.EXPECT().GenMultiIDs(gomock.Any(), 2).Return([]int64{4001, 4002}, nil)
1713+
mockRelationDAO.EXPECT().BatchCreate(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, relations []*model.PromptRelation, opts ...db.Option) error {
1714+
assert.Len(t, relations, 2)
1715+
for _, relation := range relations {
1716+
assert.Equal(t, int64(1), relation.MainPromptID)
1717+
assert.Equal(t, "", relation.MainPromptVersion)
1718+
assert.Equal(t, "test_user", relation.MainDraftUserID)
1719+
assert.Equal(t, int64(100), relation.SpaceID)
1720+
}
1721+
assert.Equal(t, int64(200), relations[0].SubPromptID)
1722+
assert.Equal(t, "v1", relations[0].SubPromptVersion)
1723+
assert.Equal(t, int64(201), relations[1].SubPromptID)
1724+
assert.Equal(t, "", relations[1].SubPromptVersion)
1725+
return nil
1726+
})
1727+
1728+
return fields{
1729+
db: mockDB,
1730+
promptBasicDAO: mockBasicDAO,
1731+
promptDraftDAO: mockDraftDAO,
1732+
promptRelationDAO: mockRelationDAO,
1733+
idgen: mockIDGen,
1734+
}
1735+
},
1736+
args: args{
1737+
ctx: context.Background(),
1738+
promptDO: &entity.Prompt{
1739+
ID: 1,
1740+
SpaceID: 100,
1741+
PromptDraft: &entity.PromptDraft{
1742+
DraftInfo: &entity.DraftInfo{
1743+
UserID: "test_user",
1744+
},
1745+
PromptDetail: &entity.PromptDetail{
1746+
PromptTemplate: &entity.PromptTemplate{
1747+
HasSnippets: true,
1748+
Snippets: []*entity.Prompt{
1749+
{
1750+
ID: 200,
1751+
PromptCommit: &entity.PromptCommit{
1752+
CommitInfo: &entity.CommitInfo{
1753+
Version: "v1",
1754+
},
1755+
},
1756+
},
1757+
{
1758+
ID: 201,
1759+
},
1760+
},
1761+
},
1762+
},
1763+
},
1764+
},
1765+
},
1766+
wantErr: nil,
1767+
},
1768+
{
1769+
name: "create draft with snippets relation error",
1770+
fieldsGetter: func(ctrl *gomock.Controller) fields {
1771+
mockDB := dbmocks.NewMockProvider(ctrl)
1772+
mockDB.EXPECT().Transaction(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, fc func(*gorm.DB) error, opts ...db.Option) error {
1773+
return fc(nil)
1774+
})
1775+
1776+
mockBasicDAO := daomocks.NewMockIPromptBasicDAO(ctrl)
1777+
mockBasicDAO.EXPECT().Get(gomock.Any(), int64(1), gomock.Any()).Return(&model.PromptBasic{
1778+
ID: 1,
1779+
SpaceID: 100,
1780+
}, nil)
1781+
1782+
mockDraftDAO := daomocks.NewMockIPromptUserDraftDAO(ctrl)
1783+
mockDraftDAO.EXPECT().Get(gomock.Any(), int64(1), "test_user", gomock.Any()).Return(nil, nil)
1784+
1785+
mockIDGen := idgenmocks.NewMockIIDGenerator(ctrl)
1786+
mockIDGen.EXPECT().GenID(gomock.Any()).Return(int64(1001), nil)
1787+
1788+
mockDraftDAO.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
1789+
mockDraftDAO.EXPECT().GetByID(gomock.Any(), int64(1001), gomock.Any()).Return(&model.PromptUserDraft{
1790+
ID: 1001,
1791+
UserID: "test_user",
1792+
}, nil)
1793+
1794+
mockRelationDAO := daomocks.NewMockIPromptRelationDAO(ctrl)
1795+
mockRelationDAO.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errorx.New("relation list error"))
1796+
1797+
return fields{
1798+
db: mockDB,
1799+
promptBasicDAO: mockBasicDAO,
1800+
promptDraftDAO: mockDraftDAO,
1801+
promptRelationDAO: mockRelationDAO,
1802+
idgen: mockIDGen,
1803+
}
1804+
},
1805+
args: args{
1806+
ctx: context.Background(),
1807+
promptDO: &entity.Prompt{
1808+
ID: 1,
1809+
SpaceID: 100,
1810+
PromptDraft: &entity.PromptDraft{
1811+
DraftInfo: &entity.DraftInfo{
1812+
UserID: "test_user",
1813+
},
1814+
PromptDetail: &entity.PromptDetail{
1815+
PromptTemplate: &entity.PromptTemplate{
1816+
HasSnippets: true,
1817+
Snippets: []*entity.Prompt{
1818+
{ID: 200},
1819+
},
1820+
},
1821+
},
1822+
},
1823+
},
1824+
},
1825+
wantErr: errorx.New("relation list error"),
1826+
},
16771827
{
16781828
name: "update draft with no changes",
16791829
fieldsGetter: func(ctrl *gomock.Controller) fields {
@@ -1798,6 +1948,121 @@ func TestManageRepoImpl_SaveDraft(t *testing.T) {
17981948
},
17991949
wantErr: nil,
18001950
},
1951+
{
1952+
name: "update draft with snippets",
1953+
fieldsGetter: func(ctrl *gomock.Controller) fields {
1954+
mockDB := dbmocks.NewMockProvider(ctrl)
1955+
mockDB.EXPECT().Transaction(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, fc func(*gorm.DB) error, opts ...db.Option) error {
1956+
return fc(nil)
1957+
})
1958+
1959+
mockBasicDAO := daomocks.NewMockIPromptBasicDAO(ctrl)
1960+
mockBasicDAO.EXPECT().Get(gomock.Any(), int64(1), gomock.Any()).Return(&model.PromptBasic{
1961+
ID: 1,
1962+
SpaceID: 100,
1963+
}, nil)
1964+
1965+
mockCommitDAO := daomocks.NewMockIPromptCommitDAO(ctrl)
1966+
mockCommitDAO.EXPECT().Get(gomock.Any(), int64(1), "v1", gomock.Any()).Return(&model.PromptCommit{
1967+
Version: "v1",
1968+
}, nil)
1969+
1970+
mockDraftDAO := daomocks.NewMockIPromptUserDraftDAO(ctrl)
1971+
mockDraftDAO.EXPECT().Get(gomock.Any(), int64(1), "test_user", gomock.Any()).Return(&model.PromptUserDraft{
1972+
ID: 1001,
1973+
BaseVersion: "v1",
1974+
}, nil)
1975+
mockDraftDAO.EXPECT().Update(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, draft *model.PromptUserDraft, opts ...db.Option) error {
1976+
assert.Equal(t, int64(1001), draft.ID)
1977+
assert.True(t, draft.HasSnippets)
1978+
return nil
1979+
})
1980+
mockDraftDAO.EXPECT().GetByID(gomock.Any(), int64(1001), gomock.Any()).Return(&model.PromptUserDraft{
1981+
ID: 1001,
1982+
UserID: "test_user",
1983+
}, nil)
1984+
1985+
mockRelationDAO := daomocks.NewMockIPromptRelationDAO(ctrl)
1986+
mockRelationDAO.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*model.PromptRelation{
1987+
{
1988+
ID: 2001,
1989+
SpaceID: 100,
1990+
MainPromptID: 1,
1991+
MainPromptVersion: "",
1992+
MainDraftUserID: "test_user",
1993+
SubPromptID: 200,
1994+
SubPromptVersion: "v1",
1995+
},
1996+
{
1997+
ID: 2002,
1998+
SpaceID: 100,
1999+
MainPromptID: 1,
2000+
MainPromptVersion: "",
2001+
MainDraftUserID: "test_user",
2002+
SubPromptID: 201,
2003+
SubPromptVersion: "old",
2004+
},
2005+
}, nil)
2006+
mockRelationDAO.EXPECT().BatchDeleteByIDs(gomock.Any(), []int64{2002}, gomock.Any()).Return(nil)
2007+
2008+
mockIDGen := idgenmocks.NewMockIIDGenerator(ctrl)
2009+
mockIDGen.EXPECT().GenMultiIDs(gomock.Any(), 1).Return([]int64{4001}, nil)
2010+
mockRelationDAO.EXPECT().BatchCreate(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, relations []*model.PromptRelation, opts ...db.Option) error {
2011+
assert.Len(t, relations, 1)
2012+
assert.Equal(t, int64(1), relations[0].MainPromptID)
2013+
assert.Equal(t, "test_user", relations[0].MainDraftUserID)
2014+
assert.Equal(t, int64(202), relations[0].SubPromptID)
2015+
assert.Equal(t, "v2", relations[0].SubPromptVersion)
2016+
return nil
2017+
})
2018+
2019+
return fields{
2020+
db: mockDB,
2021+
promptBasicDAO: mockBasicDAO,
2022+
promptCommitDAO: mockCommitDAO,
2023+
promptDraftDAO: mockDraftDAO,
2024+
promptRelationDAO: mockRelationDAO,
2025+
idgen: mockIDGen,
2026+
}
2027+
},
2028+
args: args{
2029+
ctx: context.Background(),
2030+
promptDO: &entity.Prompt{
2031+
ID: 1,
2032+
SpaceID: 100,
2033+
PromptDraft: &entity.PromptDraft{
2034+
DraftInfo: &entity.DraftInfo{
2035+
UserID: "test_user",
2036+
BaseVersion: "v1",
2037+
},
2038+
PromptDetail: &entity.PromptDetail{
2039+
PromptTemplate: &entity.PromptTemplate{
2040+
HasSnippets: true,
2041+
Snippets: []*entity.Prompt{
2042+
{
2043+
ID: 200,
2044+
PromptCommit: &entity.PromptCommit{
2045+
CommitInfo: &entity.CommitInfo{
2046+
Version: "v1",
2047+
},
2048+
},
2049+
},
2050+
{
2051+
ID: 202,
2052+
PromptCommit: &entity.PromptCommit{
2053+
CommitInfo: &entity.CommitInfo{
2054+
Version: "v2",
2055+
},
2056+
},
2057+
},
2058+
},
2059+
},
2060+
},
2061+
},
2062+
},
2063+
},
2064+
wantErr: nil,
2065+
},
18012066
{
18022067
name: "db error",
18032068
fieldsGetter: func(ctrl *gomock.Controller) fields {

0 commit comments

Comments
 (0)