| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | 
							- package mo
 
- import (
 
- 	"encoding/json"
 
- 	"testing"
 
- 	"go.mongodb.org/mongo-driver/bson"
 
- )
 
- func TestMatchBuilder(t *testing.T) {
 
- 	match := Matcher{}
 
- 	match.In("fruit", A{"apple", "orange", "banana"})
 
- 	match.Nin("fruit", A{"pear"})
 
- 	match.Eq("name", "admin")
 
- 	match.Ne("role", "user")
 
- 	match.Gt("age", 10)
 
- 	match.Gte("age", 11)
 
- 	match.Lt("age", 12)
 
- 	match.Lte("age", 13)
 
- 	match.All("security", A{"SSL", "TLS"})
 
- 	match.Regex("regexp", "/^S/", "m")
 
- 	match.Not("regexp", "/^p.*/")
 
- 	em := new(Matcher)
 
- 	em.Eq("name", "1111")
 
- 	match.ElemMatch("elem", em)
 
- 	or := Matcher{}
 
- 	or.Gt("age", 10)
 
- 	or.Lt("age", 20)
 
- 	match.Or(&or)
 
- 	and := Matcher{}
 
- 	and.Add("tags", "ssl")
 
- 	and.Add("tags", "security")
 
- 	match.And(&and)
 
- 	nor := (&Matcher{}).Lte("nor", 100).Gte("nor", 50)
 
- 	match.Nor(nor)
 
- 	done := match.Done()
 
- 	pipeline := match.Pipeline()
 
- 	t.Log(done)
 
- 	t.Log(pipeline)
 
- 	b, err := json.Marshal(&match)
 
- 	if err == nil {
 
- 		t.Log("Marshal:", string(b))
 
- 	}
 
- 	if _, err = bson.Marshal(done); err != nil {
 
- 		t.Error(err)
 
- 		return
 
- 	}
 
- 	if _, err = bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 		return
 
- 	}
 
- }
 
- func TestGroupBuilder(t *testing.T) {
 
- 	group := Grouper{}
 
- 	group.Add("_id", ID.New())
 
- 	group.Add("count", D{{Key: "$sum", Value: 1}})
 
- 	done := group.Done()
 
- 	pipeline := group.Pipeline()
 
- 	t.Log(done)
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(done); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestProjectBuilder(t *testing.T) {
 
- 	p := Projecter{}
 
- 	p.AddDisable(ID.Key())
 
- 	done := p.Done()
 
- 	pipeline := p.Pipeline()
 
- 	t.Log(done)
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(done); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestSortBuilder(t *testing.T) {
 
- 	s := Sorter{}
 
- 	s.AddASC("age")
 
- 	s.AddDESC("updateTime")
 
- 	done := s.Done()
 
- 	pipeline := s.Pipeline()
 
- 	t.Log(done)
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(done); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestLimitBuilder(t *testing.T) {
 
- 	l := &Limiter{Limit: 10}
 
- 	pipeline := l.Pipeline()
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestSkipBuilder(t *testing.T) {
 
- 	s := NewSkip(20)
 
- 	pipeline := s.Pipeline()
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestLookupBuilder(t *testing.T) {
 
- 	l := Looker{}
 
- 	l.SetFrom("user")
 
- 	l.SetLocalField("sn")
 
- 	l.SetForeignField("name")
 
- 	l.SetAs("name")
 
- 	pipeline := l.Pipeline()
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestAddSetter_SUM(t *testing.T) {
 
- 	s := Setter{}
 
- 	s.SUM("total", []string{"num1", "num2", "num3"})
 
- 	pipeline := s.Pipeline()
 
- 	t.Log(pipeline)
 
- 	if _, err := bson.Marshal(pipeline); err != nil {
 
- 		t.Error(err)
 
- 	}
 
- }
 
- func TestNewPipeline(t *testing.T) {
 
- 	l := &Limiter{Limit: 10}
 
- 	s := &Skipper{Skip: 20}
 
- 	pipe := NewPipeline(s, l)
 
- 	t.Log(pipe)
 
- }
 
 
  |