def test_set_is_identified(self) -> None: distinct_id = "777" Person.objects.create(team_id=self.team.pk, distinct_ids=[distinct_id]) person_before_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id) self.assertFalse(person_before_event["is_identified"]) process_event( distinct_id, "", "", {"event": "$identify", "properties": {},}, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( distinct_id, "", "", {"event": "$identify", "properties": {},}, self.team.pk, now().isoformat(), now().isoformat(), ) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") person_after_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id) self.assertTrue(person_after_event["is_identified"])
def test_alias_merge_properties(self) -> None: Person.objects.create( distinct_ids=["old_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "old value both", "key_on_old": "old value"}, ) Person.objects.create( distinct_ids=["new_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "new value both", "key_on_new": "new value"}, ) process_event( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) events = get_events() self.assertEqual(len(events), 1) distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"])) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") persons = get_persons(team_id=self.team.pk) self.assertEqual( persons[0]["properties"], {"key_on_both": "new value both", "key_on_new": "new value", "key_on_old": "old value",}, )
def test_distinct_team_leakage(self) -> None: team2 = Team.objects.create() Person.objects.create(team_id=team2.pk, distinct_ids=["2"], properties={"email": "*****@*****.**"}) Person.objects.create(team_id=self.team.pk, distinct_ids=["1", "2"]) try: process_event( "2", "", "", { "event": "$identify", "properties": {"$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2",}, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "2", "", "", { "event": "$identify", "properties": {"$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2",}, }, self.team.pk, now().isoformat(), now().isoformat(), ) except: pass ids: Dict[int, Any] = {self.team.pk: [], team2.pk: []} for pid in get_person_distinct_ids(team_id=self.team.pk): ids[pid["team_id"]].append(pid["distinct_id"]) for pid in get_person_distinct_ids(team_id=team2.pk): ids[pid["team_id"]].append(pid["distinct_id"]) self.assertEqual(sorted(ids[self.team.pk]), sorted(["1", "2"])) self.assertEqual(ids[team2.pk], ["2"]) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") people1 = get_persons(team_id=self.team.pk) people2 = get_persons(team_id=team2.pk) self.assertEqual(len(people1), 1) self.assertEqual(len(people2), 1) self.assertEqual(people1[0]["team_id"], self.team.pk) self.assertEqual(people1[0]["properties"], {}) self.assertEqual(people2[0]["team_id"], team2.pk) self.assertEqual(people2[0]["properties"], {"email": "*****@*****.**"})
def test_alias_merge_properties(self) -> None: with freeze_time("2020-01-04T13:01:01Z"): Person.objects.create( distinct_ids=["old_distinct_id"], team_id=self.team.pk, properties={ "key_on_both": "old value both", "key_on_old": "old value" }, ) Person.objects.create( distinct_ids=["new_distinct_id"], team_id=self.team.pk, properties={ "key_on_both": "new value both", "key_on_new": "new value" }, ) with freeze_time("2020-01-04T16:01:01Z"): process_event( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) events = get_events() self.assertEqual(len(events), 1) distinct_ids = [ item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk) ] self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"])) ch_client.execute("OPTIMIZE TABLE person") ch_client.execute("OPTIMIZE TABLE persons_up_to_date") persons = get_persons(team_id=self.team.pk) self.assertEqual( persons[0]["properties"], { "key_on_both": "new value both", "key_on_new": "new value", "key_on_old": "old value", }, )
def get_property_values_for_key(key: str, team: Team): result = ch_client.execute(SELECT_PROP_VALUES_SQL, { "team_id": team.pk, "key": key }) return result