def test_stats_returned(mock_pymongo): #db.track.aggregate({ $group: { _id: { "year":{$year:"$createdDate"},"month":{$month:"$createdDate"},"day":{$dayOfMonth: "$createdDate"}},createdDate: { $sum: 1 } } }) mock_pymongo.dead.track.find.return_value= get_sample_stats() dead = DropHandler(mock_pymongo) val = dead.stats() expected =[ {"$group": { "_id": { "year":{"$year":"$createdDate"},"month":{"$month":"$createdDate"},"day":{"$dayOfMonth": "$createdDate"},"userHash":"$userHash"}, "count": { "$sum": 1 } , } }, {"$group": { "_id": { "year":"$_id.year","month":"$_id.month","day":"$_id.day"}, "count": { "$sum": "$count" }, "distinctCount": { "$sum": 1 } } }, {"$sort": {"_id.year":1,"_id.month":1,"_id.day":1}}, ]; mock_pymongo.dead.track.aggregate.assert_called_with(expected)
def test_track_updated_when_accessed(mock_pymongo): sampleDrop = get_sample_drop() mock_pymongo.dead.drop.find_one_and_delete.return_value = sampleDrop dead = DropHandler(mock_pymongo) val = dead.pickup(sampleDrop['key']) mock_pymongo.dead.track.update.assert_called_with({"key": sampleDrop['key']}, {"$set":{"pickedUp":datetime.datetime(2012, 1, 14)},"$unset":{"key":""}}) assert sampleDrop["data"] == val
def test_return_none_when_not_existing(mock_pymongo): sampleDrop = get_sample_drop() mock_pymongo.dead.drop.find_one_and_delete.return_value=[] dead = DropHandler(mock_pymongo) val = dead.pickup(sampleDrop['key']) assert val == [] mock_pymongo.dead.drop.find_one_and_delete.assert_called_with({"key": sampleDrop['key']})
def test_timed_key_is_saved(mock_pymongo): """See if timed key is saved.""" dead = DropHandler(mock_pymongo) timed_key = dead.get_timed_key() mock_pymongo.dead.formKeys.insert_one.assert_called_with({ 'key': timed_key, "created": ANY })
def test_return_none_when_not_existing(mock_pymongo): """See if none is returned if drop doesn't exist.""" sample_drop = get_sample_drop() mock_pymongo.dead.drop.find_one_and_delete.return_value = [] dead = DropHandler(mock_pymongo) val = dead.pickup(sample_drop['key']) assert val == [] mock_pymongo.dead.drop.find_one_and_delete.assert_called_with( {'key': sample_drop['key']})
def test_drop_deleted_and_not_returned_when_24hours_old(mock_pymongo): key = "anything" sampleDrop = get_sample_drop() sampleDrop["createdDate"] = datetime.datetime(2012, 1, 12) mock_pymongo.dead.drop.find.return_value=[sampleDrop] dead = DropHandler(mock_pymongo) val = dead.pickup(key) assert val == [] mock_pymongo.dead.drop.find_one_and_delete.assert_called_with({"key": key})
def test_drop_deleted_and_not_returned_when_24hours_old(mock_pymongo): """See if drop is returned when it's more than 24 h old.""" key = "anything" sample_drop = get_sample_drop() sample_drop['createdDate'] = datetime.datetime(2012, 1, 12) mock_pymongo.dead.drop.find.return_value = [sample_drop] dead = DropHandler(mock_pymongo) val = dead.pickup(key) assert val == [] mock_pymongo.dead.drop.find_one_and_delete.assert_called_with({'key': key})
def test_drop_not_retruned_when_no_create_date(mock_pymongo): # to handle old drops sampleDrop = get_sample_drop() sampleDrop.pop('createdDate') mock_pymongo.dead.drop.find_one_and_delete.return_value=sampleDrop dead = DropHandler(mock_pymongo) val = dead.pickup(sampleDrop['key']) pprint.pprint(sampleDrop) pprint.pprint(val) mock_pymongo.dead.drop.find_one_and_delete.assert_called_with({"key": sampleDrop['key']}) assert val == []
def test_stats_returned(mock_pymongo): """See if stats are returned properly.""" mock_pymongo.dead.track.find.return_value = get_sample_stats() dead = DropHandler(mock_pymongo) stats = dead.stats() expected = [ { "$group": { "_id": { "year": { "$year": "$createdDate" }, "month": { "$month": "$createdDate" }, "day": { "$dayOfMonth": "$createdDate" }, "userHash": "$userHash" }, "count": { "$sum": 1 } } }, { "$group": { "_id": { "year": "$_id.year", "month": "$_id.month", "day": "$_id.day" }, "count": { "$sum": "$count" }, "distinctCount": { "$sum": 1 } } }, { "$sort": { "_id.year": 1, "_id.month": 1, "_id.day": 1 } }, ] pprint.pprint(expected) pprint.pprint(stats) mock_pymongo.dead.track.aggregate.assert_called_with(expected)
def test_drop_deleted_when_accessed(mock_pymongo): sampleDrop = get_sample_drop() mock_pymongo.dead.drop.find_one_and_delete.return_value=sampleDrop dead = DropHandler(mock_pymongo) import pprint pprint.pprint("XXXXX") pprint.pprint(sampleDrop) val = dead.pickup(sampleDrop['key']) pprint.pprint(val) mock_pymongo.dead.drop.find_one_and_delete.assert_called_with({"key": sampleDrop['key']}) assert sampleDrop["data"] == val
def test_drop_deleted_when_accessed(mock_pymongo): """See if drop is deleted properly when accessed.""" sample_drop = get_sample_drop() mock_pymongo.dead.drop.find_one_and_delete.return_value = sample_drop dead = DropHandler(mock_pymongo) pprint.pprint("XXXXX") pprint.pprint(sample_drop) val = dead.pickup(sample_drop['key']) pprint.pprint(val) mock_pymongo.dead.drop.find_one_and_delete.assert_called_with( {'key': sample_drop['key']}) assert sample_drop['data'] == val
def test_drop_not_returned_when_no_create_date(mock_pymongo): """See if drop is returned when there's no create date.""" # (to handle old drops) sample_drop = get_sample_drop() sample_drop.pop('createdDate') mock_pymongo.dead.drop.find_one_and_delete.return_value = sample_drop dead = DropHandler(mock_pymongo) val = dead.pickup(sample_drop['key']) pprint.pprint(sample_drop) pprint.pprint(val) mock_pymongo.dead.drop.find_one_and_delete.assert_called_with( {'key': sample_drop['key']}) assert val == []
def test_drop_is_saved(mock_pymongo): """See if drop is saved properly.""" dead = DropHandler(mock_pymongo) data = {"test": "here"} dead.drop(data) mock_pymongo.dead.drop.insert_one.assert_called_with({ 'key': ANY, 'data': data, 'createdDate': datetime.datetime(2012, 1, 14) })
def test_user_hash_is_truncated(mock_pymongo): dead = DropHandler(mock_pymongo) ipAddr ="127.0.0.1" dead.setRequestHash(ipAddr) data = {"test":"here"} dead.drop(data) saltedIP = DropHandler.salt +ipAddr m = hashlib.sha256() m.update(saltedIP.encode('utf-8')) client_hash = m.hexdigest()[:32] mock_pymongo.dead.track.insert_one.assert_called_with({"key": ANY,"userHash": client_hash, "createdDate":datetime.datetime(2012, 1, 14),"pickedUp":ANY})
def test_track_is_saved(mock_pymongo): """See if drop as tracked properly.""" dead = DropHandler(mock_pymongo) dead.set_request_hash("127.0.0.1") data = {'test': "here"} dead.drop(data) mock_pymongo.dead.track.insert_one.assert_called_with({ 'key': ANY, 'userHash': ANY, 'createdDate': datetime.datetime(2012, 1, 14), 'pickedUp': ANY })
def test_user_hash_is_truncated(mock_pymongo): """See if client_hash is set properly.""" dead = DropHandler(mock_pymongo) ip_addr = "127.0.0.1" dead.set_request_hash(ip_addr) data = {'test': "here"} dead.drop(data) salted_ip = dead.salt + ip_addr hasher = hashlib.sha256() hasher.update(salted_ip.encode('utf-8')) client_hash = hasher.hexdigest()[:32] mock_pymongo.dead.track.insert_one.assert_called_with({ 'key': ANY, 'userHash': client_hash, 'createdDate': datetime.datetime(2012, 1, 14), 'pickedUp': ANY })
def test_track_is_saved(mock_pymongo): dead = DropHandler(mock_pymongo) dead.setRequestHash("127.0.0.1") data = {"test":"here"} dead.drop(data) mock_pymongo.dead.track.insert_one.assert_called_with({"key": ANY,"userHash":ANY, "createdDate":datetime.datetime(2012, 1, 14),"pickedUp":ANY})
def test_drop_is_saved(mock_pymongo): dead = DropHandler(mock_pymongo) data = {"test":"here"} dead.drop(data) mock_pymongo.dead.drop.insert_one.assert_called_with({"key": ANY, "data":data,"createdDate":datetime.datetime(2012, 1, 14)})
def test_timed_key_is_saved(mock_pymongo): dead = DropHandler(mock_pymongo) timed_key = dead.get_timed_key() mock_pymongo.dead.formKeys.insert_one.assert_called_with({"key": timed_key,"created": ANY})