def test_new_path_setter(self): """Set self._new_path to data if valid, else raise ValueError.""" rd = Redirect('/old/path', '/new/path', 302) rd.new_path = '/newer/path' assert rd._new_path == '/newer/path' with pytest.raises(ValueError): rd.new_path = 'not/root/relative/path'
def test_add_to_app(self, app): """Add a url_rule for redirect to given app.""" rd = Redirect('/old/path', '/new/path', 302) with app.test_client(): assert '/old/path' not in [rule.rule for rule in app.url_map.iter_rules()] rd.add_to_app(app) assert '/old/path' in [rule.rule for rule in app.url_map.iter_rules()]
def test_date_created_setter(self, mock_dt): """Set with given datetime, or with current utc datetime if none given. Raise a TypeError if given non-datetime data. """ rd = Redirect('/old/path', '/new/path', 302) dt = datetime(2012, 12, 21) assert rd._date_created == datetime(2000, 1, 1) rd.date_created = dt assert rd._date_created == dt with pytest.raises(TypeError): rd.date_created = '12/12/12'
def test_eq(self): """Return true if all data in two Redirects is the same.""" dt = datetime.utcnow() rd1 = Redirect('/old/path', '/new/path', 302, dt) rd2 = Redirect('/old/path', '/new/path', 302, dt) assert rd1 is not rd2 assert rd1 == rd2 rd2.new_path = '/newer/path' assert rd1 != rd2 rd2.new_path = '/new/path' assert rd1 == rd2 rd2.old_path = '/oldest/path' assert rd1 != rd2 rd2.old_path = '/old/path' assert rd1 == rd2 rd2.status_code = 301 assert rd1 != rd2 rd2.status_code = 302 assert rd1 == rd2 rd2.date_created = datetime.utcnow() assert rd1 != rd2 rd2.date_created = dt assert rd1 == rd2
def test_status_code_setter(self): """Set data to self._status code if an integer between 300-309. Raise a TypeError if not int, or a ValueError if not between 300-309. """ rd = Redirect('/old/path', '/new/path', 302) rd.status_code = 301 assert rd._status_code == 301 with pytest.raises(TypeError): rd.status_code = '301' with pytest.raises(ValueError): rd.status_code = 200 with pytest.raises(ValueError): rd.status_code = 404 with pytest.raises(ValueError): rd.status_code = 299 with pytest.raises(ValueError): rd.status_code = 310
def test_date_created_getter(self): """Return self._date_created""" rd = Redirect('/old/path', '/new/path', 302) dt = datetime.utcnow() rd._date_created = dt assert rd.date_created == dt
def test_status_code_getter(self): """Return self._status_code""" rd = Redirect('/old/path', '/new/path', 302) rd._status_code = 301 assert rd.status_code == 301
def test_new_path_getter(self): """Return self._new_path""" rd = Redirect('/old/path', '/new/path', 302) rd._new_path = '/newer/path' assert rd.new_path == '/newer/path'
def test_to_json_and_back(self): """A Redirect converted to JSON should convert back to a redirect.""" rd1 = Redirect('/old/path', '/new/path', 302) jsondump = rd1.to_JSON() rd2 = Redirect.from_JSON(jsondump) assert rd1 == rd2
def test_redirect_path(self, mock_redirect): """Creates a redirect to self.new_path with self.status_code.""" rd = Redirect('/old/path', '/new/path', 302) rd.redirect_path() mock_redirect.assert_called_with('/new/path', 302)
def test_message(self): """Return a string describing the redirect.""" rd = Redirect('/old/path', '/new/path', 302) assert rd.message() == 'Redirect from /old/path to /new/path with '\ 'status code 302'
def test_repr(self): """Return string describing contents of redirect.""" rd = Redirect('/old/path', '/new/path', 302) assert rd.__repr__() == '<Redirect from /old/path to /new/path, '\ 'status code: 302>'