# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from physical.tests.factory import InstanceFactory


class UsedAndTotalValidator(object):

    @staticmethod
    def assertEqual(a, b):
        assert a == b, "{} NOT EQUAL {}".format(a, b)

    @classmethod
    def instances_sizes(cls, instances=None, expected_used_size=40,
                        expected_total_size=90):
        for instance in instances:
            cls.assertEqual(instance.used_size_in_bytes, expected_used_size)
            cls.assertEqual(instance.total_size_in_bytes, expected_total_size)


class InstanceHelper(object):

    model = InstanceFactory.FACTORY_FOR
    quantity_of_masters = 1

    @classmethod
    def kill_instances(cls, instances):
        for instance in instances:
            instance.status = cls.model.DEAD
            instance.save()

    @staticmethod
    def change_instances_type(instances, instance_type):
        for instance in instances:
            instance.instance_type = instance_type
            instance.save()

    @staticmethod
    def check_instance_is_master(instance, default_timeout=False):
        """
            Method for mock the real check_instance_is_master.
        """

        quantity_of_masters = instance.databaseinfra.instances.count() / 2

        return instance.id in (instance.databaseinfra.instances.values_list(
                               'id', flat=True)[quantity_of_masters:])

    @staticmethod
    def create_instances_by_quant(infra, port=3306, qt=1,
                                  total_size_in_bytes=50,
                                  used_size_in_bytes=25, instance_type=1,
                                  base_address='127', hostname=None):
        """
            Helper create instances by quantity
        """
        def _create(n):
            extra_params = dict(**{'hostname': hostname} if hostname else {})
            return InstanceFactory(
                databaseinfra=infra,
                address='{0}.7{1}.{2}.{2}'.format(
                    base_address, infra.id, n
                ),
                port=port,
                instance_type=instance_type,
                total_size_in_bytes=total_size_in_bytes,
                used_size_in_bytes=used_size_in_bytes,
                **extra_params
            )

        return map(_create, range(1, qt + 1))

@@ -73,16 +73,50 @@
 literals
+%0Afrom mock import patch, MagicMock
 %0A%0Afrom p
@@ -160,16 +160,46 @@
 Factory%0A
+from model_mommy import mommy%0A
 %0A%0Aclass 
@@ -2382,16 +2382,16 @@
      )%0A%0A
-
         
@@ -2428,8 +2428,2152 @@
 t + 1))%0A
+%0A%0Aclass DatabaseHelper(object):%0A    @staticmethod%0A    @patch('logical.models.Database.automatic_create_first_credential',%0A           MagicMock())%0A    def create(**kwargs):%0A        if 'databaseinfra' not in kwargs:%0A            kwargs%5B'databaseinfra'%5D = InfraHelper.create()%0A%0A        driver = kwargs%5B'databaseinfra'%5D.get_driver()%0A        module_path = %22%7B%7D.%7B%7D.create_database%22.format(%0A            driver.__class__.__module__,%0A            driver.__class__.__name__%0A        )%0A        with patch(module_path, MagicMock()):%0A            return mommy.make(%0A                'Database', **kwargs%0A            )%0A%0A%0Aclass PlanHelper(object):%0A    engine_map = %7B%0A        'mysql_single': %7B%0A            'class_path': 'drivers.replication_topologies.mysql.MySQLSingle',%0A            'name': 'MySQL Single 5.7.25'%0A        %7D%0A    %7D%0A    @classmethod%0A    def create(cls, engine, *kwargs):%0A        %22%22%22%0A            Engine must be: %60NAME%60_%60TOPOLOGY_TYPE%60%0A            Ex. mysql_single.The name of engine will be mysql and mysql_single%0A            will be used to get topology class_path and name. See %60engine_map%60%0A            class variable%0A        %22%22%22%0A        if engine not in cls.engine_map:%0A            raise Exception(%22Engine not mapped. Mapped engines are: %7B%7D%22.format(%0A                ', '.join(cls.engine_map.keys())%0A            ))%0A        engine_conf = cls.engine_map%5Bengine%5D%0A        engine_type = mommy.make(%0A            'EngineType', name=engine.split('_')%5B0%5D%0A        )%0A        engine = mommy.make(%0A            'Engine', engine_type=engine_type%0A        )%0A        replication_topology = mommy.make(%0A            'ReplicationTopology',%0A            name=engine_conf%5B'name'%5D,%0A            class_path=engine_conf%5B'class_path'%5D%0A        )%0A        plan = mommy.make(%0A            'Plan', engine=engine,%0A            replication_topology=replication_topology%0A        )%0A        return engine_type, engine, replication_topology, plan%0A%0A%0Aclass InfraHelper(object):%0A    @staticmethod%0A    def create(engine='mysql_single', **kwargs):%0A        if 'plan' not in kwargs:%0A            _, _, _, kwargs%5B'plan'%5D = PlanHelper.create(engine)%0A        return mommy.make('Databaseinfra', **kwargs)%0A

Create DatabaseHelper, InfraHelper and PlanHelper