def test_student_gets_added(self, insert_merge_task_mock, marker_put_mock):
        """Test a new student is added to the marker."""
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(key=marker_key, students={}, methods=methods[:])
        marker_key.get.return_value = marker

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        contact = {'name': 'jimmy'}

        update_marker(marker_key, student_key, contact.copy(), methods)

        self.assertIn(student_key.id(), marker.students)

        self.assertEqual([contact.copy()], marker.students[student_key.id()])

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)
    def test_methods_get_merged(self, insert_merge_task_mock, marker_put_mock):
        """Test that existing methods don't get duplicated and new methods get
        added.
        """
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(key=marker_key, students={}, methods=methods[:])
        marker_key.get.return_value = marker

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        contact = {'id': 1}

        new_methods = ['abc', '123', '456']

        update_marker(marker_key, student_key, contact, new_methods[:])

        all_methods = sorted(set(new_methods) | set(methods))

        self.assertEqual(all_methods, sorted(marker.methods))

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)
Beispiel #3
0
    def test_contact_gets_added_to_student(self, insert_merge_task_mock,
                                           marker_put_mock):
        """Test that new contact is added to the existing student."""
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        student_contacts = {'name': 'Joe Blow'}

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(
            key=marker_key,
            students={student_key.id(): [student_contacts.copy()]},
            methods=methods[:])
        marker_key.get.return_value = marker

        contact = {'name': 'Some Dude'}

        update_marker(marker_key, student_key, contact.copy(), methods)

        self.assertIn(student_key.id(), marker.students)

        self.assertIn(contact.copy(), marker.students[student_key.id()])

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)
Beispiel #4
0
    def test_student_gets_added(self, insert_merge_task_mock, marker_put_mock):
        """Test a new student is added to the marker."""
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(key=marker_key, students={}, methods=methods[:])
        marker_key.get.return_value = marker

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        contact = {'name': 'jimmy'}

        update_marker(marker_key, student_key, contact.copy(), methods)

        self.assertIn(student_key.id(), marker.students)

        self.assertEqual([contact.copy()], marker.students[student_key.id()])

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)
Beispiel #5
0
    def test_methods_get_merged(self, insert_merge_task_mock, marker_put_mock):
        """Test that existing methods don't get duplicated and new methods get
        added.
        """
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(key=marker_key, students={}, methods=methods[:])
        marker_key.get.return_value = marker

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        contact = {'id': 1}

        new_methods = ['abc', '123', '456']

        update_marker(marker_key, student_key, contact, new_methods[:])

        all_methods = sorted(set(new_methods) | set(methods))

        self.assertEqual(all_methods, sorted(marker.methods))

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)
Beispiel #6
0
    def post(self):
        import json

        from google.appengine.api import namespace_manager

        marker_urlsafe = self.request.get('marker')
        if not marker_urlsafe:
            logging.error('No marker key given.')
            return

        # TODO: Use message id rather than key here for namespacing purposes?
        marker_key = ndb.Key(urlsafe=marker_urlsafe)

        # TODO: Check namespace here.
        current_namespae = unicode(namespace_manager.get_namespace())
        if marker_key.namespace() != current_namespae:
            logging.error('Marker %s not in namespace %s!',
                          marker_key, current_namespae)
            return

        marker = marker_key.get()
        if not marker:
            logging.error('Marker %s not found!', marker_key)
            return

        student_urlsafe = self.request.get('student')
        if not student_urlsafe:
            logging.error('No student key given.')
            return

        # TODO: Use student id rather than key here for namespacing purposes?
        student_key = ndb.Key(urlsafe=student_urlsafe)

        contact = self.request.get('contact')
        if not contact:
            logging.error('No contact given.')
            return
        contact = json.loads(contact)

        methods = self.request.get('methods')
        if not methods:
            logging.error('No methods given.')
            return
        methods = json.loads(methods)

        update_marker(marker_key, student_key, contact, methods)
    def test_contact_gets_added_to_student(self, insert_merge_task_mock,
                                           marker_put_mock):
        """Test that new contact is added to the existing student."""
        from google.appengine.ext import ndb

        from sosbeacon.event.contact_marker import ContactMarker
        from sosbeacon.event.contact_marker import update_marker

        methods = ['abc']

        student_key = mock.Mock(spec=ndb.Key)
        student_key.id.return_value = "STUDENTKEY"

        student_contacts = {
            'name': 'Joe Blow'
        }

        marker_key = mock.Mock(spec=ndb.Key)
        marker_key.kind.return_value = "ContactMarker"

        marker = ContactMarker(
            key=marker_key,
            students={
                student_key.id(): [student_contacts.copy()]
            },
            methods=methods[:])
        marker_key.get.return_value = marker

        contact = {'name': 'Some Dude'}

        update_marker(marker_key, student_key, contact.copy(), methods)

        self.assertIn(student_key.id(), marker.students)

        self.assertIn(contact.copy(), marker.students[student_key.id()])

        self.assertTrue(marker_put_mock.called)

        self.assertEqual(1, insert_merge_task_mock.call_count)