/
githubmirror
/
postgres-compatibility-tests
Обзор
Документация
Войти
/
githubmirror
/
postgres-compatibility-tests
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
apps/urlsh/patch.diff
149 строк
4 KB
Timofey Koolin
fixes in urlsh docker
08 сен 2023, 18:16
08 сен 2023, 18:16
70a2018
Код
Авторство
О чём код?
diff -ruN /original-sources/cache/redis_test.go ./cache/redis_test.go --- /original-sources/cache/redis_test.go 2022-10-24 12:15:25.000000000 +0000 +++ ./cache/redis_test.go 1970-01-01 00:00:00.000000000 +0000 @@ -1,64 +0,0 @@ -package cache - -import ( - "net/http" - "os" - "testing" - - "github.com/adhocore/urlsh/model" - "github.com/adhocore/urlsh/util" -) - -func TestConnection(t *testing.T) { - key := "APP_CACHE_HOST" - old := os.Getenv(key) - - t.Run("nil", func(t *testing.T) { - _ = os.Setenv(key, "") - - t.Run("not nil", func(t *testing.T) { - _ = os.Setenv(key, old) - if nil == Connection() { - t.Errorf("connection should not be nil") - } - }) - }) -} - -func TestDeactivateURL(t *testing.T) { - t.Run("deactivate", func(t *testing.T) { - urlModel := model.URL{ShortCode: "testCode", OriginURL: "http://local-host/url"} - - DeactivateURL(urlModel) - if !hasURL(urlModel) { - t.Errorf("deactivated url should be in cache") - } - }) -} - -func TestLookupURL(t *testing.T) { - t.Run("save + lookup", func(t *testing.T) { - urlModel := model.URL{ShortCode: util.RandomString(8), OriginURL: "http://local-host/abc/def/ghi"} - - t.Run("save", func(t *testing.T) { - SavePopularURL(urlModel, true) - if !hasURL(urlModel) { - t.Errorf("saved url should be in cache") - } - - t.Run("lookup", func(t *testing.T) { - cachedModel, status := LookupURL(urlModel.ShortCode) - - if status != http.StatusGone { - t.Errorf("inactive url should be 410 gone") - } - if cachedModel.OriginURL != urlModel.OriginURL { - t.Errorf("cached origin_url should be same as input") - } - if cachedModel.ShortCode != urlModel.ShortCode { - t.Errorf("cached short_code should be same as input") - } - }) - }) - }) -} diff -ruN /original-sources/common/random.go ./common/random.go --- /original-sources/common/random.go 1970-01-01 00:00:00.000000000 +0000 +++ ./common/random.go 2023-08-28 11:32:08.109880233 +0000 @@ -0,0 +1,15 @@ +package common + +import ( + "crypto/rand" + "encoding/binary" +) + +func RandID() uint { + var res uint32 + err := binary.Read(rand.Reader, binary.LittleEndian, &res) + if err != nil { + panic(err) + } + return uint(res) +} diff -ruN /original-sources/model/keyword.go ./model/keyword.go --- /original-sources/model/keyword.go 2022-10-24 12:15:25.000000000 +0000 +++ ./model/keyword.go 2023-08-28 11:32:08.110880259 +0000 @@ -1,7 +1,17 @@ package model +import ( + "github.com/adhocore/urlsh/common" + "gorm.io/gorm" +) + // Keyword is model for keywords type Keyword struct { ID uint `json:"-" gorm:"primaryKey"` - Keyword string `json:"keyword" gorm:"size:25;unique;not null"` + Keyword string `json:"keyword" gorm:"size:25"` +} + +func (keyword *Keyword) BeforeCreate(tx *gorm.DB) (err error) { + keyword.ID = common.RandID() + return nil } diff -ruN /original-sources/model/url.go ./model/url.go --- /original-sources/model/url.go 2022-10-24 12:15:25.000000000 +0000 +++ ./model/url.go 2023-08-28 11:32:08.110880259 +0000 @@ -1,16 +1,20 @@ package model -import "time" +import ( + "github.com/adhocore/urlsh/common" + "gorm.io/gorm" + "time" +) // URL is model for short urls type URL struct { ID uint `json:"-" gorm:"primaryKey"` - ShortCode string `json:"short_code" gorm:"size:12;uniqueIndex;not null"` - OriginURL string `json:"origin_url" gorm:"size:2048;index;not null"` - Hits uint `json:"hits" gorm:"default:0;not null"` - Deleted bool `json:"is_deleted" gorm:"default:false;not null"` - CreatedAt time.Time `json:"-" gorm:"not null"` - UpdatedAt time.Time `json:"-" gorm:"not null"` + ShortCode string `json:"short_code" gorm:"size:12"` + OriginURL string `json:"origin_url" gorm:"size:2048"` + Hits uint `json:"hits" gorm:""` + Deleted bool `json:"is_deleted" gorm:""` + CreatedAt time.Time `json:"-" gorm:""` + UpdatedAt time.Time `json:"-" gorm:""` ExpiresOn time.Time `json:"expires_on"` Keywords []Keyword `json:"-" gorm:"many2many:url_keywords"` } @@ -24,3 +28,8 @@ return urlModel.ExpiresOn.In(time.UTC).After(time.Now().In(time.UTC)) } + +func (urlModel *URL) BeforeCreate(tx *gorm.DB) (err error) { + urlModel.ID = common.RandID() + return nil +}