def test__pull(self, mock_urllib): """ Tests the database receiver's _pull method. The expected result of this test is that the information is correctly pulled from the database. @param mock_urllib A mock URL library used to represent the connection with the web server. """ class FakeResponse(object): """ Fake response object that mocks urllib.response """ def read(self): """ Mock the repsonse accessor """ return bytes(TestDatabaseReceiver.runs, 'utf-8') mock_urllib.return_value = FakeResponse() dr = DatabaseReceiver() runs = dr._pull_runs() # Assert that runs has been properly defined assert runs # Assert tht runs contains the right information - A JSON string representing the database table from which # the data will be pulled. assert runs == json.loads(self.runs)
def test__write_runs(self): """ Tests the database receiver's _write_runs method. The expected result of this test is that data can be correctly written to the database. """ dr = DatabaseReceiver(db_name=test_db_name) runs = json.loads(self.runs) dr._write_runs(runs) con = sqlite3.connect(test_db_name) try: # Assert that the data can be pulled from the database table. This shows it has been correctly inserted. assert con.execute("SELECT * FROM runs WHERE runs.id = %s" % runs[0]["id"]).fetchone() except: raise finally: con.close()