Skip to content

Commit

Permalink
bug: verbose was ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
aeltorio committed Jul 30, 2024
1 parent 3d42c46 commit 0bda7ed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
45 changes: 43 additions & 2 deletions internal/translator/translator_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
Copyright (c) Ronan LE MEILLAT 2024
Licensed under the AGPLv3 License
https://www.gnu.org/licenses/agpl-3.0.html
Package translator provides functions for uploading, retrieving, and deleting files from Azure Blob Storage,
generating JSON documents for translation, and translating documents using the Azure Translator service.
This file contains test functions for the various operations provided by the translator package.
*/

package translator

import (
Expand All @@ -8,11 +19,14 @@ import (
"testing"
)

// BLOB_NAME is the name of the blob file used for testing.
const (
BLOB_NAME = "TestREADME.md"
)

// TestUploadFileToBlobStorage is a test function that tests the uploadFileToBlobStorage function.
func TestUploadFileToBlobStorage(t *testing.T) {
// Test configuration
config := TranslatorConfig{
TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"),
TranslatorKey: os.Getenv("TRANSLATOR_KEY"),
Expand All @@ -23,22 +37,29 @@ func TestUploadFileToBlobStorage(t *testing.T) {
Timeout: 30,
Verbose: true,
}

// File paths
filePath := "../../README.md"
blobName := BLOB_NAME

// Get absolute file path
absFilePath, err := filepath.Abs(filePath)
if err != nil {
t.Fatalf("Failed to get absolute path: %v", err)
}
err = uploadFileToBlobStorage(config, absFilePath, blobName)

// Upload file to blob storage
err = uploadFileToBlobStorage(config, absFilePath, blobName)
if err != nil {
t.Errorf("UploadFileToBlobStorage failed: %v", err)
}

// Add additional assertions or verifications here
}

// TestGetBlobURLWithSASToken is a test function that tests the getBlobURLWithSASToken function.
func TestGetBlobURLWithSASToken(t *testing.T) {
// Test configuration
config := TranslatorConfig{
TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"),
TranslatorKey: os.Getenv("TRANSLATOR_KEY"),
Expand All @@ -49,15 +70,18 @@ func TestGetBlobURLWithSASToken(t *testing.T) {
Timeout: 30,
Verbose: true,
}

// Blob name
blobName := BLOB_NAME

// Get blob URL with SAS token
urlWithSASToken, _, err := getBlobURLWithSASToken(config, blobName)
fmt.Println(urlWithSASToken)
if err != nil {
t.Errorf("getBlobURLWithSASToken failed: %v", err)
}

// test if the URL return 200 status code
// Test if the URL returns a 200 status code
_, err = http.Get(urlWithSASToken)
if err != nil {
t.Errorf("getBlobURLWithSASToken failed: %v", err)
Expand All @@ -66,7 +90,9 @@ func TestGetBlobURLWithSASToken(t *testing.T) {
// Add additional assertions or verifications here
}

// TestDeleteFileFromBlobStorage is a test function that tests the deleteFileFromBlobStorage function.
func TestDeleteFileFromBlobStorage(t *testing.T) {
// Test configuration
config := TranslatorConfig{
TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"),
TranslatorKey: os.Getenv("TRANSLATOR_KEY"),
Expand All @@ -77,21 +103,28 @@ func TestDeleteFileFromBlobStorage(t *testing.T) {
Timeout: 30,
Verbose: true,
}

// Blob name
blobName := BLOB_NAME

// Delete file from blob storage
err := deleteFileFromBlobStorage(config, blobName)
if err != nil {
t.Errorf("DeleteFileFromBlobStorage failed: %v", err)
}

// Add additional assertions or verifications here
}

// TestGenerateJSONDocument is a test function that tests the generateJSONDocument function.
func TestGenerateJSONDocument(t *testing.T) {
// Test data
sourceSASUrl := "https://example.com/sourceSASUrl"
targetSASUrl := "https://example.com/targetSASUrl"
sourceLanguage := "en"
targetLanguage := "fr"

// Expected JSON document
expectedJSON := `{
"inputs": [
{
Expand All @@ -110,23 +143,29 @@ func TestGenerateJSONDocument(t *testing.T) {
]
}`

// Generate JSON document
jsonDoc, err := generateJSONDocument(sourceSASUrl, targetSASUrl, sourceLanguage, targetLanguage)
if err != nil {
t.Errorf("generateJSONDocument failed: %v", err)
}

// Compare generated JSON document with expected JSON document
if jsonDoc != expectedJSON {
t.Errorf("generateJSONDocument returned incorrect JSON.\nExpected:\n%s\n\nActual:\n%s", expectedJSON, jsonDoc)
}

// Add additional assertions or verifications here
}

// TestTranslateDocument is a test function that tests the TranslateDocument function.
func TestTranslateDocument(t *testing.T) {
// Test data
fileToTranslate := "../../README.md"
fileTranslated := "../../README_fr.md"
sourceLanguage := "en"
targetLanguage := "fr"

// Test configuration
config := TranslatorConfig{
TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"),
TranslatorKey: os.Getenv("TRANSLATOR_KEY"),
Expand All @@ -137,6 +176,8 @@ func TestTranslateDocument(t *testing.T) {
Timeout: 30,
Verbose: true,
}

// Translate document
err := TranslateDocument(fileToTranslate, fileTranslated, sourceLanguage, targetLanguage, config)
if err != nil {
t.Errorf("TranslateDocument failed: %v", err)
Expand Down
29 changes: 26 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* Copyright (c) Ronan LE MEILLAT 2024
* Licensed under the AGPLv3 License
* https://www.gnu.org/licenses/agpl-3.0.html
*/
// Package main is the entry point for the AzurePDFTranslator application.
// It provides a command-line interface for translating PDF documents using Azure Translator and storing the translated documents in Azure Blob Storage.
package main

import (
Expand All @@ -9,6 +15,7 @@ import (
"translator/internal/translator"
)

// Constants for environment variable names
const (
envTranslatorEndpoint = "TRANSLATOR_ENDPOINT"
envTranslatorKey = "TRANSLATOR_KEY"
Expand All @@ -20,14 +27,20 @@ const (

var verbose bool

// main is the entry point of the application.
// It parses command-line arguments, loads configuration from file if specified, validates inputs, and performs the translation.
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}

// run is the main logic of the application.
// It sets up the translator configuration, validates inputs, and performs the translation.
// It returns an error if any step fails.
func run() error {
// Parse command-line arguments
endpoint := flag.String("endpoint", os.Getenv(envTranslatorEndpoint), "Azure Translator API endpoint")
key := flag.String("key", os.Getenv(envTranslatorKey), "Azure Translator API key")
region := flag.String("region", os.Getenv(envTranslatorRegion), "Azure region")
Expand All @@ -43,17 +56,20 @@ func run() error {
flag.BoolVar(&verbose, "v", false, "enable verbose logging")
flag.Parse()

// Load configuration from file if specified
if *configFile != "" {
err := loadConfigFromFile(*configFile, endpoint, key, region, blobAccount, blobAccountKey, blobContainer)
err := loadConfigFromFile(*configFile, endpoint, key, region, blobAccount, blobAccountKey, blobContainer, &verbose)
if err != nil {
return err
}
}

// Validate inputs
if err := validateInputs(*endpoint, *key, *region, *in, *out, *to, *blobAccount, *blobAccountKey, *blobContainer); err != nil {
return err
}

// Set up translator configuration
config := translator.TranslatorConfig{
TranslatorEndpoint: *endpoint,
TranslatorKey: *key,
Expand All @@ -62,12 +78,16 @@ func run() error {
BlobAccountKey: *blobAccountKey,
BlobContainerName: *blobContainer,
Timeout: *timeout,
Verbose: true,
Verbose: verbose,
}

// Perform the translation
return translator.TranslateDocument(*in, *out, *from, *to, config)
}

func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, blobAccountKey, blobContainer *string) error {
// loadConfigFromFile loads the translator configuration from a JSON file.
// It updates the endpoint, key, region, blobAccount, blobAccountKey, blobContainer, and verbose variables with the values from the file.
func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, blobAccountKey, blobContainer *string, verbose *bool) error {
file, err := os.Open(configFile)
if err != nil {
return err
Expand All @@ -86,10 +106,13 @@ func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, b
*blobAccount = config.BlobAccountName
*blobAccountKey = config.BlobAccountKey
*blobContainer = config.BlobContainerName
*verbose = config.Verbose

return nil
}

// validateInputs checks if all the required inputs are provided.
// It returns an error if any required input is missing.
func validateInputs(endpoint, key, region, in, out, to, blobAccount, blobAccountKey, blobContainer string) error {
missingArgs := []string{}
if endpoint == "" {
Expand Down

0 comments on commit 0bda7ed

Please sign in to comment.