# Path: tests/ex02_compose_v3_2/models.py
# class Compose(object):
#     version = related.StringField(required=False, default=None)
#     services = related.MappingField(Service, "name", required=False)
# 
# class Port(object):
#     """ https://docs.docker.com/compose/compose-file/#ports """
#     _short_form = related.StringField(required=False, repr=False, cmp=False)
#     target = related.IntegerField(required=False)
#     published = related.IntegerField(required=False)
#     protocol = related.ChildField(Protocol, default=Protocol.TCP)
#     mode = related.ChildField(Mode, default=Mode.HOST)
# 
#     def __attrs_post_init__(self):
#         if self._short_form:
#             # todo: handle protocol and mode parsing
#             published, target = map(int, self._short_form.split(":"))
# 
#             # set methods won't work because the class is immutable.
#             # workaround: https://github.com/python-attrs/attrs/issues/120
#             object.__setattr__(self, "published", published)
#             object.__setattr__(self, "target", target)


from os.path import join, dirname
from .models import Compose, Port
from related import to_yaml, from_yaml



YML_FILE = join(dirname(__file__), "docker-compose.yml")


def test_compose_from_yml():
    original_yaml = open(YML_FILE).read().strip()
    yml_dict = from_yaml(original_yaml)
    compose = Compose(**yml_dict)

    # basic checks
    assert compose.version == '2'

# Path: tests/ex02_compose_v3_2/models.py
# class Compose(object):
#     version = related.StringField(required=False, default=None)
#     services = related.MappingField(Service, "name", required=False)
# 
# class Port(object):
#     """ https://docs.docker.com/compose/compose-file/#ports """
#     _short_form = related.StringField(required=False, repr=False, cmp=False)
#     target = related.IntegerField(required=False)
#     published = related.IntegerField(required=False)
#     protocol = related.ChildField(Protocol, default=Protocol.TCP)
#     mode = related.ChildField(Mode, default=Mode.HOST)
# 
#     def __attrs_post_init__(self):
#         if self._short_form:
#             # todo: handle protocol and mode parsing
#             published, target = map(int, self._short_form.split(":"))
# 
#             # set methods won't work because the class is immutable.
#             # workaround: https://github.com/python-attrs/attrs/issues/120
#             object.__setattr__(self, "published", published)
#             object.__setattr__(self, "target", target)


# Path: tests/ex02_compose_v3_2/test_compose_v3_2.py
from os.path import join, dirname
from .models import Compose, Port
from related import to_yaml, from_yaml




YML_FILE = join(dirname(__file__), "docker-compose.yml")


def test_compose_from_yml():
    original_yaml = open(YML_FILE).read().strip()
    yml_dict = from_yaml(original_yaml)
    compose = Compose(**yml_dict)

    # basic checks
    assert compose.version == '2'

    assert compose.services['redis'].image == "redis"