Skip to content

Commit ab387dc

Browse files
committed
feat: add MurmurHash2 hash() implementation test (#355)
1 parent e95d982 commit ab387dc

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

tests/test_hash.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# --coding:utf-8--
3+
4+
# Copyright (c) 2020 vesoft inc. All rights reserved.
5+
#
6+
# This source code is licensed under Apache 2.0 License.
7+
8+
import pytest
9+
from nebula3.utils.hash import hash as murmur_hash
10+
11+
TEST_VECTORS = [
12+
(b"", 6142509188972423790),
13+
(b"a", 4993892634952068459),
14+
(b"abcdefgh", 8664279048047335611), # length-8 cases
15+
(b"abcdefghi", -5409788147785758033),
16+
("to_be_hashed", -1098333533029391540),
17+
("中文", -8591787916246384322),
18+
]
19+
20+
@pytest.mark.parametrize("data, expected", TEST_VECTORS)
21+
def test_known_vectors(data, expected):
22+
assert murmur_hash(data) == expected
23+
24+
25+
def test_str_bytes_equiv():
26+
"""
27+
Ensure str and bytes inputs produce the same hash.
28+
"""
29+
s = "pytest"
30+
assert murmur_hash(s) == murmur_hash(s.encode("utf-8"))
31+
32+
33+
def test_type_error():
34+
"""
35+
TypeError
36+
"""
37+
with pytest.raises(TypeError):
38+
murmur_hash(12345)

tests/test_hash_integration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# --coding:utf-8--
3+
4+
# Copyright (c) 2020 vesoft inc. All rights reserved.
5+
#
6+
# This source code is licensed under Apache 2.0 License.
7+
8+
import pytest
9+
from nebula3.Config import Config
10+
from nebula3.gclient.net import ConnectionPool
11+
from nebula3.utils.hash import hash as murmur_hash
12+
13+
@pytest.fixture(scope="module")
14+
def nebula_session():
15+
config = Config()
16+
config.max_connection_pool_size = 10
17+
pool = ConnectionPool()
18+
pool.init([("127.0.0.1", 9669)], config)
19+
session = pool.get_session('root', 'nebula')
20+
yield session
21+
pool.close()
22+
23+
@pytest.mark.parametrize("data", [
24+
"", "a", "abcdefgh", "abcdefghi", "to_be_hashed", "中文"
25+
])
26+
def test_hash_against_server(nebula_session, data):
27+
# Local Computing
28+
expected = murmur_hash(data)
29+
result = nebula_session.execute(f'YIELD hash("{data}")')
30+
assert result.is_succeeded(), result.error_msg()
31+
actual = result.row_values(0)[0].as_int()
32+
assert actual == expected

0 commit comments

Comments
 (0)