Skip to content

Commit 0285ddd

Browse files
committed
Rounding out tag support.
1 parent 1ec07c1 commit 0285ddd

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

intercom/tag.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,21 @@ def untag_companies(cls, name, companies):
4848
return cls._tag_collection(name, 'companies', companies, untag=True)
4949

5050
@classmethod
51-
def find_all_for_user(cls, id=None, email=None, user_id=None):
52-
params = {}
53-
if id:
54-
params['id'] = id
55-
if email:
56-
params['email'] = email
57-
if user_id:
58-
params['user_id'] = user_id
51+
def find_all_for_user(cls, **kwargs):
52+
return cls.find_all_for('user', **kwargs)
53+
54+
@classmethod
55+
def find_all_for_company(cls, **kwargs):
56+
return cls.find_all_for('company', **kwargs)
57+
58+
@classmethod
59+
def find_all_for(cls, taggable_type, **kwargs):
60+
params = {
61+
'taggable_type': taggable_type
62+
}
63+
res_id = kwargs.pop('id', None)
64+
if res_id:
65+
params['taggable_id'] = res_id
66+
params.update(kwargs)
67+
5968
return Tag.find_all(**params)

tests/integration/test_tags.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import unittest
55
from intercom import Intercom
66
from intercom import Tag
7+
from intercom import User
8+
from intercom import Company
79
from . import delete
810
from . import get_or_create_company
911
from . import get_or_create_user
@@ -32,47 +34,68 @@ def teardown_class(cls):
3234

3335
def test_tag_users(self):
3436
# Tag users
35-
tag = Tag.tag_users("blue", [self.user.id])
36-
self.assertEqual(tag.name, "blue")
37+
tag = Tag.tag_users('blue', [self.user.id])
38+
self.assertEqual(tag.name, 'blue')
39+
user = User.find(email=self.user.email)
40+
self.assertEqual(1, len(user.tags))
3741

3842
def test_untag_users(self):
3943
# Untag users
40-
tag = Tag.untag_users("blue", [self.user.id])
41-
self.assertEqual(tag.name, "blue")
44+
tag = Tag.untag_users('blue', [self.user.id])
45+
self.assertEqual(tag.name, 'blue')
46+
user = User.find(email=self.user.email)
47+
self.assertEqual(0, len(user.tags))
4248

4349
def test_all(self):
4450
# Iterate over all tags
4551
for tag in Tag.all():
4652
self.assertIsNotNone(tag.id)
4753

48-
# def test_all_for_user_by_id(self):
49-
# # Iterate over all tags for user
50-
# tags = Tag.find_all_for_user(id=self.user.id)
51-
# for tag in tags:
52-
# self.assertIsNotNone(tag.id)
54+
def test_all_for_user_by_id(self):
55+
# Iterate over all tags for user
56+
tags = Tag.find_all_for_user(id=self.user.id)
57+
for tag in tags:
58+
self.assertIsNotNone(tag.id)
5359

54-
# def test_all_for_user_by_email(self):
55-
# # Iterate over all tags for user
56-
# tags = Tag.find_all_for_user(email=self.user.email)
57-
# for tag in tags:
58-
# self.assertIsNotNone(tag.id)
60+
def test_all_for_user_by_email(self):
61+
# Iterate over all tags for user
62+
tags = Tag.find_all_for_user(email=self.user.email)
63+
for tag in tags:
64+
self.assertIsNotNone(tag.id)
5965

60-
# def test_all_for_user_by_user_id(self):
61-
# # Iterate over all tags for user
62-
# tags = Tag.find_all_for_user(user_id=self.user.user_id)
63-
# for tag in tags:
64-
# self.assertIsNotNone(tag.id)
66+
def test_all_for_user_by_user_id(self):
67+
# Iterate over all tags for user
68+
tags = Tag.find_all_for_user(user_id=self.user.user_id)
69+
for tag in tags:
70+
self.assertIsNotNone(tag.id)
6571

6672
def test_tag_companies(self):
6773
# Tag companies
6874
tag = Tag.tag_companies("red", [self.user.companies[0].id])
6975
self.assertEqual(tag.name, "red")
76+
company = Company.find(id=self.user.companies[0].id)
77+
self.assertEqual(1, len(company.tags))
7078

7179
def test_untag_companies(self):
7280
# Untag companies
73-
tag = Tag.untag_companies("blue", [self.user.companies[0].id])
74-
self.assertEqual(tag.name, "blue")
81+
tag = Tag.untag_companies("red", [self.user.companies[0].id])
82+
self.assertEqual(tag.name, "red")
83+
company = Company.find(id=self.user.companies[0].id)
84+
self.assertEqual(0, len(company.tags))
85+
86+
# Iterate over all tags for company
87+
def test_all_for_company_by_id(self):
88+
# Iterate over all tags for user
89+
red_tag = Tag.tag_companies("red", [self.company.id])
90+
tags = Tag.find_all_for_company(id=self.company.id)
91+
for tag in tags:
92+
self.assertEqual(red_tag.id, tag.id)
93+
Tag.untag_companies("red", [self.company.id])
7594

76-
# # Iterate over all tags for company
77-
# Tag.find_all_for_company(id='43357e2c3c77661e25000026')
78-
# Tag.find_all_for_company(company_id='6')
95+
def test_all_for_company_by_company_id(self):
96+
# Iterate over all tags for user
97+
red_tag = Tag.tag_companies("red", [self.company.id])
98+
tags = Tag.find_all_for_company(company_id=self.company.id)
99+
for tag in tags:
100+
self.assertEqual(red_tag.id, tag.id)
101+
Tag.untag_companies("red", [self.company.id])

0 commit comments

Comments
 (0)