PageRenderTime 4899ms CodeModel.GetById 43ms RepoModel.GetById 2ms app.codeStats 0ms

/app/metric/postgres/counter_integration_test.go

https://gitlab.com/epicglue/epicglue
Go | 124 lines | 89 code | 34 blank | 1 comment | 22 complexity | cd749a8f73404a85f5fe7281927e2d45 MD5 | raw file
  1. // +build integration
  2. package metric_postgres_test
  3. import (
  4. "errors"
  5. "github.com/stretchr/testify/assert"
  6. "gitlab.com/epicglue/api/connection/database/postgres"
  7. "gitlab.com/epicglue/api/helpers"
  8. "gitlab.com/epicglue/api/metric/postgres"
  9. "gitlab.com/epicglue/api/model"
  10. "testing"
  11. )
  12. func checkMetric(user_id int64, key string) (float64, error) {
  13. db := postgres.NewPostgres()
  14. query := `SELECT value FROM user_metric WHERE user_id = $1 AND key = $2`
  15. var value float64
  16. if err := db.QueryRow(query, user_id, key).Scan(&value); err != nil {
  17. return 0, err
  18. }
  19. return value, nil
  20. }
  21. func makeTestData() (*model.User, string) {
  22. user := test_helper.MakeTestUser()
  23. user.Id = 1
  24. return &user, helpers.RandomString(16)
  25. }
  26. func TestTooLongKey(t *testing.T) {
  27. user, _ := makeTestData()
  28. key := helpers.RandomString(101)
  29. metric := metric_postgres.NewPostgresMetricWithUser(user)
  30. metric.Set(key, 1)
  31. if _, err := checkMetric(user.Id, key); err != nil {
  32. assert.Equal(t, errors.New("sql: no rows in result set").Error(), err.Error())
  33. }
  34. }
  35. func TestDecimalPoints(t *testing.T) {
  36. }
  37. func TestInc(t *testing.T) {
  38. user, key := makeTestData()
  39. metric := metric_postgres.NewPostgresMetricWithUser(user)
  40. metric.Inc(key)
  41. if value, err := checkMetric(user.Id, key); err != nil {
  42. t.Error(err)
  43. } else {
  44. assert.Equal(t, 1.0, value)
  45. }
  46. metric.Inc(key)
  47. if value, err := checkMetric(user.Id, key); err != nil {
  48. t.Error(err)
  49. } else {
  50. assert.Equal(t, 2.0, value)
  51. }
  52. }
  53. func TestIncNoUser(t *testing.T) {
  54. }
  55. func TestSet(t *testing.T) {
  56. user, key := makeTestData()
  57. metric := metric_postgres.NewPostgresMetricWithUser(user)
  58. metric.Set(key, 3.33)
  59. if value, err := checkMetric(user.Id, key); err != nil {
  60. t.Error(err)
  61. } else {
  62. assert.Equal(t, 3.33, value)
  63. }
  64. metric.Set(key, 5.0)
  65. if value, err := checkMetric(user.Id, key); err != nil {
  66. t.Error(err)
  67. } else {
  68. assert.Equal(t, 5.0, value)
  69. }
  70. }
  71. func TestSetNoUser(t *testing.T) {
  72. }
  73. func TestAdd(t *testing.T) {
  74. user, key := makeTestData()
  75. metric := metric_postgres.NewPostgresMetricWithUser(user)
  76. metric.Add(key, 1)
  77. if value, err := checkMetric(user.Id, key); err != nil {
  78. t.Error(err)
  79. } else {
  80. assert.Equal(t, 1.0, value)
  81. }
  82. metric.Add(key, 5.66)
  83. if value, err := checkMetric(user.Id, key); err != nil {
  84. t.Error(err)
  85. } else {
  86. assert.Equal(t, 6.66, value)
  87. }
  88. }
  89. func TestAddNoUser(t *testing.T) {
  90. }