def Can_list_starting_after_an_id__test(): answer = list( poemtube.listpoems( FakeDb(), since_id="id2" ) ) # We don't know what order they will come back in, but even # if id2 is first, we'll get one less assert_true( len( answer ) < 3 ) print answer assert_true( "id2" not in answer )
def Can_get_an_individual_poem__test(): answer = poemtube.getpoem( FakeDb(), "id2" ) assert_equal( "id2", answer["id"] ) assert_equal( "author2", answer["author"] ) assert_equal( "title2", answer["title"] ) assert_equal( "text2", answer["text"] )
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_amend_a_poem_title__test(): db = FakeDb() # Sanity - before modifying assert_equal( { "title" : "title1", "author" : "author1", "text" : "text1", "contributor" : "user1" }, db.poems.data["id1"] ) # This is what we are testing poemtube.amendpoem( db, "id1", { "title": "title X" }, user="******" ) # Title changed assert_equal( { "title" : "title X", "author" : "author1", "text" : "text1", "contributor" : "user1" }, db.poems.data["id1"] )
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 Amending_an_invalid_id_is_an_error__test(): db = FakeDb() newpoem = {"title": "A Poem"} caught_exception = None try: json_poems.PATCH(db, "nonexistid", json.dumps(newpoem), "user3") except JsonInvalidRequest, e: caught_exception = e
def Replacing_a_nonexistent_poem_is_an_error__test(): db = FakeDb() poemtube.replacepoem( db, "nonexistid", title="title X", author="author X", text="text X", user="******" )
def test_app(): """ return new TestApp that refers to a new FakeDb, which is returned as the .fakedb member of the returned object. """ fakedb = FakeDb() poemtube.db.which_db.db = fakedb ret = TestApp(app.wsgifunc()) ret.fakedb = fakedb return ret
def Amending_with_invalid_properties_is_an_error__test(): db = FakeDb() newprops = {"foo": "Andy Balaam"} caught_exception = None try: json_poems.PATCH(db, "id3", json.dumps(newprops), "user3") except JsonInvalidRequest, e: caught_exception = e
def Can_delete_a_poem__test(): db = FakeDb() # Sanity - id1 exists assert_true( "id1" in db.poems ) # This is what we are testing - delete the item poemtube.deletepoem( db, "id1", user="******" ) # It has gone assert_false( "id1" in db.poems )
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 Can_delete_an_existing_poem__test(): db = FakeDb() # Sanity - poem exists assert_equal("title1", db.poems["id1"]["title"]) # This is what we are testing json_poems.DELETE(db, "id1", "user1") # It no longer exists assert_false("id1" in db.poems)
def Replacing_an_invalid_id_is_an_error__test(): db = FakeDb() newpoem = { "title": "A Poem", "author": "Andy Balaam", "text": "Sometimes, sometimes\nSometimes.\n" } caught_exception = None try: json_poems.PUT(db, "nonexistid", json.dumps(newpoem), "user3") except JsonInvalidRequest, e: caught_exception = e
def Can_amend_a_poem_text__test(): db = FakeDb() # This is what we are testing poemtube.amendpoem( db, "id2", { "text": "text X" }, user="******" ) # Title changed assert_equal( { "title" : "title2", "author" : "author2", "text" : "text X", "contributor" : "user2" }, db.poems.data["id2"] )
def Can_replace_an_existing_poem__test(): db = FakeDb() poemtube.replacepoem( db, "id1", title="title X", author="author X", text="text X", user="******" ) modentry = db.poems.data["id1"] assert_equal( "title X", modentry["title"] ) assert_equal( "author X", modentry["author"] ) assert_equal( "text X", modentry["text"] ) assert_equal( "user1", modentry["contributor"] )
def Can_add_a_new_poem__test(): db = FakeDb() id = poemtube.addpoem( db, title="title X", author="author X", text="text X", user="******" ) assert_equal( "title-x", id ) newentry = db.poems.data[id] assert_equal( "title X", newentry["title"] ) assert_equal( "author X", newentry["author"] ) assert_equal( "text X", newentry["text"] )
def Can_replace_an_existing_poem__test(): db = FakeDb() # Sanity - poem exists assert_equal("title3", db.poems["id3"]["title"]) newpoem = { "title": "A Poem", "author": "Andy Balaam", "text": "Sometimes, sometimes\nSometimes.\n", "contributor": "user3" } # This is what we are testing json_poems.PUT(db, "id3", json.dumps(newpoem), "user3") # The poem was replaced with what we supplied assert_equal(newpoem, db.poems.data["id3"])
def Can_add_a_new_poem__test(): db = FakeDb() newpoem = { "title": "A Poem", "author": "Andy Balaam", "text": "Sometimes, sometimes\nSometimes.\n" } j = json_poems.POST(db, json.dumps(newpoem), "user2") id = json.loads(j) assert_equals("a-poem", id) assert_equals("A Poem", db.poems[id]["title"]) assert_equals("Andy Balaam", db.poems[id]["author"]) assert_equals("Sometimes, sometimes\nSometimes.\n", db.poems[id]["text"]) assert_equals("user2", db.poems[id]["contributor"])
def Can_amend_an_existing_poem__test(): db = FakeDb() # Sanity assert_equal("title1", db.poems["id1"]["title"]) mods = {"title": "A Poem", "text": "Sometimes, sometimes\nSometimes.\n"} # This is what we are testing json_poems.PATCH(db, "id1", json.dumps(mods), "user1") # The poem was replaced with what we supplied assert_equal( { "title": "A Poem", "author": "author1", "text": "Sometimes, sometimes\nSometimes.\n", "contributor": "user1" }, db.poems.data["id1"])
def Searching_for_an_author_returns_their_poems__test(): db = FakeDb() id1 = poemtube.addpoem( db, title="t1", author="fred 1", text="t1", user="******" ) id2 = poemtube.addpoem( db, title="t2", author="fred 2", text="t2", user="******" ) id3 = poemtube.addpoem( db, title="t3", author="fred 1", text="t3", user="******" ) id4 = poemtube.addpoem( db, title="t4", author="fred 3", text="t4", user="******" ) id5 = poemtube.addpoem( db, title="t5", author="fred 1", text="t5", user="******" ) # This is what we are testing answer = poemtube.listpoems( db, search="fred 1" ) # Take the first 3 - theoretically there might be others first_3 = list( answer )[:3] # We got back the 3 poems by this author assert_equal( id1, first_3[0] ) assert_equal( id3, first_3[1] ) assert_equal( id5, first_3[2] )
def Can_list_all_poems__test(): answer = poemtube.listpoems( FakeDb() ) assert_equal( set( ( "id1", "id2", "id3" ) ), set( answer ) )
def Getting_a_nonexistent_poem_is_an_error__test(): poemtube.getpoem( FakeDb(), "nonexistentid" )
def Listing_asking_for_negative_is_an_error__test(): poemtube.listpoems( FakeDb(), count=-1 )
def Listing_asking_for_too_many_returns_all__test(): answer = poemtube.listpoems( FakeDb(), count=200 ) assert_equal( 3, len( list( answer ) ) )
def Amending_not_logged_in_is_an_error__test(): poemtube.amendpoem( FakeDb(), "id1", { "text": "t" }, user=None )
def Amending_someone_elses_poem_is_an_error__test(): poemtube.amendpoem( FakeDb(), "id1", { "text": "t" }, user="******" )
def Amending_an_unknown_id_is_an_error__test(): poemtube.amendpoem( FakeDb(), "unknown", { "text": "t" }, user="******" )
def Amending_using_unknown_property_is_an_error__test(): poemtube.amendpoem( FakeDb(), "id1", { "text": "t", "badprop": "foo" }, user="******" )
def Attempting_to_change_contributor_is_an_error__test(): poemtube.amendpoem( FakeDb(), "id1", { "text": "t", "contributor": "foo" }, user="******" )