check_test.go 2.0 KB
package protocol

import (
	"encoding/json"
	"fmt"
	"testing"
)

func TestSelfChecks(t *testing.T) {
	nsc := func(id, pid int64, checkItem, answer string) SelfCheck {
		item := SelfCheck{
			CheckItem: checkItem, Answer: answer, Id: id, ParentId: pid,
		}
		item.GroupId = pid
		if pid == 0 {
			item.GroupId = id
		}
		return item
	}
	nscNo := func(id, pid int64, checkItem, answer, reason string) SelfCheck {
		item := nsc(id, pid, checkItem, answer)
		item.Reason = reason
		return item
	}
	input := SelfChecks([]SelfCheck{
		nsc(1, 0, "1", ""),
		nsc(2, 1, "2", OptionYes),
		nsc(3, 1, "3", OptionNo),
		nsc(4, 1, "4", OptionUncertain),
		nsc(5, 1, "5", ""),

		nsc(6, 0, "6", ""),
		nsc(7, 6, "7", OptionUncertain),
		nscNo(8, 6, "8", OptionNo, " "),
		nsc(9, 6, "9", OptionUncertain),
		nsc(10, 6, "10", ""),

		nsc(11, 0, "11", OptionYes),
		nsc(12, 0, "12", OptionYes),
	})
	input.SetSelfChecksLevel1ByRule()
	if input[0].Answer != OptionYes || input[5].Answer != OptionNo {
		t.Fatal(fmt.Sprintf("SetSelfChecksLevel1ByRule Fail"))
	}
	if input.Static()[0].Total != 3 || input.Static()[0].Item != OptionYes {
		t.Fatal(fmt.Sprintf("Static Fail"))
	}
	if input[7].Reason != " " {
		t.Fatal("reason data error")
	}
	input.Format()
	if input[7].Reason == " " {
		t.Fatal("reason data error")
	}
	t.Log(input.String())

	inputCompare := SelfChecks([]SelfCheck{
		nsc(1, 0, "1", ""),
		nsc(2, 1, "2", OptionYes),
		nsc(3, 1, "3", OptionNo),
		nsc(4, 1, "4", OptionNo), //diff
		nsc(5, 1, "5", ""),

		nsc(6, 0, "6", ""),
		nsc(7, 6, "7", OptionUncertain),
		nscNo(8, 6, "8", OptionNo, " "),
		nsc(9, 6, "9", OptionUncertain),
		nsc(10, 6, "10", ""),

		nsc(11, 0, "11", OptionYes),
		nsc(12, 0, "12", OptionNo), //diff
	})
	inputCompare.SetSelfChecksLevel1ByRule()
	inputCompare.Format()
	data, err := json.Marshal(input)
	if err != nil {
		t.Fatal(err)
	}
	o, err := inputCompare.Compare(string(data))
	if err != nil {
		t.Fatal(err)
	}
	if len(o) != 2 {
		t.Fatal("Compare fail")
	}
	t.Log("Compare:", o.String())
}

func TestSiftingResults(t *testing.T) {

}