def __repr__(self):
return self.render_as_string()
+ def __copy__(self):
+ return self.__class__.create(
+ self.drivername,
+ self.username,
+ self.password,
+ self.host,
+ self.port,
+ self.database,
+ # note this is an immutabledict of str-> str / tuple of str,
+ # also fully immutable. does not require deepcopy
+ self.query,
+ )
+
+ def __deepcopy__(self, memo):
+ return self.__copy__()
+
def __hash__(self):
return hash(str(self))
+import copy
from unittest.mock import call
from unittest.mock import MagicMock
from unittest.mock import Mock
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import is_false
+from sqlalchemy.testing import is_not
from sqlalchemy.testing import is_true
from sqlalchemy.testing import mock
from sqlalchemy.testing import ne_
is_true(url1 != url3)
is_false(url1 == url3)
+ def test_copy(self):
+ url1 = url.make_url(
+ "dialect://user:pass@host/db?arg1%3D=param1&arg2=param+2"
+ )
+ url2 = copy.copy(url1)
+ eq_(url1, url2)
+ is_not(url1, url2)
+
+ def test_deepcopy(self):
+ url1 = url.make_url(
+ "dialect://user:pass@host/db?arg1%3D=param1&arg2=param+2"
+ )
+ url2 = copy.deepcopy(url1)
+ eq_(url1, url2)
+ is_not(url1, url2)
+ is_not(url1.query, url2.query) # immutabledict of immutable k/v,
+ # but it copies it on constructor
+ # in any case if params are present
+
@testing.combinations(
"drivername",
"username",
url.make_url("drivername:///?%s" % expected),
)
+ @testing.combinations(
+ "drivername://",
+ "drivername://?foo=bar",
+ "drivername://?foo=bar&foo=bat",
+ )
+ def test_query_dict_immutable(self, urlstr):
+ url_obj = url.make_url(urlstr)
+
+ with expect_raises_message(TypeError, ".*immutable"):
+ url_obj.query["foo"] = "hoho"
+
@testing.combinations(
(
"foo1=bar1&foo2=bar2",