from unittest import TestCase
from webtest import TestApp

from ichnaea import main


def _make_app():
    global_config = {}
    wsgiapp = main(global_config, database='sqlite://')
    return TestApp(wsgiapp)


class TestSearch(TestCase):

    def test_not_found(self):
        app = _make_app()
        res = app.post_json('/v1/search',
            {"cell": [{"mcc": 1, "mnc": 2, "lac": 3, "cid": 4}]})
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.body, '{"status": "not_found"}')


class TestMeasure(TestCase):

    def test_ok(self):
        app = _make_app()
        res = app.post_json('/v1/location/12.345678/23.456789', {})
        self.assertEqual(res.status_code, 204)
        self.assertEqual(res.body, '')


class TestHeartbeat(TestCase):

    def test_ok(self):
        app = _make_app()
        res = app.get('/__heartbeat__')
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.body, '{"status": "OK"}')

@@ -77,16 +77,44 @@
 rt main%0A
+from ichnaea.db import Cell%0A
 %0A%0Adef _m
@@ -254,32 +254,697 @@
 rch(TestCase):%0A%0A
+    def test_ok(self):%0A        app = _make_app()%0A        session = app.app.registry.db.session()%0A        cell = Cell()%0A        cell.lat = 12345678%0A        cell.lon = 23456789%0A        cell.mcc = 123%0A        cell.mnc = 1%0A        cell.lac = 2%0A        cell.cid = 1234%0A        session.add(cell)%0A        session.commit()%0A%0A        res = app.post_json('/v1/search',%0A            %7B%22cell%22: %5B%7B%22mcc%22: 123, %22mnc%22: 1, %22lac%22: 2, %22cid%22: 1234%7D%5D%7D)%0A        self.assertEqual(res.status_code, 200)%0A        self.assertEqual(res.content_type, 'application/json')%0A        self.assertEqual(res.body, '%7B%22status%22: %22ok%22, %22lat%22: 12.345678, '%0A            '%22lon%22: 23.456789, %22accuracy%22: 20000%7D')%0A%0A
     def test_not

test successful search