improving code coverage

This commit is contained in:
Julien Neuhart
2018-04-05 10:36:38 +02:00
parent 15916e80e6
commit ee15a52001
8 changed files with 95 additions and 14 deletions

View File

@@ -161,8 +161,10 @@ var levels = map[string]logrus.Level{
type wrongLoggingLevelError struct{}
const wrongLoggingLevelErrorMessage = "Accepted values for logging level: DEBUG, INFO, WARN, ERROR, FATAL, PANIC"
func (e *wrongLoggingLevelError) Error() string {
return "Accepted values for logging level: DEBUG, INFO, WARN, ERROR, FATAL, PANIC"
return wrongLoggingLevelErrorMessage
}
// getLoggingLevelFromFileConfig returns a logrus level if a matching was found
@@ -186,8 +188,10 @@ var formatters = map[string]logrus.Formatter{
type wrongLoggingFormatError struct{}
const wrongLoggingFormatErrorMessage = "Accepted value for logging format: text, json"
func (e *wrongLoggingFormatError) Error() string {
return "Accepted value for logging format: text, json"
return wrongLoggingFormatErrorMessage
}
// getLoggingLevelFromFileConfig returns a logrus Formatter if a matching was found

View File

@@ -53,3 +53,17 @@ func TestNewAppConfig(t *testing.T) {
t.Error("AppConfig should have been instantiated!")
}
}
func TestWrongLoggingLevelError(t *testing.T) {
err := &wrongLoggingLevelError{}
if err.Error() != wrongLoggingLevelErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), wrongLoggingLevelErrorMessage)
}
}
func TestWrongLoggingFormatError(t *testing.T) {
err := &wrongLoggingFormatError{}
if err.Error() != wrongLoggingFormatErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), wrongLoggingFormatErrorMessage)
}
}

View File

@@ -30,8 +30,10 @@ func WithContentType(r *http.Request, contentType ghttp.ContentType) *http.Reque
type contentTypeNotFoundError struct{}
const contentTypeNotFoundErrorMessage = "The 'Content-Type' was not found in request context"
func (e *contentTypeNotFoundError) Error() string {
return "The 'Content-Type' was not found in request context"
return contentTypeNotFoundErrorMessage
}
// GetContentType returns the content type if found in
@@ -57,8 +59,10 @@ func WithConverter(r *http.Request, converter *converter.Converter) *http.Reques
type converterNotFoundError struct{}
const converterNotFoundErrorMessage = "The converter was not found in request context"
func (e *converterNotFoundError) Error() string {
return "The converter was not found in request context"
return converterNotFoundErrorMessage
}
// GetConverter returns the converter if found in
@@ -84,8 +88,10 @@ func WithResultFilePath(r *http.Request, resultFilePath string) *http.Request {
type resultFilePathNotFoundError struct{}
const resultFilePathNotFoundErrorMessage = "The result file path was not found in request context"
func (e *resultFilePathNotFoundError) Error() string {
return "The result file path was not found in request context"
return resultFilePathNotFoundErrorMessage
}
// GetResultFilePath returns the result file path if found in

View File

@@ -75,3 +75,24 @@ func TestGetResultFilePath(t *testing.T) {
t.Error("Context should have a converter entry!")
}
}
func TestContentTypeNotFoundError(t *testing.T) {
err := &contentTypeNotFoundError{}
if err.Error() != contentTypeNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), contentTypeNotFoundErrorMessage)
}
}
func TestConverterNotFoundError(t *testing.T) {
err := &converterNotFoundError{}
if err.Error() != converterNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), converterNotFoundErrorMessage)
}
}
func TestResultFilePathNotFoundError(t *testing.T) {
err := &resultFilePathNotFoundError{}
if err.Error() != resultFilePathNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), resultFilePathNotFoundErrorMessage)
}
}

View File

@@ -83,10 +83,12 @@ var filesTypes = map[ghttp.ContentType]Type{
ghttp.ZipContentType: OfficeType,
}
type fileTypeNotFound struct{}
type fileTypeNotFoundError struct{}
func (e *fileTypeNotFound) Error() string {
return "The file type was not found for the given 'Content-Type'"
const fileTypeNotFoundErrorMessage = "The file type was not found for the given 'Content-Type'"
func (e *fileTypeNotFoundError) Error() string {
return fileTypeNotFoundErrorMessage
}
// findFileType tries to detect what kind of file is the given file.
@@ -98,7 +100,7 @@ func findFileType(f *os.File) (Type, error) {
t, ok := filesTypes[ct]
if !ok {
return 999, &fileTypeNotFound{}
return 999, &fileTypeNotFoundError{}
}
return t, nil
@@ -124,17 +126,19 @@ var filesExtensions = map[Type]Ext{
OfficeType: OfficeExt,
}
type fileExtNotFound struct{}
type fileExtNotFoundError struct{}
func (e *fileExtNotFound) Error() string {
return "The file extension was not found for the given file type"
const fileExtNotFoundErrorMessage = "The file extension was not found for the given file type"
func (e *fileExtNotFoundError) Error() string {
return fileExtNotFoundErrorMessage
}
// reworkFilePath renames a file in the considered directory and adds its extension.
func reworkFilePath(workingDir string, f *File) (*File, error) {
ext, ok := filesExtensions[f.Type]
if !ok {
return nil, &fileExtNotFound{}
return nil, &fileExtNotFoundError{}
}
if ext != OfficeExt {

View File

@@ -50,3 +50,17 @@ func TestReworkFilePath(t *testing.T) {
os.RemoveAll(workingDir)
}
func TestFileTypeNotFoundError(t *testing.T) {
err := &fileTypeNotFoundError{}
if err.Error() != fileTypeNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), fileTypeNotFoundErrorMessage)
}
}
func TestFileExtNotFoundError(t *testing.T) {
err := &fileExtNotFoundError{}
if err.Error() != fileExtNotFoundErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), fileExtNotFoundErrorMessage)
}
}

View File

@@ -43,8 +43,10 @@ func FindAuthorizedContentType(h http.Header) (ContentType, error) {
type notAuthorizedFileContentTypeError struct{}
const notAuthorizedFileContentTypeErrorMessage = "Unable to detect an authorized file content type"
func (e *notAuthorizedFileContentTypeError) Error() string {
return fmt.Sprintf("Unable to detect a file content type")
return notAuthorizedFileContentTypeErrorMessage
}
// SniffContentType tries to detect the content type of a file.

View File

@@ -1,6 +1,7 @@
package http
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
@@ -40,3 +41,18 @@ func TestSniffContentType(t *testing.T) {
t.Error("It should have been able to retrieve an authorized content type from a PDF file!")
}
}
func TestNotAuthorizedContentTypeError(t *testing.T) {
err := &notAuthorizedContentTypeError{}
message := fmt.Sprintf("Accepted values for 'Content-Type': %s, %s", OctetStreamContentType, MultipartFormDataContentType)
if err.Error() != message {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), message)
}
}
func TestNotAuthorizedFileContentTypeError(t *testing.T) {
err := &notAuthorizedFileContentTypeError{}
if err.Error() != notAuthorizedFileContentTypeErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), notAuthorizedFileContentTypeErrorMessage)
}
}