github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/component/component_test.go (about)

     1  package component
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/qri-io/dataset"
     7  )
     8  
     9  func TestConvertDatasetToComponents(t *testing.T) {
    10  	ds := dataset.Dataset{}
    11  	ds.Meta = &dataset.Meta{
    12  		Title: "test",
    13  	}
    14  	ds.Structure = &dataset.Structure{
    15  		Format: "json",
    16  	}
    17  
    18  	comp := ConvertDatasetToComponents(&ds, nil)
    19  
    20  	sub := comp.Base().GetSubcomponent("commit")
    21  	if sub != nil {
    22  		t.Errorf("expected nil commit")
    23  	}
    24  
    25  	sub = comp.Base().GetSubcomponent("body")
    26  	if sub != nil {
    27  		t.Errorf("expected nil body")
    28  	}
    29  
    30  	sub = comp.Base().GetSubcomponent("meta")
    31  	if sub == nil {
    32  		t.Fatalf("expected meta component")
    33  	}
    34  	meta, ok := sub.(*MetaComponent)
    35  	if !ok {
    36  		t.Fatalf("expected meta component type conversion to MetaComponent")
    37  	}
    38  	if meta.Value.Title != "test" {
    39  		t.Errorf("expected meta.title \"%s\", got \"%s\"", "test", meta.Value.Title)
    40  	}
    41  
    42  	sub = comp.Base().GetSubcomponent("structure")
    43  	if sub == nil {
    44  		t.Fatalf("expected structure component")
    45  	}
    46  	structure, ok := sub.(*StructureComponent)
    47  	if !ok {
    48  		t.Fatalf("expected structure component type conversion to StructureComponent")
    49  	}
    50  	if structure.Value.Format != "json" {
    51  		t.Errorf("expected structure.title \"%s\", got \"%s\"", "json", structure.Value.Format)
    52  	}
    53  }
    54  
    55  func TestToDataset(t *testing.T) {
    56  	dsComp := DatasetComponent{}
    57  	dsComp.Base().SetSubcomponent(
    58  		"commit",
    59  		BaseComponent{
    60  			Format:     "json",
    61  			SourceFile: "testdata/commit.json",
    62  		},
    63  	)
    64  
    65  	ds, err := ToDataset(&dsComp)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	if ds == nil {
    70  		t.Fatal("ds is nil")
    71  	}
    72  	if ds.Commit == nil {
    73  		t.Errorf("expected commit component")
    74  	}
    75  	if ds.Commit.Message != "test" {
    76  		t.Errorf("expected commit.message \"%s\", got \"%s\"", "test", ds.Commit.Message)
    77  	}
    78  }