<commit_before>package rest

import (
	"reflect"
	"testing"
	"time"

	"github.com/lfq7413/tomato/cache"
	"github.com/lfq7413/tomato/cloud"
	"github.com/lfq7413/tomato/config"
	"github.com/lfq7413/tomato/errs"
	"github.com/lfq7413/tomato/livequery"
	"github.com/lfq7413/tomato/orm"
	"github.com/lfq7413/tomato/types"
	"github.com/lfq7413/tomato/utils"
)

func Test_NewWrite(t *testing.T) {
	var auth *Auth
	var className string
	var query types.M
	var data types.M
	var originalData types.M
	var clientSDK map[string]string
	var result *Write
	var err error
	var expect *Write
	var expectErr error
	/***************************************************************/
	auth = nil
	className = "user"
	query = nil
	data = types.M{
		"objectId": "1001",
	}
	originalData = nil
	clientSDK = nil
	_, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expectErr = errs.E(errs.InvalidKeyName, "objectId is an invalid field name.")
	if reflect.DeepEqual(expectErr, err) == false {
		t.Error("expect:", expectErr, "result:", err)
	}
	/***************************************************************/
	auth = nil
	className = "user"
	query = nil
	data = types.M{
		"key": "hello",
	}
	originalData = nil
	clientSDK = nil
	result, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expect = &Write{
		auth:                       Nobody(),
		className:                  "user",
		query:                      nil,
		data:                       types.M{"key": "hello"},
		originalData:               nil,
		storage:                    types.M{},
		RunOptions:                 types.M{},
		response:                   nil,
		updatedAt:                  utils.TimetoString(time.Now().UTC()),
		responseShouldHaveUsername: false,
		clientSDK:                  nil,
	}
	if reflect.DeepEqual(expect, result) == false {
		t.Error("expect:", expect, "result:", result)
	}
	/***************************************************************/
	auth = nil
	className = "user"
	query = types.M{
		"objectId": "1001",
	}
	data = types.M{
		"key": "hello",
	}
	originalData = types.M{
		"key": "hi",
	}
	clientSDK = nil
	result, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expect = &Write{
		auth:                       Nobody(),
		className:                  "user",
		query:                      types.M{"objectId": "1001"},
		data:                       types.M{"key": "hello"},
		originalData:               types.M{"key": "hi"},
		storage:                    types.M{},
		RunOptions:                 types.M{},
		response:                   nil,
		updatedAt:                  utils.TimetoString(time.Now().UTC()),
		responseShouldHaveUsername: false,
		clientSDK:                  nil,
	}
	if reflect.DeepEqual(expect, result) == false {
		t.Error("expect:", expect, "result:", result)
	}
}

func Test_Execute_Write(t *testing.T) {
	//
}

