# Path: models/alias.py
# class Alias(ndb.Model):
#     """Models an instance of an alias."""
#     name = ndb.StringProperty(required=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
# 
# Path: testing/factories/alias.py
# def create_alias_with_employee_username(
#     name='jda',
#     username='jd',
# ):
#     alias = Alias(
#         name=name,
#         owner_key=Employee.get_key_for_username(username),
#     )
#     alias.put()
#     return alias
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })


import unittest
import logic.alias
from models import Alias
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee




class AliasTest(unittest.TestCase):
    nosegae_datastore_v3 = True

    def test_get_alias(self):
        create_employee(username='fuz')
        create_alias_with_employee_username(name='fuzzi', username='fuz')

        self.assertIsNotNone(logic.alias.get_alias('fuzzi'))

    def test_save_alias(self):
        johnd = create_employee(username='johnd')
        self.assertEqual(Alias.query().count(), 0)

        alias = logic.alias.save_alias('johnny', 'johnd')

        self.assertEqual(Alias.query().count(), 1)
        self.assertEqual(alias.name, 'johnny')
        self.assertEqual(alias.owner_key, johnd.key)

    def test_delete_alias(self):
        create_employee(username='janed')
        alias = logic.alias.save_alias('jane', 'janed')
        self.assertEqual(Alias.query().count(), 1)

        logic.alias.delete_alias(alias.key.id())


# Path: models/alias.py
# class Alias(ndb.Model):
#     """Models an instance of an alias."""
#     name = ndb.StringProperty(required=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
# 
# Path: testing/factories/alias.py
# def create_alias_with_employee_username(
#     name='jda',
#     username='jd',
# ):
#     alias = Alias(
#         name=name,
#         owner_key=Employee.get_key_for_username(username),
#     )
#     alias.put()
#     return alias
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })


# Path: tests/logic/alias_test.py
import unittest
import logic.alias
from models import Alias
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee





class AliasTest(unittest.TestCase):
    nosegae_datastore_v3 = True

    def test_get_alias(self):
        create_employee(username='fuz')
        create_alias_with_employee_username(name='fuzzi', username='fuz')

        self.assertIsNotNone(logic.alias.get_alias('fuzzi'))

    def test_save_alias(self):
        johnd = create_employee(username='johnd')
        self.assertEqual(Alias.query().count(), 0)

        alias = logic.alias.save_alias('johnny', 'johnd')

        self.assertEqual(Alias.query().count(), 1)
        self.assertEqual(alias.name, 'johnny')
        self.assertEqual(alias.owner_key, johnd.key)

    def test_delete_alias(self):
        create_employee(username='janed')
        alias = logic.alias.save_alias('jane', 'janed')
        self.assertEqual(Alias.query().count(), 1)

        logic.alias.delete_alias(alias.key.id())


        self.assertEqual(Alias.query().count(), 0)