<commit_before>package db

import (
	"fmt"
	"testing"
	"time"

	"github.com/Gawainli/funnel/utils"

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

type Account struct {
	ID        bson.ObjectId `json:"_id" bson:"_id"`
	UID       int           `json:"uid" bson:"uid"`
	UserName  string        `json:"username" bson:"username"`
	Pwd       string        `json:"pwd" bson:"pwd"`
	Accid     string        `json:"accid" bson:"accid"`
	LoginTime time.Time     `json:"logintime" bson:"logintime"`
}

func (a *Account) GenObjectID() bson.ObjectId {
	a.ID = bson.NewObjectId()
	return a.ID
}

func (a *Account) GetObjectID() bson.ObjectId {
	return a.ID
}

func connectMongo() error {
	return ConnectMongoDB("mongodb://192.168.0.98:27017", "test")
}

func Test_MongoDBInsert(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}
	acc := Account{}
	acc.GenObjectID()
	acc.UID = GetNextSeq(utils.GenRandIntN(100))
	acc.LoginTime = time.Now()
	acc.UserName = "ht5"
	acc.Pwd = "<PASSWORD>"

	// err = AddData("accounts", js)
	query := func(c *mgo.Collection) error {
		return c.Insert(&acc)
	}
	QueryWithCollection("testaccount", query)
	if err != nil {
		t.Error(err.Error())
	}
}

func Test_MongoDBGetByID(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}

	acc := Account{}
	objid := bson.ObjectIdHex("5007e1e773ea8212f8d0d9d7")
	query := func(c *mgo.Collection) error {
		return c.FindId(objid).One(&acc)
	}

	err = QueryWithCollection("testaccount", query)
	fmt.Println("err:", err.Error())
	fmt.Println("account id", acc.ID.Hex())
	fmt.Println("account time", acc.LoginTime.Local())
}

func Test_MongoDBGet(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}

	acc := Account{}
	key := "username"
	param := "tw_892644980644208640"
	query := func(c *mgo.Collection) error {
		return c.Find(bson.M{key: param}).One(&acc)
	}

	err = QueryWithCollection("accounts", query)
	if err != nil {
		fmt.Println("err:", err.Error())
	}
	fmt.Println("account id", acc.ID.Hex())
	fmt.Println("account time", acc.LoginTime.Local())
}

func Test_MongoDBIncrID(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}
	id := GetNextSeq(100)
	fmt.Println(id)
}<commit_msg>test(mongo):test set seq<commit_after>package db

import (
	"fmt"
	"testing"
	"time"

	"github.com/Gawainli/funnel/utils"

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

type Account struct {
	ID        bson.ObjectId `json:"_id" bson:"_id"`
	UID       int           `json:"uid" bson:"uid"`
	UserName  string        `json:"username" bson:"username"`
	Pwd       string        `json:"pwd" bson:"pwd"`
	Accid     string        `json:"accid" bson:"accid"`
	LoginTime time.Time     `json:"logintime" bson:"logintime"`
}

func (a *Account) GenObjectID() bson.ObjectId {
	a.ID = bson.NewObjectId()
	return a.ID
}

func (a *Account) GetObjectID() bson.ObjectId {
	return a.ID
}

func connectMongo() error {
	return ConnectMongoDB("mongodb://192.168.1.111:27017", "test")
}

func Test_MongoDBInsert(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}
	acc := Account{}
	acc.GenObjectID()
	acc.UID = GetNextSeq("testc", "countid", utils.GenRandIntN(100))
	acc.LoginTime = time.Now()
	acc.UserName = "ht5"
	acc.Pwd = "<PASSWORD>"

	// err = AddData("accounts", js)
	query := func(c *mgo.Collection) error {
		return c.Insert(&acc)
	}
	QueryWithCollection("testaccount", query)
	if err != nil {
		t.Error(err.Error())
	}
}

func Test_MongoDBGetByID(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}

	acc := Account{}
	objid := bson.ObjectIdHex("5007e1e773ea8212f8d0d9d7")
	query := func(c *mgo.Collection) error {
		return c.FindId(objid).One(&acc)
	}

	err = QueryWithCollection("testaccount", query)
	fmt.Println("err:", err.Error())
	fmt.Println("account id", acc.ID.Hex())
	fmt.Println("account time", acc.LoginTime.Local())
}

func Test_MongoDBGet(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}

	acc := Account{}
	key := "username"
	param := "tw_892644980644208640"
	query := func(c *mgo.Collection) error {
		return c.Find(bson.M{key: param}).One(&acc)
	}

	err = QueryWithCollection("accounts", query)
	if err != nil {
		fmt.Println("err:", err.Error())
	}
	fmt.Println("account id", acc.ID.Hex())
	fmt.Println("account time", acc.LoginTime.Local())
}

func Test_MongoDBIncrID(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}
	id := GetNextSeq("testc", "countid", 100)
	fmt.Println(id)
}

func Test_SetSeqBase(t *testing.T) {
	err := connectMongo()
	if err != nil {
		t.Error(err.Error())
	}
	num := SetNextSeqBaseNum("testc", "countid", 1000)
	fmt.Println(num)
}
{'fraction_non_alphanumeric': 0.18040370058873004, 'fraction_numerical': 0.02502102607232969, 'mean_word_length': 3.9244306418219463, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4074454', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1931, 'n_words': 364}