forked from narrowcast/facebook-ads-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_facebook.py
229 lines (182 loc) · 8.18 KB
/
test_facebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import unittest
import facebook
USER_ID = os.environ['USER_ID']
ACCOUNT_ID = os.environ['ACCOUNT_ID']
CAMPAIGN_ID = os.environ['CAMPAIGN_ID']
GROUP_ID = os.environ['GROUP_ID']
CREATIVE_ID = os.environ['CREATIVE_ID']
OFFSITE_PIXEL_ID = os.environ['OFFSITE_PIXEL_ID']
PAGE_ID = os.environ['PAGE_ID']
STORY_ID = os.environ['STORY_ID']
class FacebookAdsAPITest(unittest.TestCase):
"""Tests for Facebook Ads API client."""
def setUp(self):
try:
self.access_token = os.environ['FACEBOOK_ACCESS_TOKEN']
self.app_id = os.environ['FACEBOOK_APP_ID']
self.app_secret = os.environ['FACEBOOK_APP_SECRET']
self.api = facebook.AdsAPI(
self.access_token, self.app_id, self.app_secret)
except KeyError:
raise Exception("FACEBOOK_ACCESS_TOKEN, FACEBOOK_APP_ID, and "
"FACEBOOK_APP_SECRET must be set as environmental "
"variables.")
def tearDown(self):
pass
def test_debug_token(self):
response = self.api.debug_token(self.access_token)
self.assertNotIn('error', response)
self.assertEqual(str(response['data']['app_id']), self.app_id)
def test_error_handling(self):
try:
response = self.api.debug_token('')
except facebook.AdsAPIError as e:
pass
def test_get_adusers(self):
response = self.api.get_adusers(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_adaccount(self):
response = self.api.get_adaccount(ACCOUNT_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adaccounts(self):
response = self.api.get_adaccounts(USER_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adcampaign(self):
response = self.api.get_adcampaign(CAMPAIGN_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adcampaigns(self):
response = self.api.get_adcampaigns(ACCOUNT_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adgroup(self):
response = self.api.get_adgroup(GROUP_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adgroups_by_adaccount(self):
response = self.api.get_adgroups_by_adaccount(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_adgroups_by_adcampaign(self):
response = self.api.get_adgroups_by_adcampaign(CAMPAIGN_ID)
self.assertNotIn('error', response)
def test_get_adcreative(self):
response = self.api.get_adcreative(CREATIVE_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adcreatives(self):
response = self.api.get_adcreatives(ACCOUNT_ID, ['id'])
self.assertNotIn('error', response)
def test_get_adimages(self):
response = self.api.get_adimages(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_adimages_by_hashes(self):
response = self.api.get_adimages(ACCOUNT_ID, [
'1026f5ca40f31a8808732e2c59817c3d',
'0d65031128a5126fba23f4085f9b5256'])
self.assertNotIn('error', response)
def test_get_stats_by_adaccount(self):
response = self.api.get_stats_by_adaccount(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_stats_by_adcampaign(self):
response = self.api.get_stats_by_adcampaign(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_stats_by_adgroup(self):
response = self.api.get_stats_by_adgroup(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_stats_by_adgroup_with_ids(self):
response = self.api.get_stats_by_adgroup(ACCOUNT_ID, [GROUP_ID])
self.assertNotIn('error', response)
def test_get_adreport_stats(self):
response = self.api.get_adreport_stats(
ACCOUNT_ID, 'last_28_days', 'all_days', ['account_id'])
self.assertNotIn('error', response)
def test_get_conversion_stats_by_adaccount(self):
response = self.api.get_conversion_stats_by_adaccount(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_conversion_stats_by_adcampaign(self):
response = self.api.get_conversion_stats_by_adcampaign(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_conversion_stats_by_adgroup(self):
response = self.api.get_conversion_stats_by_adgroup(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_conversion_stats(self):
response = self.api.get_conversion_stats(GROUP_ID)
self.assertNotIn('error', response)
def test_get_offsite_pixel(self):
response = self.api.get_offsite_pixel(OFFSITE_PIXEL_ID)
self.assertNotIn('error', response)
def test_get_offsite_pixels(self):
response = self.api.get_offsite_pixels(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_keyword_stats(self):
response = self.api.get_keyword_stats(GROUP_ID)
self.assertNotIn('error', response)
def test_get_ratecard(self):
response = self.api.get_ratecard(ACCOUNT_ID)
self.assertNotIn('error', response)
def test_get_reach_estimate(self):
targeting_spec = {'countries': ['KR']}
response = self.api.get_reach_estimate(
ACCOUNT_ID, 'KRW', targeting_spec)
self.assertNotIn('error', response)
def test_get_adcampaign_list(self):
responses = self.api.get_adcampaign_list(ACCOUNT_ID)
for response in responses:
self.assertNotIn('error', response)
def test_get_adcampaign_detail(self):
responses = self.api.get_adcampaign_detail(
ACCOUNT_ID, CAMPAIGN_ID, 'last_28_days')
for response in responses:
self.assertNotIn('error', response)
def test_get_user_pages(self):
response = self.api.get_user_pages(USER_ID)
self.assertNotIn('error', response)
def test_get_page_access_token(self):
response = self.api.get_page_access_token(PAGE_ID)
self.assertNotIn('error', response)
def test_get_autocomplete_data(self):
response = self.api.get_autocomplete_data("", 'adcountry', limit=1000)
self.assertNotIn('error', response)
def test_create_link_page_post(self):
response = self.api.create_link_page_post(
PAGE_ID, 'http://www.youtube.com/watch?v=JJXuBSx_1yE',
'Link page post creation test',
)
self.assertNotIn('error', response)
def test_create_link_page_post_with_custom_image(self):
try:
thumbnail = open('kodim23.png', 'rb')
response = self.api.create_link_page_post(
PAGE_ID, 'http://www.virect.com/', thumbnail=thumbnail
)
self.assertNotIn('error', response)
except facebook.AdsAPIError as e:
print e.message
def test_create_video_page_post(self):
try:
source = open('afm.mp4', 'rb')
response = self.api.create_video_page_post(PAGE_ID, source=source)
self.assertNotIn('error', response)
except facebook.AdsAPIError as e:
print e.message
def test_create_adcampaign(self):
response = self.api.create_adcampaign(
ACCOUNT_ID, 'Test Campaign', 1, 100)
self.assertNotIn('error', response)
def test_create_adcreative_type_27(self):
response = self.api.create_adcreative_type_27(
ACCOUNT_ID, PAGE_ID, story_id=STORY_ID,
name='Test Type 27 Ad Creative')
self.assertNotIn('error', response)
def test_create_adgroup(self):
targeting = {'countries': ['KR']}
conversion_specs = [{"action.type": ["offsite_conversion"],
"offsite_pixel": [OFFSITE_PIXEL_ID]}]
response = self.api.create_adgroup(
ACCOUNT_ID, 'Test Ad Group', 'ABSOLUTE_OCPM', {'ACTIONS': 1000},
CAMPAIGN_ID, CREATIVE_ID, targeting, conversion_specs
)
self.assertNotIn('error', response)
def test_create_offsite_pixel(self):
response = self.api.create_offsite_pixel(
ACCOUNT_ID, 'Test Pixel', 'CHECKOUT')
self.assertNotIn('error', response)
if __name__ == '__main__':
unittest.main()