Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

"""Test the fulltext implementation""" 

import logging 

import os 

import transaction 

import urllib 

 

from nose.tools import ok_ 

from pyramid import testing 

from unittest import TestCase 

 

from bookie.bcelery import tasks 

from bookie.lib.readable import ReadContent 

from bookie.lib.readable import ReadUrl 

 

from bookie.models import DBSession 

from bookie.tests import empty_db 

 

 

LOG = logging.getLogger(__file__) 

API_KEY = None 

 

 

class TestReadable(TestCase): 

    """Test that our fulltext classes function""" 

 

    def test_url_content(self): 

        """Test that we set the correct status""" 

 

        url = 'http://lococast.net/archives/475' 

        read = ReadUrl.parse(url) 

 

        ok_(read.status == 200, "The status is 200" + str(read.status)) 

        ok_(not read.is_image(), "The content is not an image") 

        ok_(read.content is not None, "Content should not be none") 

        ok_('Lococast' in read.content, 

            "The word Lococast is in the content: " + str(read.content)) 

 

    def test_404_url(self): 

        """Test that we get the proper errors in a missing url""" 

        url = 'http://lococast.net/archives/001' 

        read = ReadUrl.parse(url) 

 

        ok_(read.status == 404, "The status is 404: " + str(read.status)) 

        ok_(not read.is_image(), "The content is not an image") 

        ok_(read.content is None, "Content should be none") 

 

    def test_given_content(self): 

        """Test that we can parse out given html content ahead of time""" 

 

        file_path = os.path.dirname(__file__) 

        html_content = open(os.path.join(file_path, 'readable_sample.html')) 

 

        read = ReadContent.parse(html_content) 

 

        ok_(read.status == 1, "The status is 1: " + str(read.status)) 

        ok_(not read.is_image(), "The content is not an image") 

        ok_(read.content is not None, "Content should not be none") 

        ok_('Bookie' in read.content, 

            u"The word Bookie is in the content: " + unicode(read.content)) 

 

    def test_non_net_url(self): 

        """I might be bookmarking something internal bookie can't access""" 

        test_url = "http://r2" 

        read = ReadUrl.parse(test_url) 

 

        ok_(read.status == 901, "The status is 901: " + str(read.status)) 

        ok_(not read.is_image(), "The content is not an image") 

        ok_(read.content is None, 

            "Content should be none: " + str(read.content)) 

 

    def test_image_url(self): 

        """Verify we don't store, but just tag an image url""" 

        img_url = 'http://www.ndftz.com/nickelanddime.png' 

        read = ReadUrl.parse(img_url) 

 

        ok_(read.status == 200, "The status is 200: " + str(read.status)) 

        ok_(read.content is None, "Content should be none: ") 

 

    def test_nonworking_url(self): 

        """Testing some urls we know we had issues with initially""" 

        urls = { 

            'CouchSurfing': ('http://allthatiswrong.wordpress.com/2010/01' 

                             '/24/a-criticism-of-couchsurfing-and-review-o' 

                             'f-alternatives/#problems'), 

            # 'Electronic': ('https://www.fbo.gov/index?s=opportunity&mode=' 

            #                'form&tab=core&id=dd11f27254c796f80f2aadcbe415' 

            #                '8407'), 

        } 

 

        for key, url in urls.iteritems(): 

            LOG.debug(url) 

            read = ReadUrl.parse(url) 

            LOG.debug(read) 

 

            ok_(read.status == 200, "The status is 200: " + str(read.status)) 

            ok_(read.content is not None, "Content should not be none: ") 

 

 

class TestReadableFulltext(TestCase): 

    """Test that our fulltext index function""" 

 

    def setUp(self): 

        """Setup Tests""" 

        from pyramid.paster import get_app 

        from bookie.tests import BOOKIE_TEST_INI 

        app = get_app(BOOKIE_TEST_INI, 'bookie') 

        from webtest import TestApp 

        self.testapp = TestApp(app) 

        testing.setUp() 

        global API_KEY 

        if API_KEY is None: 

            res = DBSession.execute( 

                "SELECT api_key FROM users WHERE username = 'admin'").\ 

                fetchone() 

            API_KEY = res['api_key'] 

 

    def tearDown(self): 

        """Tear down each test""" 

        testing.tearDown() 

        empty_db() 

 

    def _get_good_request(self): 

        """Return the basics for a good add bookmark request""" 

        session = DBSession() 

        prms = { 

            'url': u'http://google.com', 

            'description': u'This is my google desc', 

            'extended': u'And some extended notes about it in full form', 

            'tags': u'python search', 

            'api_key': API_KEY, 

            'content': 'bmark content is the best kind of content man', 

        } 

 

        req_params = urllib.urlencode(prms) 

        res = self.testapp.post('/api/v1/admin/bmark', 

                                params=req_params) 

        session.flush() 

        transaction.commit() 

        tasks.reindex_fulltext_allbookmarks(sync=True) 

        return res 

 

    def test_restlike_search(self): 

        """Verify that our search still works in a restful url method""" 

        # first let's add a bookmark we can search on 

        self._get_good_request() 

 

        search_res = self.testapp.get( 

            '/api/v1/admin/bmarks/search/search?search_content=True') 

 

        ok_(search_res.status == '200 OK', 

            "Status is 200: " + search_res.status) 

        ok_('python' in search_res.body, 

            "We should find the python tag in the results: " + search_res.body)