def test_get_config(self): """ Should return a config given an id """ # set up mock_description = PropertyMock( return_value=( ('config_id',), ('config_name',), ('key_value_pairs',), ) ) type(self.mock_get_cur.return_value).description = mock_description self.mock_get_cur.return_value.fetchone.return_value = ( 1, 'mock-config-name', 'a=b\nc=d', ) # run SUT test_config = get_config(1) # confirm we selected the given config self.mock_get_cur.return_value.execute.assert_called_once_with( "SELECT * FROM config WHERE config_id=%s", (1,), ) # confirm the return is correct self.assertEqual(test_config, { "config_id": 1, "config_name": "mock-config-name", "key_value_pairs": "a=b\nc=d", }) # confirm we closed the cursor self.mock_get_cur.return_value.close.assert_called_once_with()
def new_config(based_on_id=None): """ Make a new config based on the given config or an empty one """ if based_on_id: based_on_config = get_config(based_on_id) key_value_pairs_text = based_on_config['key_value_pairs'] else: key_value_pairs_text = '' cursor = get_cursor() cursor.execute( "INSERT INTO config (key_value_pairs) VALUES (%s) RETURNING config_id", (key_value_pairs_text,), ) config_id = cursor.fetchone()[0] cursor.close() return config_id