GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.1 KiB

  1. package parsers
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. "time"
  7. )
  8. func TestIRCCloudPost(t *testing.T) {
  9. table := []struct {
  10. Input string
  11. TS string
  12. Kind string
  13. Nick string
  14. Text string
  15. Skipped bool
  16. }{
  17. {
  18. "[2016-07-18 12:57:17] — \u001FCharacter_Name\u001F does things.",
  19. "2016-07-18 12:57:17", "action", "Character_Name", "does things.",
  20. false,
  21. },
  22. {
  23. "[2016-07-18 13:04:06] — Character keeps their mouth shut again for the time beings.",
  24. "2016-07-18 13:04:06", "action", "Character", "keeps their mouth shut again for the time beings.",
  25. false,
  26. },
  27. {
  28. "[2016-07-18 12:34:42] <Victoria_Steels> \"I can keep it professional.\"",
  29. "2016-07-18 12:34:42", "text", "Victoria_Steels", "\"I can keep it professional.\"",
  30. false,
  31. },
  32. {
  33. "[2016-07-18 10:41:10] * UserZzz → User",
  34. "", "", "", "",
  35. true,
  36. },
  37. {
  38. "[2016-07-18 12:53:55] → Someone joined",
  39. "", "", "", "",
  40. true,
  41. },
  42. {
  43. "[2016-07-18 22:14:15] ⇐ SomeoneElse quit (s@example.com): Ping timeout: 547 seconds",
  44. "", "", "", "",
  45. true,
  46. },
  47. }
  48. for i, row := range table {
  49. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  50. post, err := IRCCloudPost(row.Input, time.UTC)
  51. if err != nil && err != ErrSkip {
  52. t.Fatal("Could not parse post:", err)
  53. }
  54. if row.Skipped {
  55. if err == ErrSkip {
  56. return
  57. }
  58. t.Fatal("Row should be skipped")
  59. }
  60. if !row.Skipped && err == ErrSkip {
  61. t.Fatal("Row should not be skipped, but is")
  62. }
  63. assert.Equal(t, row.TS, post.Time.Format("2006-01-02 15:04:05"), "Timestamps should match.")
  64. assert.Equal(t, row.Kind, post.Kind, "Kinds should match.")
  65. assert.Equal(t, row.Nick, post.Nick, "Kinds should match.")
  66. assert.Equal(t, row.Text, post.Text, "Kinds should match.")
  67. })
  68. }
  69. }
  70. func TestIRCCloudLog(t *testing.T) {
  71. type logLine struct {
  72. TS string
  73. Kind string
  74. Nick string
  75. Text string
  76. }
  77. logs := [][]logLine{
  78. {
  79. {"2011-09-23 15:37:16", "action", "Jason_Wolfe", "sheds a bit of the tension he was harboring as Markus extends his hand."},
  80. {"2011-09-23 15:41:15", "action", "Markus_Vasquez", "gives another nod, but there's no smile from him."},
  81. {"2011-09-23 15:47:22", "action", "Jason_Wolfe", "fights the temptation to shove his hand back in his pocket as he notices Markus."},
  82. {"2011-09-23 15:47:22", "action", "Jason_Wolfe", "warmer to wear."},
  83. {"2011-09-23 15:47:32", "text", "Dante", "((you're*))"},
  84. {"2011-09-23 15:48:45", "action", "Markus_Vasquez", "grunts as they walk toward the door, shaking his head slightly."},
  85. },
  86. {
  87. {"2011-09-25 21:03:55", "scene", "=Scene=", "It's early evening on the 11th of November, and the weather is clear."},
  88. {"2011-09-25 21:03:55", "scene", "=Scene=", "café on the eastern edge of town."},
  89. {"2011-09-25 21:08:33", "action", "Sofia_Tennhausen", "is sat on a small table outside the cafe, a cup of lukewarm coffee in her hand."},
  90. },
  91. }
  92. results, err := IRCCloudLogs(testLog, time.UTC, time.Hour*6)
  93. if err != nil {
  94. t.Fatal("Parse", err)
  95. }
  96. if len(results) != len(logs) {
  97. t.Fatal("Two logs expected, got", len(logs))
  98. }
  99. for i, log := range logs {
  100. result := results[i]
  101. if len(result.Posts) != len(log) {
  102. t.Error(len(log), "posts expected in log", i, "but got", len(result.Posts))
  103. for _, post := range result.Posts {
  104. t.Log(*post)
  105. }
  106. return
  107. }
  108. for j, post := range result.Posts {
  109. t.Run(fmt.Sprintf("Log_%d_Post_%d", i, j), func(t *testing.T) {
  110. assert.Equal(t, log[j].TS, post.Time.Format("2006-01-02 15:04:05"), "Timestamps should match.")
  111. assert.Equal(t, log[j].Kind, post.Kind, "Kinds should match.")
  112. assert.Equal(t, log[j].Nick, post.Nick, "Kinds should match.")
  113. assert.Equal(t, log[j].Text, post.Text, "Kinds should match.")
  114. })
  115. }
  116. }
  117. }
  118. var testLog = `
  119. [2011-09-23 15:37:16] Jason_Wolfe sheds a bit of the tension he was harboring as Markus extends his hand.
  120. [2011-09-23 15:41:15] Markus_Vasquez gives another nod, but there's no smile from him.
  121. [2011-09-23 15:47:22] Jason_Wolfe fights the temptation to shove his hand back in his pocket as he notices Markus.
  122. [2011-09-23 15:47:22] Jason_Wolfe warmer to wear.
  123. [2011-09-23 15:47:32] <Dante> ((you're*))
  124. [2011-09-23 15:48:45] Markus_Vasquez grunts as they walk toward the door, shaking his head slightly.
  125. [2011-09-23 18:00:28] Tyranniac quit (Tyranniac@example.com): Ping timeout: 260 seconds
  126. [2011-09-23 18:58:39] Tyranniac joined (Tyranniac@example.com)
  127. [2011-09-23 18:58:39] * ChanServ set +v Tyranniac
  128. [2011-09-24 01:45:21] Hobo joined (Hobo@example.com)
  129. [2011-09-24 11:37:07] Hobo quit (Hobo@example.com):
  130. [2011-09-24 21:01:34] Tyranniac quit (Tyranniac@example.com): Ping timeout: 264 seconds
  131. [2011-09-24 23:00:32] Tyranniac joined (Tyranniac@example.com)
  132. [2011-09-25 20:54:43] * Bowe Sofia_Tennhausen
  133. [2011-09-25 21:03:55] <=Scene=> It's early evening on the 11th of November, and the weather is clear.
  134. [2011-09-25 21:03:55] <=Scene=> café on the eastern edge of town.
  135. [2011-09-25 21:08:33] Sofia_Tennhausen is sat on a small table outside the cafe, a cup of lukewarm coffee in her hand.
  136. `