Added test for filetype determination
This commit is contained in:
		
							parent
							
								
									2081998ca9
								
							
						
					
					
						commit
						112d05b592
					
				
					 2 changed files with 63 additions and 1 deletions
				
			
		
							
								
								
									
										2
									
								
								Makefile
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -17,7 +17,7 @@ run: ## Run the application
 | 
			
		|||
 | 
			
		||||
test: ## Run test
 | 
			
		||||
	echo "Testing..."
 | 
			
		||||
	go test ./tests/... -v
 | 
			
		||||
	go test ./server/internal/store -v
 | 
			
		||||
 | 
			
		||||
clean: ## Delete build artifacts
 | 
			
		||||
	echo "Cleaning..."
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										62
									
								
								server/internal/store/filetype_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								server/internal/store/filetype_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,62 @@
 | 
			
		|||
package store_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"git.jmbit.de/jmb/scanfile/server/internal/store"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type testFile struct {
 | 
			
		||||
  Name string
 | 
			
		||||
  MimeType string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testFiles = []testFile{
 | 
			
		||||
  {
 | 
			
		||||
    Name: "zip.zip",
 | 
			
		||||
    MimeType: "application/zip",
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    Name: "zip.7z",
 | 
			
		||||
    MimeType: "application/x-7z-compressed",
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    Name: "test.txt",
 | 
			
		||||
    MimeType: "text/plain; charset=utf-8",
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    Name: "macro-example.xlsm",
 | 
			
		||||
    MimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
 | 
			
		||||
  },
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setconf() error {
 | 
			
		||||
  absPath, err := filepath.Abs("../../../tests/files")
 | 
			
		||||
  if err != nil {
 | 
			
		||||
    return err
 | 
			
		||||
  }
 | 
			
		||||
  viper.Set("store.path", absPath)
 | 
			
		||||
  return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func TestFileType(t *testing.T) {
 | 
			
		||||
  err := setconf()
 | 
			
		||||
  if err != nil {
 | 
			
		||||
    t.Fatal(err)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for i, file := range testFiles {
 | 
			
		||||
    mimeType, err := store.GetFileType(file.Name)
 | 
			
		||||
    if err != nil {
 | 
			
		||||
      t.Error(err)
 | 
			
		||||
    }
 | 
			
		||||
    t.Logf("[%d/%d] File %s Expected: %s, Result: %s", i+1, len(testFiles), file.Name, file.MimeType, mimeType)
 | 
			
		||||
    if mimeType != file.MimeType {
 | 
			
		||||
      t.Fail()
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue