def Searching_for_an_author_returns_their_poems__test(): db = FakeDb() p1 = {"title": "ti1", "author": "Sara", "text": ""} p2 = {"title": "ti2", "author": "Jose", "text": ""} p3 = {"title": "ti3", "author": "Sara", "text": ""} p4 = {"title": "ti4", "author": "Chinua", "text": ""} p5 = {"title": "ti5", "author": "Sara", "text": ""} id1 = json.loads(json_poems.POST(db, json.dumps(p1), "user1")) id2 = json.loads(json_poems.POST(db, json.dumps(p2), "user1")) id3 = json.loads(json_poems.POST(db, json.dumps(p3), "user1")) id4 = json.loads(json_poems.POST(db, json.dumps(p4), "user1")) id5 = json.loads(json_poems.POST(db, json.dumps(p5), "user1")) # This is what we are testing j = json_poems.GET(db, "", None, {"search": "Sara"}) # Parse the answer - should be valid JSON, then clip to 3 results ans = json.loads(j)[:3] # We got back the 3 poems by Sara assert_equal(id1, ans[0]) assert_equal(id3, ans[1]) assert_equal(id5, ans[2])
def Can_list_n_poems__test(): # This is what we are testing j = json_poems.GET(FakeDb(), "", None, {"count": "2"}) # We got back the number we asked for lst = json.loads(j) assert_equal(2, len(lst))
def Can_list_poems__test(): # This is what we are testing j = json_poems.GET(FakeDb(), "", user=None) # Parse it and sort the answer lst = json.loads(j) lst.sort() assert_equal(["id1", "id2", "id3"], lst)
def Can_get_single_poem__test(): # This is what we are testing j = json_poems.GET(FakeDb(), "id3", None) # Parse the answer - should be valid JSON ans = json.loads(j) assert_equal("id3", ans["id"]) assert_equal("title3", ans["title"]) assert_equal("author3", ans["author"]) assert_equal("text3", ans["text"])
def Getting_a_nonexistent_poem_is_an_error__test(): caught_exception = None try: json_poems.GET(FakeDb(), "nonexistid", None) except JsonInvalidRequest, e: caught_exception = e
def Negative_count_is_an_error__test(): caught_exception = None try: json_poems.GET(FakeDb(), "", None, {"count": "-2"}) except JsonInvalidRequest, e: caught_exception = e
def Nonnumeric_count_is_an_error__test(): caught_exception = None try: json_poems.GET(FakeDb(), "", None, {"count": "2foo"}) except JsonInvalidRequest, e: caught_exception = e