From ee15a520013ee9519a7142c6deeb2a5d218f39fa Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Thu, 5 Apr 2018 10:36:38 +0200 Subject: [PATCH] improving code coverage --- app/config/config.go | 8 ++++++-- app/config/config_test.go | 14 ++++++++++++++ app/context/context.go | 12 +++++++++--- app/context/context_test.go | 21 +++++++++++++++++++++ app/converter/file/file.go | 20 ++++++++++++-------- app/converter/file/file_test.go | 14 ++++++++++++++ app/http/http.go | 4 +++- app/http/http_test.go | 16 ++++++++++++++++ 8 files changed, 95 insertions(+), 14 deletions(-) diff --git a/app/config/config.go b/app/config/config.go index 48fa1639..0288fe21 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -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 diff --git a/app/config/config_test.go b/app/config/config_test.go index 2e809a80..d6d48eef 100644 --- a/app/config/config_test.go +++ b/app/config/config_test.go @@ -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) + } +} diff --git a/app/context/context.go b/app/context/context.go index eabb1275..0510e009 100644 --- a/app/context/context.go +++ b/app/context/context.go @@ -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 diff --git a/app/context/context_test.go b/app/context/context_test.go index 15010eed..e82579ed 100644 --- a/app/context/context_test.go +++ b/app/context/context_test.go @@ -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) + } +} diff --git a/app/converter/file/file.go b/app/converter/file/file.go index b1d5b7ca..df5045dd 100644 --- a/app/converter/file/file.go +++ b/app/converter/file/file.go @@ -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 { diff --git a/app/converter/file/file_test.go b/app/converter/file/file_test.go index 4f99f76a..cf3df8bf 100644 --- a/app/converter/file/file_test.go +++ b/app/converter/file/file_test.go @@ -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) + } +} diff --git a/app/http/http.go b/app/http/http.go index adf0630d..6c133a14 100644 --- a/app/http/http.go +++ b/app/http/http.go @@ -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. diff --git a/app/http/http_test.go b/app/http/http_test.go index 92bc54b0..c0d90cde 100644 --- a/app/http/http_test.go +++ b/app/http/http_test.go @@ -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 := ¬AuthorizedContentTypeError{} + 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 := ¬AuthorizedFileContentTypeError{} + if err.Error() != notAuthorizedFileContentTypeErrorMessage { + t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), notAuthorizedFileContentTypeErrorMessage) + } +}