scran import is working
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
"mediarr/ingest"
|
||||
)
|
||||
|
||||
func TestNodeIngestScan(t *testing.T) {
|
||||
@@ -189,3 +190,128 @@ func findNodeTestChild(t *testing.T, children []document.Document, name string)
|
||||
t.Fatalf("enfant %q introuvable dans %+v", name, children)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestNodeIngestImportAndPrune(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
err, node := NewNode(NewNodeParams{DatabaseName: "ingest_prune", StoragePath: "store"})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := node.Close(); err != nil {
|
||||
t.Errorf("Close : %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||
Kind: identity.KindUser,
|
||||
Name: "owner",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||
}
|
||||
|
||||
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Films",
|
||||
PolicyID: ident.MustParse("00000000-0000-7000-8000-000000000101"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateLibrary : %v", err)
|
||||
}
|
||||
|
||||
source := filepath.Join(t.TempDir(), "library")
|
||||
if err := os.MkdirAll(source, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll : %v", err)
|
||||
}
|
||||
writeNodeTestFile(t, filepath.Join(source, "keep.mkv"), "keep")
|
||||
writeNodeTestFile(t, filepath.Join(source, "remove.txt"), "remove")
|
||||
|
||||
if _, err := node.Ingest().Scan(ctx, owner.ID, library.ID, source); err != nil {
|
||||
t.Fatalf("Scan : %v", err)
|
||||
}
|
||||
|
||||
if _, err := node.Documents().CreateChild(ctx, owner.ID, document.CreateChildParams{
|
||||
Kind: document.KindFile,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "manual.txt",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateChild(manual) : %v", err)
|
||||
}
|
||||
|
||||
extra := filepath.Join(t.TempDir(), "extra.jpg")
|
||||
writeNodeTestFile(t, extra, "extra")
|
||||
imported, err := node.Ingest().Import(ctx, owner.ID, library.ID, extra)
|
||||
if err != nil {
|
||||
t.Fatalf("Import : %v", err)
|
||||
}
|
||||
if imported.Files != 1 || imported.Created != 1 || imported.Updated != 0 || imported.Unchanged != 0 {
|
||||
t.Fatalf("résultat import = %+v", imported)
|
||||
}
|
||||
|
||||
imported, err = node.Ingest().Import(ctx, owner.ID, library.ID, extra)
|
||||
if err != nil {
|
||||
t.Fatalf("Import(doublon) : %v", err)
|
||||
}
|
||||
if imported.Files != 1 || imported.Created != 0 || imported.Updated != 0 || imported.Unchanged != 1 {
|
||||
t.Fatalf("résultat import doublon = %+v", imported)
|
||||
}
|
||||
|
||||
imported, err = node.Ingest().Import(ctx, owner.ID, library.ID, source)
|
||||
if err != nil {
|
||||
t.Fatalf("Import(dossier) : %v", err)
|
||||
}
|
||||
if imported.Files != 2 || imported.Created != 0 || imported.Updated != 0 || imported.Unchanged != 2 {
|
||||
t.Fatalf("résultat import dossier = %+v", imported)
|
||||
}
|
||||
|
||||
if err := os.Remove(filepath.Join(source, "remove.txt")); err != nil {
|
||||
t.Fatalf("Remove : %v", err)
|
||||
}
|
||||
|
||||
result, err := node.Ingest().ScanWithOptions(ctx, owner.ID, library.ID, source, ingest.ScanOptions{Prune: true})
|
||||
if err != nil {
|
||||
t.Fatalf("ScanWithOptions : %v", err)
|
||||
}
|
||||
if result.HasErrors() {
|
||||
t.Fatalf("ScanWithOptions erreurs : %v", result.Errors)
|
||||
}
|
||||
if result.Pruned != 1 {
|
||||
t.Fatalf("pruned = %d, attendu 1", result.Pruned)
|
||||
}
|
||||
|
||||
children, err := node.Documents().ListChildren(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren : %v", err)
|
||||
}
|
||||
if hasNodeTestChild(children, "remove.txt") {
|
||||
t.Fatal("remove.txt toujours présent après prune")
|
||||
}
|
||||
if !hasNodeTestChild(children, "keep.mkv") {
|
||||
t.Fatal("keep.mkv absent après prune")
|
||||
}
|
||||
if !hasNodeTestChild(children, "manual.txt") {
|
||||
t.Fatal("manual.txt supprimé alors qu'il n'est pas issu du scan")
|
||||
}
|
||||
if !hasNodeTestChild(children, "extra.jpg") {
|
||||
t.Fatal("extra.jpg supprimé alors qu'il vient d'une autre racine")
|
||||
}
|
||||
}
|
||||
|
||||
func hasNodeTestChild(children []document.Document, name string) bool {
|
||||
for i := range children {
|
||||
if children[i].Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user