Add document tests
This commit is contained in:
parent
acc2699b0e
commit
c5fbe7e56b
@ -54,7 +54,7 @@ type aclTree struct {
|
|||||||
snapshotValidator *snapshotValidator
|
snapshotValidator *snapshotValidator
|
||||||
changeBuilder *changeBuilder
|
changeBuilder *changeBuilder
|
||||||
|
|
||||||
sync.Mutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildACLTree(
|
func BuildACLTree(
|
||||||
@ -86,6 +86,7 @@ func BuildACLTree(
|
|||||||
}
|
}
|
||||||
aclTree.removeOrphans()
|
aclTree.removeOrphans()
|
||||||
t.SetHeads(aclTree.Heads())
|
t.SetHeads(aclTree.Heads())
|
||||||
|
listener.Rebuild(aclTree)
|
||||||
|
|
||||||
return aclTree, nil
|
return aclTree, nil
|
||||||
}
|
}
|
||||||
@ -183,13 +184,19 @@ func (a *aclTree) rebuildFromThread(fromStart bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) ACLState() *ACLState {
|
func (a *aclTree) ACLState() *ACLState {
|
||||||
|
a.RLock()
|
||||||
|
defer a.RUnlock()
|
||||||
return a.aclState
|
return a.aclState
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change, error) {
|
func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change, error) {
|
||||||
// TODO: add snapshot creation logic
|
// TODO: add snapshot creation logic
|
||||||
a.Lock()
|
a.Lock()
|
||||||
defer a.Unlock()
|
defer func() {
|
||||||
|
a.Unlock()
|
||||||
|
// TODO: should this be called in a separate goroutine to prevent accidental cycles (tree->updater->tree)
|
||||||
|
a.updateListener.Update(a)
|
||||||
|
}()
|
||||||
|
|
||||||
a.changeBuilder.Init(a.aclState, a.fullTree, a.accountData)
|
a.changeBuilder.Init(a.aclState, a.fullTree, a.accountData)
|
||||||
err := build(a.changeBuilder)
|
err := build(a.changeBuilder)
|
||||||
@ -213,13 +220,11 @@ func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change,
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.thread.SetHeads([]string{ch.Id})
|
a.thread.SetHeads([]string{ch.Id})
|
||||||
a.updateListener.Update(a)
|
|
||||||
return ch, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
||||||
a.Lock()
|
a.Lock()
|
||||||
defer a.Unlock()
|
|
||||||
// TODO: make proper error handling, because there are a lot of corner cases where this will break
|
// TODO: make proper error handling, because there are a lot of corner cases where this will break
|
||||||
var err error
|
var err error
|
||||||
var mode Mode
|
var mode Mode
|
||||||
@ -230,6 +235,7 @@ func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
|||||||
}
|
}
|
||||||
a.removeOrphans()
|
a.removeOrphans()
|
||||||
a.thread.SetHeads(a.fullTree.Heads())
|
a.thread.SetHeads(a.fullTree.Heads())
|
||||||
|
a.Unlock()
|
||||||
switch mode {
|
switch mode {
|
||||||
case Append:
|
case Append:
|
||||||
a.updateListener.Update(a)
|
a.updateListener.Update(a)
|
||||||
@ -286,20 +292,20 @@ func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) Iterate(f func(change *Change) bool) {
|
func (a *aclTree) Iterate(f func(change *Change) bool) {
|
||||||
a.Lock()
|
a.RLock()
|
||||||
defer a.Unlock()
|
defer a.RUnlock()
|
||||||
a.fullTree.Iterate(a.fullTree.RootId(), f)
|
a.fullTree.Iterate(a.fullTree.RootId(), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) IterateFrom(s string, f func(change *Change) bool) {
|
func (a *aclTree) IterateFrom(s string, f func(change *Change) bool) {
|
||||||
a.Lock()
|
a.RLock()
|
||||||
defer a.Unlock()
|
defer a.RUnlock()
|
||||||
a.fullTree.Iterate(s, f)
|
a.fullTree.Iterate(s, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) HasChange(s string) bool {
|
func (a *aclTree) HasChange(s string) bool {
|
||||||
a.Lock()
|
a.RLock()
|
||||||
defer a.Unlock()
|
defer a.RUnlock()
|
||||||
_, attachedExists := a.fullTree.attached[s]
|
_, attachedExists := a.fullTree.attached[s]
|
||||||
_, unattachedExists := a.fullTree.unAttached[s]
|
_, unattachedExists := a.fullTree.unAttached[s]
|
||||||
_, invalidExists := a.fullTree.invalidChanges[s]
|
_, invalidExists := a.fullTree.invalidChanges[s]
|
||||||
@ -307,13 +313,13 @@ func (a *aclTree) HasChange(s string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) Heads() []string {
|
func (a *aclTree) Heads() []string {
|
||||||
a.Lock()
|
a.RLock()
|
||||||
defer a.Unlock()
|
defer a.RUnlock()
|
||||||
return a.fullTree.Heads()
|
return a.fullTree.Heads()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) Root() *Change {
|
func (a *aclTree) Root() *Change {
|
||||||
a.Lock()
|
a.RLock()
|
||||||
defer a.Unlock()
|
defer a.RUnlock()
|
||||||
return a.fullTree.Root()
|
return a.fullTree.Root()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -169,6 +169,9 @@ func (tb *treeBuilder) findBreakpoint(heads []string) (breakpoint string, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
shId := ch.SnapshotId
|
shId := ch.SnapshotId
|
||||||
|
if ch.IsSnapshot {
|
||||||
|
shId = ch.Id
|
||||||
|
}
|
||||||
if slice.FindPos(snapshotIds, shId) == -1 {
|
if slice.FindPos(snapshotIds, shId) == -1 {
|
||||||
snapshotIds = append(snapshotIds, shId)
|
snapshotIds = append(snapshotIds, shId)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,10 @@ type plainTextDocument struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *plainTextDocument) Text() string {
|
func (p *plainTextDocument) Text() string {
|
||||||
|
if p.state != nil {
|
||||||
return p.state.Text
|
return p.state.Text
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *plainTextDocument) AddText(text string) error {
|
func (p *plainTextDocument) AddText(text string) error {
|
||||||
|
|||||||
@ -1,53 +1,48 @@
|
|||||||
package plaintextdocument
|
package plaintextdocument
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/account"
|
||||||
// "github.com/anytypeio/go-anytype-infrastructure-experiments/testutils/threadbuilder"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/testutils/threadbuilder"
|
||||||
// "github.com/stretchr/testify/assert"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/thread"
|
||||||
// "testing"
|
"github.com/stretchr/testify/assert"
|
||||||
//)
|
"testing"
|
||||||
//
|
)
|
||||||
//func TestDocument_Build(t *testing.T) {
|
|
||||||
// thread, err := threadbuilder.NewThreadBuilderWithTestName("threadbuilder/userjoinexample.yml")
|
func TestDocument_NewPlainTextDocument(t *testing.T) {
|
||||||
// if err != nil {
|
keychain := threadbuilder.NewKeychain()
|
||||||
// t.Fatal(err)
|
keychain.AddSigningKey("A")
|
||||||
// }
|
keychain.AddEncryptionKey("A")
|
||||||
// keychain := thread.GetKeychain()
|
data := &account.AccountData{
|
||||||
// accountData := &AccountData{
|
Identity: keychain.GetIdentity("A"),
|
||||||
// Identity: keychain.GetIdentity("A"),
|
SignKey: keychain.SigningKeys["A"],
|
||||||
// EncKey: keychain.EncryptionKeys["A"],
|
EncKey: keychain.EncryptionKeys["A"],
|
||||||
// }
|
}
|
||||||
// doc := NewDocument(thread, NewPlainTextDocumentStateProvider(), accountData)
|
|
||||||
// res, err := doc.Build()
|
doc, err := NewPlainTextDocument(data, thread.NewInMemoryThread, "Some text")
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Fatal(err)
|
t.Fatalf("should not create document with error: %v", err)
|
||||||
// }
|
}
|
||||||
//
|
assert.Equal(t, doc.Text(), "Some text")
|
||||||
// st := res.(*DocumentState)
|
}
|
||||||
// assert.Equal(t, st.Text, "some text|first")
|
|
||||||
//}
|
func TestDocument_PlainTextDocument_AddText(t *testing.T) {
|
||||||
//
|
keychain := threadbuilder.NewKeychain()
|
||||||
//func TestDocument_Update(t *testing.T) {
|
keychain.AddSigningKey("A")
|
||||||
// thread, err := threadbuilder.NewThreadBuilderWithTestName("threadbuilder/userjoinexample.yml")
|
keychain.AddEncryptionKey("A")
|
||||||
// if err != nil {
|
data := &account.AccountData{
|
||||||
// t.Fatal(err)
|
Identity: keychain.GetIdentity("A"),
|
||||||
// }
|
SignKey: keychain.SigningKeys["A"],
|
||||||
// keychain := thread.GetKeychain()
|
EncKey: keychain.EncryptionKeys["A"],
|
||||||
// accountData := &AccountData{
|
}
|
||||||
// Identity: keychain.GetIdentity("A"),
|
|
||||||
// EncKey: keychain.EncryptionKeys["A"],
|
doc, err := NewPlainTextDocument(data, thread.NewInMemoryThread, "Some text")
|
||||||
// }
|
if err != nil {
|
||||||
// doc := NewDocument(thread, NewPlainTextDocumentStateProvider(), accountData)
|
t.Fatalf("should not create document with error: %v", err)
|
||||||
// res, err := doc.Build()
|
}
|
||||||
// if err != nil {
|
|
||||||
// t.Fatal(err)
|
err = doc.AddText("Next")
|
||||||
// }
|
if err != nil {
|
||||||
//
|
t.Fatalf("should be able to add document: %v", err)
|
||||||
// st := res.(*DocumentState)
|
}
|
||||||
// assert.Equal(t, st.Text, "some text|first")
|
assert.Equal(t, doc.Text(), "Some text|Next")
|
||||||
//
|
}
|
||||||
// rawChs := thread.GetUpdatedChanges()
|
|
||||||
// res, updateResult, err := doc.Update(rawChs...)
|
|
||||||
// assert.Equal(t, updateResult, UpdateResultAppend)
|
|
||||||
// assert.Equal(t, res.(*DocumentState).Text, "some text|first|second")
|
|
||||||
//}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user