|
3 | 3 | [clj-http.client :as client] |
4 | 4 | [eamonnsullivan.github-api-lib.core :as sut])) |
5 | 5 |
|
| 6 | +(def repo-id-response-success (slurp "./test/eamonnsullivan/fixtures/repo-response-success.json")) |
| 7 | +(def repo-id-response-failure (slurp "./test/eamonnsullivan/fixtures/repo-response-failure.json")) |
| 8 | +(def repo-topic-response-success (slurp "./test/eamonnsullivan/fixtures/repo-topic-response.json")) |
| 9 | + |
6 | 10 | (defn test-args-to-get |
7 | 11 | [url opts] |
8 | 12 | (is (= "access-token" (:username opts))) |
|
144 | 148 | :kf #(if (-> % :data :search :pageInfo :hasNextPage) |
145 | 149 | (-> % :data :search :pageInfo :endCursor) |
146 | 150 | nil))))))) |
| 151 | + |
| 152 | +(deftest test-get-repo-id |
| 153 | + (with-redefs [sut/http-post (fn [_ _ _] {:body repo-id-response-success})] |
| 154 | + (testing "parses id from successful response" |
| 155 | + (is (= "MDEwOlJlcG9zaXRvcnkyOTczNzY1NTc=" (sut/get-repo-id "secret-token" "owner" "repo-name")))) |
| 156 | + (testing "handles url instead of separate owner/repo-name" |
| 157 | + (is (= "MDEwOlJlcG9zaXRvcnkyOTczNzY1NTc=" (sut/get-repo-id "secret-token" "owner/repo-name"))) |
| 158 | + (is (= "MDEwOlJlcG9zaXRvcnkyOTczNzY1NTc=" (sut/get-repo-id "secret-token" "https://github.com/owner/repo-name"))))) |
| 159 | + (with-redefs [sut/http-post (fn [_ _ _] {:body repo-id-response-failure})] |
| 160 | + (testing "throws exception on error" |
| 161 | + (is (thrown-with-msg? RuntimeException |
| 162 | + #"Could not resolve to a Repository with the name 'eamonnsullivan/not-there'." |
| 163 | + (sut/get-repo-id "secret-token" "owner" "repo-name"))))) |
| 164 | + (with-redefs [sut/parse-repo (fn [_] (throw (ex-info "error" {})))] |
| 165 | + (testing "passes on thrown exceptions from parse-repo" |
| 166 | + (is (thrown-with-msg? RuntimeException |
| 167 | + #"error" |
| 168 | + (sut/get-repo-id "secret-token" "whatever/whatever")))))) |
| 169 | + |
| 170 | +(deftest test-get-page-of-topics |
| 171 | + (with-redefs [sut/http-post (fn [_ _ _] {:body repo-topic-response-success})] |
| 172 | + (testing "gets topics from a page" |
| 173 | + (is (= {:data |
| 174 | + {:node |
| 175 | + {:repositoryTopics |
| 176 | + {:nodes |
| 177 | + [{:topic {:name "cps"}} |
| 178 | + {:topic {:name "cam"}} |
| 179 | + {:topic {:name "dpub"}} |
| 180 | + {:topic {:name "node"}} |
| 181 | + {:topic {:name "frontend"}} |
| 182 | + {:topic {:name "optimo"}}], |
| 183 | + :pageInfo |
| 184 | + {:hasNextPage false, :endCursor "Y3Vyc29yOnYyOpHOARzyUg=="}}}}} |
| 185 | + (sut/get-page-of-topics "secret-token" "repo-id" 10 nil)))))) |
| 186 | + |
| 187 | +(defn fake-paging-response |
| 188 | + [_ _ _ cursor] |
| 189 | + (let [first-page {:data |
| 190 | + {:node |
| 191 | + {:repositoryTopics |
| 192 | + {:nodes |
| 193 | + [ {:topic {:name "tag1"}} {:topic {:name "tag2"}}] |
| 194 | + :pageInfo {:hasNextPage true, :endCursor "cursor"}}}}} |
| 195 | + last-page {:data |
| 196 | + {:node |
| 197 | + {:repositoryTopics |
| 198 | + {:nodes |
| 199 | + [ {:topic {:name "tag3"}} {:topic {:name "tag4"}}] |
| 200 | + :pageInfo {:hasNextPage false, :endCursor "cursor2"}}}}}] |
| 201 | + (if-not cursor |
| 202 | + first-page |
| 203 | + last-page))) |
| 204 | + |
| 205 | +(deftest test-get-repo-topics |
| 206 | + (with-redefs [sut/get-repo-id (fn[_ _] "some-id") |
| 207 | + sut/get-page-of-topics fake-paging-response] |
| 208 | + (testing "follows pages" |
| 209 | + (is (= ["tag1" "tag2" "tag3" "tag4"] |
| 210 | + (sut/get-repo-topics "secret-token" "eamonnsullivan/github-api-lib")))))) |
0 commit comments