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

import ConfigParser 

import os 

import random 

import shutil 

import transaction 

import unittest 

from pyramid import testing 

 

# tools we use to empty tables 

from bookie.models import bmarks_tags 

from bookie.models import DBSession 

from bookie.models import Bmark 

from bookie.models import Hashed 

from bookie.models import Readable 

from bookie.models import Tag 

from bookie.models.applog import AppLog 

from bookie.models.auth import Activation 

from bookie.models.auth import User 

from bookie.models.queue import ImportQueue 

from bookie.models.fulltext import _reset_index 

 

global_config = {} 

 

ini = ConfigParser.ConfigParser() 

 

# we need to pull the right ini for the test we want to run 

# by default pullup test.ini, but we might want to test mysql, pgsql, etc 

test_ini = os.environ.get('BOOKIE_TEST_INI', None) 

if not test_ini: 

    test_ini = 'test.ini' 

 

ini.read(test_ini) 

settings = dict(ini.items('app:bookie')) 

 

BOOKIE_TEST_INI = test_ini 

print "USING TEST INI: ", BOOKIE_TEST_INI 

 

# clean up whoosh index between test runs 

whoosh_idx = settings['fulltext.index'] 

try: 

    # if this is a sqlite db then try to take care of the db file 

    shutil.rmtree(whoosh_idx) 

except: 

    pass 

 

 

def gen_random_word(wordLen): 

    word = '' 

    for i in xrange(wordLen): 

        word += random.choice(('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs' 

                               'tuvwxyz0123456789/&=')) 

    return word 

 

 

class TestDBBase(unittest.TestCase): 

    def setUp(self): 

        """Setup Tests""" 

        testing.setUp() 

        self.trans = transaction.begin() 

 

    def tearDown(self): 

        """Tear down each test""" 

        testing.tearDown() 

        self.trans.abort() 

 

 

class TestViewBase(unittest.TestCase): 

    """In setup, bootstrap the app and make sure we clean up after ourselves 

 

    """ 

    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.app = TestApp(app) 

        testing.setUp() 

        res = DBSession.execute( 

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

            fetchone() 

        self.api_key = res['api_key'] 

 

    def tearDown(self): 

        """Tear down each test""" 

        testing.tearDown() 

        empty_db() 

 

    def _login_admin(self): 

        """Make the login call to the app""" 

        self.app.post( 

            '/login', 

            params={ 

                "login": "admin", 

                "password": "admin", 

                "form.submitted": "Log In", 

            }, 

            status=302) 

 

 

def empty_db(): 

    """On teardown, remove all the db stuff""" 

    DBSession.execute(bmarks_tags.delete()) 

    Readable.query.delete() 

    Bmark.query.delete() 

    Tag.query.delete() 

    # we can't remove the toread tag we have from our commands 

    Hashed.query.delete() 

    ImportQueue.query.delete() 

    # Delete the users not admin in the system. 

    Activation.query.delete() 

    User.query.filter(User.username != 'admin').delete() 

 

    AppLog.query.delete() 

    DBSession.flush() 

    transaction.commit() 

 

    # Clear the fulltext index as well. 

    _reset_index() 

 

 

# unit tests we want to make sure get run 

# from bookie.lib.test_tagcommands import *