# Path: tests/compat.py
# class RequestFactory:
#     def method(self, method, url, params, body=None, root_path=None, **headers):
#         from flask.ctx import AppContext
#         from flask import Flask
#         import os
#         if not root_path:
#             root_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib/tri_form/', )
#         app = AppContext(Flask('tri_form', root_path=root_path))
#         app.push()
#         from werkzeug.test import create_environ
#         from tri_form.compat import HttpRequest
# 
#         # We use the django style where headers are HTTP_
#         for k, v in headers.items():
#             assert k.startswith('HTTP_')
# 
#         # ...but flask adds HTTP_ itself, so we have to cut them off here
#         headers = {k[len('HTTP_'):]: v for k, v in headers.items()}
# 
#         return HttpRequest(create_environ(path=url, query_string=params, method=method, data=body, headers=headers))
# 
#     def get(self, url, params=None, **headers):
#         return self.method('GET', url, params=params, **headers)
# 
#     def post(self, url, params=None, **headers):
#         return self.method('POST', url, params={}, body=params, **headers)


import re
import pytest
        import os
    import os
from collections import defaultdict
from datetime import date, time
from datetime import datetime
from decimal import Decimal
from tri_form.compat import (
    ValidationError,
    field_defaults_factory,
)
from bs4 import BeautifulSoup
from tri_form.compat import Template, smart_str
from tri_declarative import (
    getattr_path,
    setattr_path,
    Namespace,
    class_shortcut,
    Shortcut,
)
from tri_struct import Struct
from tri_form import (
    AVOID_EMPTY_FORM,
    Form,
    Field,
    register_field_factory,
    bool_parse,
    render_attrs,
    decimal_parse,
    url_parse,
    render_template,
    Link,
    datetime_parse,
    datetime_iso_formats,
    int_parse,
    create_members_from_model,
    member_from_model,
    Action,
    float_parse,
    get_name_field,
)
from .compat import RequestFactory
    from tests.models import Foo
    from .models import Foo, Bar
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from tests.models import Foo
    from tests.models import Foo, Bar
    from tests.models import Foo, FieldFromModelForeignKeyTest
    from tests.models import Foo
    from django.db.models import Field as DjangoField, Model
    from django.db.models import TextField, Model
    from django.db.models import TextField, Model
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import Foo
    from django.db.models import fields
    from tests.models import Foo
    from django.db.models import CharField
    from tests.models import Foo
    from django.db.models import CharField
    from django.db.models import QuerySet
    from tests.models import Foo, FieldFromModelForeignKeyTest
    from django.db.models import QuerySet
    from tests.models import Foo, FieldFromModelManyToManyTest
    from tests.models import FieldFromModelOneToOneTest
    from tests.models import Bar
    from tests.models import FooField, RegisterFieldFactoryTest
    from django.db import models
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from django.db import models
    from django.db.models import CASCADE
    from tests.models import Foo
    from .models import Bar
    from django.db import models
    from .models import Foo, Bar
    from .models import Foo
    from django.template import RequestContext
    from tests.models import FromModelWithInheritanceTest
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import FormFromModelTest
    from django.db.models import (
        Model,
        IntegerField,
        CharField,
        ForeignKey,
        CASCADE,
    )
    assert Form(data=dict(foo=' 7  '), fields=[Field.integer(name='foo')]).fields[0].parsed_data == 7
    actual_errors = Form(data=dict(foo=' foo  '), fields=[Field.integer(name='foo')]).fields[0].errors
    assert_one_error_and_matches_reg_exp(actual_errors, r"invalid literal for int\(\) with base 10: u?'foo'")


def test_float_field():
    assert Form(data=dict(foo=' 7.3  '), fields=[Field.float(name='foo')]).fields[0].parsed_data == 7.3
    assert Form(data=dict(foo=' foo  '), fields=[Field.float(name='foo')]).fields[0].errors == {"could not convert string to float: foo"}


def test_email_field():
    assert Form(data=dict(foo=' 5  '), fields=[Field.email(name='foo')]).fields[0].errors == {u'Enter a valid email address.'}
    assert Form(data=dict(foo='foo@example.com'), fields=[Field.email(name='foo')]).is_valid()


def test_phone_field():
    assert Form(data=dict(foo=' asdasd  '), fields=[Field.phone_number(name='foo')]).fields[0].errors == {u'Please use format +<country code> (XX) XX XX. Example of US number: +1 (212) 123 4567 or +1 212 123 4567'}
    assert Form(data=dict(foo='+1 (212) 123 4567'), fields=[Field.phone_number(name='foo')]).is_valid()
    assert Form(data=dict(foo='+46 70 123 123'), fields=[Field.phone_number(name='foo')]).is_valid()


def test_render_template_string():
    assert Form(data=dict(foo='7'), fields=[Field(name='foo', template=None, template_string='{{ field.value }} {{ form.style }}')]).compact() == '7 compact\n' + AVOID_EMPTY_FORM.format('') + '\n'


def test_render_template():
    assert '<form' in Form(request=RequestFactory().get('/'), data=dict(foo='7'), fields=[Field(name='foo')]).render()


def test_render_on_dunder_html():

