21
21
from elasticsearch_dsl import connections , serializer
22
22
23
23
24
+ class DummyElasticsearch :
25
+ def __init__ (self , * args , hosts , ** kwargs ):
26
+ self .hosts = hosts
27
+
28
+
24
29
def test_default_connection_is_returned_by_default ():
25
30
c = connections .Connections ()
26
31
@@ -33,27 +38,36 @@ def test_default_connection_is_returned_by_default():
33
38
34
39
35
40
def test_get_connection_created_connection_if_needed ():
36
- c = connections .Connections ()
37
- c .configure (default = {"hosts" : ["es.com" ]}, local = {"hosts" : ["localhost" ]})
41
+ c = connections .Connections (elasticsearch_class = DummyElasticsearch )
42
+ c .configure (
43
+ default = {"hosts" : ["https://es.com:9200" ]},
44
+ local = {"hosts" : ["https://localhost:9200" ]},
45
+ )
38
46
39
47
default = c .get_connection ()
40
48
local = c .get_connection ("local" )
41
49
42
- assert isinstance (default , Elasticsearch )
43
- assert isinstance (local , Elasticsearch )
50
+ assert isinstance (default , DummyElasticsearch )
51
+ assert isinstance (local , DummyElasticsearch )
44
52
45
- assert [{ "host" : " es.com" }] == default . transport . hosts
46
- assert [{ "host" : " localhost" }] == local . transport . hosts
53
+ assert default . hosts == [ "https:// es.com:9200" ]
54
+ assert local . hosts == [ "https:// localhost:9200" ]
47
55
48
56
49
57
def test_configure_preserves_unchanged_connections ():
50
- c = connections .Connections ()
58
+ c = connections .Connections (elasticsearch_class = DummyElasticsearch )
51
59
52
- c .configure (default = {"hosts" : ["es.com" ]}, local = {"hosts" : ["localhost" ]})
60
+ c .configure (
61
+ default = {"hosts" : ["https://es.com:9200" ]},
62
+ local = {"hosts" : ["https://localhost:9200" ]},
63
+ )
53
64
default = c .get_connection ()
54
65
local = c .get_connection ("local" )
55
66
56
- c .configure (default = {"hosts" : ["not-es.com" ]}, local = {"hosts" : ["localhost" ]})
67
+ c .configure (
68
+ default = {"hosts" : ["https://not-es.com:9200" ]},
69
+ local = {"hosts" : ["https://localhost:9200" ]},
70
+ )
57
71
new_default = c .get_connection ()
58
72
new_local = c .get_connection ("local" )
59
73
@@ -62,9 +76,12 @@ def test_configure_preserves_unchanged_connections():
62
76
63
77
64
78
def test_remove_connection_removes_both_conn_and_conf ():
65
- c = connections .Connections ()
79
+ c = connections .Connections (elasticsearch_class = DummyElasticsearch )
66
80
67
- c .configure (default = {"hosts" : ["es.com" ]}, local = {"hosts" : ["localhost" ]})
81
+ c .configure (
82
+ default = {"hosts" : ["https://es.com:9200" ]},
83
+ local = {"hosts" : ["https://localhost:9200" ]},
84
+ )
68
85
c .add_connection ("local2" , object ())
69
86
70
87
c .remove_connection ("default" )
@@ -77,15 +94,16 @@ def test_remove_connection_removes_both_conn_and_conf():
77
94
78
95
79
96
def test_create_connection_constructs_client ():
80
- c = connections .Connections ()
81
- c .create_connection ("testing" , hosts = ["es.com" ])
97
+ c = connections .Connections (elasticsearch_class = DummyElasticsearch )
98
+ c .create_connection ("testing" , hosts = ["https:// es.com:9200 " ])
82
99
83
100
con = c .get_connection ("testing" )
84
- assert [{ "host" : " es.com" }] == con . transport . hosts
101
+ assert con . hosts == [ "https:// es.com:9200" ]
85
102
86
103
87
104
def test_create_connection_adds_our_serializer ():
88
- c = connections .Connections ()
89
- c .create_connection ("testing" , hosts = ["es.com" ])
105
+ c = connections .Connections (elasticsearch_class = Elasticsearch )
106
+ c .create_connection ("testing" , hosts = ["https:// es.com:9200 " ])
90
107
91
- assert c .get_connection ("testing" ).transport .serializer is serializer .serializer
108
+ c_serializers = c .get_connection ("testing" ).transport .serializers
109
+ assert c_serializers .serializers ["application/json" ] is serializer .serializer
0 commit comments