Example #1
0
func TestInsertCloseErr(t *testing.T) {
	db, mock, err := NewMock()
	if err != nil {
		t.Fatalf("got NewMock() = _, _, %v, want = _, _, nil", err)
	}
	mock.ExpectBegin()
	want := errors.New("STMT ERROR")
	stmt := mock.ExpectPrepare("COPY \\\"data_raw\\\" \\(\\\"serial\\\", \\\"type\\\", \\\"data\\\", \\\"time\\\", \\\"device\\\"\\) FROM STDIN").WillReturnCloseError(want)
	stmt.ExpectExec().WillReturnResult(sqlmock.NewResult(0, 0))
	mock.ExpectRollback()

	if err := db.Insert(func() (*decoders.DataPoint, error) { return nil, nil }); err == nil || err.Error() != want.Error() {
		t.Errorf("got db.Insert() = %v, want = %v", err, want)
	}
}
Example #2
0
func TestInsertErr(t *testing.T) {
	db, mock, err := NewMock()
	if err != nil {
		t.Fatalf("got NewMock() = _, _, %v, want = _, _, nil", err)
	}
	mock.ExpectBegin()
	query := "COPY \\\"data_raw\\\" \\(\\\"serial\\\", \\\"type\\\", \\\"data\\\", \\\"time\\\", \\\"device\\\"\\) FROM STDIN"
	stmt := mock.ExpectPrepare(query)
	stmt.ExpectExec().WillReturnResult(sqlmock.NewResult(0, 0))
	mock.ExpectRollback()

	want := "call to exec query 'COPY \"data_raw\" (\"serial\", \"type\", \"data\", \"time\", \"device\") FROM STDIN' with args [0 \x00 0 0001-01-01 00:00:00 +0000 UTC <nil>], was not expected, next expectation is: ExpectedRollback => expecting transaction Rollback"
	if err := db.Insert(func() (*decoders.DataPoint, error) { return &decoders.DataPoint{}, nil }); err == nil || err.Error() != want {
		t.Errorf("got db.Insert(iter) = %v, want = %s", err, want)
	}
}
Example #3
0
func TestInsertCloseErr(t *testing.T) {
	db, mock, err := NewMock()
	if err != nil {
		t.Fatalf("got NewMock() = _, _, %v, want = _, _, nil", err)
	}
	mock.ExpectBegin()
	want := errors.New("STMT ERROR")
	stmt := mock.ExpectPrepare("COPY \\\"data_raw\\\" \\(\\\"serial\\\", \\\"type\\\", \\\"data\\\", \\\"time\\\", \\\"device\\\"\\) FROM STDIN").WillReturnCloseError(want)
	stmt.ExpectExec().WillReturnResult(sqlmock.NewResult(0, 0))
	mock.ExpectRollback()

	if err := db.Insert(func() (*decoders.DataPoint, error) { return nil, nil }); err == nil || err.Error() != want.Error() {
		// TODO: Figure out why this test doesn't work.
		// Disabled until issue is fixed:
		// https://github.com/DATA-DOG/go-sqlmock/issues/25
		//t.Errorf("got db.Insert() = %v, want = %v", err, want)
	}
}
Example #4
0
func TestDelete(t *testing.T) {
	db, mock, err := sqlmock.New()
	assert.Nil(t, err)

	taskId := 1

	mock.ExpectExec("delete from tasks").
		WithArgs(taskId).
		WillReturnResult(sqlmock.NewResult(1, 1))

	contextObject := config.ContextObject{}
	contextObject.ErrorLogFile, err = os.OpenFile(errorLogFilePath, os.O_APPEND|os.O_WRONLY, 0600)
	contextObject.Db = db

	Delete(contextObject, taskId)

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expections: %s", err)
	}
}
Example #5
0
func TestAdd(t *testing.T) {
	db, mock, err := sqlmock.New()
	assert.Nil(t, err)

	task := "dring water"
	priority := "High"

	mock.ExpectExec("insert into tasks").
		WithArgs(task, priority).
		WillReturnResult(sqlmock.NewResult(1, 1))

	contextObject := config.ContextObject{}
	contextObject.ErrorLogFile, err = os.OpenFile(errorLogFilePath, os.O_APPEND|os.O_WRONLY, 0600)
	contextObject.Db = db

	Add(contextObject, task, priority)

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expections: %s", err)
	}
}
Example #6
0
func TestModel(t *testing.T) {
	Convey("Model", t, func() {
		db, mock, _ := sqlmock.New()
		conn := NewConnection(db, "mock_db", nil)

		model := &MockModel{}
		model.Id.Scan("1")
		model.FirstName.Scan("Mock")

		modelWithEmbedded := &MockModelEmbedded{}

		Convey("ModelFields", func() {
			Convey("On Ptr to Struct", func() {
				fields := ModelFields(model)
				So(fields, ShouldContain, field.Name("Id"))
				So(fields, ShouldContain, field.Name("FirstName"))
				So(fields, ShouldContain, field.Name("Org"))
				So(len(fields), ShouldEqual, 3)
			})

			Convey("With embedded struct", func() {
				fields := ModelFields(modelWithEmbedded)
				So(fields, ShouldContain, field.Name("Id"))
				So(fields, ShouldContain, field.Name("FirstName"))
				So(fields, ShouldContain, field.Name("Org"))
				So(fields, ShouldContain, field.Name("Created"))
				So(fields, ShouldContain, field.Name("Modified"))
				So(len(fields), ShouldEqual, 5)
			})

			Convey("With embedded Model interface", func() {
				m := &MockModelInterfaceEmbedded{model}
				fields := ModelFields(m)
				So(fields, ShouldContain, field.Name("Id"))
				So(fields, ShouldContain, field.Name("FirstName"))
				So(fields, ShouldContain, field.Name("Org"))
				So(len(fields), ShouldEqual, 3)
			})

		})

		Convey("ModelGetField", func() {

			Convey("When field exists", func() {
				rawModelField, err := ModelGetField(model, "Id")
				So(err, ShouldBeNil)

				f, ok := rawModelField.(*field.NullString)
				So(ok, ShouldBeTrue)
				So(f.String, ShouldEqual, "1")
			})

			Convey("When field does not exist", func() {
				rawModelField, err := ModelGetField(model, "NotAField")
				So(rawModelField, ShouldBeNil)
				So(err, ShouldNotBeNil)
			})

			Convey("Field from Embedded struct", func() {
				modelWithEmbedded.Id.Scan("12")
				idField, err := ModelGetField(modelWithEmbedded, "Id")
				So(err, ShouldBeNil)
				f, ok := idField.(*field.NullString)
				So(ok, ShouldBeTrue)
				So(f.String, ShouldEqual, "12")
			})

			Convey("Fields from embedded Model interface", func() {
				m := &MockModelInterfaceEmbedded{Model: model}
				rawModelField, err := ModelGetField(m, "Id")
				So(err, ShouldBeNil)

				f, ok := rawModelField.(*field.NullString)
				So(ok, ShouldBeTrue)
				So(f.String, ShouldEqual, "1")
			})
		})

		Convey("ModelTableName", func() {
			Convey("Builds table name with connection database prepended", func() {
				sess := conn.NewSession(nil)
				So(ModelTableName(sess, model), ShouldEqual, "mock_db.mocks")
			})
		})

		Convey("NewSelect", func() {

			Convey("Without fields", func() {
				mock.ExpectQuery("SELECT `id`, `first_name`, `org` FROM mock_db\\.mocks").WillReturnRows(sqlmock.NewRows([]string{"id", "first_name"}).FromCSVString("2,mocker"))
				err := NewSelect(conn.NewSession(nil), model, nil).LoadStruct(model)
				So(err, ShouldBeNil)
			})

			Convey("With fields", func() {
				mock.ExpectQuery("SELECT `id` FROM mock_db\\.mocks").WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("2"))
				err := NewSelect(conn.NewSession(nil), model, field.Names{"Id"}).LoadStruct(model)
				So(err, ShouldBeNil)
				So(model.Id.String, ShouldEqual, "2")
			})
		})

		Convey("NewInsert", func() {

			Convey("Without fields", func() {
				mock.ExpectExec("INSERT INTO `mock_db`\\.`mocks` \\(`first_name`,`org`\\) VALUES \\('Mock',NULL\\)").WillReturnResult(sqlmock.NewResult(2, 1))

				_, err := NewInsert(conn.NewSession(nil), model, nil).Record(model).Exec()
				So(err, ShouldBeNil)
			})

			Convey("With fields", func() {
				mock.ExpectExec("INSERT INTO `mock_db`\\.`mocks` \\(`first_name`\\) VALUES \\('Mock'\\)").WillReturnResult(sqlmock.NewResult(3, 1))
				_, err := NewInsert(conn.NewSession(nil), model, field.Names{"FirstName"}).Record(model).Exec()
				So(err, ShouldBeNil)
			})

			Convey("With Custom Primary Key", func() {
				modelCust := &MockModelCustomPrimaryKey{}
				modelCust.FirstName.Scan("Custom Key")
				mock.ExpectExec("INSERT INTO `mock_db`\\.`mocks` \\((`id`|,|`first_name`)+\\) VALUES \\(('abc-123-xyz-789'|,|'Custom Key')+\\)").WillReturnResult(sqlmock.NewResult(4, 1))
				_, err := NewInsert(conn.NewSession(nil), modelCust, field.Names{"FirstName"}).Record(modelCust).Exec()
				So(err, ShouldBeNil)
			})
		})

		Convey("NewUpdate", func() {

			Convey("Without fields", func() {
				mock.ExpectExec("UPDATE `mock_db`\\.`mocks` SET (`first_name` = 'Mock'|, |`org` = NULL)+ WHERE \\(id = '1'\\)").WillReturnResult(sqlmock.NewResult(0, 1))

				_, err := NewUpdate(conn.NewSession(nil), model, nil).Where("id = ?", model.Id.String).Exec()
				So(err, ShouldBeNil)
			})

			Convey("With fields", func() {
				mock.ExpectExec("UPDATE `mock_db`\\.`mocks` SET `first_name` = 'Mock' WHERE \\(id = '1'\\)").WillReturnResult(sqlmock.NewResult(0, 1))
				_, err := NewUpdate(conn.NewSession(nil), model, field.Names{"FirstName"}).Where("id = ?", model.Id.String).Exec()
				So(err, ShouldBeNil)
			})
		})

		Convey("ModelLoadMap", func() {
			dataMap := map[string]interface{}{
				"id":         "1234",
				"first_name": "James",
			}
			ModelLoadMap(model, dataMap)
			So(model.Id.String, ShouldEqual, "1234")
			So(model.FirstName.String, ShouldEqual, "James")
		})

		Convey("ModelChangedFields", func() {
			model := &MockModel{}
			model.Id.Scan("1")
			model.FirstName.Scan("James James James")
			Convey("No changed fields", func() {
				f, err := ModelDirtyFields(model)
				So(len(f), ShouldEqual, 0)
				So(err, ShouldBeNil)
			})

			Convey("Changed", func() {
				model.FirstName.Scan("Santa")
				f, err := ModelDirtyFields(model)
				So(len(f), ShouldEqual, 1)
				So(err, ShouldBeNil)
			})
		})

		Convey("ModelGetSetFields", func() {
			model := &MockModel{}

			Convey("No set fields", func() {
				f, err := ModelGetSetFields(model)
				So(len(f), ShouldEqual, 0)
				So(err, ShouldBeNil)
			})

			Convey("Changed", func() {
				model.Id.Scan("1")
				model.FirstName.Scan("James James James")
				f, err := ModelGetSetFields(model)
				So(len(f), ShouldEqual, 2)
				So(err, ShouldBeNil)
			})
		})

		Convey("ModelValidate", func() {

			Convey("With validation error", func() {
				err := ModelValidate(conn.NewSession(nil), model, nil)
				So(err, ShouldNotBeNil)
				So(err, ShouldHaveSameTypeAs, &ValidationErrors{})
				So(err.Error(), ShouldEqual, "Field: [FirstName] Alias: [matches] Message: Value [Mock] did not equal first argument [Pete]")
			})

			Convey("Without validation error", func() {
				model.FirstName.Scan("Pete")
				err := ModelValidate(conn.NewSession(nil), model, nil)
				So(err, ShouldBeNil)
			})

		})
	})
}
Example #7
-1
func TestInsert(t *testing.T) {
	oldVerbosity := constants.Verbose
	constants.Verbose = true
	defer func() { constants.Verbose = oldVerbosity }()

	closureIndex := 0
	var iter = func() (*decoders.DataPoint, error) {
		if closureIndex >= 3 {
			return nil, nil
		}
		point := &decoders.DataPoint{
			Serial: 64,
			Type:   'T',
			Data:   int64(closureIndex),
			Time:   time.Unix(int64(500+closureIndex), 0),
		}
		closureIndex++
		return point, nil
	}

	db, mock, err := NewMock()
	if err != nil {
		t.Fatalf("got NewMock() = _, _, %v, want = _, _, nil", err)
	}
	mock.ExpectBegin()
	query := "COPY \\\"data_raw\\\" \\(\\\"serial\\\", \\\"type\\\", \\\"data\\\", \\\"time\\\", \\\"device\\\"\\) FROM STDIN"
	stmt := mock.ExpectPrepare(query)
	stmt.ExpectExec().WithArgs(64, "T", 0, time.Unix(500, 0), nil).WillReturnResult(sqlmock.NewResult(0, 1))
	stmt.ExpectExec().WithArgs(64, "T", 1, time.Unix(501, 0), nil).WillReturnResult(sqlmock.NewResult(0, 1))
	stmt.ExpectExec().WithArgs(64, "T", 2, time.Unix(502, 0), nil).WillReturnResult(sqlmock.NewResult(0, 1))
	stmt.ExpectExec().WillReturnResult(sqlmock.NewResult(0, 0))
	mock.ExpectCommit()

	if err := db.Insert(iter); err != nil {
		t.Errorf("got db.Insert(iter) = %v, want = nil", err)
	}
}