# Path: tests/compat.py
# class RequestFactory:
#     def method(self, method, url, params, body=None, root_path=None, **headers):
#         from flask.ctx import AppContext
#         from flask import Flask
#         import os
#         if not root_path:
#             root_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib/tri_form/', )
#         app = AppContext(Flask('tri_form', root_path=root_path))
#         app.push()
#         from werkzeug.test import create_environ
#         from tri_form.compat import HttpRequest
# 
#         # We use the django style where headers are HTTP_
#         for k, v in headers.items():
#             assert k.startswith('HTTP_')
# 
#         # ...but flask adds HTTP_ itself, so we have to cut them off here
#         headers = {k[len('HTTP_'):]: v for k, v in headers.items()}
# 
#         return HttpRequest(create_environ(path=url, query_string=params, method=method, data=body, headers=headers))
# 
#     def get(self, url, params=None, **headers):
#         return self.method('GET', url, params=params, **headers)
# 
#     def post(self, url, params=None, **headers):
#         return self.method('POST', url, params={}, body=params, **headers)


# Path: tests/test_forms.py
import re
import pytest
        import os
    import os
from collections import defaultdict
from datetime import date, time
from datetime import datetime
from decimal import Decimal
from tri_form.compat import (
    ValidationError,
    field_defaults_factory,
)
from bs4 import BeautifulSoup
from tri_form.compat import Template, smart_str
from tri_declarative import (
    getattr_path,
    setattr_path,
    Namespace,
    class_shortcut,
    Shortcut,
)
from tri_struct import Struct
from tri_form import (
    AVOID_EMPTY_FORM,
    Form,
    Field,
    register_field_factory,
    bool_parse,
    render_attrs,
    decimal_parse,
    url_parse,
    render_template,
    Link,
    datetime_parse,
    datetime_iso_formats,
    int_parse,
    create_members_from_model,
    member_from_model,
    Action,
    float_parse,
    get_name_field,
)
from .compat import RequestFactory
    from tests.models import Foo
    from .models import Foo, Bar
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from tests.models import Foo
    from tests.models import Foo, Bar
    from tests.models import Foo, FieldFromModelForeignKeyTest
    from tests.models import Foo
    from django.db.models import Field as DjangoField, Model
    from django.db.models import TextField, Model
    from django.db.models import TextField, Model
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import FormFromModelTest
    from tests.models import Foo
    from django.db.models import fields
    from tests.models import Foo
    from django.db.models import CharField
    from tests.models import Foo
    from django.db.models import CharField
    from django.db.models import QuerySet
    from tests.models import Foo, FieldFromModelForeignKeyTest
    from django.db.models import QuerySet
    from tests.models import Foo, FieldFromModelManyToManyTest
    from tests.models import FieldFromModelOneToOneTest
    from tests.models import Bar
    from tests.models import FooField, RegisterFieldFactoryTest
    from django.db import models
    from django.contrib.auth.models import User
    from django.contrib.auth.models import User
    from django.db import models
    from django.db.models import CASCADE
    from tests.models import Foo
    from .models import Bar
    from django.db import models
    from .models import Foo, Bar
    from .models import Foo
    from django.template import RequestContext
    from tests.models import FromModelWithInheritanceTest
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import ExpandModelTestB
    from tests.models import FormFromModelTest
    from django.db.models import (
        Model,
        IntegerField,
        CharField,
        ForeignKey,
        CASCADE,
    )

    assert Form(data=dict(foo=' 7  '), fields=[Field.integer(name='foo')]).fields[0].parsed_data == 7
    actual_errors = Form(data=dict(foo=' foo  '), fields=[Field.integer(name='foo')]).fields[0].errors
    assert_one_error_and_matches_reg_exp(actual_errors, r"invalid literal for int\(\) with base 10: u?'foo'")


def test_float_field():
    assert Form(data=dict(foo=' 7.3  '), fields=[Field.float(name='foo')]).fields[0].parsed_data == 7.3
    assert Form(data=dict(foo=' foo  '), fields=[Field.float(name='foo')]).fields[0].errors == {"could not convert string to float: foo"}


def test_email_field():
    assert Form(data=dict(foo=' 5  '), fields=[Field.email(name='foo')]).fields[0].errors == {u'Enter a valid email address.'}
    assert Form(data=dict(foo='foo@example.com'), fields=[Field.email(name='foo')]).is_valid()


def test_phone_field():
    assert Form(data=dict(foo=' asdasd  '), fields=[Field.phone_number(name='foo')]).fields[0].errors == {u'Please use format +<country code> (XX) XX XX. Example of US number: +1 (212) 123 4567 or +1 212 123 4567'}
    assert Form(data=dict(foo='+1 (212) 123 4567'), fields=[Field.phone_number(name='foo')]).is_valid()
    assert Form(data=dict(foo='+46 70 123 123'), fields=[Field.phone_number(name='foo')]).is_valid()


def test_render_template_string():
    assert Form(data=dict(foo='7'), fields=[Field(name='foo', template=None, template_string='{{ field.value }} {{ form.style }}')]).compact() == '7 compact\n' + AVOID_EMPTY_FORM.format('') + '\n'


def test_render_template():
    assert '<form' in Form(request=RequestFactory().get('/'), data=dict(foo='7'), fields=[Field(name='foo')]).render()


def test_render_on_dunder_html():

    form = Form(request=RequestFactory().get('/'), data=dict(foo='7'), fields=[Field(name='foo')])