Skip to content

Instantly share code, notes, and snippets.

@mrios
Created October 30, 2012 17:04
Show Gist options
  • Save mrios/3981559 to your computer and use it in GitHub Desktop.
Save mrios/3981559 to your computer and use it in GitHub Desktop.
Test Csonv Relational
test("Csonv Relational: 1. Has One"
, function()
{
var url1=
"assets/favorites/books.csv"
var url2=
"assets/favorites/authors.csv"
stop()
can.when(
can.ajax({url: url1})
, can.ajax({url: url2})
).then(
function(csv_data1, csv_data2){
var csv_data=[]
csv_data[0]={data: csv_data1[0], url: url1 }
csv_data[1]={data: csv_data2[0], url: url2 }
var options=
{
//relation: "integer;string;books:n:author"
}
var result=
Csonv.merge_data(csv_data, options)
console.log("Books", result)
ok(result, "Csonv Relational 1. has one OK");
start()
}
)
}
)
module("sru/fixtures/store2"
, {
setup:
function()
{
var line_separator
= '\n'
, field_separator
= ','
, unquote
= function(item)
{
return item.replace(/^"|"$/g,"")
}
, toCsv
= function(raw_text)
{
var lines
= raw_text
.split(line_separator)
, headers
= lines[0]
//.shift()
.split(field_separator)
.map(unquote)
return lines
.map(
function(line)
{
var result ={}
, fields
return line
.split(field_separator)
.map(unquote)
}
)
}
, toJson
= function(csv_data)
{
var lines
= csv_data
, headers
= lines
.shift()
return lines
.map(
function(line)
{
var result ={}
, fields
= line
.map(unquote)
can
.each(
headers
, function(header,index)
{
result[header.toLowerCase()]=fields[index]
}
)
return result
}
)
}
can.ajaxPrefilter(
function(options)
{
return (/\.csv/.test(options.url))
?'csv-json'
:options.dataType
}
)
can.ajaxSetup(
{
accepts:
{
'csv': "text/csv"
, 'csv-json': "text/csv"
}
, contents:
{
'csv': /csv/
, 'csv-json': /csv-json/
}
, converters:
{
"text csv":toCsv
, "csv csv-json":toJson
}
}
)
function fix_pipe(data)
{
return data
}
can.Construct(
'SRU.fixtures.store2'
, {
defaults:
{
line_separator:'\n'
, field_separator:','
}
, union:
function()
{
var args
= arguments
return can.when
.apply(
null
, can.map(
args
, this._get_one
)
).pipe(
function()
{
return Array
.prototype
.concat
.apply([],arguments)
}
)
}
, join:
function(left_dfd,right_url,pred,fix_join)
{
var self
= this
return can.when(
left_dfd
, self.get(right_url)
)
.then(
function(data_left, data_right)
{
return can.map(
data_left[0]
, function(lobj)
{
var prop='joined'
lobj[prop]
= can
.grep(
data_right[0]
, function(rval)
{
return pred(lobj,rval)
}
)
return fix_join(lobj)
}
)
}
)
}
, get:
function(url)
{
return this._get_one(url)
}
, _get_one:
function(what)
{
return can
.ajax(what)
//.pipe(toJson)
}
}
, {
init:
function(options)
{
this.options=can.extend(options,this.constructor.defaults)
this.results=can.Deferred()
this.results.resolveWith(this)
this.results.promise(this)
}
, start:
function()
{
return can
.when
.apply(
null
, can
.map(
this.deferreds
, function()
{
return can.isDeferred(dfd)
?dfd
:can.Deferred(dfd)
}
)
)
}
}
)
}
}
)
test("csv2json ajax convert"
, function()
{
stop();
var store= can.ajax(
"assets/instituciones_ot.csv"
)
.then(
function(items)
{
ok(items,'OK')
start()
return items
}
)
}
)
test("Store2-Deferreds-get"
, function()
{
stop();
var store
= SRU.fixtures
.store2
.get(
"assets/instituciones_ot.csv"
).then(
function(items)
{
ok(items,'OK')
start()
return items
}
)
}
)
test("Store2-Deferreds-union"
, function()
{
stop();
var store
= SRU.fixtures
.store2
.union(
"assets/instituciones_ot.csv"
, "assets/instituciones_univ.csv"
).then(
function(items)
{
ok(items,'OK')
start()
return items
}
)
}
)
test("Store2-Deferreds-join"
, function()
{
stop();
var join
= SRU.fixtures
.store2
.join(
SRU.fixtures
.store2
.get(
"assets/provincias.csv"
)
, "assets/instituciones_univ.csv"
, function(provincia, institucion)
{
return institucion.provincia==provincia.id_provincia
}
, function(item)
{
item.instituciones=item.joined
delete item.joined
return item
}
)
.then(
function(items)
{
ok(items[0].joined==undefined,'OK')
ok(items[0].instituciones,'OK')
start()
}
)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment