<commit_before>package models

import (
	"socialapi/request"
	"socialapi/workers/common/runner"
	"testing"

	"github.com/koding/bongo"
	. "github.com/smartystreets/goconvey/convey"
)

// createNewChannelWithTest creates a new account
// And inits a channel
func createNewChannelWithTest() *Channel {
	// init account
	creator := CreateAccountWithTest()

	// init channel
	c := NewChannel()
	// set Creator id
	c.CreatorId = creator.Id
	return c
}

func TestChannelNewChannel(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()

	c := NewChannel()

	Convey("given a NewChannel", t, func() {

		Convey("it should have Name as set", func() {
			So(c.Name, ShouldNotBeBlank)
		})

		Convey("it should have GroupName as set", func() {
			So(c.GroupName, ShouldNotBeBlank)
		})

		Convey("it should have TypeConstant as set", func() {
			So(c.TypeConstant, ShouldEqual, Channel_TYPE_DEFAULT)
		})

		Convey("it should have PrivacyConstant as set", func() {
			So(c.PrivacyConstant, ShouldEqual, Channel_PRIVACY_PRIVATE)
		})
	})
}

func TestChannelNewCollaborationChannel(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()<commit_msg>Socialapi: created tests for channel search<commit_after>package models

import (
	"socialapi/models"
	"socialapi/request"
	"socialapi/workers/common/runner"
	"testing"

	"github.com/koding/bongo"
	. "github.com/smartystreets/goconvey/convey"
)

// createNewChannelWithTest creates a new account
// And inits a channel
func createNewChannelWithTest() *Channel {
	// init account
	creator := CreateAccountWithTest()

	// init channel
	c := NewChannel()
	// set Creator id
	c.CreatorId = creator.Id
	return c
}

func TestChannelNewChannel(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()

	c := NewChannel()

	Convey("given a NewChannel", t, func() {

		Convey("it should have Name as set", func() {
			So(c.Name, ShouldNotBeBlank)
		})

		Convey("it should have GroupName as set", func() {
			So(c.GroupName, ShouldNotBeBlank)
		})

		Convey("it should have TypeConstant as set", func() {
			So(c.TypeConstant, ShouldEqual, Channel_TYPE_DEFAULT)
		})

		Convey("it should have PrivacyConstant as set", func() {
			So(c.PrivacyConstant, ShouldEqual, Channel_PRIVACY_PRIVATE)
		})
	})
}

func TestChannelSearch(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()

	c := NewChannel()

	Convey("moderation needed channels", t, func() {
		account := CreateAccountWithTest()
		groupChannel := CreateTypedPublicChannelWithTest(
			account.Id,
			models.Channel_TYPE_GROUP,
		)

		c.CreatorId = account.Id
		c.GroupName = groupChannel.Name
		c.TypeConstant = Channel_TYPE_TOPIC
		c.PrivacyConstant = Channel_PRIVACY_PUBLIC
		c.MetaBits.Mark(NeedsModeration)

		So(c.Create(), ShouldBeNil)
		Convey("should not be in search results", func() {
			channels, err := NewChannel().Search(&request.Query{
				Name:      c.Name,
				GroupName: groupChannel.GroupName,
				AccountId: account.Id,
			})
			So(err, ShouldBeNil)
			So(len(channels), ShouldEqual, 0)
		})

		Convey("after removing needs moderation flag", func() {
			// byname doesnt filter
			channel, err := NewChannel().ByName(&request.Query{
				Name:      c.Name,
				GroupName: groupChannel.GroupName,
				AccountId: account.Id,
			})
			So(err, ShouldBeNil)
			So(channel, ShouldNotBeNil)

			channel.MetaBits.UnMark(NeedsModeration)

			So(channel.Update(), ShouldBeNil)

			Convey("we should be able to search them", func() {
				channels, err := NewChannel().Search(&request.Query{
					Name:      c.Name,
					GroupName: groupChannel.GroupName,
					AccountId: account.Id,
					Privacy:   channel.PrivacyConstant,
				})
				So(err, ShouldBeNil)
				So(len(channels), ShouldEqual, 1)
				So(channels[0].Id, ShouldEqual, channel.Id)
			})
		})
	})
}

func TestChannelNewCollaborationChannel(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()
{'fraction_non_alphanumeric': 0.14136003726129484, 'fraction_numerical': 0.0006986492780624127, 'mean_word_length': 3.8973774230330673, 'pattern_counts': {'":': 0, '<': 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': '5436509', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1470, 'n_words': 347}