# Path: mcm/comparators.py
# class UniqueKeyComparator:
# 
# 
#     def __init__(self, keys):
# 
#         self.keys = keys
#         self.SET = []
#         self.ADD = []
#         self.NO_DELETE = []
# 
# 
#     def compare(self, wanted, present):
# 
#         for wanted_rule in wanted:
#             present_rule = self.findPair(searched=wanted_rule, present=present)
#             diff = wanted_rule - present_rule
#             self.decide(wanted_rule, diff, present_rule)
# 
#         DEL = set(present) - set(self.NO_DELETE)
#         return tuple(self.ADD), tuple(self.SET), tuple(DEL)
# 
# 
#     def decide(self, wanted, difference, present):
# 
#         if wanted and difference and not present:
#             self.ADD.append( wanted )
#         elif wanted and difference and present:
#             self.NO_DELETE.append(present)
#             difference['.id'] = present['.id']
#             self.SET.append( difference )
#         elif wanted and not difference and present:
#             self.NO_DELETE.append(present)
# 
# 
#     def findPair(self, searched, present):
# 
#         for row in present:
#             if row.isunique(other=searched, keys=self.keys):
#                 return row
#         else:
#             return CmdPathRow(dict())
# 
# class SingleElementComparator:
# 
# 
#     def compare(self, wanted, present):
#         SET = tuple()
#         for wanted_row, present_row in zip(wanted, present):
#             diff = wanted_row - present_row
#             SET = (diff,) if diff else tuple()
# 
#         return tuple(), SET, tuple()
# 
# class OrderedComparator:
# 
# 
#     def __init__(self):
# 
#         self.DEL = []
#         self.SET= []
#         self.ADD = []
# 
# 
#     def decide(self, wanted, difference, present):
# 
#         if wanted and difference and not present:
#             self.ADD.append( wanted )
#         elif not wanted and not difference and present:
#             self.DEL.append( present )
#         elif wanted and difference and present:
#             difference['.id'] = present['.id']
#             self.SET.append( difference )
# 
# 
#     def compare(self, wanted, present):
# 
#         fillval = CmdPathRow(dict())
#         for wanted_rule, present_rule in zip_longest(wanted, present, fillvalue=fillval):
#             diff = wanted_rule - present_rule
#             self.decide(wanted_rule, diff, present_rule)
# 
#         return tuple(self.ADD), tuple(self.SET), tuple(self.DEL)
# 
# Path: mcm/datastructures.py
# class CmdPathRow(dict):
# 
# 
#     def __str__(self):
#         bool_map = {True:'yes', False:'no', None:'""', '':'""'}
#         return ' '.join('{}={}'.format(key, bool_map.get(value, value)) for key, value in self.items())
# 
# 
#     def __hash__(self):
#         return hash(tuple(self.items()))
# 
# 
#     def __sub__(self, other):
#         '''Return new instance with key,valie pairs from self not listed in other.'''
# 
#         diff = dict(set(self.items()) - set(other.items()))
#         return CmdPathRow( diff )
# 
# 
#     def isunique(self, other, keys):
#         '''Test whether every key,value pair (from keys) is in other.'''
# 
#         pairs = set( (key,self[key]) for key in keys )
#         return pairs <= set(other.items())


import pytest
from mcm.comparators import UniqueKeyComparator, SingleElementComparator, OrderedComparator
from mcm.datastructures import CmdPathRow
# -*- coding: UTF-8 -*-




