Ejemplo n.º 1
0
    def test_get_env(self):
        """ Should return an environment given and id """
        # set up
        mock_description = PropertyMock(
            return_value=(
                ("environment_id",),
                ("environment_name",),
                ("settings",),
            )
        )
        type(self.mock_get_cur.return_value).description = mock_description
        self.mock_get_cur.return_value.fetchone.return_value = (
            2,
            'mock-env-name',
            'setting=something\nwhatever=foobar',
        )

        # run SUT
        test_env = get_env(2)

        # confirm we selected the given environment
        self.mock_get_cur.return_value.execute.assert_called_once_with(
            "SELECT * FROM environment WHERE environment_id=%s",
            (2,),
        )

        # confirm the return is correct
        self.assertEqual(test_env, {
                "environment_id": 2,
                "environment_name": "mock-env-name",
                "settings": 'setting=something\nwhatever=foobar',
            })

        # confirm we closed the cursor
        self.mock_get_cur.return_value.close.assert_called_once_with()
Ejemplo n.º 2
0
def new_env(based_on_id=None,
            infrastructure_backend=None,
            environment_name='qa-sandbox'):
    """ Make a new environment based on the given config or an empty one """
    if infrastructure_backend is None:
        infrastructure_backend = cfg('default_infrastructure_backend', None)
    if based_on_id:
        based_on_env = get_env(based_on_id)
        settings_value = based_on_env['settings']
        infrastructure_backend = based_on_env['infrastructure_backend']
    else:
        settings_value = ''

    cursor = get_cursor()
    cursor.execute(
        "INSERT INTO environment\n" + \
        "(settings, infrastructure_backend, environment_name)\n" + \
        "VALUES (%s, %s, %s)\n" + \
        "RETURNING environment_id",
        (settings_value, infrastructure_backend, environment_name),
    )
    env_id = cursor.fetchone()[0]
    cursor.close()
    return env_id