github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdocs/docs_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 doltdocs
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    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 TestAddNewerTextAndValueFromTable(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  	// If no tbl/schema is provided, doc Text and Value should be nil.
    37  	doc1 := Doc{DocPk: LicenseDoc}
    38  	doc1Text, err := getDocTextFromTbl(ctx, nil, nil, doc1.DocPk)
    39  	require.NoError(t, err)
    40  	doc1.Text = doc1Text
    41  	assert.Nil(t, doc1.Text)
    42  
    43  	// Create table with no rows
    44  	sch := createTestDocsSchema()
    45  	rows := []row.Row{}
    46  	m, _ := createTestRows(t, ddb.ValueReadWriter(), sch, rows)
    47  	tbl, err := CreateTestTable(ddb.ValueReadWriter(), sch, m)
    48  	require.NoError(t, err)
    49  
    50  	// If a table doesn't have doc row, doc Text and Value should remain nil
    51  	doc2 := Doc{DocPk: LicenseDoc}
    52  	doc2Text, err := getDocTextFromTbl(ctx, tbl, &sch, doc2.DocPk)
    53  	require.NoError(t, err)
    54  	doc2.Text = doc2Text
    55  	assert.Nil(t, doc2.Text)
    56  
    57  	// If a table doesn't have doc row, and Text and Value are originally non-nil, they should be updated to nil.
    58  	doc3 := Doc{DocPk: LicenseDoc, Text: []byte("Something in newer text field")}
    59  	doc3Text, err := getDocTextFromTbl(ctx, tbl, &sch, doc3.DocPk)
    60  	require.NoError(t, err)
    61  	doc3.Text = doc3Text
    62  	assert.Nil(t, doc3.Text)
    63  
    64  	// Update tbl to have 2 doc rows, readme and license
    65  	rows = getDocRows(t, sch, types.String("text in doc_text"))
    66  	m, _ = createTestRows(t, ddb.ValueReadWriter(), sch, rows)
    67  	tbl, err = CreateTestTable(ddb.ValueReadWriter(), sch, m)
    68  	require.NoError(t, err)
    69  
    70  	// If a table has a doc row, Text and Value and should be updated to the `doc_text` value in that row.
    71  	doc4 := Doc{DocPk: LicenseDoc, Text: []byte("Something in newer text field")}
    72  	doc4Text, err := getDocTextFromTbl(ctx, tbl, &sch, doc4.DocPk)
    73  	require.NoError(t, err)
    74  	doc4.Text = doc4Text
    75  	assert.Equal(t, "text in doc_text", string(doc4.Text))
    76  
    77  	// If a table has a doc row, and Text and Value are originally non-nil, they should be updated to the `doc_text` value.
    78  	doc5 := Doc{DocPk: LicenseDoc}
    79  	doc5Text, err := getDocTextFromTbl(ctx, tbl, &sch, doc5.DocPk)
    80  	require.NoError(t, err)
    81  	doc5.Text = doc5Text
    82  	assert.Equal(t, "text in doc_text", string(doc5.Text))
    83  }
    84  
    85  func TestAddNewerTextAndDocPkFromRow(t *testing.T) {
    86  	ctx := context.Background()
    87  	ddb, _ := doltdb.LoadDoltDB(ctx, types.Format_Default, doltdb.InMemDoltDB)
    88  	ddb.WriteEmptyRepo(ctx, "billy bob", "bigbillieb@fake.horse")
    89  
    90  	sch := createTestDocsSchema()
    91  
    92  	emptyRow, err := row.New(types.Format_Default, sch, row.TaggedValues{})
    93  	require.NoError(t, err)
    94  
    95  	// Text and DocName should be nil from an empty row
    96  	doc1 := Doc{}
    97  	text, err := getDocTextFromRow(emptyRow)
    98  	require.NoError(t, err)
    99  	assert.Nil(t, text)
   100  	docPk, err := getDocPKFromRow(emptyRow)
   101  	require.NoError(t, err)
   102  	doc1.DocPk = docPk
   103  	assert.Equal(t, "", doc1.DocPk)
   104  
   105  	licenseRow, err := row.New(types.Format_Default, sch, row.TaggedValues{
   106  		schema.DocNameTag: types.String(LicenseDoc),
   107  		schema.DocTextTag: types.String("license!"),
   108  	})
   109  	require.NoError(t, err)
   110  
   111  	// Text and DocName should be added to doc from row
   112  	doc2 := Doc{}
   113  	text, err = getDocTextFromRow(licenseRow)
   114  	require.NoError(t, err)
   115  	doc2.Text = text
   116  	assert.Equal(t, "license!", string(doc2.Text))
   117  	docPk, err = getDocPKFromRow(licenseRow)
   118  	require.NoError(t, err)
   119  	doc2.DocPk = docPk
   120  	assert.Equal(t, LicenseDoc, doc2.DocPk)
   121  
   122  	// When Text and DocName are non-nil, they should be updated from the row provided.
   123  	doc3 := Doc{DocPk: "invalid", Text: []byte("something")}
   124  	text, err = getDocTextFromRow(licenseRow)
   125  	require.NoError(t, err)
   126  	doc3.Text = text
   127  	assert.Equal(t, "license!", string(doc3.Text))
   128  	docPk, err = getDocPKFromRow(licenseRow)
   129  	require.NoError(t, err)
   130  	doc3.DocPk = docPk
   131  	assert.Equal(t, LicenseDoc, doc3.DocPk)
   132  }
   133  
   134  func CreateTestTable(vrw types.ValueReadWriter, tSchema schema.Schema, rowData types.Map) (*doltdb.Table, error) {
   135  	schemaVal, err := encoding.MarshalSchemaAsNomsValue(context.Background(), vrw, tSchema)
   136  
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	empty, _ := types.NewMap(context.Background(), vrw)
   142  	tbl, err := doltdb.NewTable(context.Background(), vrw, schemaVal, rowData, empty, nil)
   143  
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	return tbl, nil
   149  }
   150  
   151  func createTestDocsSchema() schema.Schema {
   152  	typedColColl := schema.NewColCollection(
   153  		schema.NewColumn(doltdb.DocPkColumnName, schema.DocNameTag, types.StringKind, true, schema.NotNullConstraint{}),
   154  		schema.NewColumn(doltdb.DocTextColumnName, schema.DocTextTag, types.StringKind, false),
   155  	)
   156  	sch, err := schema.SchemaFromCols(typedColColl)
   157  	if err != nil {
   158  		panic(err)
   159  	}
   160  	return sch
   161  }
   162  
   163  func getDocRows(t *testing.T, sch schema.Schema, rowVal types.Value) []row.Row {
   164  	rows := make([]row.Row, 2)
   165  	row1 := makeDocRow(t, sch, LicenseDoc, rowVal)
   166  	rows[0] = row1
   167  	row2 := makeDocRow(t, sch, ReadmeDoc, rowVal)
   168  	rows[1] = row2
   169  
   170  	return rows
   171  }
   172  
   173  func makeDocRow(t *testing.T, sch schema.Schema, pk string, rowVal types.Value) row.Row {
   174  	row, err := row.New(types.Format_Default, sch, row.TaggedValues{
   175  		schema.DocNameTag: types.String(pk),
   176  		schema.DocTextTag: rowVal,
   177  	})
   178  	require.NoError(t, err)
   179  
   180  	return row
   181  }
   182  
   183  func createTestRows(t *testing.T, vrw types.ValueReadWriter, sch schema.Schema, rows []row.Row) (types.Map, []row.Row) {
   184  	ctx := context.Background()
   185  	var err error
   186  
   187  	m, err := types.NewMap(ctx, vrw)
   188  	require.NoError(t, err)
   189  	ed := m.Edit()
   190  
   191  	for _, r := range rows {
   192  		ed = ed.Set(r.NomsMapKey(sch), r.NomsMapValue(sch))
   193  	}
   194  
   195  	m, err = ed.Map(ctx)
   196  	require.NoError(t, err)
   197  
   198  	return m, rows
   199  }