<commit_before>from unittest import TestCase, main, mock

from backend.waldur.logic.requests import GetServicesRequest, InputRequest, QA
from backend.waldur.logic.requests import GetProjectsRequest


def mocked_query_services_0_names(method, data, endpoint):
    return create_services_response()


def mocked_query_services_1_name(method, data, endpoint):
    return create_services_response("test1")


def mocked_query_services_2_names(method, data, endpoint):
    return create_services_response("test1", "test2")


def create_services_response(*names):
    return [
        {
            'services': [
                {
                    'name': name
                } for name in names
            ]
        }
    ]


class TestGetServicesRequests(TestCase):
    def setUp(self):
        self.get_services = GetServicesRequest()
        self.get_services.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_0_names)
    def test_get_services_0(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_1_name)
    def test_get_services_1(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_2_names)
    def test_get_services_2(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("test2", response['data'])


def mocked_query_projects_0_names(method, data, endpoint):
    return create_projects_response()


def mocked_query_projects_1_name(method, data, endpoint):
    return create_projects_response("test1")


def mocked_query_projects_2_names(method, data, endpoint):
    return create_projects_response("test1", "test2")


def create_projects_response(*names):
    return [
        {
            'projects': [
                {
                    'name': name
                } for name in names
            ]
        }
    ]


class TestGetProjectsRequests(TestCase):
    def setUp(self):
        self.get_services = GetProjectsRequest()
        self.get_services.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_0_names)
    def test_get_services_0(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_1_name)
    def test_get_services_1(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_2_names)
    def test_get_services_2(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("test2", response['data'])


def returns_true(*args):
    return True


class TestInputRequestSingleQuestion(TestCase):
    QUESTION = "What's up?"
    ANSWERS  = ['y', 'yes']
    BAD_END_MSG = "how dare you"

    def setUp(self):
        self.ir = InputRequest([<commit_msg>tests for get_vms and get_org. #28<commit_after>from unittest import TestCase, main, mock

from backend.waldur.logic.requests import GetServicesRequest, InputRequest, QA
from backend.waldur.logic.requests import GetProjectsRequest, GetOrganisationsRequest, GetVmsRequest


def mocked_query_services_0_names(method, data, endpoint):
    return create_services_response()


def mocked_query_services_1_name(method, data, endpoint):
    return create_services_response("test1")


def mocked_query_services_2_names(method, data, endpoint):
    return create_services_response("test1", "test2")


def create_services_response(*names):
    return [
        {
            'services': [
                {
                    'name': name
                } for name in names
            ]
        }
    ]


class TestGetServicesRequests(TestCase):
    def setUp(self):
        self.get_services = GetServicesRequest()
        self.get_services.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_0_names)
    def test_get_services_0(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_1_name)
    def test_get_services_1(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_services_2_names)
    def test_get_services_2(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("test2", response['data'])


def mocked_query_projects_0_names(method, data, endpoint):
    return create_projects_response()


def mocked_query_projects_1_name(method, data, endpoint):
    return create_projects_response("test1")


def mocked_query_projects_2_names(method, data, endpoint):
    return create_projects_response("test1", "test2")


def create_projects_response(*names):
    return [
        {
            'projects': [
                {
                    'name': name
                } for name in names
            ]
        }
    ]


class TestGetProjectsRequests(TestCase):
    def setUp(self):
        self.get_services = GetProjectsRequest()
        self.get_services.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_0_names)
    def test_get_services_0(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_1_name)
    def test_get_services_1(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_projects_2_names)
    def test_get_services_2(self, mock):
        response = self.get_services.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("test2", response['data'])


def mocked_query_get_vms_0_names(method, data, endpoint):
    return create_get_vms_response()


def mocked_query_get_vms_1_name(method, data, endpoint):
    return create_get_vms_response(("test1",["1.1","1.0"]))


def mocked_query_get_vms_2_names(method, data, endpoint):
    return create_get_vms_response(("test1", ["1.1"]), ("test2", ["1.2"]))


def create_get_vms_response(*pairs):
    return [
        {
            'name': name,
            'external_ips': external_ips
        } for name, external_ips in pairs
    ]


class TestGetVmsRequests(TestCase):
    def setUp(self):
        self.get_vms = GetVmsRequest()
        self.get_vms.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_vms_0_names)
    def test_get_vms_0(self, mock):
        response = self.get_vms.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_vms_1_name)
    def test_get_vms_1(self, mock):
        response = self.get_vms.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("1.1", response['data'])
        self.assertIn("1.0", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_vms_2_names)
    def test_get_vms_2(self, mock):
        response = self.get_vms.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("1.1", response['data'])
        self.assertIn("test2", response['data'])
        self.assertIn("1.2", response['data'])


def mocked_query_get_organisations_0_names(method, data, endpoint):
    return create_get_organisations_response()


def mocked_query_get_organisations_1_name(method, data, endpoint):
    return create_get_organisations_response("test1")


def mocked_query_get_organisations_2_names(method, data, endpoint):
    return create_get_organisations_response("test1", "test2")


def create_get_organisations_response(*names):
    return [
        {
            'name': name
        } for name in names
    ]


class TestGetOrganisationsRequests(TestCase):
    def setUp(self):
        self.get_organisations = GetOrganisationsRequest()
        self.get_organisations.set_token("asd")

    def assert_correct_response_format(self, response, type="text"):
        self.assertIn('data', response)
        self.assertIn('type', response)
        self.assertEqual(type, response['type'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_organisations_0_names)
    def test_get_organisations_0(self, mock):
        response = self.get_organisations.process()
        self.assert_correct_response_format(response)

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_organisations_1_name)
    def test_get_organisations_1(self, mock):
        response = self.get_organisations.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])

    @mock.patch('common.request.WaldurConnection.query', side_effect=mocked_query_get_organisations_2_names)
    def test_get_organisations_2(self, mock):
        response = self.get_organisations.process()
        self.assert_correct_response_format(response)
        self.assertIn("test1", response['data'])
        self.assertIn("test2", response['data'])


def returns_true(*args):
    return True


class TestInputRequestSingleQuestion(TestCase):
    QUESTION = "What's up?"
    ANSWERS  = ['y', 'yes']
    BAD_END_MSG = "how dare you"

    def setUp(self):
        self.ir = InputRequest([
{'fraction_non_alphanumeric': 0.11021666523935943, 'fraction_numerical': 0.009248950929177015, 'mean_word_length': 3.805761316872428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '505293', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3500, 'n_words': 545}