package bongo

import (
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

// For test usage
func getConnection() *Connection {
	conf := &Config{
		ConnectionString: "localhost",
		Database:         "bongotest",
	}

	conn, err := Connect(conf)

	if err != nil {
		panic(err)
	}

	return conn
}

func TestConnect(t *testing.T) {
	Convey("should be able to connect to a database using a config", t, func() {
		conf := &Config{
			ConnectionString: "localhost",
			Database:         "bongotest",
		}

		conn, err := Connect(conf)
		defer conn.Session.Close()
		So(err, ShouldEqual, nil)

		err = conn.Session.Ping()
		So(err, ShouldEqual, nil)
	})
}

func TestRetrieveCollection(t *testing.T) {
	Convey("should be able to retrieve a collection instance from a connection", t, func() {
		conn := getConnection()
		defer conn.Session.Close()
		col := conn.Collection("tests")

		So(col.Name, ShouldEqual, "tests")
		So(col.Connection, ShouldEqual, conn)
	})
}

package bongo

import (
	"testing"

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

// For test usage
func getConnection() *Connection {
	conf := &Config{
		ConnectionString: "localhost",
		Database:         "bongotest",
	}

	conn, err := Connect(conf)

	if err != nil {
		panic(err)
	}

	return conn
}

func TestFailSSLConnec(t *testing.T) {
	Convey("should fail to connect to a database because of unsupported ssl flag", t, func() {
		conf := &Config{
			ConnectionString: "mongodb://localhost?ssl=true",
			Database:         "bongotest",
		}

		_, err := Connect(conf)
		So(err.Error(), ShouldEqual, "cannot parse given URI mongodb://localhost?ssl=true due to error: unsupported connection URL option: ssl=true")
	})
}

func TestConnect(t *testing.T) {
	Convey("should be able to connect to a database using a config", t, func() {
		conf := &Config{
			ConnectionString: "localhost",
			Database:         "bongotest",
		}

		conn, err := Connect(conf)
		defer conn.Session.Close()
		So(err, ShouldEqual, nil)

		err = conn.Session.Ping()
		So(err, ShouldEqual, nil)
	})
}

func TestRetrieveCollection(t *testing.T) {
	Convey("should be able to retrieve a collection instance from a connection", t, func() {
		conn := getConnection()
		defer conn.Session.Close()
		col := conn.Collection("tests")

		So(col.Name, ShouldEqual, "tests")
		So(col.Connection, ShouldEqual, conn)
	})
}

Add test that throws out connection string error
Add test that throws out connection string error
