mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-04 05:10:40 +00:00
PackageCollection class.
This commit is contained in:
Vendored
+33
@@ -3,6 +3,7 @@ package debian
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/smira/aptly/database"
|
||||
"github.com/smira/aptly/utils"
|
||||
debc "github.com/smira/godebiancontrol"
|
||||
"github.com/ugorji/go/codec"
|
||||
@@ -108,3 +109,35 @@ func (p *Package) VerifyFile(filepath string) bool {
|
||||
}
|
||||
return st.Size() == p.Filesize
|
||||
}
|
||||
|
||||
// PackageCollection does management of packages in DB
|
||||
type PackageCollection struct {
|
||||
db database.Storage
|
||||
}
|
||||
|
||||
// NewPackageCollection creates new PackageCollection and binds it to database
|
||||
func NewPackageCollection(db database.Storage) *PackageCollection {
|
||||
return &PackageCollection{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// ByKey find package in DB by its key
|
||||
func (collection *PackageCollection) ByKey(key []byte) (*Package, error) {
|
||||
encoded, err := collection.db.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := &Package{}
|
||||
err = p.Decode(encoded)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Update adds or updates information about package in DB
|
||||
func (collection *PackageCollection) Update(p *Package) error {
|
||||
return collection.db.Put(p.Key(), p.Encode())
|
||||
}
|
||||
|
||||
Vendored
+32
@@ -1,6 +1,7 @@
|
||||
package debian
|
||||
|
||||
import (
|
||||
"github.com/smira/aptly/database"
|
||||
debc "github.com/smira/godebiancontrol"
|
||||
. "launchpad.net/gocheck"
|
||||
)
|
||||
@@ -66,3 +67,34 @@ func (s *PackageSuite) TestEquals(c *C) {
|
||||
p2.Depends = []string{"package1"}
|
||||
c.Check(p.Equals(p2), Equals, false)
|
||||
}
|
||||
|
||||
type PackageCollectionSuite struct {
|
||||
collection *PackageCollection
|
||||
p *Package
|
||||
db database.Storage
|
||||
}
|
||||
|
||||
var _ = Suite(&PackageCollectionSuite{})
|
||||
|
||||
func (s *PackageCollectionSuite) SetUpTest(c *C) {
|
||||
para := make(debc.Paragraph)
|
||||
for k, v := range packagePara {
|
||||
para[k] = v
|
||||
}
|
||||
s.p = NewPackageFromControlFile(para)
|
||||
s.db, _ = database.OpenDB(c.MkDir())
|
||||
s.collection = NewPackageCollection(s.db)
|
||||
}
|
||||
|
||||
func (s *PackageCollectionSuite) TearDownTest(c *C) {
|
||||
s.db.Close()
|
||||
}
|
||||
|
||||
func (s *PackageCollectionSuite) TestUpdateByKey(c *C) {
|
||||
err := s.collection.Update(s.p)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
p2, err := s.collection.ByKey(s.p.Key())
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(p2.Equals(s.p), Equals, true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user