# Path: diplomacy/tests/helpers.py
# def create_units(units, turn, governments):
#     parsed = (
#         (gvt, unitstr[0], parse(unitstr))
#         for gvt, uset in units.items()
#         for unitstr in uset
#     )
#     return models.Unit.objects.bulk_create([
#         models.Unit(turn=turn,
#                     government=governments[gvt],
#                     u_type=u_type,
#                     subregion=get_subregion(udict.get('territory', ''),
#                                             udict.get('subname', ''),
#                                             udict.get('sr_type', '')))
#         for gvt, u_type, udict in parsed
#     ])
# 
# def create_orders(orders, turn, governments):
#     order_posts = {
#         gvt: models.OrderPost.objects.create(turn=turn, government=governments[gvt])
#         for gvt in orders
#     }
# 
#     parsed = (
#         (gvt, action, regexp.match(orderstr))
#         for gvt, oset in orders.items()
#         for orderstr in oset
#         for action, regexp in order_patterns.items()
#     )
#     parsed = (
#         (gvt, action, omatch.groupdict(''))
#         for gvt, action, omatch in parsed
#         if omatch is not None
#     )
#     parsed = (
#         (gvt, action,
#          parse(odict.get('actor', '')),
#          parse(odict.get('assist', '')),
#          parse(odict.get('target', '')),
#          bool(odict.get('via_convoy')))
#         for gvt, action, odict in parsed
#     )
# 
#     final_orders = [
#         models.Order(post=order_posts[gvt],
#                      actor=get_subregion(strict=(turn.season == 'FA'), **actor),
#                      action=action,
#                      assist=get_subregion(**assist) if assist else '',
#                      target=get_subregion(
#                          sr_type=assist.get('sr_type', '') if assist else actor.get('sr_type', ''),
#                          **target
#                      ) if target else '',
#                      via_convoy=via_convoy)
#         for gvt, action, actor, assist, target, via_convoy in parsed
#     ]
#     return models.Order.objects.bulk_create(final_orders)
# 
# Path: diplomacy/engine/standard.py

# 
# Path: diplomacy/engine/check.py
# def is_legal(order, units, owns, season):
#     builds = builds_available(units, owns)
# 
#     if not order['actor']:
#         if season != 'FA':
#             return False
#         unit = ()
#     else:
#         unit = [u for u in units if u['subregion'] == order['actor']]
# 
#     if not order['actor'] or not order['action']:
#         return (season == 'FA' and
#                 builds.get(order['government'], 0) > 0)
#     if order['action'] != 'B' and not unit:
#         return False
# 
#     if season in ('S', 'F'):
#         if unit[0]['government'] != order['government']:
#             return False
#     elif season in ('SR', 'FR'):
#         unit = [u for u in unit if u['dislodged']]
#         if not unit:
#             return False
#         if unit[0]['government'] != order['government']:
#             return False
#     elif order['action'] == 'D':
#         if unit and unit[0]['government'] != order['government']:
#             return False
#     elif order['action'] == 'B':
#         if not any(o['territory'] == get_territory(order['actor'])
#                    and o['government'] == order['government']
#                    for o in owns):
#             return False
# 
#     actions = {'H': valid_hold,
#                'M': valid_move,
#                'S': valid_support,
#                'C': valid_convoy,
#                'B': valid_build,
#                'D': valid_disband}
#     tree = actions[order['action']](order['actor'], units, owns, season)
#     if not tree or order['assist'] not in tree:
#         return False
#     tree = tree[order['assist']]
#     if order['target'] not in tree:
#         return False
#     return True
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
#     if not sr_token:
#         return u''
#     return sr_token.split(u'.')[0]


from django.test import TestCase
from . import factories
from .helpers import create_units, create_orders
from .. import models
from ..engine import standard
from ..engine.check import is_legal
from ..engine.utils import get_territory
                == ('north-atlantic-ocean', 'mid-atlantic-ocean', 'france', 'F')
                for u in units))

        self.assertTrue(
            any((get_territory(u['subregion']), u['government'], u['dislodged'], u['u_type'])
                == ('clyde', 'england', True, 'F')
                for u in units))

        self.assertTrue(
            any((get_territory(u['subregion']), get_territory(u['previous']), u['government'], u['u_type'])
                == ('clyde', 'norway', 'russia', 'A')
                for u in units))

    def test_second_order_paradox_with_two_solutions(self):
        # DATC 6.F.22
        units = {"England": ("F Edinburgh", "F London"),
                 "France": ("A Brest", "F English Channel"),
                 "Germany": ("F Belgium", "F Picardy"),
                 "Russia": ("A Norway", "F North Sea")}
        T = models.Turn.objects.get()
        create_units(units, T, self.governments)

        orders = {"England": ("F Edinburgh M North Sea",
                              "F London S F Edinburgh - North Sea"),
                  "France": ("A Brest M London",
                             "F English Channel C A Brest - London"),
                  "Germany": ("F Belgium S F Picardy - English Channel",
                              "F Picardy M English Channel"),
                  "Russia": ("A Norway M Belgium",
                             "F North Sea C A Norway - Belgium")}

