Exemplo n.º 1
0
    def test_raise_errors(self):

        base_dir = self.tmpdir.mkdir('base')
        base_dir.mkdir('tests')
        base_dir.join('Makefile').write('make')
        base_dir.mkdir('code').join('__init__.py').write('# ...')
        os.chdir(str(base_dir))

        checker = StructureChecker()
        self.mocker.patch.object(
            checker,
            'REQUIRED_STRUCTURE',
            [
                File('requirements.txt', 'to require'),
                File('Makefile', 'to make'),
                Directory('tests', 'to test'),
                Directory('code', 'to code'),
                Directory('images', 'to image'),
            ])
        checker.is_valid()

        with pytest.raises(checker.BrokenStructure) as e:
            checker.raise_errors()

        text = e.value.args[0]
        expected = remove_white_chars('''

            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Project Structure Errors Detected

            REQUIRED STRUCTURE:
            ├── code/
            │   └── __init__.py
            ├── tests/
            ├── .git/
            ├── .gitignore
            ├── env.sh
            ├── Makefile
            ├── pytest.ini
            ├── README.md
            ├── requirements.txt
            ├── test-requirements.txt
            └── setup.py

            ERRORS:
            + Missing: `requirements.txt` file. Its purpose is: to require
            + Missing: `images` directory. Its purpose is: to image
        ''')

        assert remove_white_chars(text) == expected
Exemplo n.º 2
0
    def test_init__config_does_not_exist(self):

        current_cwd = os.getcwd()
        os.chdir(str(self.tmpdir))

        copy = self.mocker.patch.object(Copier, 'copy')

        result = self.runner.invoke(cli, ['init', 'src_dir'])
        assert result.exit_code == 1
        assert remove_white_chars(result.output) == remove_white_chars('''
            Error:
            Please install `lily_assistant` and run
            `lily_assistant init <src_dir>` before running
            `lily init <src_dir>`
        ''')
        assert copy.call_count == 0
        os.chdir(current_cwd)
Exemplo n.º 3
0
    def test_render_examples(self):

        conf = deepcopy(CONF)
        conf['examples'] = {
            '404 (NOT_FOUND)': {
                'response': {
                    'where': 'here',
                    'error': 'not found',
                    'status': 404,
                },
            },
            '200 (YO)': {
                'response': {
                    'id': 45,
                    'age': 879,
                },
            },
        }

        command = Command('BULK_READ_TASKS', conf)

        assert (remove_white_chars(
            command.render_examples()) == remove_white_chars(
                normalize_indentation(
                    '''
                /**
                 * Examples for BULK_READ_TASKS
                 */
                export const BulkReadTasksExamples = {
                    "200 (YO)": {
                        "age": 879,
                        "id": 45
                    },

                    "404 (NOT_FOUND)": {
                        "error": "not found",
                        "status": 404,
                        "where": "here"
                    }
                }
            ''', 0)))
Exemplo n.º 4
0
    def test_find_project_name__missing_project_directory(self):

        base_dir = self.tmpdir.mkdir('base')
        base_dir.mkdir('code')
        os.chdir(str(base_dir))

        with pytest.raises(StructureChecker.BrokenStructure) as e:
            StructureChecker.find_project_name()

        text = e.value.args[0]
        expected = remove_white_chars('''

            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Couldn't find main project directory

            Required structure:

            └── <project_dir>
                └── __init__.py
        ''')

        assert remove_white_chars(text) == expected
