(t *testing.T)
| 1187 | } |
| 1188 | |
| 1189 | func TestCreateMonitor_ForeignKeyViolation(t *testing.T) { |
| 1190 | db, mock, err := sqlmock.New() |
| 1191 | assert.NoError(t, err) |
| 1192 | defer func() { _ = db.Close() }() |
| 1193 | |
| 1194 | ds := Datasource{Conn: db} |
| 1195 | |
| 1196 | monitor := model.BalanceMonitor{ |
| 1197 | BalanceID: "bln_nonexistent", |
| 1198 | Condition: model.AlertCondition{ |
| 1199 | Field: "balance", |
| 1200 | Operator: "<", |
| 1201 | Value: 1000, |
| 1202 | }, |
| 1203 | } |
| 1204 | |
| 1205 | mock.ExpectExec(regexp.QuoteMeta(` |
| 1206 | INSERT INTO ledgerforge.balance_monitors (monitor_id, balance_id, field, operator, value, precision, precise_value, description, call_back_url, created_at) |
| 1207 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) |
| 1208 | `)). |
| 1209 | WithArgs(sqlmock.AnyArg(), monitor.BalanceID, monitor.Condition.Field, monitor.Condition.Operator, monitor.Condition.Value, sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg()). |
| 1210 | WillReturnError(&pq.Error{Code: "23503", Message: "foreign_key_violation"}) |
| 1211 | |
| 1212 | _, err = ds.CreateMonitor(monitor) |
| 1213 | assert.Error(t, err) |
| 1214 | apiErr, ok := err.(apierror.APIError) |
| 1215 | assert.True(t, ok) |
| 1216 | assert.Equal(t, apierror.ErrBadRequest, apiErr.Code) |
| 1217 | |
| 1218 | err = mock.ExpectationsWereMet() |
| 1219 | assert.NoError(t, err) |
| 1220 | } |
| 1221 | |
| 1222 | func TestGetMonitorByID_Success(t *testing.T) { |
| 1223 | db, mock, err := sqlmock.New() |
nothing calls this directly
no test coverage detected