/tests/output/sql/assert_test.js

https://github.com/Khan/live-editor · JavaScript · 278 lines · 245 code · 21 blank · 12 comment · 24 complexity · 8dcd570505acff6a126ede5ab886ea6d MD5 · raw file

  1. describe("Challenge Assertions - SQL Tables", function() {
  2. var basicTest = function() {
  3. staticTest($._("Create a table and insert some values"), function() {
  4. var description = $._("Now let's add 3 books into our bookshelf");
  5. var template = "CREATE TABLE _$1 (id INTEGER, name TEXT, rating " +
  6. "INTEGER);" +
  7. "INSERT INTO _$1 VALUES(1, \"book1\", 5);" +
  8. "INSERT INTO _$1 VALUES(1, \"book2\", 4);" +
  9. "INSERT INTO _$1 VALUES(2, \"book3\", 5);";
  10. var templateDB = initTemplateDB(template);
  11. var result = allPass(matchTableCount(templateDB),
  12. matchTableRowCount(templateDB),
  13. matchTableColumnCount(templateDB));
  14. var result = fail();
  15. if (!passes(matchTableCount(templateDB))) {
  16. result = fail("Not enough tables!");
  17. } else if (passes(matchTableRowCount(1))) {
  18. result = fail("You only have 1 row, add 2 more!");
  19. } else if (!passes(matchTableRowCount(templateDB))) {
  20. result = fail("Not enough rows!");
  21. } else if (passes(matchTableColumnCount(1))) {
  22. result = fail("You only have 1 column, add 2 more!");
  23. } else if (!passes(matchTableColumnCount(templateDB))) {
  24. result = fail("Not enough columns!");
  25. } else if (!passes(matchTableColumnNames(templateDB))) {
  26. result = fail("Not the right column names!");
  27. } else {
  28. result = matchTableColumnNames(templateDB);
  29. }
  30. assertMatch(result, description, "INSERT INTO _ VALUES (...);");
  31. });
  32. }.toString().replace(/^function.*?{([\s\S]*?)}$/, "$1");
  33. // No code should not be accepted
  34. var userCode = "";
  35. assertTest({
  36. title: "No code not accepted",
  37. code: userCode,
  38. pass: false,
  39. validate: basicTest,
  40. errors: [{}]
  41. });
  42. userCode = "CREATE TABLE books (id INTEGER);" +
  43. "INSERT INTO books VALUES(1);";
  44. assertTest({
  45. title: "Has a certain # of rows",
  46. code: userCode,
  47. pass: false,
  48. validate: basicTest,
  49. fromTests: true,
  50. reason: "You only have 1 row, add 2 more!",
  51. errors: [{}]
  52. });
  53. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  54. "INTEGER);";
  55. assertTest({
  56. title: "Doesn't have enough rows",
  57. code: userCode,
  58. pass: false,
  59. validate: basicTest,
  60. fromTests: true,
  61. reason: "Not enough rows!",
  62. errors: [{}]
  63. });
  64. // Not enough columns should not be accepted
  65. userCode = "CREATE TABLE books (id INTEGER);" +
  66. "INSERT INTO books VALUES(1);" +
  67. "INSERT INTO books VALUES(2);" +
  68. "INSERT INTO books VALUES(3);";
  69. assertTest({
  70. title: "Has a certain # of columns",
  71. code: userCode,
  72. pass: false,
  73. validate: basicTest,
  74. fromTests: true,
  75. reason: "You only have 1 column, add 2 more!",
  76. errors: [{}]
  77. });
  78. // Not enough columns should not be accepted
  79. userCode = "CREATE TABLE books (id INTEGER, name TEXT);" +
  80. "INSERT INTO books VALUES(1, \"book1\");" +
  81. "INSERT INTO books VALUES(1, \"book2\");" +
  82. "INSERT INTO books VALUES(2, \"book3\");";
  83. assertTest({
  84. title: "Doesn't have the right # of columns",
  85. code: userCode,
  86. pass: false,
  87. validate: basicTest,
  88. fromTests: true,
  89. reason: "Not enough columns!",
  90. errors: [{}]
  91. });
  92. // Not matching columns should not be accepted
  93. userCode = "CREATE TABLE books (id INTEGER, author TEXT, year TEXT);" +
  94. "INSERT INTO books VALUES(1, \"book1\", \"1984\");" +
  95. "INSERT INTO books VALUES(1, \"book2\", \"1944\");" +
  96. "INSERT INTO books VALUES(2, \"book3\", \"1934\");";
  97. assertTest({
  98. title: "Wrong column names",
  99. code: userCode,
  100. pass: false,
  101. validate: basicTest,
  102. fromTests: true,
  103. reason: "Not the right column names!",
  104. errors: [{}]
  105. });
  106. // Valid input
  107. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  108. "INTEGER);" +
  109. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  110. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  111. "INSERT INTO books VALUES(2, \"book3\", 5);";
  112. assertTest({
  113. title: "Everything should match",
  114. code: userCode,
  115. pass: true,
  116. validate: basicTest
  117. });
  118. });
  119. describe("Challenge Assertions - SQL Results", function() {
  120. var basicTest = function() {
  121. staticTest($._("Query a table of values"), function() {
  122. var description = $._("Now let's add 3 books into our bookshelf");
  123. var template = "CREATE TABLE _$1 (id INTEGER, name TEXT, rating " +
  124. "INTEGER);" +
  125. "INSERT INTO _$1 VALUES(1, \"book1\", 5);" +
  126. "INSERT INTO _$1 VALUES(1, \"book2\", 4);" +
  127. "INSERT INTO _$1 VALUES(2, \"book3\", 5);" +
  128. "SELECT name FROM _$1";
  129. var templateDB = initTemplateDB(template);
  130. var result = fail();
  131. if (!passes(matchResultCount(templateDB))) {
  132. result = fail("Not matching results!")
  133. } else if (!passes(matchResultRowCount(0, templateDB))) {
  134. result = fail("Not matching rows!");
  135. } else if (!passes(matchResultColumnCount(0, templateDB))) {
  136. result = fail("Not matching columns!");
  137. } else if (!passes(matchResultRowValues(0, templateDB))) {
  138. result = fail("Not matching row values!");
  139. } else if (!passes(matchResultColumnNames(0, templateDB))) {
  140. result = fail("Not matching column names!");
  141. } else if (passes(matchResultRowValues(0, templateDB, {ignoreOrder: true}))) {
  142. result = fail("You got the row values but in wrong order!");
  143. } else {
  144. result = pass();
  145. }
  146. assertMatch(result, description, "INSERT INTO _ VALUES (...);");
  147. });
  148. }.toString().replace(/^function.*?{([\s\S]*?)}$/, "$1");
  149. // Not enough results
  150. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  151. "INTEGER);" +
  152. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  153. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  154. "INSERT INTO books VALUES(2, \"book3\", 5);";
  155. assertTest({
  156. title: "It should show failing message for result count",
  157. code: userCode,
  158. pass: false,
  159. validate: basicTest,
  160. fromTests: true,
  161. reason: "Not matching results!",
  162. errors: [{}]
  163. });
  164. // Not enough rows
  165. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  166. "INTEGER);" +
  167. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  168. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  169. "INSERT INTO books VALUES(2, \"book3\", 5);" +
  170. "SELECT name FROM books LIMIT 2;";
  171. assertTest({
  172. title: "It should show failing message for result row count",
  173. code: userCode,
  174. pass: false,
  175. validate: basicTest,
  176. fromTests: true,
  177. reason: "Not matching rows!",
  178. errors: [{}]
  179. });
  180. // Not matching columns
  181. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  182. "INTEGER);" +
  183. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  184. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  185. "INSERT INTO books VALUES(2, \"book3\", 5);" +
  186. "SELECT * FROM books";
  187. assertTest({
  188. title: "It should show failing message for result column count",
  189. code: userCode,
  190. pass: false,
  191. validate: basicTest,
  192. fromTests: true,
  193. reason: "Not matching columns!",
  194. errors: [{}]
  195. });
  196. // Not matching row values
  197. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  198. "INTEGER);" +
  199. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  200. "INSERT INTO books VALUES(2, \"book2\", 4);" +
  201. "INSERT INTO books VALUES(3, \"book3\", 5);" +
  202. "SELECT name FROM books ORDER BY name ASC";
  203. assertTest({
  204. title: "It should show that you got values but in wrong order ",
  205. code: userCode,
  206. pass: false,
  207. validate: basicTest,
  208. fromTests: true,
  209. reason: "You got the row values but in wrong order!",
  210. errors: [{}]
  211. });
  212. // Not matching row values
  213. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  214. "INTEGER);" +
  215. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  216. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  217. "INSERT INTO books VALUES(2, \"book3\", 5);" +
  218. "SELECT id FROM books";
  219. assertTest({
  220. title: "It should show failing message for result row values",
  221. code: userCode,
  222. pass: false,
  223. validate: basicTest,
  224. fromTests: true,
  225. reason: "Not matching row values!",
  226. errors: [{}]
  227. });
  228. // Not matching column names
  229. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  230. "INTEGER);" +
  231. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  232. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  233. "INSERT INTO books VALUES(2, \"book3\", 5);" +
  234. "SELECT name AS book_name FROM books";
  235. assertTest({
  236. title: "It should show failing message for result column names",
  237. code: userCode,
  238. pass: false,
  239. validate: basicTest,
  240. fromTests: true,
  241. reason: "Not matching column names!",
  242. errors: [{}]
  243. });
  244. // Valid input
  245. userCode = "CREATE TABLE books (id INTEGER, name TEXT, rating " +
  246. "INTEGER);" +
  247. "INSERT INTO books VALUES(1, \"book1\", 5);" +
  248. "INSERT INTO books VALUES(1, \"book2\", 4);" +
  249. "INSERT INTO books VALUES(2, \"book3\", 5);" +
  250. "SELECT name FROM books;";
  251. assertTest({
  252. title: "Everything should match",
  253. code: userCode,
  254. pass: true,
  255. validate: basicTest
  256. });
  257. });