<commit_before>package models

import (
	"testing"
	"time"

	. "github.com/franela/goblin"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func Test(t *testing.T) {
	var collection *mgo.Collection
	var dao *IntervalDao

	g := Goblin(t)
	g.Describe("IntervalDao", func() {

		g.BeforeEach(func() {
			var err error
			dao, err = createDao()
			g.Assert(err).Equal(nil)
			collection = dao.getDBCollection()
			cleanCollection(collection)
		})

		g.It("should save an Interval", func() {
			userID := bson.NewObjectId()
			now := time.Now()

			insertErr := dao.Save(NewIntervalStart(userID, now))

			g.Assert(insertErr).Equal(nil)
			var interval Interval
			findErr := collection.Find(bson.M{"userid": userID}).One(&interval)
			g.Assert(findErr).Equal(nil)
			g.Assert(interval.Start.Unix()).Equal(now.Unix())
			g.Assert(interval.UserID).Equal(userID)
		})

		g.It("should find all intervals by userID", func() {
			userID := bson.NewObjectId()
			dao.Save(NewIntervalStart(userID, time.Now()))
			dao.Save(NewIntervalStart(userID, time.Now()))
			dao.Save(NewIntervalStart(bson.NewObjectId(), time.Now()))

			intervals, err := dao.FindByUserID(userID)

			g.Assert(err).Equal(nil)
			g.Assert(len(intervals)).Equal(2)
			g.Assert(intervals[0].UserID).Equal(userID)
			g.Assert(intervals[1].UserID).Equal(userID)
		})
	})
}

func createDao() (*IntervalDao, error) {
	session, err := createSession()
	dao := NewIntervalDao(session, "timetracker")
	return dao, err
}

func createSession() (*mgo.Session, error) {
	return mgo.Dial("localhost")
}

func cleanCollection(collection *mgo.Collection) error {
	return collection.DropCollection()
}<commit_msg>switched to testframework gocheck<commit_after>package models

import (
	"testing"
	"time"

	. "gopkg.in/check.v1"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type IntervalDaoSuite struct {
	collection *mgo.Collection
	dao        *IntervalDao
}

func (suite *IntervalDaoSuite) SetUpTest(c *C) {
	suite.dao, _ = createDao()
	suite.collection = suite.dao.getDBCollection()
	cleanCollection(suite.collection)
}

func (suite *IntervalDaoSuite) TestSave(c *C) {
	userID := bson.NewObjectId()
	now := time.Now()

	insertErr := suite.dao.Save(NewIntervalStart(userID, now))

	c.Assert(insertErr, IsNil)
	var interval Interval
	findErr := suite.collection.Find(bson.M{"userid": userID}).One(&interval)
	c.Assert(findErr, IsNil)
	c.Assert(interval.Start.Unix(), Equals, now.Unix())
	c.Assert(interval.UserID, Equals, userID)
}

func (suite *IntervalDaoSuite) TestFindByUserID(c *C) {
	userID := bson.NewObjectId()
	suite.dao.Save(NewIntervalStart(userID, time.Now()))
	suite.dao.Save(NewIntervalStart(userID, time.Now()))
	suite.dao.Save(NewIntervalStart(bson.NewObjectId(), time.Now()))

	intervals, err := suite.dao.FindByUserID(userID)

	c.Assert(err, IsNil)
	c.Assert(intervals, HasLen, 2)
	c.Assert(intervals[0].UserID, Equals, userID)
	c.Assert(intervals[1].UserID, Equals, userID)
}

func createDao() (*IntervalDao, error) {
	session, err := createSession()
	dao := NewIntervalDao(session, "timetracker")
	return dao, err
}

func createSession() (*mgo.Session, error) {
	return mgo.Dial("localhost")
}

func cleanCollection(collection *mgo.Collection) error {
	return collection.DropCollection()
}

func Test(t *testing.T) { TestingT(t) }

var _ = Suite(&IntervalDaoSuite{})
{'fraction_non_alphanumeric': 0.15995189416716776, 'fraction_numerical': 0.003307276007215875, 'mean_word_length': 4.95169946332737, 'pattern_counts': {'":': 2, '<': 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': '4002794', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1259, 'n_words': 215}