func Test_getUserAndRoleACL_Write(t *testing.T) {
	var schema types.M
	var object types.M
	var className string
	var w *Write
	var auth *Auth
	var query types.M
	var data types.M
	var originalData types.M
	var expect []string
	/***************************************************************/
	auth = Master()
	query = nil
	data = types.M{}
	originalData = nil
	w, _ = NewWrite(auth, "user", query, data, originalData, nil)
	w.getUserAndRoleACL()
	if _, ok := w.RunOptions["acl"]; ok {
		t.Error("findOptions[acl] exist")
	}
	/***************************************************************/
	auth = Nobody()<commit_msg>添加 Execute 的单元测试<commit_after>package rest

import (
	"fmt"
	"reflect"
	"testing"
	"time"

	"github.com/lfq7413/tomato/cache"
	"github.com/lfq7413/tomato/cloud"
	"github.com/lfq7413/tomato/config"
	"github.com/lfq7413/tomato/errs"
	"github.com/lfq7413/tomato/livequery"
	"github.com/lfq7413/tomato/orm"
	"github.com/lfq7413/tomato/types"
	"github.com/lfq7413/tomato/utils"
)

func Test_NewWrite(t *testing.T) {
	var auth *Auth
	var className string
	var query types.M
	var data types.M
	var originalData types.M
	var clientSDK map[string]string
	var result *Write
	var err error
	var expect *Write
	var expectErr error
	/***************************************************************/
	auth = nil
	className = "user"
	query = nil
	data = types.M{
		"objectId": "1001",
	}
	originalData = nil
	clientSDK = nil
	_, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expectErr = errs.E(errs.InvalidKeyName, "objectId is an invalid field name.")
	if reflect.DeepEqual(expectErr, err) == false {
		t.Error("expect:", expectErr, "result:", err)
	}
	/***************************************************************/
	auth = nil
	className = "user"
	query = nil
	data = types.M{
		"key": "hello",
	}
	originalData = nil
	clientSDK = nil
	result, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expect = &Write{
		auth:                       Nobody(),
		className:                  "user",
		query:                      nil,
		data:                       types.M{"key": "hello"},
		originalData:               nil,
		storage:                    types.M{},
		RunOptions:                 types.M{},
		response:                   nil,
		updatedAt:                  utils.TimetoString(time.Now().UTC()),
		responseShouldHaveUsername: false,
		clientSDK:                  nil,
	}
	if reflect.DeepEqual(expect, result) == false {
		t.Error("expect:", expect, "result:", result)
	}
	/***************************************************************/
	auth = nil
	className = "user"
	query = types.M{
		"objectId": "1001",
	}
	data = types.M{
		"key": "hello",
	}
	originalData = types.M{
		"key": "hi",
	}
	clientSDK = nil
	result, err = NewWrite(auth, className, query, data, originalData, clientSDK)
	expect = &Write{
		auth:                       Nobody(),
		className:                  "user",
		query:                      types.M{"objectId": "1001"},
		data:                       types.M{"key": "hello"},
		originalData:               types.M{"key": "hi"},
		storage:                    types.M{},
		RunOptions:                 types.M{},
		response:                   nil,
		updatedAt:                  utils.TimetoString(time.Now().UTC()),
		responseShouldHaveUsername: false,
		clientSDK:                  nil,
	}
	if reflect.DeepEqual(expect, result) == false {
		t.Error("expect:", expect, "result:", result)
	}
}

func Test_Execute_Write(t *testing.T) {
	var className string
	var w *Write
	var auth *Auth
	var query types.M
	var data types.M
	var originalData types.M
	var err error
	var result types.M
	var results types.S
	/***************************************************************/
	initEnv()
	className = "user"
	auth = Master()
	query = nil
	data = types.M{"username": "joe"}
	originalData = nil
	w, err = NewWrite(auth, className, query, data, originalData, nil)
	result, err = w.Execute()
	if err != nil || result == nil {
		t.Error("expect:", nil, "result:", result, err)
	}
	results, _ = orm.TomatoDBController.Find(className, types.M{}, types.M{})
	if len(results) != 1 {
		t.Error("expect:", "len 1", "result:", results)
	}
	id := utils.M(result["response"])["objectId"]
	auth = Master()
	query = types.M{"objectId": id}
	data = types.M{"username": "jack"}
	originalData = types.M{"username": "joe"}
	w, err = NewWrite(auth, className, query, data, originalData, nil)
	result, err = w.Execute()
	fmt.Println(result)
	if err != nil || result == nil {
		t.Error("expect:", nil, "result:", result, err)
	}
	results, _ = orm.TomatoDBController.Find(className, types.M{}, types.M{})
	fmt.Println(results)
	if len(results) != 1 {
		t.Error("expect:", "len 1", "result:", results)
	}
	orm.TomatoDBController.DeleteEverything()
}

func Test_getUserAndRoleACL_Write(t *testing.T) {
	var schema types.M
	var object types.M
	var className string
	var w *Write
	var auth *Auth
	var query types.M
	var data types.M
	var originalData types.M
	var expect []string
	/***************************************************************/
	auth = Master()
	query = nil
	data = types.M{}
	originalData = nil
	w, _ = NewWrite(auth, "user", query, data, originalData, nil)
	w.getUserAndRoleACL()
	if _, ok := w.RunOptions["acl"]; ok {
		t.Error("findOptions[acl] exist")
	}
	/***************************************************************/
	auth = Nobody()
{'fraction_non_alphanumeric': 0.2150096525096525, 'fraction_numerical': 0.0111003861003861, 'mean_word_length': 2.987012987012987, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1024765', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2650, 'n_words': 637}