<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2013 - <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

from functionaltests.api import base


class TestAssemblyController(base.TestCase):

    def test_assemblies_get_all(self):
        resp, body = self.client.get('v1/assemblies')
        data = json.loads(body)
        self.assertEqual(resp.status, 200)
        self.assertEqual(data, [])<commit_msg>Add CRUD functionnal tests to assembly<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2013 - <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

from functionaltests.api import base

sample_data = {"name": "test_assembly",
               "description": "A test to create assembly",
               "project_id": "project_id",
               "user_id": "user_id"}

plan_sample_data = {"name": "test_plan",
                    "description": "A test to create plan",
                    "project_id": "project_id",
                    "user_id": "user_id"}


class TestAssemblyController(base.TestCase):

    def _assert_output_expected(self, body_data, data):
        self.assertEqual(body_data['description'], data['description'])
        self.assertEqual(body_data['plan_uri'], data['plan_uri'])
        self.assertEqual(body_data['name'], data['name'])
        self.assertIsNotNone(body_data['uuid'])

    def _delete_assembly(self, uuid, plan_uuid):
        resp, _ = self.client.delete('v1/assemblies/%s' % uuid)
        self.assertEqual(resp.status, 204)
        self._delete_plan(plan_uuid)

    def _delete_plan(self, plan_uuid):
        resp, _ = self.client.delete('v1/plans/%s' % plan_uuid)
        self.assertEqual(resp.status, 204)

    def _create_assembly(self):
        p_uuid = self._create_plan()
        sample_data['plan_uri'] = "%s/v1/plans/%s" % (self.client.base_url,
                                                      p_uuid)
        jsondata = json.dumps(sample_data)
        resp, body = self.client.post('v1/assemblies', jsondata)
        self.assertEqual(resp.status, 201)
        out_data = json.loads(body)
        uuid = out_data['uuid']
        self.assertIsNotNone(uuid)
        return uuid, p_uuid

    def _create_plan(self):
        jsondata = json.dumps(plan_sample_data)
        resp, body = self.client.post('v1/plans', jsondata)
        self.assertEqual(resp.status, 201)
        out_data = json.loads(body)
        uuid = out_data['uuid']
        self.assertIsNotNone(uuid)
        return uuid

    def test_assemblies_get_all(self):
        resp, body = self.client.get('v1/assemblies')
        data = json.loads(body)
        self.assertEqual(resp.status, 200)
        self.assertEqual(data, [])

    def test_assemblies_create(self):
        p_uuid = self._create_plan()
        sample_data['plan_uri'] = "%s/v1/plans/%s" % (self.client.base_url,
                                                      p_uuid)
        sample_json = json.dumps(sample_data)
        resp, body = self.client.post('v1/assemblies', sample_json)
        self.assertEqual(resp.status, 201)
        json_data = json.loads(body)
        self._assert_output_expected(json_data, sample_data)
        self._delete_assembly(json_data['uuid'], p_uuid)

    def test_assemblies_get(self):
        uuid, plan_uuid = self._create_assembly()
        sample_data['plan_uri'] = "%s/v1/plans/%s" % (self.client.base_url,
                                                      plan_uuid)
        resp, body = self.client.get('v1/assemblies/%s' % uuid)
        self.assertEqual(resp.status, 200)
        json_data = json.loads(body)
        self._assert_output_expected(json_data, sample_data)
        self._delete_assembly(uuid, plan_uuid)

    def test_assemblies_put(self):
        uuid, plan_uuid = self._create_assembly()
        uri = "%s/v1/plans/%s" % (self.client.base_url, plan_uuid)
        updated_data = {"name": "test_assembly updated",
                        "description": "A test to create assembly updated",
                        "plan_uri": uri,
                        "project_id": "project_id updated",
                        "user_id": "user_id updated"}
        updated_json = json.dumps(updated_data)
        resp, body = self.client.put('v1/assemblies/%s' % uuid, updated_json)
        self.assertEqual(resp.status, 200)
        json_data = json.loads(body)
        self._assert_output_expected(json_data, updated_data)
        self._delete_assembly(uuid, plan_uuid)

    def test_assemblies_delete(self):
        uuid, plan_uuid = self._create_assembly()
        resp, body = self.client.delete('v1/assemblies/%s' % uuid)
        self.assertEqual(resp.status, 204)
        self.assertEqual(body, '')
        self._delete_plan(plan_uuid)
{'fraction_non_alphanumeric': 0.11227701993704092, 'fraction_numerical': 0.010842952081147255, 'mean_word_length': 2.6473214285714284, 'pattern_counts': {'":': 13, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5310968', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1671, 'n_words': 404}