github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/diff/docs_diff_test.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package diff
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdocs"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/schema/encoding"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  func TestDocDiff(t *testing.T) {
    32  	ctx := context.Background()
    33  	ddb, _ := doltdb.LoadDoltDB(ctx, types.Format_Default, doltdb.InMemDoltDB)
    34  	ddb.WriteEmptyRepo(ctx, "billy bob", "bigbillieb@fake.horse")
    35  
    36  	cs, _ := doltdb.NewCommitSpec("master")
    37  	cm, _ := ddb.Resolve(ctx, cs, nil)
    38  
    39  	root, err := cm.GetRootValue()
    40  	assert.NoError(t, err)
    41  
    42  	docs := doltdocs.Docs{
    43  		{DocPk: doltdocs.LicenseDoc},
    44  		{DocPk: doltdocs.ReadmeDoc},
    45  	}
    46  
    47  	// DocsDiff between a root and itself should return no added, modified or removed docs.
    48  	added, modified, removed, err := DocsDiff(ctx, root, root, docs)
    49  	assert.NoError(t, err)
    50  
    51  	if len(added)+len(modified)+len(removed) != 0 {
    52  		t.Error("Bad doc diff when comparing two repos")
    53  	}
    54  
    55  	// Create tbl1 with one license row
    56  	sch := createTestDocsSchema()
    57  	licRow := makeDocRow(t, sch, doltdocs.LicenseDoc, types.String("license row"))
    58  	m, _ := createTestRows(t, ddb.ValueReadWriter(), sch, []row.Row{licRow})
    59  	tbl1, err := CreateTestTable(ddb.ValueReadWriter(), sch, m)
    60  	assert.NoError(t, err)
    61  
    62  	// Create root2 with tbl1 on it (one doc: license)
    63  	root2, err := root.PutTable(ctx, doltdb.DocTableName, tbl1)
    64  	assert.NoError(t, err)
    65  
    66  	// DocsDiff between root and root2 should return one added doc, LICENSE.md
    67  	added, modified, removed, err = DocsDiff(ctx, root, root2, docs)
    68  	assert.NoError(t, err)
    69  
    70  	if len(added) != 1 || added[0] != "LICENSE.md" || len(modified)+len(removed) != 0 {
    71  		t.Error("Bad table diff after adding a single table")
    72  	}
    73  
    74  	// Create tbl2 with one readme row
    75  	readmeRow := makeDocRow(t, sch, doltdocs.ReadmeDoc, types.String("readme row"))
    76  	m, _ = createTestRows(t, ddb.ValueReadWriter(), sch, []row.Row{readmeRow})
    77  	tbl2, err := CreateTestTable(ddb.ValueReadWriter(), sch, m)
    78  	assert.NoError(t, err)
    79  
    80  	// Create root3 with tbl2 on it (one doc: readme)
    81  	root3, err := root.PutTable(ctx, doltdb.DocTableName, tbl2)
    82  	assert.NoError(t, err)
    83  
    84  	// DocsDiff between root2 and root3 should return one removed doc (license) and one added doc (readme).
    85  	added, modified, removed, err = DocsDiff(ctx, root2, root3, docs)
    86  	assert.NoError(t, err)
    87  
    88  	if len(removed) != 1 || removed[0] != "LICENSE.md" || len(added) != 1 || added[0] != "README.md" || len(modified) != 0 {
    89  		t.Error("Bad table diff after adding a single table")
    90  	}
    91  
    92  	// Create tbl3 with 2 doc rows (readme, license)
    93  	readmeRowUpdated := makeDocRow(t, sch, doltdocs.ReadmeDoc, types.String("a different readme"))
    94  	m, _ = createTestRows(t, ddb.ValueReadWriter(), sch, []row.Row{readmeRowUpdated, licRow})
    95  	tbl3, err := CreateTestTable(ddb.ValueReadWriter(), sch, m)
    96  	assert.NoError(t, err)
    97  
    98  	// Create root4 with tbl3 on it (two docs: readme and license)
    99  	root4, err := root3.PutTable(ctx, doltdb.DocTableName, tbl3)
   100  	assert.NoError(t, err)
   101  
   102  	// DocsDiff between root3 and root4 should return one added doc (license) and one modified doc (readme).
   103  	added, modified, removed, err = DocsDiff(ctx, root3, root4, nil)
   104  	assert.NoError(t, err)
   105  
   106  	if len(added) != 1 || added[0] != "LICENSE.md" || len(modified) != 1 || modified[0] != "README.md" || len(removed) != 0 {
   107  		t.Error("Bad table diff after adding a single table")
   108  	}
   109  
   110  	// DocsDiff between root4 and root shows 2 remove docs (license, readme)
   111  	added, modified, removed, err = DocsDiff(ctx, root4, root, nil)
   112  	assert.NoError(t, err)
   113  
   114  	if len(removed) != 2 || len(modified) != 0 || len(added) != 0 {
   115  		t.Error("Bad table diff after adding a single table")
   116  	}
   117  }
   118  
   119  func CreateTestTable(vrw types.ValueReadWriter, tSchema schema.Schema, rowData types.Map) (*doltdb.Table, error) {
   120  	schemaVal, err := encoding.MarshalSchemaAsNomsValue(context.Background(), vrw, tSchema)
   121  
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	empty, _ := types.NewMap(context.Background(), vrw)
   127  	tbl, err := doltdb.NewTable(context.Background(), vrw, schemaVal, rowData, empty, nil)
   128  
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	return tbl, nil
   134  }
   135  
   136  func createTestDocsSchema() schema.Schema {
   137  	typedColColl := schema.NewColCollection(
   138  		schema.NewColumn(doltdb.DocPkColumnName, schema.DocNameTag, types.StringKind, true, schema.NotNullConstraint{}),
   139  		schema.NewColumn(doltdb.DocTextColumnName, schema.DocTextTag, types.StringKind, false),
   140  	)
   141  	sch, err := schema.SchemaFromCols(typedColColl)
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	return sch
   146  }
   147  
   148  func makeDocRow(t *testing.T, sch schema.Schema, pk string, rowVal types.Value) row.Row {
   149  	row, err := row.New(types.Format_Default, sch, row.TaggedValues{
   150  		schema.DocNameTag: types.String(pk),
   151  		schema.DocTextTag: rowVal,
   152  	})
   153  	assert.NoError(t, err)
   154  
   155  	return row
   156  }
   157  
   158  func createTestRows(t *testing.T, vrw types.ValueReadWriter, sch schema.Schema, rows []row.Row) (types.Map, []row.Row) {
   159  	ctx := context.Background()
   160  	var err error
   161  
   162  	m, err := types.NewMap(ctx, vrw)
   163  	assert.NoError(t, err)
   164  	ed := m.Edit()
   165  
   166  	for _, r := range rows {
   167  		ed = ed.Set(r.NomsMapKey(sch), r.NomsMapValue(sch))
   168  	}
   169  
   170  	m, err = ed.Map(ctx)
   171  	assert.NoError(t, err)
   172  
   173  	return m, rows
   174  }