-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for fuzzy case-insensitive match in PropertyRef #1383
Open
achantavy
wants to merge
2
commits into
master
Choose a base branch
from
fuzzymatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,21 @@ | |
'github_username': 'mbsimp-son', # pure lowercase | ||
}, | ||
] | ||
|
||
FAKE_EMPLOYEE2_DATA = [ | ||
{ | ||
'id': 123, | ||
'email': '[email protected]', | ||
'first_name': 'Homer', | ||
'last_name': 'Simpson', | ||
'name': 'Homer Simpson', | ||
'github_username': 'jsimpso', # substring | ||
}, | ||
{ | ||
'id': 456, | ||
'email': '[email protected]', | ||
'first_name': 'Marge', | ||
'last_name': 'Simpson', | ||
'github_username': 'mbsimp', # substring | ||
}, | ||
] |
44 changes: 44 additions & 0 deletions
44
tests/data/graph/querybuilder/sample_models/fake_emps_githubusers_fuzzy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from dataclasses import dataclass | ||
|
||
from cartography.models.core.common import PropertyRef | ||
from cartography.models.core.nodes import CartographyNodeProperties | ||
from cartography.models.core.nodes import CartographyNodeSchema | ||
from cartography.models.core.relationships import CartographyRelProperties | ||
from cartography.models.core.relationships import CartographyRelSchema | ||
from cartography.models.core.relationships import LinkDirection | ||
from cartography.models.core.relationships import make_target_node_matcher | ||
from cartography.models.core.relationships import OtherRelationships | ||
from cartography.models.core.relationships import TargetNodeMatcher | ||
|
||
|
||
@dataclass(frozen=True) | ||
class FakeEmp2ToGitHubUserRelProperties(CartographyRelProperties): | ||
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class FakeEmp2ToGitHubUser(CartographyRelSchema): | ||
target_node_label: str = 'GitHubUser' | ||
target_node_matcher: TargetNodeMatcher = make_target_node_matcher( | ||
{'username': PropertyRef('github_username', fuzzy_and_ignore_case=True)}, | ||
) | ||
direction: LinkDirection = LinkDirection.OUTWARD | ||
rel_label: str = "IDENTITY_GITHUB" | ||
properties: FakeEmp2ToGitHubUserRelProperties = FakeEmp2ToGitHubUserRelProperties() | ||
|
||
|
||
@dataclass(frozen=True) | ||
class FakeEmp2NodeProperties(CartographyNodeProperties): | ||
id: PropertyRef = PropertyRef('id') | ||
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True) | ||
email: PropertyRef = PropertyRef('email') | ||
github_username: PropertyRef = PropertyRef('github_username') | ||
|
||
|
||
@dataclass(frozen=True) | ||
class FakeEmp2Schema(CartographyNodeSchema): | ||
label: str = 'FakeEmployee2' | ||
properties: FakeEmp2NodeProperties = FakeEmp2NodeProperties() | ||
other_relationships: OtherRelationships = OtherRelationships([ | ||
FakeEmp2ToGitHubUser(), | ||
]) |
27 changes: 27 additions & 0 deletions
27
tests/integration/cartography/graph/test_querybuilder_fuzzy_case_insensitive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from cartography.client.core.tx import load | ||
from cartography.intel.github.users import load_organization_users | ||
from tests.data.graph.querybuilder.sample_data.case_insensitive_prop_ref import FAKE_EMPLOYEE2_DATA | ||
from tests.data.graph.querybuilder.sample_data.case_insensitive_prop_ref import FAKE_GITHUB_ORG_DATA | ||
from tests.data.graph.querybuilder.sample_data.case_insensitive_prop_ref import FAKE_GITHUB_USER_DATA | ||
from tests.data.graph.querybuilder.sample_models.fake_emps_githubusers_fuzzy import FakeEmp2Schema | ||
from tests.integration.util import check_rels | ||
|
||
TEST_UPDATE_TAG = 123456789 | ||
|
||
|
||
def test_load_team_members_data_fuzzy(neo4j_session): | ||
# Arrange: Load some fake GitHubUser nodes to the graph | ||
load_organization_users( | ||
neo4j_session, | ||
FAKE_GITHUB_USER_DATA, | ||
FAKE_GITHUB_ORG_DATA, | ||
TEST_UPDATE_TAG, | ||
) | ||
|
||
# Act: Create team members | ||
load(neo4j_session, FakeEmp2Schema(), FAKE_EMPLOYEE2_DATA, lastupdated=TEST_UPDATE_TAG) | ||
|
||
# Assert we can create relationships using a fuzzy, case insensitive match | ||
assert check_rels(neo4j_session, 'FakeEmployee2', 'email', 'GitHubUser', 'username', 'IDENTITY_GITHUB') == { | ||
('[email protected]', 'HjsimPson'), ('[email protected]', 'mbsimp-son'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from cartography.graph.querybuilder import build_ingestion_query | ||
from tests.data.graph.querybuilder.sample_models.fake_emps_githubusers import FakeEmpSchema | ||
from tests.data.graph.querybuilder.sample_models.fake_emps_githubusers_fuzzy import FakeEmp2Schema | ||
from tests.data.graph.querybuilder.sample_models.simple_node import SimpleNodeSchema | ||
from tests.data.graph.querybuilder.sample_models.simple_node import SimpleNodeWithSubResourceSchema | ||
from tests.unit.cartography.graph.helpers import remove_leading_whitespace_and_empty_lines | ||
|
@@ -90,3 +91,35 @@ def test_build_ingestion_query_case_insensitive_match(): | |
actual_query = remove_leading_whitespace_and_empty_lines(query) | ||
expected_query = remove_leading_whitespace_and_empty_lines(expected) | ||
assert actual_query == expected_query | ||
|
||
|
||
def test_build_ingestion_query_fuzzy_case_insensitive(): | ||
query = build_ingestion_query(FakeEmp2Schema()) | ||
|
||
expected = """ | ||
UNWIND $DictList AS item | ||
MERGE (i:FakeEmployee2{id: item.id}) | ||
ON CREATE SET i.firstseen = timestamp() | ||
SET | ||
i.lastupdated = $lastupdated, | ||
i.email = item.email, | ||
i.github_username = item.github_username | ||
WITH i, item | ||
CALL { | ||
WITH i, item | ||
OPTIONAL MATCH (n0:GitHubUser) | ||
WHERE | ||
toLower(n0.username) CONTAINS toLower(item.github_username) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SecPrez this is what the final rendered query will look like |
||
WITH i, item, n0 WHERE n0 IS NOT NULL | ||
MERGE (i)-[r0:IDENTITY_GITHUB]->(n0) | ||
ON CREATE SET r0.firstseen = timestamp() | ||
SET | ||
r0.lastupdated = $lastupdated | ||
} | ||
""" | ||
|
||
# Assert: compare query outputs while ignoring leading whitespace. | ||
actual_query = remove_leading_whitespace_and_empty_lines(query) | ||
expected_query = remove_leading_whitespace_and_empty_lines(expected) | ||
assert actual_query == expected_query |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SecPrez: this will allow us to connect 2 nodes in a "fuzzy" way, lemme know if this helps