def test_create_table(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() db.create_specializer_table() db.connection.execute.assert_called_with( 'create table test (fname text, variant text, key text, perf real)' )
def test_delete(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() db.table_exists = Mock(return_value=True) db.delete("foo", "foo_v1", "KEY") db.connection.execute.assert_called_with( "delete from test where fname=? and variant=? and key=?", ("foo", "foo_v1", "KEY"))
def test_insert(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() db.table_exists = Mock(return_value=True) db.create_specializer_table() db.insert("func", "func", "KEY", 4.321) db.connection.execute.assert_called_with( 'insert into test values (?,?,?,?)', ("func", "func", "KEY", 4.321))
def test_update(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() db.table_exists = Mock(return_value=True) mock_cursor = Mock() mock_cursor.fetchone.return_value = (1, ) db.connection.cursor.return_value = mock_cursor db.update("foo", "foo_v1", "KEY", 3.21) db.connection.execute.assert_called_with( "update test set perf=? where fname=? and variant=? and key=?", (3.21, "foo", "foo_v1", "KEY"))
def test_create_if_insert_into_nonexistent_table(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() # this is kind of a complicated situation. we want the cursor to # return an array when fetchall() is called on it, and we want this # cursor to be created when the mock connection is asked for a cursor mock_cursor = Mock() mock_cursor.fetchall.return_value = [] db.connection.cursor.return_value = mock_cursor db.create_specializer_table = Mock() db.insert("func", "v1", "KEY", 4.321) self.assertTrue(db.create_specializer_table.called)
def test_get(self): db = asp_module.ASPDB("test") db.close() # close the real connection so we can mock it out db.connection = Mock() db.table_exists = Mock(return_value=True) db.create_specializer_table() # see note about mocks in test_create_if... mock_cursor = Mock() mock_cursor.fetchall.return_value = ['hello'] db.connection.cursor.return_value = mock_cursor db.create_specializer_table = Mock() db.get("func") mock_cursor.execute.assert_called_with( "select * from test where fname=?", ("func", ))
def test_create_db_if_nonexistent(self): db = asp_module.ASPDB("test") self.assertTrue(db.connection)
def test_creating_db(self): db = asp_module.ASPDB("test_specializer")