Skip to content

Commit

Permalink
Added a bunch of unit tests and changed the logic for validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Aug 20, 2024
1 parent dba194e commit 3b52aec
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 142 deletions.
10 changes: 5 additions & 5 deletions entity/cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"github.com/AmadlaOrg/hery/entity/cmd/util"
"github.com/AmadlaOrg/hery/entity/cmd/validation"
"github.com/AmadlaOrg/hery/entity/get"
"github.com/AmadlaOrg/hery/storage"
"github.com/spf13/cobra"
Expand All @@ -11,11 +13,9 @@ var GetCmd = &cobra.Command{
Use: "get",
Short: "Get entity and its dependencies",
Run: func(cmd *cobra.Command, args []string) {
concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
if len(args) == 0 {
log.Fatal("no entity URI specified")
} else if len(args) > 60 {
log.Fatal("too many entity URIs (the limit is 60)")
util.Concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
if err := validation.Entities(args); err != nil {
log.Fatal(err)
}

getService := get.NewGetService()
Expand Down
3 changes: 2 additions & 1 deletion entity/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/AmadlaOrg/hery/entity"
"github.com/AmadlaOrg/hery/entity/cmd/util"
"github.com/AmadlaOrg/hery/storage"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -13,7 +14,7 @@ var ListCmd = &cobra.Command{
Use: "list",
Short: "List all entities",
Run: func(cmd *cobra.Command, args []string) {
concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
util.Concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
entities, err := entity.CrawlDirectoriesParallel(paths.Entities)
if err != nil {
fmt.Println("Error crawling directories:", err)
Expand Down
6 changes: 3 additions & 3 deletions entity/cmd/concoct.go → entity/cmd/util/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package util

import (
collectionPkgCmd "github.com/AmadlaOrg/hery/collection/cmd"
Expand All @@ -13,7 +13,7 @@ var (
newStorageService = storage.NewStorageService
)

// concoct sets up the necessary collection and storage paths and executes the provided handler function.
// Concoct sets up the necessary collection and storage paths and executes the provided handler function.
// It retrieves the collection name using the getCollectionFlag function,
// initializes a new storage service, and gets the paths for the specified collection.
// If any errors occur during these steps, they are logged and the handler is not called.
Expand All @@ -22,7 +22,7 @@ var (
// - cmd: The cobra command that triggered this function.
// - args: The arguments passed to the cobra command.
// - handler: A function that takes the collection name, storage paths (AbsPaths), and arguments, and performs the main logic.
func concoct(
func Concoct(
cmd *cobra.Command,
args []string,
handler func(collectionName string, paths *storage.AbsPaths, args []string)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package util

/*type MockStorageService struct {
mock.Mock
Expand Down
108 changes: 36 additions & 72 deletions entity/cmd/validate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"errors"
"fmt"
collectionPkgCmd "github.com/AmadlaOrg/hery/collection/cmd"
"github.com/AmadlaOrg/hery/entity"
"github.com/AmadlaOrg/hery/entity/cmd/util"
"github.com/AmadlaOrg/hery/entity/cmd/validation"
"github.com/AmadlaOrg/hery/entity/get"
entityValidation "github.com/AmadlaOrg/hery/entity/validation"
"github.com/AmadlaOrg/hery/storage"
Expand All @@ -12,55 +13,42 @@ import (
)

var (
isValidateAll bool
isRm bool
isValidateAll bool
isRm bool
getCollectionFlag = collectionPkgCmd.GetCollectionFlag
)

var ValidateCmd = &cobra.Command{
Use: "valid",
Short: "Validate entity or schemas",
Run: func(cmd *cobra.Command, args []string) {
if !isValidateAll && (len(args) == 0 || (len(args) == 0 && isRm)) {
// Print the usage information (helper message)
if (!isValidateAll && (len(args) == 0 || (len(args) == 0 && isRm))) || isValidateAll && isRm {
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
return
} else if isValidateAll && isRm {
err := cmd.Help()
if err != nil {
} else if isRm {
if err := validation.Entities(args); err != nil {
log.Fatal(err)
}
return
}

concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
if isRm {
argsLen := len(args) - 1

if len(args) == 0 {
log.Fatal("no entity URI specified")
} else if argsLen > 60 {
log.Fatal("too many entity URIs (the limit is 60)")
}

//err := TmpEntityCheck(collectionName, args)
getService := get.NewGetService()
err := getService.GetInTmp(collectionName, paths, args)
if err != nil {
log.Fatal(err)
}

println(paths.Entities)
} else {
println(args)
println(isValidateAll)
collectionName, err := getCollectionFlag()
if err != nil {
log.Fatal(err)
}

for _, arg := range args {
println(arg)
}
//err := TmpEntityCheck(collectionName, args)
getService := get.NewGetService()
paths, err := getService.GetInTmp(collectionName, args)
if err != nil {
log.Fatal(err)
}

println(paths.Entities)
return
} else if isValidateAll {
util.Concoct(cmd, args, func(collectionName string, paths *storage.AbsPaths, args []string) {
entityList, err := entity.CrawlDirectoriesParallel(paths.Entities)
if err != nil {
log.Fatal(err)
Expand All @@ -84,20 +72,20 @@ var ValidateCmd = &cobra.Command{
println(entity.AbsPath)
println(entity.Name)
}
}

// Add your validation logic here
// entityDir, err := storage.Path()
// if err != nil {
// fmt.Println("could not get the root storage directory:", err)
// return
// }
// err = entity.Validate(entityDir)
// if err != nil {
// fmt.Println("Error validating entities:", err)
// return
// }
})
// Add your validation logic here
// entityDir, err := storage.Path()
// if err != nil {
// fmt.Println("could not get the root storage directory:", err)
// return
// }
// err = entity.Validate(entityDir)
// if err != nil {
// fmt.Println("Error validating entities:", err)
// return
// }
})
}
},
}

Expand All @@ -114,27 +102,3 @@ func init() {
false,
"Remove entity after validating if it wasn't already downloaded")
}

// TmpEntityCheck
func TmpEntityCheck(collectionName string, entities []string) error {
storageService := storage.NewStorageService()

// Replace paths with temporary directory before .<collectionName>
tmpPaths, err := storageService.TmpPaths(collectionName)
if err != nil {
return err
}

err = storageService.MakePaths(*tmpPaths)
if err != nil {
return err
}

getService := get.NewGetService()
err = getService.Get(collectionName, tmpPaths, entities)
if err != nil {
return errors.New(fmt.Sprintf("error getting entity: %s", err))
}

return nil
}
16 changes: 16 additions & 0 deletions entity/cmd/validation/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package validation

import (
"errors"
)

// Entities function is for validating passing entities in args
func Entities(entities []string) error {
numEntities := len(entities)
if numEntities == 0 {
return errors.New("no entity URI specified")
} else if numEntities > 60 {
return errors.New("too many entity URIs (the limit is 60)")
}
return nil
}
43 changes: 43 additions & 0 deletions entity/cmd/validation/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package validation

import (
"errors"
"testing"
)

func TestEntities(t *testing.T) {
tests := []struct {
name string
entities []string
expectedError error
}{
{
name: "No Entities",
entities: []string{},
expectedError: errors.New("no entity URI specified"),
},
{
name: "Valid Entities",
entities: make([]string, 30), // Example of a valid number of entities
expectedError: nil,
},
{
name: "Too Many Entities",
entities: make([]string, 61),
expectedError: errors.New("too many entity URIs (the limit is 60)"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Entities(tt.entities)
if tt.expectedError != nil {
if err == nil || err.Error() != tt.expectedError.Error() {
t.Errorf("expected error %v, got %v", tt.expectedError, err)
}
} else if err != nil {
t.Errorf("expected no error, got %v", err)
}
})
}
}
26 changes: 18 additions & 8 deletions entity/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

// EntityGetter is an interface for getting entities.
type EntityGetter interface {
GetInTmp(collectionName string, entities []string) (storage.AbsPaths, error)
Get(collectionName string, storagePath string, args []string) error
download(collectionName string, storagePaths *storage.AbsPaths, entitiesMeta []entity.Entity) error
}
Expand All @@ -29,30 +30,30 @@ type GetterService struct {
Builder build.MetaBuilder
}

// GetInTmp
func (gs *GetterService) GetInTmp(collectionName string, storagePaths *storage.AbsPaths, entities []string) error {
// GetInTmp retrieves entities based on the provided collection name and entities
func (gs *GetterService) GetInTmp(collectionName string, entities []string) (storage.AbsPaths, error) {
storageService := storage.NewStorageService()

// Replace paths with temporary directory before .<collectionName>
storagePaths, err := storageService.TmpPaths(collectionName)
if err != nil {
return err
return storage.AbsPaths{}, err
}

err = storageService.MakePaths(*storagePaths)
if err != nil {
return err
return *storagePaths, err
}

err = gs.Get(collectionName, storagePaths, entities)
if err != nil {
return err
return *storagePaths, err
}

return nil
return *storagePaths, nil
}

// Get retrieves entities based on the provided collection name and arguments.
// Get retrieves entities based on the provided collection name and entities
func (gs *GetterService) Get(collectionName string, storagePaths *storage.AbsPaths, entities []string) error {
entityBuilds := make([]entity.Entity, len(entities))
for i, e := range entities {
Expand Down Expand Up @@ -97,7 +98,10 @@ func (gs *GetterService) download(collectionName string, storagePaths *storage.A
// Download the Entity with `git clone`
if err := gs.Git.FetchRepo(entityMeta.RepoUrl, entityMeta.AbsPath); err != nil {
errCh <- fmt.Errorf("error fetching repo: %v", err)
} else if !entityMeta.IsPseudoVersion {
}

// Changes the repository to the tag (version) that was pass
if !entityMeta.IsPseudoVersion {
if err := gs.Git.CheckoutTag(entityMeta.AbsPath, entityMeta.Version); err != nil {
errCh <- fmt.Errorf("error checking out version: %v", err)
}
Expand All @@ -109,6 +113,12 @@ func (gs *GetterService) download(collectionName string, storagePaths *storage.A
return
}

err = gs.EntityValidation.Entity(collectionName, entityMeta.AbsPath)
if err != nil {
errCh <- fmt.Errorf("error validating entity: %v", err)
return
}

var subEntitiesMeta []entity.Entity
for key, value := range read {
if key == "_entity" {
Expand Down
Loading

0 comments on commit 3b52aec

Please sign in to comment.