Exemplo n.º 5
0
    def test_render(self):

        self.mocker.patch.object(BaseRenderer, 'render').return_value = {
            'LIST_ITEMS': {
                'path_conf': {
                    'path': '/items/',
                    'parameters': {}
                },
                'method': 'get',
                'meta': Meta(
                    title='hi there',
                    description='what?',
                    domain=Domain(id='d', name='items management')),
            },
            'CREATED': {
                'path_conf': {
                    'path': '/items/',
                    'parameters': {}
                },
                'method': 'post',
                'meta': Meta(
                    title='hi there',
                    description='what?',
                    domain=Domain(id='d', name='items management')),
            },
        }
        self.mocker.patch.object(
            MarkdownRenderer, 'get_examples'
        ).return_value = {
            'CREATED': {
                '201 (CREATED)': {
                    'response': {
                        'content_type': 'application/json',
                        'status': 201,
                        'content': {
                            'user_id': 434,
                            '@event': 'CREATED',
                        }
                    },
                    'request': {
                        'path': 'hello/hi',
                        'headers': {
                            'HI-THERE': 'JSON',
                        },
                        'content': {
                            'hi': 'there'
                        }
                    },
                    'description': 'CREATE',
                    'method': 'post'
                },
            },
            'LIST_ITEMS': {
                '200 (LISTED)': {
                    'response': {
                        'content_type': 'application/json',
                        'status': 200,
                        'content': {
                            'user_id': 434,
                            '@event': 'LISTED',
                        }
                    },
                    'request': {
                        'path': 'wat/178',
                        'headers': {
                            'HI-THERE': 'JSON',
                        }
                    },
                    'description': 'SERVER_ERROR',
                    'method': 'get'
                },
                '502 (SERVER_ERROR)': {
                    'response': {
                        'content_type': 'application/json',
                        'status': 502,
                        'content': {
                            'user_id': 434,
                            '@type': 'error',
                            '@event': 'SERVER_ERROR',
                        }
                    },
                    'request': {
                        'path': 'hello/world',
                        'headers': {
                            'HI-THERE': 'JSON',
                        }
                    },
                    'description': 'SERVER_ERROR',
                    'method': 'get'
                },
            }
        }

        assert remove_white_chars(
            MarkdownRenderer(Mock()).render()
        ) == remove_white_chars('''

            # CoSphere API

            CoSphere's API with hypermedia links

            ## items management

            ### CREATED: POST /items/

            hi there
            what?

            #### 201 (CREATED)

            Request:
            ```http
            POST hello/hi HTTP/1.1
            HI-THERE: JSON
            {
                "hi": "there"
            }
            ```

            Respone:
            ```json
            {
                "@event": "CREATED",
                "user_id": 434
            }
            ```

            ### LIST_ITEMS: GET /items/
            hi there
            what?

            #### 200 (LISTED)

            Request:
            ```http
            GET wat/178 HTTP/1.1
            HI-THERE: JSON
            ```

            Respone:
            ```json
            {
                "@event": "LISTED",
                "user_id": 434
            }
            ```

            #### 502 (SERVER_ERROR)

            Request:
            ```http
            GET hello/world HTTP/1.1
            HI-THERE: JSON
            ```

            Respone:
            ```json
            {
                "@event": "SERVER_ERROR",
                "@type": "error",
                "user_id": 434
            }
            ```

        ''')
Exemplo n.º 6
0
    def test_render_api_ts(self):

        command = lambda x: Mock(render_facade=Mock(return_value=x))  # noqa
        commands_by_domain = {
            Domain('cards', 'Cards Management'): {
                'READ_CARDS': command('    <READ_CARDS>'),
                'DELETE_CARD': command('    <DELETE_CARD>'),
            },
            Domain('paths', 'Path Management'): {
                'CREATE_PATH': command('    <CREATE_PATH>'),
            },
        }
        services_path = str(self.services_dir)

        self.renderer.render_api_ts(commands_by_domain)

        assert os.listdir(services_path) == ['api.service.ts']
        with open(os.path.join(services_path, 'api.service.ts'), 'r') as f:
            assert remove_white_chars(f.read()) == remove_white_chars(normalize_indentation('''
                /**
                  * THIS FILE WAS AUTOGENERATED, ALL MANUAL CHANGES CAN BE
                  * OVERWRITTEN
                  */

                /**
                 * Facade API Service for all domains
                 */
                import { Injectable, Injector } from '@angular/core';
                import { Observable } from 'rxjs';

                import * as X from '../domains/index';

                @Injectable()
                export class APIService {

                    constructor(private injector: Injector) {}

                    /**
                     * Cards Management domain
                     */
                    private _cardsDomain: X.CardsDomain;

                    public get cardsDomain(): X.CardsDomain {
                        if (!this._cardsDomain) {
                            this._cardsDomain = this.injector.get(X.CardsDomain);
                        }

                        return this._cardsDomain;
                    }

                    <DELETE_CARD>

                    <READ_CARDS>

                    /**
                     * Path Management domain
                     */
                    private _pathsDomain: X.PathsDomain;

                    public get pathsDomain(): X.PathsDomain {
                        if (!this._pathsDomain) {
                            this._pathsDomain = this.injector.get(X.PathsDomain);
                        }

                        return this._pathsDomain;
                    }

                    <CREATE_PATH>

                }
            ''', 0))  # noqa