class TestApi(TestCase): def setUp(self): self.api = AnalyticsApi() def test_api_create_user(self): _ = User.objects.create(username='******') _ = User.objects.create(username='******') staff1 = User.objects.create(username='******', is_staff=True) staff2 = User.objects.create(username='******', is_staff=True) usernames = [str(x) for x in self.api.staff_usernames()] self.assertEqual(usernames, [staff1.username, staff2.username])
class TestMockedApi(TestCase): def setUp(self): self.api = AnalyticsApi() def test_mocked_api_active_users_filters_by_is_active_true(self): active_user = MockModel(mock_name='active user', is_active=True) inactive_user = MockModel(mock_name='inactive user', is_active=False) User.objects.add(*[active_user, inactive_user]) results = [x for x in self.api.active_users()] assert active_user in results assert inactive_user not in results def test_mocked_api_create_user(self): attrs = dict((k, v) for (k, v) in mommy.prepare(User).__dict__.items() if k[0] != '_') user = self.api.create_user(**attrs) assert isinstance(user, User) for k, v in attrs.items(): assert getattr(user, k) == v def test_mocked_api_today_visitors_counts_todays_logins(self): past_visitors = [ MockModel(last_login=(date.today() - timedelta(days=1))), MockModel(last_login=(date.today() - timedelta(days=2))), MockModel(last_login=(date.today() - timedelta(days=3))), ] today_visitors = [ MockModel(last_login=date.today()), MockModel(last_login=date.today()) ] User.objects.add(*(past_visitors + today_visitors)) count = self.api.today_visitors_count() assert count == len(today_visitors) assert User.objects.filter( last_login__year__lte=date.today().year).exists() is True assert User.objects.filter(last_login__year__gt=date.today().year + 1).exists() is False
class TestMockedApi(TestCase): def setUp(self): self.api = AnalyticsApi() def test_api_active_users_filters_by_is_active_true(self): active_user = MockModel(mock_name='active user', is_active=True) inactive_user = MockModel(mock_name='inactive user', is_active=False) User.objects.add(*[active_user, inactive_user]) results = [x for x in self.api.active_users()] assert active_user in results assert inactive_user not in results def test_api_create_user(self): attrs = dict((k, v) for (k, v) in mommy.prepare(User).__dict__.items() if k[0] != '_') user = self.api.create_user(**attrs) assert isinstance(user, User) for k, v in attrs.items(): assert getattr(user, k) == v # noinspection PyUnresolvedReferences assert User.save.call_count == 1 def test_api_today_visitors_counts_todays_logins(self): past_visitors = [ MockModel(last_login=(date.today() - timedelta(days=1))), MockModel(last_login=(date.today() - timedelta(days=2))), MockModel(last_login=(date.today() - timedelta(days=3))), ] today_visitors = [ MockModel(last_login=date.today()), MockModel(last_login=date.today()) ] User.objects.add(*(past_visitors + today_visitors)) count = self.api.today_visitors_count() assert count == len(today_visitors)
def setUp(self): self.api = AnalyticsApi()
from django.http import HttpResponse from analytics.api import AnalyticsApi api = AnalyticsApi() def active_users(*args, **kwargs): return HttpResponse(', '.join( [x.username for x in api.active_users()] ))