def setUp(self):
     self.ua=UserAccess('tc_user_test.db')
     self.ua.create_db()
     self.ua.add_db()
     data = {'users':[]}
     data['users'].append({'username':'******','password':'******'})
     data['users'].append({'username':'******','password':'******'})
     data['users'].append({'username':'******','password':'******'})
     with open ('data.json', 'w') as outfile:
         json.dump(data, outfile)
     with open ('data.json') as data_file:
         self.data = json.load(data_file)
class UserAccessTest(unittest.TestCase):
    def setUp(self):
        self.ua=UserAccess('tc_user_test.db')
        self.ua.create_db()
        self.ua.add_db()
        data = {'users':[]}
        data['users'].append({'username':'******','password':'******'})
        data['users'].append({'username':'******','password':'******'})
        data['users'].append({'username':'******','password':'******'})
        with open ('data.json', 'w') as outfile:
            json.dump(data, outfile)
        with open ('data.json') as data_file:
            self.data = json.load(data_file)

    def test_db_exists(self):
        self.assertEqual(self.ua.chk_db(), True)
        self.assertEqual(os.path.exists('tc_user_test.db'), True)
        os.remove('tc_user_test.db')
        self.assertEqual(self.ua.chk_db(), False)
        self.assertEqual(os.path.exists('tc_user_test.db'), False)
        self.ua.create_db()

    def test_db_add(self):
        engine = create_engine('sqlite:///%s' % 'tc_user_test.db')
        Base = declarative_base()
        Base.metadata.bind = engine
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        session.query()
        qry=session.query(Basics).filter(Basics.id=='1').all()
        self.assertEqual(qry[0].login.username, 'jsmith')
        self.assertEqual(qry[0].login.password, 'pass123')
        self.assertEqual(qry[0].city, 'San Francisco')
        self.assertEqual(qry[0].age, 21)
        self.assertEqual(qry[0].state, 'CA')
        self.assertEqual(qry[0].job, 'Janitor')

    def test_get_user_filter_group(self):
        url = 'http://www.example.com/users/index.html?state=CA&group=age'
        qry =self.ua.get_user_url(url)
        self.assertEqual(qry[0].login.username, 'cjones')
        self.assertEqual(qry[0].login.password, '456pass')
        self.assertEqual(qry[0].city, 'Los Angeles')
        self.assertEqual(qry[0].age, 21)
        self.assertEqual(qry[0].state, 'CA')
        self.assertEqual(qry[0].job, 'Lawyer')

    def test_get_user_filter(self):
        url = 'http://www.example.com/users/index.html?job=Lawyer'
        qry =self.ua.get_user_url(url)
        self.assertEqual(qry[0].login.username, 'cjones')
        self.assertEqual(qry[0].login.password, '456pass')
        self.assertEqual(qry[0].city, 'Los Angeles')
        self.assertEqual(qry[0].age, 21)
        self.assertEqual(qry[0].state, 'CA')
        self.assertEqual(qry[0].job, 'Lawyer')

    def test_is_user(self):
        self.assertEqual(self.ua.is_user(self.data['users'][0]), True)
        self.assertEqual(self.ua.is_user(self.data['users'][1]), False)
        self.assertEqual(self.ua.is_user(self.data['users'][2]), True)

    def test_is_user_bad_data(self):
        d={'apple':'cjones','password':'******'}
        self.assertRaises(ValueError,lambda: self.ua.is_user(d))
        d={'username':'******','apple':'456pass'}
        self.assertRaises(ValueError,lambda: self.ua.is_user(d))

    def test_paginate(self):
        self.ua.set_paginate_max(2)
        url = 'http://www.example.com/users/index.html?state=CA&group=age'
        qry =self.ua.get_user_url(url)
        self.assertEqual(self.ua.paginate_max,2)
        self.assertEqual(qry[0].login.username, 'cjones')
        self.assertEqual(qry[0].login.password, '456pass')
        self.assertEqual(qry[0].city, 'Los Angeles')
        self.assertEqual(qry[0].age, 21)
        self.assertEqual(qry[0].state, 'CA')
        self.assertEqual(qry[0].job, 'Lawyer')

    def tearDown(self):
        if os.path.exists('tc_user_test.db'):
            os.remove('tc_user_test.db')
            os.remove('data.json')