|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package v1_22 //nolint |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/migrations/base" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_UpdateBadgeColName(t *testing.T) { |
| 16 | + type Badge struct { |
| 17 | + ID int64 `xorm:"pk autoincr"` |
| 18 | + Description string |
| 19 | + ImageURL string |
| 20 | + } |
| 21 | + |
| 22 | + // Prepare and load the testing database |
| 23 | + x, deferable := base.PrepareTestEnv(t, 0, new(BadgeUnique), new(Badge)) |
| 24 | + defer deferable() |
| 25 | + if x == nil || t.Failed() { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + oldBadges := []Badge{ |
| 30 | + {ID: 1, Description: "Test Badge 1", ImageURL: "https://example.com/badge1.png"}, |
| 31 | + {ID: 2, Description: "Test Badge 2", ImageURL: "https://example.com/badge2.png"}, |
| 32 | + {ID: 3, Description: "Test Badge 3", ImageURL: "https://example.com/badge3.png"}, |
| 33 | + } |
| 34 | + |
| 35 | + for _, badge := range oldBadges { |
| 36 | + _, err := x.Insert(&badge) |
| 37 | + assert.NoError(t, err) |
| 38 | + } |
| 39 | + |
| 40 | + if err := UseSlugInsteadOfIDForBadges(x); err != nil { |
| 41 | + assert.NoError(t, err) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + got := []BadgeUnique{} |
| 46 | + if err := x.Table("badge").Asc("id").Find(&got); !assert.NoError(t, err) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + for i, e := range oldBadges { |
| 51 | + got := got[i] |
| 52 | + assert.Equal(t, e.ID, got.ID) |
| 53 | + assert.Equal(t, fmt.Sprintf("%d", e.ID), got.Slug) |
| 54 | + } |
| 55 | + |
| 56 | + // TODO: check if badges have been updated |
| 57 | +} |
0 commit comments