Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added test for Processor.go functions #1124

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions filesystem/processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filesystem

import (
"testing"
"os"


)

func TestNewProcessor(t *testing.T) {
t.Run("checks for a processor instance with given options", func(t *testing.T) {
// Create a sample options struct
mockOptions := options{
processFileCallBack: func(sourcecFilePath, destinationFilePath string, config interface{}) (err error) {
return nil
},
additionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {
return nil
},
deletionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {
return nil
},
mismatchCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {
return nil
},
config: nil,
}


processor := newProcessor(mockOptions)

// Check that the returned processor is not nil
if processor == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check if the config and callbacks are preserved.

t.Error("Expected non-nil processor, but got nil")
}


})
}
func TestProcessFile(t *testing.T) {

t.Run("test for the scenario where the processing of a file is successful", func(t *testing.T) {
source := "path/to/source/file.txt"
destination := "path/to/destination/file.txt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix formatting

options := options{
processFileCallBack: func(sourcecFilePath, destinationFilePath string, config interface{}) (err error) {

return nil
},
additionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
deletionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
mismatchCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
config: nil,
}
processor := newProcessor(options)
// Invoke the code under test
err := processor.processFile(source, destination)

// Assert the result
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix formatting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check if the file was processed correctly.

t.Errorf("Unexpected error during processing: %v", err)
}
})
}

func TestProcessDirectory(t *testing.T) {

t.Run("test for scenario when a existing source and existing destination directory is processed succesfully ", func(t *testing.T) {
source := "testdata/source"
destination := "testdata/destination"
mockOptions := options{
processFileCallBack: func(sourcecFilePath, destinationFilePath string, config interface{}) (err error) {

return nil
},
additionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
deletionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
mismatchCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
config: nil,
}

// Call the newProcessor function
processor := newProcessor(mockOptions)

// Call the code under test
err := processor.processDirectory(source, destination)


if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
})

}


func TestProcess(t *testing.T) {
t.Run("test for scenario where it succesfully processes existing source and destination directory", func(t *testing.T) {
// Initialize test data
source := "testdata/source/empty.txt"
destination := "testdata/destination"
mockOptions := options{
processFileCallBack: func(sourcecFilePath, destinationFilePath string, config interface{}) (err error) {

return nil
},
additionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
deletionCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
mismatchCallBack: func(sourcePath, destinationPath string, config interface{}) (err error) {

return nil
},
config: nil,
}
processor := newProcessor(mockOptions)


err := processor.process(source, destination)

// Check the result
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}


_, err = os.Lstat(destination)
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
})
}
Empty file.
Empty file.
Loading