<commit_before><commit_msg>Add tests for RESTful server<commit_after>import datetime

from flask.ext.testing import TestCase
import mongomock
import mock

from server import app, get_db

class BaseTest(TestCase):
    def create_app(self):
        app.config['TESTING'] = True
        return app

    def setUp(self):
        self.mongo_patcher = mock.patch('pymongo.MongoClient', mongomock.MongoClient)
        self.mongo_patcher.start()

    def tearDown(self):
        self.mongo_patcher.stop()


class TestGeoNotesApi(BaseTest):
    NOTE1 = {
        'txt': "test",
        'lat': 0,
        'lng': 0,
        'dt': datetime.datetime(2012, 11, 10, 0, 0),
    }

    NOTE2 = {
        'txt': "test",
        'lat': 0,
        'lng': 0,
        'dt': datetime.datetime(2012, 11, 10, 0, 0),
    }

    def test_get_empty(self):
        resp = self.client.get("/geonotes")
        self.assert200(resp)
        self.assertEquals(resp.json, [])

    def test_get(self):
        _id = get_db().insert(self.NOTE1)
        resp = self.client.get("/geonotes")
        self.assert200(resp)
        self.assertEquals(len(resp.json), 1)
        self.assertEqual(resp.json[0]['id'], str(_id))

    def test_get_several(self):
        _id1 = get_db().insert(self.NOTE1)
        _id2 = get_db().insert(self.NOTE2)
        resp = self.client.get("/geonotes")
        self.assert200(resp)
        self.assertEquals(len(resp.json), 2)

    def test_post_one_error(self):
        resp = self.client.post("/geonotes", {})
        self.assert400(resp)

        resp = self.client.post("/geonotes", {'lat': 9})
        self.assert400(resp)

    def test_post_one(self):
        to_post = {
            'text_content': 'test',
            'lat': 5,
            'lng': 9,
            'date': datetime.datetime(2013, 11, 10, 0, 0).isoformat()
        }
        resp = self.client.post("/geonotes", data=to_post)
        self.assertStatus(resp, 201)
        self.assertTrue('id' in resp.json)


class TestGeoNoteApi(BaseTest):
    NOTE1 = {
        'txt': "test",
        'lat': 0,
        'lng': 0,
        'dt': datetime.datetime(2012, 11, 10, 0, 0),
    }

    def test_get(self):
        id_ = get_db().insert(self.NOTE1)
        resp = self.client.get('/geonotes/%s' % id_)
        self.assert200(resp)
        self.assertEqual(resp.json['id'], str(id_))


{'fraction_non_alphanumeric': 0.1514762516046213, 'fraction_numerical': 0.03508771929824561, 'mean_word_length': 2.2745098039215685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572163', 'n_tokens_mistral': 864, 'n_tokens_neox': 778, 'n_words': 150}