|
| 1 | +from graphene.test import Client |
| 2 | +from schema import schema |
| 3 | + |
| 4 | + |
| 5 | +def test_basic_query(): |
| 6 | + client = Client(schema) |
| 7 | + executed = client.execute(''' |
| 8 | +query { |
| 9 | + user (id: "VXNlcjox") { |
| 10 | + name |
| 11 | + } |
| 12 | +} |
| 13 | +''') |
| 14 | + assert executed == { |
| 15 | + 'data': { |
| 16 | + 'user': { |
| 17 | + 'name': 'me', |
| 18 | + }, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +def test_all_users_query(): |
| 24 | + client = Client(schema) |
| 25 | + executed = client.execute(''' |
| 26 | +query { |
| 27 | + allUsers { |
| 28 | + edges { |
| 29 | + node { |
| 30 | + id |
| 31 | + name |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +''') |
| 37 | + assert executed == { |
| 38 | + 'data': { |
| 39 | + 'allUsers': { |
| 40 | + 'edges': [{ |
| 41 | + 'node': { |
| 42 | + 'id': 'VXNlcjox', |
| 43 | + 'name': 'me' |
| 44 | + } |
| 45 | + }, { |
| 46 | + 'node': { |
| 47 | + 'id': 'VXNlcjoy', |
| 48 | + 'name': 'other' |
| 49 | + } |
| 50 | + }] |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_viewer_query(): |
| 57 | + client = Client(schema) |
| 58 | + executed = client.execute(''' |
| 59 | +query { |
| 60 | + viewer { |
| 61 | + totalCount |
| 62 | + completedCount |
| 63 | + todos { |
| 64 | + edges { |
| 65 | + node { |
| 66 | + id |
| 67 | + complete |
| 68 | + text |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + anyTodos: todos (status: "any") { |
| 73 | + edges { |
| 74 | + node { |
| 75 | + id |
| 76 | + complete |
| 77 | + text |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + completedTodos: todos (status: "completed") { |
| 82 | + edges { |
| 83 | + node { |
| 84 | + id |
| 85 | + complete |
| 86 | + text |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +''') |
| 93 | + completed_todo = { |
| 94 | + 'node': { |
| 95 | + 'id': 'VG9kbzox', |
| 96 | + 'complete': True, |
| 97 | + 'text': 'Taste JavaScript', |
| 98 | + }, |
| 99 | + } |
| 100 | + remaining_todo = { |
| 101 | + 'node': { |
| 102 | + 'id': 'VG9kbzoy', |
| 103 | + 'complete': False, |
| 104 | + 'text': 'Buy a unicorn', |
| 105 | + }, |
| 106 | + } |
| 107 | + assert executed == { |
| 108 | + 'data': { |
| 109 | + 'viewer': { |
| 110 | + 'totalCount': 2, |
| 111 | + 'completedCount': 1, |
| 112 | + 'todos': { |
| 113 | + 'edges': [ |
| 114 | + completed_todo, |
| 115 | + remaining_todo, |
| 116 | + ], |
| 117 | + }, |
| 118 | + 'anyTodos': { |
| 119 | + 'edges': [ |
| 120 | + completed_todo, |
| 121 | + remaining_todo, |
| 122 | + ], |
| 123 | + }, |
| 124 | + 'completedTodos': { |
| 125 | + 'edges': [ |
| 126 | + completed_todo, |
| 127 | + ], |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | +def test_mutation(): |
| 135 | + client = Client(schema) |
| 136 | + executed = client.execute(''' |
| 137 | +query { |
| 138 | + user (id: "VXNlcjox") { |
| 139 | + completedCount |
| 140 | + } |
| 141 | + todo (id: "VG9kbzox") { |
| 142 | + complete |
| 143 | + text |
| 144 | + } |
| 145 | +} |
| 146 | +''') |
| 147 | + assert executed == { |
| 148 | + 'data': { |
| 149 | + 'user': { |
| 150 | + 'completedCount': 1, |
| 151 | + }, |
| 152 | + 'todo': { |
| 153 | + 'complete': True, |
| 154 | + 'text': 'Taste JavaScript', |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + variables = { |
| 160 | + 'input': { |
| 161 | + 'id': 'VG9kbzox', |
| 162 | + 'complete': False, |
| 163 | + 'clientMutationId': 0, |
| 164 | + } |
| 165 | + } |
| 166 | + executed = client.execute(''' |
| 167 | +mutation ($input: ChangeTodoStatusInput!) { |
| 168 | + changeTodoStatus(input: $input) { |
| 169 | + todo { id complete } |
| 170 | + viewer { id completedCount } |
| 171 | + } |
| 172 | +}''', variable_values=variables) |
| 173 | + assert executed == { |
| 174 | + 'data': { |
| 175 | + 'changeTodoStatus': { |
| 176 | + 'todo': { |
| 177 | + 'id': 'VG9kbzox', |
| 178 | + 'complete': False, |
| 179 | + }, |
| 180 | + 'viewer': { |
| 181 | + 'id': 'VXNlcjox', |
| 182 | + 'completedCount': 0, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + executed = client.execute(''' |
| 189 | +query { |
| 190 | + user (id: "VXNlcjox") { |
| 191 | + completedCount |
| 192 | + } |
| 193 | + todo (id: "VG9kbzox") { |
| 194 | + complete |
| 195 | + text |
| 196 | + } |
| 197 | +} |
| 198 | +''') |
| 199 | + assert executed == { |
| 200 | + 'data': { |
| 201 | + 'user': { |
| 202 | + 'completedCount': 0, |
| 203 | + }, |
| 204 | + 'todo': { |
| 205 | + 'complete': False, |
| 206 | + 'text': 'Taste JavaScript', |
| 207 | + }, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + # revert changes |
| 212 | + variables = { |
| 213 | + 'input': { |
| 214 | + 'id': 'VG9kbzox', |
| 215 | + 'complete': True, |
| 216 | + 'clientMutationId': 0, |
| 217 | + }, |
| 218 | + } |
| 219 | + executed = client.execute(''' |
| 220 | +mutation ($input: ChangeTodoStatusInput!) { |
| 221 | + changeTodoStatus(input: $input) { |
| 222 | + todo { id complete } |
| 223 | + viewer { id completedCount } |
| 224 | + } |
| 225 | +}''', variable_values=variables) |
| 226 | + assert executed == { |
| 227 | + 'data': { |
| 228 | + 'changeTodoStatus': { |
| 229 | + 'todo': { |
| 230 | + 'id': 'VG9kbzox', |
| 231 | + 'complete': True, |
| 232 | + }, |
| 233 | + 'viewer': { |
| 234 | + 'id': 'VXNlcjox', |
| 235 | + 'completedCount': 1, |
| 236 | + }, |
| 237 | + }, |
| 238 | + }, |
| 239 | + } |
0 commit comments