class TestMemoryDbWithEnv(TestCase):
    def __init__(self, obj):
        super(TestCase, self).__init__(obj)
        self.conn = None

    def setUp(self):
        self.conn = CityHallDbFactory()
        self.conn.open()
        self.conn.create_default_tables()

    def test_create_user(self):
        count_before = len(self.conn.authTable)
        db = self.conn.get_db()
        users_root = db.get_env_root('users')
        db.create('cityhall', users_root, 'test', '')
        test_folder = db.get_child(users_root, 'test')
        test_id = test_folder['id']
        self.conn.get_db().create_user('cityhall', 'test', '', test_id)
        count_after = len(self.conn.authTable)
        self.assertEqual(count_before + 1, count_after)

        last_entry = self.conn.authTable[count_before]
        self.assertEqual(last_entry['user'], 'test')
        self.assertTrue(last_entry['active'])
        self.assertEqual(last_entry['user_root'], test_id)
        self.assertEqual(last_entry['author'], 'cityhall')

    def test_create_same_user_is_noop(self):
        db = self.conn.get_db()
        users_root = db.get_env_root('users')
        db.create('cityhall', users_root, 'test', '')
        test_folder = db.get_child(users_root, 'test')
        id = test_folder['id']

        self.conn.get_db().create_user('cityhall', 'test', '', id)

        count_before = len(self.conn.authTable)
        self.conn.get_db().create_user('cityhall', 'test', '', id)
        count_after = len(self.conn.authTable)
        self.assertEqual(count_before, count_after)

    def test_authenticate(self):
        self.assertFalse(self.conn.authenticate('test', ''))
        db = self.conn.get_db()
        users_env = db.get_env_root('users')
        db.create('cityhall', users_env, 'test', '')
        created = db.get_child(users_env, 'test')
        self.conn.get_db().create_user('cityhall', 'test', '', created['id'])
        self.assertTrue(self.conn.authenticate('test', ''))

    def test_create_root(self):
        vals_before = len(self.conn.valsTable)
        self.conn.get_db().create_root('cityhall', 'dev')
        vals_after = len(self.conn.valsTable)

        self.assertEqual(vals_before+1, vals_after)

    def test_create_env_returns_status(self):
        self.assertTrue(
            self.conn.get_db().create_root('cityhall', 'dev'),
            "If the root is created, create_env returns true"
        )
        self.assertIsNot(
            self.conn.get_db().create_root('cityhall', 'dev'),
            "If creating the root fails, create_env returns false"
        )

    def test_create_same_env_is_noop(self):
        db = self.conn.get_db()
        db.create_root('cityhall', 'dev')

        vals_before = len(self.conn.valsTable)
        auth_before = len(self.conn.authTable)
        db.create_root('cityhall', 'dev')
        self.assertEqual(vals_before, len(self.conn.valsTable))
        self.assertEqual(auth_before, len(self.conn.authTable))

    def test_create_root_is_complete(self):
        self.conn.get_db().create_root('cityhall', 'dev')

        val = self.conn.valsTable[-1]
        self.assertEqual('dev', val['name'])
        self.assertEqual(val['id'], val['parent'])
        self.assertEqual('', val['value'])
        self.assertEqual('cityhall', val['author'])
        self.assertEqual('', val['override'])
        self.assertTrue(val['active'])
        self.assertTrue(val['first_last'])

    def test_get_env_root_on_nonexistant(self):
        self.assertEqual(-1, self.conn.get_db().get_env_root('dev'))

    def test_get_env_root_on_existant(self):
        db = self.conn.get_db()
        db.create_root('cityhall', 'dev')
        val = self.conn.valsTable[-1]
        self.assertEqual(val['id'], db.get_env_root('dev'))