# Path: diplomacy/tests/helpers.py
# def create_units(units, turn, governments):
#     parsed = (
#         (gvt, unitstr[0], parse(unitstr))
#         for gvt, uset in units.items()
#         for unitstr in uset
#     )
#     return models.Unit.objects.bulk_create([
#         models.Unit(turn=turn,
#                     government=governments[gvt],
#                     u_type=u_type,
#                     subregion=get_subregion(udict.get('territory', ''),
#                                             udict.get('subname', ''),
#                                             udict.get('sr_type', '')))
#         for gvt, u_type, udict in parsed
#     ])
# 
# def create_orders(orders, turn, governments):
#     order_posts = {
#         gvt: models.OrderPost.objects.create(turn=turn, government=governments[gvt])
#         for gvt in orders
#     }
# 
#     parsed = (
#         (gvt, action, regexp.match(orderstr))
#         for gvt, oset in orders.items()
#         for orderstr in oset
#         for action, regexp in order_patterns.items()
#     )
#     parsed = (
#         (gvt, action, omatch.groupdict(''))
#         for gvt, action, omatch in parsed
#         if omatch is not None
#     )
#     parsed = (
#         (gvt, action,
#          parse(odict.get('actor', '')),
#          parse(odict.get('assist', '')),
#          parse(odict.get('target', '')),
#          bool(odict.get('via_convoy')))
#         for gvt, action, odict in parsed
#     )
# 
#     final_orders = [
#         models.Order(post=order_posts[gvt],
#                      actor=get_subregion(strict=(turn.season == 'FA'), **actor),
#                      action=action,
#                      assist=get_subregion(**assist) if assist else '',
#                      target=get_subregion(
#                          sr_type=assist.get('sr_type', '') if assist else actor.get('sr_type', ''),
#                          **target
#                      ) if target else '',
#                      via_convoy=via_convoy)
#         for gvt, action, actor, assist, target, via_convoy in parsed
#     ]
#     return models.Order.objects.bulk_create(final_orders)
# 
# Path: diplomacy/engine/standard.py

# 
# Path: diplomacy/engine/check.py
# def is_legal(order, units, owns, season):
#     builds = builds_available(units, owns)
# 
#     if not order['actor']:
#         if season != 'FA':
#             return False
#         unit = ()
#     else:
#         unit = [u for u in units if u['subregion'] == order['actor']]
# 
#     if not order['actor'] or not order['action']:
#         return (season == 'FA' and
#                 builds.get(order['government'], 0) > 0)
#     if order['action'] != 'B' and not unit:
#         return False
# 
#     if season in ('S', 'F'):
#         if unit[0]['government'] != order['government']:
#             return False
#     elif season in ('SR', 'FR'):
#         unit = [u for u in unit if u['dislodged']]
#         if not unit:
#             return False
#         if unit[0]['government'] != order['government']:
#             return False
#     elif order['action'] == 'D':
#         if unit and unit[0]['government'] != order['government']:
#             return False
#     elif order['action'] == 'B':
#         if not any(o['territory'] == get_territory(order['actor'])
#                    and o['government'] == order['government']
#                    for o in owns):
#             return False
# 
#     actions = {'H': valid_hold,
#                'M': valid_move,
#                'S': valid_support,
#                'C': valid_convoy,
#                'B': valid_build,
#                'D': valid_disband}
#     tree = actions[order['action']](order['actor'], units, owns, season)
#     if not tree or order['assist'] not in tree:
#         return False
#     tree = tree[order['assist']]
#     if order['target'] not in tree:
#         return False
#     return True
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
#     if not sr_token:
#         return u''
#     return sr_token.split(u'.')[0]


# Path: diplomacy/tests/test_datc_convoys.py
from django.test import TestCase
from . import factories
from .helpers import create_units, create_orders
from .. import models
from ..engine import standard
from ..engine.check import is_legal
from ..engine.utils import get_territory

                == ('north-atlantic-ocean', 'mid-atlantic-ocean', 'france', 'F')
                for u in units))

        self.assertTrue(
            any((get_territory(u['subregion']), u['government'], u['dislodged'], u['u_type'])
                == ('clyde', 'england', True, 'F')
                for u in units))

        self.assertTrue(
            any((get_territory(u['subregion']), get_territory(u['previous']), u['government'], u['u_type'])
                == ('clyde', 'norway', 'russia', 'A')
                for u in units))

    def test_second_order_paradox_with_two_solutions(self):
        # DATC 6.F.22
        units = {"England": ("F Edinburgh", "F London"),
                 "France": ("A Brest", "F English Channel"),
                 "Germany": ("F Belgium", "F Picardy"),
                 "Russia": ("A Norway", "F North Sea")}
        T = models.Turn.objects.get()
        create_units(units, T, self.governments)

        orders = {"England": ("F Edinburgh M North Sea",
                              "F London S F Edinburgh - North Sea"),
                  "France": ("A Brest M London",
                             "F English Channel C A Brest - London"),
                  "Germany": ("F Belgium S F Picardy - English Channel",
                              "F Picardy M English Channel"),
                  "Russia": ("A Norway M Belgium",
                             "F North Sea C A Norway - Belgium")}

        create_orders(orders, T, self.governments)