def test_getEntryKeys(): """ Test getting all database keys. """ dbEnv = dbing.setupTestDbEnv() ser = "{'reputer': 'Søren', 'reputee': 'Kierkegaard', 'repute': {'rid': 'dda6555f-21c8-45ff-9633-f9b5cdc59f45', " \ "'feature': 'clarity', 'value': 5}})" dat = json.dumps(ser) key1 = "søren-dda6555f-21c8-45ff-9633-f9b5cdc59f45" dbing.putEntry(key1, dat) ser = "{'reputer': 'Henrik', 'reputee': 'Ibsen', 'repute': {'rid': 'dda6555f-21c8-45ff-9633-f9b5cdc59f46', " \ "'feature': 'clarity', 'value': 5}})" dat = json.dumps(ser) key2 = "henrik-dda6555f-21c8-45ff-9633-f9b5cdc59f46" dbing.putEntry(key2, dat) ser = "{'reputer': 'Inger', 'reputee': 'Christiansen', 'repute': {'rid': 'dda6555f-21c8-45ff-9633-f9b5cdc59f47', " \ "'feature': 'clarity', 'value': 5}})" dat = json.dumps(ser) key3 = "inger-dda6555f-21c8-45ff-9633-f9b5cdc59f47" dbing.putEntry(key3, dat) glist = dbing.getEntryKeys() assert [key2, key3, key1] cleanupTmpBaseDir(dbEnv.path()) print("test/db/test_dbing: test_getEntryKeys() \033[92mPASSED\033[0m")
def test_setupTestPrime(): priming.setupTest() assert os.path.exists(dbing.gDbDirPath) cleanupTmpBaseDir(dbing.gDbDirPath) assert not os.path.exists(dbing.gDbDirPath) print( "test/prime/test_priming: test_setupTestPrime() \033[92mPASSED\033[0m") # ================================================== # # EOF # # ================================================== #
def test_setupPrime(): baseDirPath = setupTmpBaseDir() assert baseDirPath.startswith("/tmp/reputation") assert baseDirPath.endswith("test") dbDirPath = os.path.join(baseDirPath, "reputation/db") os.makedirs(dbDirPath) assert os.path.exists(dbDirPath) priming.setup(dbDirPath=dbDirPath) assert dbing.gDbDirPath == dbDirPath cleanupTmpBaseDir(baseDirPath) assert not os.path.exists(baseDirPath) print("test/prime/test_priming: test_setupPrime() \033[92mPASSED\033[0m")
def test_setupDbEnv(): baseDirPath = setupTmpBaseDir() assert baseDirPath.startswith("/tmp/reputation") assert baseDirPath.endswith("test") dbDirPath = os.path.join(baseDirPath, "reputation/db") os.makedirs(dbDirPath) assert os.path.exists(dbDirPath) env = dbing.setupDbEnv(baseDirPath=dbDirPath) assert env.path() == dbDirPath assert dbing.gDbDirPath == dbDirPath assert dbing.gDbEnv is env data = ODict() dbCore = dbing.gDbEnv.open_db(b'core') with dbing.gDbEnv.begin(db=dbCore, write=True) as txn: data["name"] = "Søren Kierkegaard" data["city"] = "Copenhagen" datab = json.dumps(data, indent=2).encode("utf-8") txn.put(b'person_0', datab) person_0 = txn.get(b'person_0') assert person_0 == datab data["name"] = "Henrik Ibsen" data["city"] = "Skien" datab = json.dumps(data, indent=2).encode("utf-8") txn.put(b'person_1', datab) person_1 = txn.get(b'person_1') assert person_1 == datab person_0 = txn.get(b'person_0') assert person_0 != datab data = json.loads(person_0.decode("utf-8")) assert data["name"] == "Søren Kierkegaard" assert data["city"] == "Copenhagen" cleanupTmpBaseDir(dbDirPath) assert not os.path.exists(dbDirPath) print("test/db/test_dbing: test_setupDbEnv() \033[92mPASSED\033[0m") # ================================================== # # EOF # # ================================================== #
def test_putGetEntry(): """ Test adding and getting a database entry. """ dbEnv = dbing.setupTestDbEnv() ser = "{'reputer': 'Søren', 'reputee': 'Kierkegaard', 'repute': {'rid': 'dda6555f-21c8-45ff-9633-f9b5cdc59f45', " \ "'feature': 'clarity', 'value': 5}})" dat = json.dumps(ser) key = "søren-dda6555f-21c8-45ff-9633-f9b5cdc59f45" dbing.putEntry(key, dat) gser = dbing.getEntry(key) assert gser == ser cleanupTmpBaseDir(dbEnv.path()) print("test/db/test_dbing: test_putGetEntry() \033[92mPASSED\033[0m")
def test_reputationResourceOnGet(app): """ Test GET to reputation endpoint. Parameters: app - WSGI instance """ priming.setupTest() dbing.setupTestDbEnv() response = app.simulate_get('/reputation/') assert response.content == b'{"title":"Error","description":"A valid query is required."}' assert response.status == falcon.HTTP_400 response = app.simulate_get('/reputation/søren') assert response.content == b'{"title":"Error","description":"Reputee could not be found."}' assert response.status == falcon.HTTP_400 ser = json.dumps({ "reputee": "søren", "clout": { "score": 5.0 }, "confidence": 1, "reach": { "score": 5.0, "confidence": 1 }, "clarity": { "score": 5.0, "confidence": 1 } }) dbing.putEntry("søren", ser, dbn="reputation") response = app.simulate_get('/reputation/søren') assert response.content == b'{"reputee":"s\\u00f8ren","clout":{"score":5.0},"confidence":1,"reach":{"score":5.0,' \ b'"confidence":1},"clarity":{"score":5.0,"confidence":1}}' assert response.status == falcon.HTTP_200 helping.cleanupTmpBaseDir(dbing.gDbDirPath) print( "test/end/test_ending: test_reputationResourceOnGet() \033[92mPASSED\033[0m" )
def test_deleteEntry(): """ Test deleting a database entry. """ dbEnv = dbing.setupTestDbEnv() ser = "{'reputer': 'Søren', 'reputee': 'Kierkegaard', 'repute': {'rid': 'dda6555f-21c8-45ff-9633-f9b5cdc59f45', " \ "'feature': 'clarity', 'value': 5}})" dat = json.dumps(ser) key = "s\u00f8ren-dda6555f-21c8-45ff-9633-f9b5cdc59f45" dbing.putEntry(key, dat, dbn="raw") gser = dbing.getEntry(key, dbn="raw") assert gser == ser dbing.deleteEntry(key, dbn="raw") with pytest.raises(dbing.DatabaseError): dbing.getEntry(key) cleanupTmpBaseDir(dbEnv.path()) print("test/db/test_dbing: test_deleteEntry() \033[92mPASSED\033[0m")
def test_reputationResourceOnPost(app): """ Test POST to reputation endpoint. Parameters: app - WSGI instance """ priming.setupTest() dbing.setupTestDbEnv() response = app.simulate_post('/reputation/søren') assert response.content == b'{"title":"Error","description":"Invalid or missing signature header."}' assert response.status == falcon.HTTP_400 body = b'' header = {"Signature": ""} did, sk = helping.makeTestDid() header["Signature"] = 'signer="' + helping.signResource(body, sk) + '"' response = app.simulate_post('/reputation/søren', headers=header) assert response.content == b'{"title":"Error","description":"Malformed URI."}' assert response.status == falcon.HTTP_400 response = app.simulate_post('/reputation/', headers=header) assert response.content == b'{"title":"Error","description":"A valid JSON document is required."}' assert response.status == falcon.HTTP_400 body = b'Testing ... 1 ... 2 ... 3' header = {"Signature": ""} header["Signature"] = 'signer="' + helping.signResource(body, sk) + '"' response = app.simulate_post('/reputation/', body=body, headers=header) assert response.content == b'{"title":"Error","description":"Could not decode the request body. The JSON was malformed or not encoded as UTF-8."}' assert response.status == falcon.HTTP_422 ser = json.dumps({"reputee": did.encode('utf-8')}) header = {"Signature": ""} header["Signature"] = 'signer="' + helping.signResource(ser, sk) + '"' response = app.simulate_post('/reputation/', body=ser, headers=header) assert response.content == b'{"title":"Error","description":"The request body could not be validated."}' assert response.status == falcon.HTTP_400 ser = json.dumps({ "reputer": "Søren", "reputee": did, "repute": { "rid": "dda6555f-21c8-45ff-9633-f9b5cdc59f45", "feature": "clarity", "value": 5 } }).encode('utf-8') header = {"Signature": ""} header["Signature"] = 'signer="' + helping.signResource(ser, sk) + '"' response = app.simulate_post('/reputation/', body=ser, headers=header) assert response.content == b'{"Message":"entry successfully created."}' assert response.status == falcon.HTTP_201 helping.cleanupTmpBaseDir(dbing.gDbDirPath) print( "test/end/test_ending: test_reputationResourceOnPost() \033[92mPASSED\033[0m" ) # ================================================== # # EOF # # ================================================== #