Added compressutil tests

This commit is contained in:
vishalnayak 2016-08-09 02:26:38 -04:00
parent f994c7ccd3
commit c81460add6
2 changed files with 217 additions and 2 deletions

View file

@ -6,7 +6,6 @@ import (
"compress/lzw" "compress/lzw"
"fmt" "fmt"
"io" "io"
"log"
) )
const ( const (
@ -156,6 +155,5 @@ func Decompress(data []byte, config *CompressionConfig) ([]byte, bool, error) {
return nil, false, err return nil, false, err
} }
log.Printf("decompressedBytes: %s\n", string(jsonBuf.Bytes()))
return jsonBuf.Bytes(), false, nil return jsonBuf.Bytes(), false, nil
} }

View file

@ -0,0 +1,217 @@
package compressutil
import (
"bytes"
"compress/gzip"
"encoding/json"
"testing"
)
func TestCompressUtil_CompressDecompress(t *testing.T) {
input := map[string]interface{}{
"sample": "data",
"verification": "process",
}
// Encode input into JSON
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(input); err != nil {
t.Fatal(err)
}
inputJSONBytes := buf.Bytes()
// Test nil configuration
if _, err := Compress(inputJSONBytes, nil); err == nil {
t.Fatal("expected an error")
}
// Test invalid configuration
if _, err := Compress(inputJSONBytes, &CompressionConfig{}); err == nil {
t.Fatal("expected an error")
}
// Compress input using lzw format
compressedJSONBytes, err := Compress(inputJSONBytes, &CompressionConfig{
Type: CompressionTypeLzw,
})
if err != nil {
t.Fatal("expected an error")
}
if len(compressedJSONBytes) == 0 {
t.Fatal("failed to compress data in lzw format")
}
// Check the presense of the canary
if compressedJSONBytes[0] != CompressionCanary {
t.Fatal("bad: compression canary: expected: %d actual: %d", CompressionCanary, compressedJSONBytes[0])
}
// Decompress the input and check the output
decompressedJSONBytes, uncompressed, err := Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeLzw,
})
if err != nil {
t.Fatal(err)
}
if uncompressed {
t.Fatal("failed to recognize compressed data")
}
if len(decompressedJSONBytes) == 0 {
t.Fatal("failed to decompress lzw formatted data")
}
if string(inputJSONBytes) != string(decompressedJSONBytes) {
t.Fatalf("bad: mismatch: inputJSONBytes: %s\n decompressedJSONBytes: %s", string(inputJSONBytes), string(decompressedJSONBytes))
}
// Decompress using wrong format
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err == nil {
t.Fatal("expected an error")
}
// Compress input using Gzip format, assume DefaultCompression
compressedJSONBytes, err = Compress(inputJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err != nil {
t.Fatal("expected an error")
}
if len(compressedJSONBytes) == 0 {
t.Fatal("failed to compress data in lzw format")
}
// Check the presense of the canary
if compressedJSONBytes[0] != CompressionCanary {
t.Fatal("bad: compression canary: expected: %d actual: %d", CompressionCanary, compressedJSONBytes[0])
}
// Decompress the input and check the output
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err != nil {
t.Fatal(err)
}
if uncompressed {
t.Fatal("failed to recognize compressed data")
}
if len(decompressedJSONBytes) == 0 {
t.Fatal("failed to decompress lzw formatted data")
}
if string(inputJSONBytes) != string(decompressedJSONBytes) {
t.Fatalf("bad: mismatch: inputJSONBytes: %s\n decompressedJSONBytes: %s", string(inputJSONBytes), string(decompressedJSONBytes))
}
// Compress input using Gzip format: DefaultCompression
compressedJSONBytes, err = Compress(inputJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
GzipCompressionLevel: gzip.DefaultCompression,
})
if err != nil {
t.Fatal("expected an error")
}
if len(compressedJSONBytes) == 0 {
t.Fatal("failed to compress data in lzw format")
}
// Check the presense of the canary
if compressedJSONBytes[0] != CompressionCanary {
t.Fatal("bad: compression canary: expected: %d actual: %d", CompressionCanary, compressedJSONBytes[0])
}
// Decompress the input and check the output
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err != nil {
t.Fatal(err)
}
if uncompressed {
t.Fatal("failed to recognize compressed data")
}
if len(decompressedJSONBytes) == 0 {
t.Fatal("failed to decompress lzw formatted data")
}
if string(inputJSONBytes) != string(decompressedJSONBytes) {
t.Fatalf("bad: mismatch: inputJSONBytes: %s\n decompressedJSONBytes: %s", string(inputJSONBytes), string(decompressedJSONBytes))
}
// Decompress using wrong format
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeLzw,
})
if err == nil {
t.Fatal("expected an error")
}
// Compress input using Gzip format, BestCompression
compressedJSONBytes, err = Compress(inputJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
GzipCompressionLevel: gzip.BestCompression,
})
if err != nil {
t.Fatal("expected an error")
}
if len(compressedJSONBytes) == 0 {
t.Fatal("failed to compress data in lzw format")
}
// Check the presense of the canary
if compressedJSONBytes[0] != CompressionCanary {
t.Fatal("bad: compression canary: expected: %d actual: %d", CompressionCanary, compressedJSONBytes[0])
}
// Decompress the input and check the output
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err != nil {
t.Fatal(err)
}
if uncompressed {
t.Fatal("failed to recognize compressed data")
}
if len(decompressedJSONBytes) == 0 {
t.Fatal("failed to decompress lzw formatted data")
}
if string(inputJSONBytes) != string(decompressedJSONBytes) {
t.Fatalf("bad: mismatch: inputJSONBytes: %s\n decompressedJSONBytes: %s", string(inputJSONBytes), string(decompressedJSONBytes))
}
// Compress input using Gzip format, BestSpeed
compressedJSONBytes, err = Compress(inputJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
GzipCompressionLevel: gzip.BestSpeed,
})
if err != nil {
t.Fatal("expected an error")
}
if len(compressedJSONBytes) == 0 {
t.Fatal("failed to compress data in lzw format")
}
// Check the presense of the canary
if compressedJSONBytes[0] != CompressionCanary {
t.Fatal("bad: compression canary: expected: %d actual: %d", CompressionCanary, compressedJSONBytes[0])
}
// Decompress the input and check the output
decompressedJSONBytes, uncompressed, err = Decompress(compressedJSONBytes, &CompressionConfig{
Type: CompressionTypeGzip,
})
if err != nil {
t.Fatal(err)
}
if uncompressed {
t.Fatal("failed to recognize compressed data")
}
if len(decompressedJSONBytes) == 0 {
t.Fatal("failed to decompress lzw formatted data")
}
if string(inputJSONBytes) != string(decompressedJSONBytes) {
t.Fatalf("bad: mismatch: inputJSONBytes: %s\n decompressedJSONBytes: %s", string(inputJSONBytes), string(decompressedJSONBytes))
}
}