-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(controller): add unit tests for package entry encoding and decoding
- Loading branch information
1 parent
1b71a57
commit d2000a8
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package vacuum | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHandleAsyncStorePackage_NilPackage(t *testing.T) { | ||
logE := logrus.NewEntry(logrus.New()) | ||
|
||
vacuumCtrl := New(nil, nil) | ||
|
||
// Test | ||
err := vacuumCtrl.handleAsyncStorePackage(logE, nil) | ||
|
||
// Assert | ||
assert.Error(t, err) | ||
assert.Equal(t, "vacuumPkg is nil", err.Error()) | ||
} | ||
func TestEncodePackageEntry(t *testing.T) { | ||
pkgEntry := &PackageEntry{ | ||
LastUsageTime: time.Now(), | ||
Package: &Package{Name: "test-package"}, | ||
} | ||
|
||
data, err := encodePackageEntry(pkgEntry) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, data) | ||
|
||
var decodedEntry PackageEntry | ||
err = json.Unmarshal(data, &decodedEntry) | ||
assert.NoError(t, err) | ||
assert.Equal(t, pkgEntry.LastUsageTime.Unix(), decodedEntry.LastUsageTime.Unix()) | ||
assert.Equal(t, pkgEntry.Package.Name, decodedEntry.Package.Name) | ||
} | ||
|
||
func TestDecodePackageEntry(t *testing.T) { | ||
pkgEntry := &PackageEntry{ | ||
LastUsageTime: time.Now(), | ||
Package: &Package{Name: "test-package"}, | ||
} | ||
|
||
data, err := json.Marshal(pkgEntry) | ||
assert.NoError(t, err) | ||
|
||
decodedEntry, err := decodePackageEntry(data) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, decodedEntry) | ||
assert.Equal(t, pkgEntry.LastUsageTime.Unix(), decodedEntry.LastUsageTime.Unix()) | ||
assert.Equal(t, pkgEntry.Package.Name, decodedEntry.Package.Name) | ||
} | ||
|
||
func TestDecodePackageEntry_Error(t *testing.T) { | ||
_, err := decodePackageEntry([]byte("invalid json")) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "unmarshal package entry") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters