def test_update(): """ A simple update """ starting_db = create_db(STARTING_DB_INPUT) actual: dict = o_obj.update_object_in_db(starting_db, "some_uid", INP) assert actual == EXPECTED
def test_single_create(): actual: dict = c_obj.create_object_in_db(create_db(), INP) assert actual.get("uid", False) # remove uid to check if object is still the same del actual["uid"] assert actual == json.loads(INP)
def test_get_invalid_obj(): """ Getting an object that doesn't exist """ db = create_db() with pytest.raises(ValueError): g_obj.get_single_object_from_db(db, "bad uid")
def test_get_one_valid_obj(): """ Getting a valid object """ db = create_db(STARTING_DB_INPUT) actual: dict = g_obj.get_single_object_from_db(db, "some_uid") assert actual == STARTING_DB_INPUT[0]
def test_update_to_non_json(): """ Don't explicitly require a uid because we can get that from the URL """ starting_db = create_db(STARTING_DB_INPUT) with pytest.raises(ValueError): o_obj.update_object_in_db(starting_db, "some_uid", "this isn't json :(")
def test_no_uid_causes_error(): """ If the UID doesn't exist in the DB, we should return an error so people can't arbitrarily generate UIDs This could break idempotence but I would ask clarifying questions """ empty = create_db() with pytest.raises(ValueError): o_obj.update_object_in_db(empty, "some_uid", INP)
def test_update_twice_same_result(): """ Updating twice should produce the same result """ starting_db = create_db(STARTING_DB_INPUT) actual: dict = o_obj.update_object_in_db(starting_db, "some_uid", INP) actual2: dict = o_obj.update_object_in_db(starting_db, "some_uid", INP) assert actual == EXPECTED == actual2
def test_multi_create(): db = create_db() actual: dict = c_obj.create_object_in_db(db, INP) actual2: dict = c_obj.create_object_in_db(db, INP) assert actual["uid"] != actual2["uid"] del actual["uid"] del actual2["uid"] assert actual == actual2
def test_delete_on_real_object_causes_no_error(): """ Deleting an object should cause no error """ db = create_db() d_obj.delete_object_from_db(db, "my_uid") # assert no errors assert not db.get_item(Key={"uid": "my_uid"}).get("Item", False)
def test_multiple_deletes_result_in_no_error(): """ Deleting an object multiple times should only ensure that that object doesn't exist (idempotent) """ db = create_db() d_obj.delete_object_from_db(db, "my_uid") d_obj.delete_object_from_db(db, "my_uid") # assert no errors assert not db.get_item(Key={"uid": "my_uid"}).get("Item", False)
def test_attempt_to_add_uid_key_causes_error(): """ Don't allow UIDs to be updated to different values because it could interfere with others objects """ starting_db = create_db(STARTING_DB_INPUT) starting_db.put_item( Item={"uid": "I can TOTALLY update someone else's object"}) with pytest.raises(ValueError): o_obj.update_object_in_db( starting_db, "some_uid", json.dumps({"uid": "I can TOTALLY update someone else's object"}))
def test_get_all_valid_objs(): """ Getting all objects """ db = create_db(STARTING_DB_INPUT) db.put_item(Item={"uid": "another_uid", "data": "something"}) paginator = boto3.client( "dynamodb", 'us-east-1').get_paginator("scan").paginate(TableName=db.table_name) actual = map(lambda x: x["url"], g_obj.get_all_object_links_from_db(paginator, BASE_URL)) assert set(actual) == {f"{BASE_URL}/another_uid", f"{BASE_URL}/some_uid"}
def test_allow_relaxed_update(): """ Since we can gather which UID should be updated from the URL, we should relax the object required to not include a UID """ starting_db = create_db(STARTING_DB_INPUT) response = o_obj.update_object_in_db( starting_db, "some_uid", json.dumps( {"my_key": "I don't include a uid, but passed it in the url"})) assert response == { "uid": "some_uid", "my_key": "I don't include a uid, but passed it in the url" }
def test_bad_json_forbidden(): inp = "this is some very bad json" with pytest.raises(ValueError): c_obj.create_object_in_db(create_db(), inp)
def test_uid_key_forbidden(): inp = json.dumps({"uid": "test"}) with pytest.raises(ValueError): c_obj.create_object_in_db(create_db(), inp)