# Path: privagal/core/factories.py
# class TokenFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = Token
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


from django.test import TestCase
from ..factories import TokenFactory
from ..models import Token
# -*- coding: utf-8 -*-
from __future__ import unicode_literals




class TokenManagerTestCase(TestCase):

    def setUp(self):
        Token.objects.all().delete()
        Token.objects.reset_singleton()

    def test_no_token(self):
        self.assertIsNone(Token.objects.singleton())

    def test_singleton_cache_with_no_token(self):
        """
        Don't try request, even if there is no token.
        """
        self.assertNumQueries(1, func=Token.objects.singleton)
        self.assertNumQueries(0, func=Token.objects.singleton)

    def test_singleton_reset(self):
        """
        Redo request if asked.
        """

# Path: privagal/core/factories.py
# class TokenFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = Token
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


# Path: privagal/core/tests/test_models.py
from django.test import TestCase
from ..factories import TokenFactory
from ..models import Token

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




class TokenManagerTestCase(TestCase):

    def setUp(self):
        Token.objects.all().delete()
        Token.objects.reset_singleton()

    def test_no_token(self):
        self.assertIsNone(Token.objects.singleton())

    def test_singleton_cache_with_no_token(self):
        """
        Don't try request, even if there is no token.
        """
        self.assertNumQueries(1, func=Token.objects.singleton)
        self.assertNumQueries(0, func=Token.objects.singleton)

    def test_singleton_reset(self):
        """
        Redo request if asked.
        """

        self.assertNumQueries(1, func=Token.objects.singleton)