# Path: alerts/events/factories.py
# class DAppFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.DApp
# 
#     name = factory.Sequence(lambda n: faker.name())
#     authentication_code = factory.Sequence(lambda n: randomSHA256())
#     user = factory.SubFactory(UserFactory)
# 
# class UserFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.User
# 
#     email = factory.Sequence(lambda n: faker.email())
# 
# class EventFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.Event
# 
#     name = faker.name()
#     # alert = factory.SubFactory(AlertFactory)
# 
#     @factory.LazyAttribute
#     def alert(self):
#         return AlertFactory()
# 
# class EventValueFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.EventValue
# 
#     property = faker.name()
#     value = faker.name()
#     event = factory.SubFactory(EventFactory)
# 
# class AlertFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.Alert
# 
#     dapp = factory.SubFactory(DAppFactory)
#     contract = factory.Sequence(lambda n: '0x{:040d}'.format(n))
# 
#     @factory.LazyAttribute
#     def abi(self):
#         abi_json = '[{"inputs": [{"type": "address", "name": ""}], "constant": true, "name": "isInstantiation",' \
#             '"payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"},' \
#             '{"inputs": [{"type": "address[]", "name": "_owners"}, {"type": "uint256", "name": "_required"},' \
#             '{"type": "uint256", "name": "_dailyLimit"}], "constant": false, "name": "create", "payable": false,' \
#             '"outputs": [{"type": "address", "name": "wallet"}], "type": "function"},' \
#             '{"inputs": [{"type": "address", "name": ""}, {"type": "uint256", "name": ""}],' \
#             '"constant": true, "name": "instantiations", "payable": false, "outputs":' \
#             '[{"type": "address", "name": ""}], "type": "function"}, {"inputs": [{"type":' \
#             '"address", "name": "creator"}], "constant": true, "name": "getInstantiationCount",' \
#             '"payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"},' \
#             '{"inputs": [{"indexed": false, "type": "address", "name": "sender"}, {"indexed": false,' \
#             '"type": "address", "name": "instantiation"}], "type": "event", "name": "ContractInstantiation",' \
#             '"anonymous": false}]'
# 
#         return abi_json


from django.test import TestCase
from events.models import (User, DApp, EventValue, Event, Alert)
from alerts.events.factories import (DAppFactory, UserFactory, EventFactory, EventValueFactory, AlertFactory)
from django.db import IntegrityError
import random
import hashlib
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


class TestEvent(TestCase):

    def test_create_dapp(self):
        dapp = DAppFactory()
        dapp_obj = DApp.objects.get(name=dapp.name)
        self.assertEquals(dapp.name, dapp_obj.name)
        self.assertEquals(dapp.authentication_code, dapp_obj.authentication_code)
        self.assertRaises(DApp.DoesNotExist, DApp.objects.get, name='giacomo_fake_name')

        with self.assertRaises(IntegrityError): # duplicate key error
            dapp_duplicate = DApp()
            dapp_duplicate.user = dapp_obj.user
            dapp_duplicate.authentication_code = dapp_obj.authentication_code
            dapp_duplicate.name = dapp_obj.name
            dapp_duplicate.save()

    def test_create_user(self):

# Path: alerts/events/factories.py
# class DAppFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.DApp
# 
#     name = factory.Sequence(lambda n: faker.name())
#     authentication_code = factory.Sequence(lambda n: randomSHA256())
#     user = factory.SubFactory(UserFactory)
# 
# class UserFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.User
# 
#     email = factory.Sequence(lambda n: faker.email())
# 
# class EventFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.Event
# 
#     name = faker.name()
#     # alert = factory.SubFactory(AlertFactory)
# 
#     @factory.LazyAttribute
#     def alert(self):
#         return AlertFactory()
# 
# class EventValueFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.EventValue
# 
#     property = faker.name()
#     value = faker.name()
#     event = factory.SubFactory(EventFactory)
# 
# class AlertFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = models.Alert
# 
#     dapp = factory.SubFactory(DAppFactory)
#     contract = factory.Sequence(lambda n: '0x{:040d}'.format(n))
# 
#     @factory.LazyAttribute
#     def abi(self):
#         abi_json = '[{"inputs": [{"type": "address", "name": ""}], "constant": true, "name": "isInstantiation",' \
#             '"payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"},' \
#             '{"inputs": [{"type": "address[]", "name": "_owners"}, {"type": "uint256", "name": "_required"},' \
#             '{"type": "uint256", "name": "_dailyLimit"}], "constant": false, "name": "create", "payable": false,' \
#             '"outputs": [{"type": "address", "name": "wallet"}], "type": "function"},' \
#             '{"inputs": [{"type": "address", "name": ""}, {"type": "uint256", "name": ""}],' \
#             '"constant": true, "name": "instantiations", "payable": false, "outputs":' \
#             '[{"type": "address", "name": ""}], "type": "function"}, {"inputs": [{"type":' \
#             '"address", "name": "creator"}], "constant": true, "name": "getInstantiationCount",' \
#             '"payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"},' \
#             '{"inputs": [{"indexed": false, "type": "address", "name": "sender"}, {"indexed": false,' \
#             '"type": "address", "name": "instantiation"}], "type": "event", "name": "ContractInstantiation",' \
#             '"anonymous": false}]'
# 
#         return abi_json


# Path: alerts/events/tests/test_models.py
from django.test import TestCase
from events.models import (User, DApp, EventValue, Event, Alert)
from alerts.events.factories import (DAppFactory, UserFactory, EventFactory, EventValueFactory, AlertFactory)
from django.db import IntegrityError
import random
import hashlib

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


class TestEvent(TestCase):

    def test_create_dapp(self):
        dapp = DAppFactory()
        dapp_obj = DApp.objects.get(name=dapp.name)
        self.assertEquals(dapp.name, dapp_obj.name)
        self.assertEquals(dapp.authentication_code, dapp_obj.authentication_code)
        self.assertRaises(DApp.DoesNotExist, DApp.objects.get, name='giacomo_fake_name')

        with self.assertRaises(IntegrityError): # duplicate key error
            dapp_duplicate = DApp()
            dapp_duplicate.user = dapp_obj.user
            dapp_duplicate.authentication_code = dapp_obj.authentication_code
            dapp_duplicate.name = dapp_obj.name
            dapp_duplicate.save()

    def test_create_user(self):

        dapp = DAppFactory()