from django.utils import unittest
from nose.tools import eq_, ok_
from mock import patch, Mock
from django.contrib.gis import geos
import datetime

from make_mozilla.events import models

class VenueTest(unittest.TestCase):
    def test_location_is_always_initialized_as_a_point_object(self):
        venue = models.Venue()
        self.assertIsNotNone(venue.location.wkt)

    def test_latitude_can_be_set(self):
        venue = models.Venue()
        venue.latitude = 51.456

        eq_(venue.location, geos.Point(0, 51.456))
    
    def test_latitude_can_be_gotten(self):
        venue = models.Venue(location = geos.Point(42, 24))

        eq_(venue.latitude, 24.0)

    def test_longitude_can_be_gotten(self):
        venue = models.Venue(location = geos.Point(42, 24))

        eq_(venue.longitude, 42.0)

    def test_longitude_can_be_set(self):
        venue = models.Venue()
        venue.longitude = 51.456

        eq_(venue.location, geos.Point(51.456, 0))

class EventTest(unittest.TestCase):
    @patch.object(models.Event.objects, 'filter')
    def test_upcoming_events_can_be_retrieved(self, mock_query_set):
        mock_item = Mock()
        mock_query_set.return_value = [mock_item]
        mock_now = Mock()
        with patch('make_mozilla.events.models.datetime') as mock_datetime:
            mock_datetime.now.return_value = mock_now

            eq_(models.Event.upcoming(), [mock_item])

            mock_query_set.assert_called_with(start__gte = mock_now)

    # TO DO - do this with real DB objects...
    # @patch.object(models, 'Point')
    # @patch.object(models.Event.objects, 'filter')
    # def test_upcoming_events_can_be_retrieved(self, mock_query_set):
    #     pass

@@ -27,16 +27,35 @@
 nittest%0A
+import django.test%0A
 from nos
@@ -996,28 +996,31 @@
 s EventTest(
-unit
+django.
 test.TestCas
@@ -1031,622 +1031,1188 @@
     
-@patch.object(models.Event.objects, 'filter')%0A    def test_upcoming_events_can_be_retrieved(self, mock_query_set):%0A        mock_item = Mock()%0A        mock_query_set.return_value = %5Bmock_item%5D%0A        mock_now = Mock()%0A        with patch('make_mozilla.events.models.datetime') as mock_datetime:%0A            mock_datetime.now.return_value
+def setup_events(self):%0A        london = models.Venue(name = %22Test Venue%22, street_address = %220 Somewhere St%22, %0A                country = %22UK%22)%0A        london.latitude = 51.510345%0A        london.longitude = -0.127072%0A        london.save()%0A        berlin = models.Venue(name = %22Berlin test Venue%22, street_address = %22Somewhere Str. 0%22, %0A                country = %22Germany%22)%0A        berlin.latitude = 52.50693980%0A        berlin.longitude = 13.42415920%0A        berlin.save()%0A%0A        start = datetime.datetime.now() + datetime.timedelta(days = 1)%0A        end = start + datetime.timedelta(hours = 3)%0A%0A        e1 = models.Event(name = %22E1%22, venue = london, organiser_email
  = 
+'
 mo
-ck_now%0A%0A            eq_(models.Event.upcoming(), %5Bmock_item%5D)%0A%0A            mock_query_set.assert_called_with(start__gte = mock_now)%0A%0A    # TO DO - do this with real DB objects...%0A    # @patch.object(models, 'Point')%0A    # @patch.object(models.Event.objects, 'filter')
+z@example.com',%0A                start = start, end = end)%0A        e1.save()%0A        e2 = models.Event(name = %22E2%22, venue = berlin, organiser_email = 'moz@example.com',%0A                start = start, end = end)%0A        e2.save()%0A%0A        return (e1, e2)%0A%0A    def test_upcoming_events_can_be_retrieved(self):%0A        (e1, e2) = self.setup_events()%0A        actual = models.Event.upcoming()%0A        eq_(len(actual), 2)%0A        ok_(e1.id in %5Bx.id for x in actual%5D)%0A        ok_(e2.id in %5Bx.id for x in actual%5D)%0A
 %0A   
- #
  def
@@ -2225,32 +2225,44 @@
 upcoming_events_
+near_london_
 can_be_retrieved
@@ -2270,38 +2270,167 @@
 self
-, mock_query_set):
+):%0A        (e1, e2) = self.setup_events()%0A        actual = models.Event.near(51.5154460, -0.13165810)%0A        eq_(len(actual), 1)
 %0A    
-#
     
- pass
+eq_(actual%5B0%5D.id, e1.id)
 %0A

Verify upcoming and nearest with integrated DB tests