from mock import Mock
import unittest2
from apps.places.models import Source

from molly.apps.places import models

class TestPointsOfInterest(unittest2.TestCase):

    def setUp(self):
        self._mock_mongo = Mock()
        self._pois = models.PointsOfInterest(self._mock_mongo)

    def test_add_adds_to_database(self):
        self._mock_mongo.pois.find_one.return_value = None
        poi = models.PointOfInterest()
        self._pois.add_or_update(poi)
        self._mock_mongo.pois.insert.assert_called_once_with(poi.as_dict())

    def test_add_or_update_checks_for_uri_clashes_before_adding(self):
        self._mock_mongo.pois.find_one.return_value = {'_id': 'abcdef', 'sources': []}
        poi = models.PointOfInterest(uri='/test:test')
        self._pois.add_or_update(poi)
        poi_dict = poi.as_dict()
        poi_dict.update({'_id': 'abcdef'})
        self.assertFalse(self._mock_mongo.pois.insert.called)
        self._mock_mongo.pois.update.assert_called_once_with(poi_dict)

    def test_add_or_update_does_not_update_if_source_has_not_changed(self):
        self._mock_mongo.pois.find_one.return_value = {
            '_id': 'abcdef',
            'sources': [{'url': 'http://www.example.com', 'version': 1, 'attribution': 'OSM'}]
        }

        poi = models.PointOfInterest(
            uri='/test:test', sources=[Source(url='http://www.example.com', version=1, attribution='OSM')]
        )
        self._pois.add_or_update(poi)

        self.assertFalse(self._mock_mongo.pois.insert.called)
        self.assertFalse(self._mock_mongo.pois.update.called)

@@ -35,46 +35,8 @@
 est2
-%0Afrom apps.places.models import Source
 %0A%0Afr
@@ -619,16 +619,29 @@
 alue = %7B
+%0A            
 '_id': '
@@ -661,17 +661,99 @@
 rces': %5B
-%5D
+models.Source(url='http://www.example.com', version=1, attribution='OSM')%5D%0A        
 %7D%0A      
@@ -1354,16 +1354,16 @@
 terest(%0A
-
         
@@ -1393,16 +1393,23 @@
 ources=%5B
+models.
 Source(u

Fix failing unit test