forked from gabrielfalcao/jquery-yql
/
test.yql.js
126 lines (103 loc) · 4.11 KB
/
test.yql.js
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
module("YQL");
var oldAjax = $.ajax;
test('It should recover data from a given query', function() {
expect(5);
$.ajax = function (params){
equal(params.url, "http://query.yahooapis.com/v1/public/yql");
equal(params.dataType, "jsonp", "dataType of the request should be jsonp");
equal(params.async, true);
same(params.data, {format: "json", callback: "?", q: "SELECT * FROM something", env: 'store://datatables.org/alltableswithkeys'});
params.success("it should be that huge JSON");
}
$.yql(
'SELECT * FROM something',
function (data){
equal(data, "it should be that huge JSON");
}
);
$.ajax = oldAjax;
});
asyncTest('It should fetch the right jsonp from yahoo api', function() {
expect(2);
$.yql(
'SELECT * FROM weather.forecast WHERE location=90210',
function (data){
equal(typeof data.query.results, "object", "the query result should be a javascript object");
equal(typeof data.query.results.channel.wind, "object", "the result contains wind-related data");
start();
}
);
});
test('It should replace variables in the query string', function () {
expect(1);
$.ajax = function (params) {
equal(params.data.q, "SELECT * FROM weather.forecast WHERE location=90210");
};
$.yql(
'SELECT * FROM weather.forecast WHERE location=#{location}',
{location: '90210'},
function (data) { }
)
$.ajax = oldAjax;
});
test('It should replace another variable in the query string', function () {
expect(1);
$.ajax = function (params) {
equal(params.data.q, 'SELECT * FROM woman WHERE hairColor="red"');
};
$.yql(
'SELECT * FROM woman WHERE hairColor=#{color}',
{color: 'red'},
function (data) { }
)
$.ajax = oldAjax;
});
asyncTest('Querying a yahoo official table (flickr)', function () {
expect(3);
$.yql("SELECT * FROM flickr.photos.recent", function (data) {
ok(data.query.results.photo.length > 0, "Cool, the result ain't empty")
ok(data.query.results.photo[0].farm, "Has farm info")
ok(data.query.results.photo[0].title, "Has title")
start();
});
});
asyncTest('Querying a community table (github)', function () {
expect(1);
$.yql("SELECT * FROM github.repo WHERE id='gabrielfalcao' AND repo='jquery-yql'", function (data) {
equal(data.query.results.repository.url, "http://github.com/gabrielfalcao/jquery-yql");
start();
});
});
test('It should cast into integer only when is suitable', function() {
expect(1);
$.ajax = function (params){
equals(params.data.q, 'SELECT * FROM flickr.photos.search WHERE lat = "-12.55" AND lon = "100.23"');
}
$.yql(
"SELECT * FROM flickr.photos.search WHERE lat = #{latitude} AND lon = #{longitude}",
{
latitude: "-12.55",
longitude: "100.23",
}
);
$.ajax = oldAjax;
});
asyncTest('It can fetch lastfm data', function() {
expect(1);
$.yql(
"SELECT * FROM lastfm.geo.getevents WHERE api_key=#{key} AND lat=#{latitude} AND long=#{longitude}",
{
key: "5a2242815c81e144c48b34716014ec7e",
latitude: "-23.550511",
longitude: "-46.633428"
},
function (data){
try {
equal(data.query.results.lfm.status, "ok");
}catch (e) {
ok(false, "should not reach here");
}
start();
}
);
});