<commit_before>package main

import (
	"socialapi/models"
	"socialapi/workers/common/tests"
	"testing"

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

// var AccountOldId = bson.NewObjectId()

func TestChannelDeleteWithPostgreRecord(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while  creating account", t, func() {
			Convey("First Create User", func() {
				Convey("channel creation should be successfully", func() {
					channel, accounts := createChannelWithParticipants("Group_Only_postgre")
					So(channel, ShouldNotBeNil)
					So(accounts, ShouldNotBeNil)
					Convey("participant should be in the channel", func() {
						participants, err := channel.FetchChannelParticipants()
						So(err, ShouldBeNil)
						So(len(participants), ShouldBeGreaterThan, 0)
					})
					Convey("channel deletion should be successfully if groups are not in mongodb", func() {
						err := models.DeleteChannelsIfGroupNotInMongo()
						So(err, ShouldBeNil)
						Convey("participants&channel should not be in postgres", func() {
							_, err := channel.FetchChannelParticipants()
							So(err, ShouldNotBeNil)
							So(err, ShouldEqual, bongo.RecordNotFound)
							fetchedChannels, err := models.NewChannel().FetchByIds([]int64{channel.Id})
							So(err, ShouldBeNil)
							So(len(fetchedChannels), ShouldEqual, 0)
						})
					})
				})
			})
		})
	})
}

//
// func accountCreatorWithCount(count int) error {
// 	for i := 0; i < count; i++ {
// 		account := models.NewAccount()
// 		account.OldId = bson.NewObjectId().Hex()
// 		account, err := rest.CreateAccount(account)
// 		if err != nil {
// 			return err
// 		}
// 	}
// 	return nil
// }<commit_msg>go/tests: add helpers for channel creations<commit_after>package main

import (
	"socialapi/models"
	"socialapi/workers/common/tests"
	"testing"

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

// var AccountOldId = bson.NewObjectId()

func TestChannelDeleteWithPostgreRecord(t *testing.T) {
	tests.WithRunner(t, func(r *runner.Runner) {
		Convey("while  creating account", t, func() {
			Convey("First Create User", func() {
				Convey("channel creation should be successfully", func() {
					channel, accounts := createChannelWithParticipants("Group_Only_postgre")
					So(channel, ShouldNotBeNil)
					So(accounts, ShouldNotBeNil)
					Convey("participant should be in the channel", func() {
						participants, err := channel.FetchChannelParticipants()
						So(err, ShouldBeNil)
						So(len(participants), ShouldBeGreaterThan, 0)
					})
					Convey("channel deletion should be successfully if groups are not in mongodb", func() {
						err := models.DeleteChannelsIfGroupNotInMongo()
						So(err, ShouldBeNil)
						Convey("participants&channel should not be in postgres", func() {
							_, err := channel.FetchChannelParticipants()
							So(err, ShouldNotBeNil)
							So(err, ShouldEqual, bongo.RecordNotFound)
							fetchedChannels, err := models.NewChannel().FetchByIds([]int64{channel.Id})
							So(err, ShouldBeNil)
							So(len(fetchedChannels), ShouldEqual, 0)
						})
					})
				})
			})
		})
	})
}

func createChannelWithParticipants(groupName string) (*models.Channel, []*models.Account) {
	account1 := models.CreateAccountWithTest()
	account2 := models.CreateAccountWithTest()
	account3 := models.CreateAccountWithTest()
	accounts := []*models.Account{account1, account2, account3}

	channel := models.CreateTypedGroupedChannelWithTest(account1.Id, models.Channel_TYPE_DEFAULT, groupName)
	models.AddParticipantsWithTest(channel.Id, account1.Id, account2.Id, account3.Id)

	return channel, accounts
}

func createGroupDataWithChecksInBothDB(groupName string) (*models.Channel, *models.Account) {
	account := models.CreateAccountInBothDbsWithCheck()

	groupChannel := models.CreateTypedGroupedChannelWithTest(
		account.Id,
		models.Channel_TYPE_GROUP,
		groupName,
	)

	_, err := groupChannel.AddParticipant(account.Id)
	So(err, ShouldBeNil)

	// we recommend you to not ignore errors.
	// In this case we need to try to create same named group in mongo.
	// then it gives key duplication error, so err checking is disabled here.
	models.CreateGroupInMongo(groupName, groupChannel.Id)
	// So(err, ShouldBeNil)

	return groupChannel, account
}

//
// func accountCreatorWithCount(count int) error {
// 	for i := 0; i < count; i++ {
// 		account := models.NewAccount()
// 		account.OldId = bson.NewObjectId().Hex()
// 		account, err := rest.CreateAccount(account)
// 		if err != nil {
// 			return err
// 		}
// 	}
// 	return nil
// }
{'fraction_non_alphanumeric': 0.1289410741465536, 'fraction_numerical': 0.004348771472059143, 'mean_word_length': 3.8625792811839323, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3314347', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1454, 'n_words': 319}