def test_deleting_stale_items(self):
		old_1 = fake_item(google_id = 'old_1')
		old_2 = fake_item(google_id = 'old_2')
		new_1 = fake_item(google_id = 'new_1')
		new_2 = fake_item(google_id = 'new_2')
		new_3 = fake_item(google_id = 'new_3')
		
		old_items = [old_1, old_2]
		new_items = [new_1, new_2, new_3]
		all_items = old_items + new_items

		for the_item in all_items:
			self.db.add_item(the_item)
		
		self.db.prepare_for_download()
		
		# simulate getting of the new items again:
		for item_id in google_ids(new_items):
			self.db.update_content_for_item(self.db.get(item_id))
		
		self.assertEqual(sorted(google_ids(self.db.get_items_list())), sorted(google_ids(all_items)))

		self.db.cleanup_stale_items()
		print google_ids(self.db.get_items_list())
		
		self.assertEqual(sorted(google_ids(self.db.get_items_list())), sorted(google_ids(new_items)))
	def test_updating_item_contents(self):
		assert self.db.get('sample_id') == None
		item = fake_item()
		self.db.add_item(fake_item())
		item = fake_item(content='content2', tag_name='tagname2')
		self.db.update_content_for_item(item)
		self.assertEqual(1 , len(self.db.get_items_list('is_stale = 0 and google_id = \'sample_id\'')))
		item = list(self.db.get_items('google_id = "sample_id"'))[0]
		self.assertEqual(item.content, 'content2')
		self.assertEqual(item.tag_name, 'tagname2')
	def test_deleting_an_item(self):
		a = fake_item(google_id = 'a')
		b = fake_item(google_id = 'b')
		self.db.add_item(a)
		self.db.add_item(b)
		items = list(self.db.get_items())
		assert len(items) == 2
		
		# now remove it
		self.db.remove_item(a)
		items = list(self.db.get_items())
		assert len(items) == 1
		assert items[0].google_id == 'b'
	def test_google_sync_failures(self):
		self.db.add_item(fake_item(google_id = 'b', is_read = True, is_dirty = True))
		app_globals.READER.gr.set_read.return_value = False
		self.assertRaises(Exception, self.db.sync_to_google)
		
		# item should still be marked as read (and dirty)
		assert self.db.get_items_list('is_dirty = 0') == []
		assert len(self.db.get_items_list('is_dirty = 1')) == 1
		assert len(self.db.get_items_list('is_read = 1')) == 1
	def test_cleanup(self):
		res_folders = ['a','b','blah','blah2','c','d']
		ensure_dir_exists(self.output_folder + '/_resources')
		for res_folder in res_folders:
			ensure_dir_exists(self.output_folder + '/_resources/' + res_folder)
			touch_file(self.output_folder + '/_resources/' + res_folder + '/image.jpg')
	
		assert os.listdir(self.output_folder + '/_resources') == res_folders
		
		# insert some existing items
		self.db.add_item(fake_item(google_id = 'b', is_read = False))
		self.db.add_item(fake_item(google_id = 'd', is_read = False))
		self.db.add_item(fake_item(google_id = 'c', is_read = True))
		
		# clean up that mess!
		self.db.sync_to_google() # remove all the read items
		self.db.cleanup()
		
		assert os.listdir(self.output_folder + '/_resources') == ['b','d']
	def test_adding_unicode(self):
		# add to DB
		input_item = fake_item(title=u'caf\xe9')
		self.db.add_item(input_item)
		
		# grab it out
		items = list(self.db.get_items())
		assert len(items) == 1
		item = items[0]
		
		# and check it still looks the same:
		assert item.title == u'caf\xe9'
	def test_google_sync(self):
		# mock out the google reader
		reader = app_globals.READER.gr
		reader.set_read.return_value = 'OK'
		reader.add_star.return_value = 'OK'
		reader.set_unread.return_value = 'OK'
		reader.del_star.return_value = 'OK'
		
		self.db.add_item(fake_item(google_id = 'b', title='item b', is_read = False, is_dirty = True))
		self.db.add_item(fake_item(google_id = 'd', title='item d', is_read = False, is_dirty = True))
		self.db.add_item(fake_item(google_id = 'c', title='item c', is_starred = True, is_read = True, is_dirty = True))

		self.db.sync_to_google()
		assert reader.method_calls == [
			('set_read', ('c',), {}),
			('add_star', ('c',), {})]
		
		assert self.db.get_items_list('is_dirty = 1') == []
		# c should have been deleted because it was read
		assert sorted(map(lambda x: x.title, self.db.get_items_list('is_dirty = 0'))) == ['item b','item d']
		self.db.reload()
		# check that changes have been saved
		assert sorted(map(lambda x: x.title, self.db.get_items_list('is_dirty = 0'))) == ['item b','item d']
	def test_changing_item_feed(self):
		# add to DB
		input_item = fake_item(tag_name=u'tagname_1')
		self.db.add_item(input_item)
		
		input_item.tag_name = 'tagname_2'
		self.db.update_feed_for_item(input_item)

		# grab it out
		items = list(self.db.get_items())
		assert len(items) == 1
		item = items[0]

		self.assertEqual(item.tag_name, u'tagname_2')
	def test_adding_items(self):
		# add to DB
		input_item = fake_item()
		self.db.add_item(input_item)
		
		# grab it out
		items = list(self.db.get_items())
		assert len(items) == 1
		item = items[0]
		
		# and check it still looks the same:
		for attr in ['url','title','feed_name','tag_name','google_id','is_read','is_dirty','is_shared','is_starred','date','content']:
			self.assertEqual( getattr(item, attr), getattr(input_item, attr) )

		# test updating
		item.is_read = True
		self.db.update_item(item)
		items = list(self.db.get_items())
		assert len(items) == 1
		item = items[0]
		assert item.is_read == True
	def test_is_read(self):
		assert self.db.is_read('sample_id') == None
		self.db.add_item(fake_item())
		assert self.db.is_read('sample_id') == False
		self.db.sql('update items set is_read = 1 where google_id = "sample_id"')
		assert self.db.is_read('sample_id') == True