import codecs
import json
import os
from smoke_signal import app, init_db
import unittest
import tempfile

SAMPLE_RSS = "file://" + app.root_path + "/test_resources/sample_rss.xml"


def get_json(response):
    return json.loads(codecs.decode(response.get_data(), 'utf-8'))


class SmokeSignalTestCase(unittest.TestCase):
    def setUp(self):
        self.db_fd, self.db_path = tempfile.mkstemp()
        app.config['DATABASE_PATH'] = 'sqlite:///' + self.db_path
        app.config['TESTING'] = True
        self.app = app.test_client()
        with app.app_context():
            init_db()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(self.db_path)

    def test_alive(self):
        resp = self.app.get('/')
        assert resp.status_code == 200

    def test_empty_feedlist(self):
        resp = self.app.get('/feeds/')
        assert resp.status_code == 204
        resp = self.app.get('/feeds/1')
        assert resp.status_code == 404

    def add_feed(self, url):
        return self.app.post('/feeds/', data=json.dumps({'url': url}),
                             content_type='application/json')

    def test_add_invalid_feed(self):
        resp = self.add_feed("http://example.com")
        assert resp.status_code == 404

    def test_add_valid_feed(self):
        resp = self.add_feed(SAMPLE_RSS)
        assert resp.status_code == 200
        data = get_json(resp)
        assert data["url"] == SAMPLE_RSS
        return data

    def test_add_and_get_feed_list(self):
        feed = self.test_add_valid_feed()
        resp = self.app.get("/feeds/")
        assert resp.status_code == 200
        feed_list = get_json(resp)
        assert feed in feed_list

    def test_add_and_get_feed(self):
        feed = self.test_add_valid_feed()
        resp = self.app.get("/feeds/{}".format(feed["id"]))
        assert resp.status_code == 200
        entry_list = get_json(resp)
        assert entry_list != []
        return entry_list

    def test_mark_entry_as_read(self):
        entry_list = self.test_add_and_get_feed()
        entry = entry_list[0]
        assert entry["feed_id"] is not None
        assert entry["entry_id"] is not None
        resp = self.app.post("/feeds/{}/read/{}".format(entry["feed_id"],
                                                        entry["entry_id"]))
        assert resp.status_code == 200
        entry = get_json(resp)
        assert entry["read"]

if __name__ == "__main__":
    unittest.main()

@@ -793,16 +793,17 @@
 pty_feed
+_
 list(sel
@@ -972,16 +972,17 @@
     def 
+_
 add_feed
@@ -1140,19 +1140,13 @@
 def 
-test_add_in
+_get_
 vali
@@ -1171,32 +1171,33 @@
     resp = self.
+_
 add_feed(%22http:/
@@ -1193,68 +1193,49 @@
 eed(
-%22http://example.com%22)%0A        assert resp.status_code == 404
+SAMPLE_RSS)%0A        return get_json(resp)
 %0A%0A  
@@ -1241,32 +1241,34 @@
    def test_add_
+in
 valid_feed(self)
@@ -1289,16 +1289,17 @@
  = self.
+_
 add_feed
@@ -1295,34 +1295,44 @@
 f._add_feed(
-SAMPLE_RSS
+%22http://example.com%22
 )%0A        as
@@ -1360,40 +1360,84 @@
  == 
-200%0A        data = get_json(resp
+404%0A%0A    def test_add_valid_feed(self):%0A        data = self._get_valid_feed(
 )%0A  
@@ -1554,32 +1554,28 @@
 feed = self.
-test_add
+_get
 _valid_feed(
@@ -1731,37 +1731,37 @@
     def 
-test_add_and_get_feed
+_get_entries_response
 (self):%0A
@@ -1780,24 +1780,20 @@
  = self.
-test_add
+_get
 _valid_f
@@ -1800,36 +1800,36 @@
 eed()%0A        re
-sp =
+turn
  self.app.get(%22/
@@ -1862,46 +1862,218 @@
 %5D))%0A
-        assert resp.status_code == 200
+%0A    def test_add_and_get_feed(self):%0A        resp = self._get_entries_response()%0A        assert resp.status_code == 200%0A%0A    def test_add_feed_and_get_entries(self):%0A        resp = self._get_entries_response()
 %0A   
@@ -2140,34 +2140,8 @@
 = %5B%5D
-%0A        return entry_list
 %0A%0A  
@@ -2202,35 +2202,45 @@
 t = 
-self.test_add_and_get_feed(
+get_json(self._get_entries_response()
 )%0A  
@@ -2606,16 +2606,543 @@
 read%22%5D%0A%0A
+    def _get_read_entry(self):%0A        entry_list = get_json(self._get_entries_response())%0A        entry = entry_list%5B0%5D%0A        resp = self.app.post(%22/feeds/%7B%7D/read/%7B%7D%22.format(entry%5B%22feed_id%22%5D,%0A                                                        entry%5B%22entry_id%22%5D))%0A        return get_json(resp)%0A%0A    def test_read_entries(self):%0A        read_entry = self._get_read_entry()%0A        resp = self.app.get(%22/feeds/%7B%7D%22.format(read_entry%5B%22feed_id%22%5D))%0A        entry_list = get_json(resp)%0A        assert read_entry in entry_list%0A%0A
 if __nam

Add a few more tests