@pytest.fixture
def compare_data(request):
    single = {
        'wanted':CmdPathRow({"primary-ntp":"1.1.1.1"}),
        'present':CmdPathRow({"primary-ntp":"213.222.193.35"}),
        'difference':CmdPathRow({"primary-ntp":"1.1.1.1"}),
    }
    default = {
        'wanted':CmdPathRow({'name':'admin', 'group':'read'}),
        'present':CmdPathRow({'name':'admin', 'group':'full', '.id':'*2'}),
        'extra':CmdPathRow({'name':'operator', 'group':'read', '.id':'*3'}),

# Path: mcm/comparators.py
# class UniqueKeyComparator:
# 
# 
#     def __init__(self, keys):
# 
#         self.keys = keys
#         self.SET = []
#         self.ADD = []
#         self.NO_DELETE = []
# 
# 
#     def compare(self, wanted, present):
# 
#         for wanted_rule in wanted:
#             present_rule = self.findPair(searched=wanted_rule, present=present)
#             diff = wanted_rule - present_rule
#             self.decide(wanted_rule, diff, present_rule)
# 
#         DEL = set(present) - set(self.NO_DELETE)
#         return tuple(self.ADD), tuple(self.SET), tuple(DEL)
# 
# 
#     def decide(self, wanted, difference, present):
# 
#         if wanted and difference and not present:
#             self.ADD.append( wanted )
#         elif wanted and difference and present:
#             self.NO_DELETE.append(present)
#             difference['.id'] = present['.id']
#             self.SET.append( difference )
#         elif wanted and not difference and present:
#             self.NO_DELETE.append(present)
# 
# 
#     def findPair(self, searched, present):
# 
#         for row in present:
#             if row.isunique(other=searched, keys=self.keys):
#                 return row
#         else:
#             return CmdPathRow(dict())
# 
# class SingleElementComparator:
# 
# 
#     def compare(self, wanted, present):
#         SET = tuple()
#         for wanted_row, present_row in zip(wanted, present):
#             diff = wanted_row - present_row
#             SET = (diff,) if diff else tuple()
# 
#         return tuple(), SET, tuple()
# 
# class OrderedComparator:
# 
# 
#     def __init__(self):
# 
#         self.DEL = []
#         self.SET= []
#         self.ADD = []
# 
# 
#     def decide(self, wanted, difference, present):
# 
#         if wanted and difference and not present:
#             self.ADD.append( wanted )
#         elif not wanted and not difference and present:
#             self.DEL.append( present )
#         elif wanted and difference and present:
#             difference['.id'] = present['.id']
#             self.SET.append( difference )
# 
# 
#     def compare(self, wanted, present):
# 
#         fillval = CmdPathRow(dict())
#         for wanted_rule, present_rule in zip_longest(wanted, present, fillvalue=fillval):
#             diff = wanted_rule - present_rule
#             self.decide(wanted_rule, diff, present_rule)
# 
#         return tuple(self.ADD), tuple(self.SET), tuple(self.DEL)
# 
# Path: mcm/datastructures.py
# class CmdPathRow(dict):
# 
# 
#     def __str__(self):
#         bool_map = {True:'yes', False:'no', None:'""', '':'""'}
#         return ' '.join('{}={}'.format(key, bool_map.get(value, value)) for key, value in self.items())
# 
# 
#     def __hash__(self):
#         return hash(tuple(self.items()))
# 
# 
#     def __sub__(self, other):
#         '''Return new instance with key,valie pairs from self not listed in other.'''
# 
#         diff = dict(set(self.items()) - set(other.items()))
#         return CmdPathRow( diff )
# 
# 
#     def isunique(self, other, keys):
#         '''Test whether every key,value pair (from keys) is in other.'''
# 
#         pairs = set( (key,self[key]) for key in keys )
#         return pairs <= set(other.items())


# Path: tests/integration/test_compare.py
import pytest
from mcm.comparators import UniqueKeyComparator, SingleElementComparator, OrderedComparator
from mcm.datastructures import CmdPathRow

# -*- coding: UTF-8 -*-




@pytest.fixture
def compare_data(request):
    single = {
        'wanted':CmdPathRow({"primary-ntp":"1.1.1.1"}),
        'present':CmdPathRow({"primary-ntp":"213.222.193.35"}),
        'difference':CmdPathRow({"primary-ntp":"1.1.1.1"}),
    }
    default = {
        'wanted':CmdPathRow({'name':'admin', 'group':'read'}),
        'present':CmdPathRow({'name':'admin', 'group':'full', '.id':'*2'}),
        'extra':CmdPathRow({'name':'operator', 'group':'read', '.id':'*3'}),

        'difference':CmdPathRow({'group':'read', '.id':'*2'}),