Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 60943808
authored
Sep 01, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a test for merging of existing attribute with foreign key attribute
1 parent
13b436cf
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
122 additions
and
50 deletions
test/associations/belongs-to.test.js
test/associations/has-many.test.js
test/associations/has-one.test.js
test/include/findAll.test.js
test/support.js
test/associations/belongs-to.test.js
View file @
6094380
...
...
@@ -510,7 +510,26 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
describe
(
'allows the user to provide an attribute definition object as foreignKey'
,
function
()
{
it
(
'works with a column that hasnt been defined before'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
});
Task
.
belongsTo
(
User
,
{
foreignKey
:
{
allowNull
:
false
,
fieldName
:
'uid'
}
});
expect
(
Task
.
rawAttributes
.
uid
).
to
.
be
.
defined
expect
(
Task
.
rawAttributes
.
uid
.
allowNull
).
to
.
be
.
false
;
expect
(
Task
.
rawAttributes
.
uid
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Task
.
rawAttributes
.
uid
.
referencesKey
).
to
.
equal
(
'id'
)
});
it
(
'works when taking a column directly from the object'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
...
...
@@ -530,35 +549,23 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
expect
(
Profile
.
rawAttributes
.
user_id
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Profile
.
rawAttributes
.
user_id
.
referencesKey
).
to
.
equal
(
'uid'
)
expect
(
Profile
.
rawAttributes
.
user_id
.
allowNull
).
to
.
be
.
false
})
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
}
});
Task
.
belongsTo
(
User
,
{
foreignKey
:
{
allowNull
:
false
}
});
expect
(
Task
.
rawAttributes
.
userUid
.
allowNull
).
to
.
be
.
false
;
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
it
(
'works when merging with an existing definition'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{
projectId
:
{
defaultValue
:
42
,
type
:
Sequelize
.
INTEGER
}
});
})
,
Project
=
this
.
sequelize
.
define
(
'project'
,
{});
Project
.
belongsTo
(
User
,
{
foreignKey
:
Project
.
rawAttributes
.
user_id
});
Task
.
belongsTo
(
Project
,
{
foreignKey
:
{
allowNull
:
true
}
});
expect
(
Project
.
rawAttributes
.
user_id
.
references
).
to
.
equal
(
User
.
getTableName
());
expect
(
Project
.
rawAttributes
.
user_id
.
referencesKey
).
to
.
equal
(
'uid'
);
expect
(
Task
.
rawAttributes
.
projectId
).
to
.
be
.
defined
expect
(
Task
.
rawAttributes
.
projectId
.
defaultValue
).
to
.
equal
(
42
);
expect
(
Task
.
rawAttributes
.
projectId
.
allowNull
).
to
.
be
.
ok
;
})
});
it
(
'should throw an error if foreignKey and as result in a name clash'
,
function
()
{
...
...
test/associations/has-many.test.js
View file @
6094380
...
...
@@ -2206,23 +2206,22 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
describe
(
'allows the user to provide an attribute definition object as foreignKey'
,
function
()
{
it
(
'works with a column that hasnt been defined before'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
}
});
,
User
=
this
.
sequelize
.
define
(
'user'
,
{});
User
.
hasMany
(
Task
,
{
foreignKey
:
{
fieldName
:
'user_
id'
,
fieldName
:
'u
id'
,
allowNull
:
false
}
});
expect
(
Task
.
rawAttributes
.
user_id
.
allowNull
).
to
.
be
.
false
;
expect
(
Task
.
rawAttributes
.
uid
).
to
.
be
.
defined
;
expect
(
Task
.
rawAttributes
.
uid
.
allowNull
).
to
.
be
.
false
;
expect
(
Task
.
rawAttributes
.
uid
.
references
).
to
.
equal
(
User
.
getTableName
());
expect
(
Task
.
rawAttributes
.
uid
.
referencesKey
).
to
.
equal
(
'id'
);
Task
.
hasMany
(
User
,
{
foreignKey
:
{
...
...
@@ -2230,18 +2229,56 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}
});
expect
(
Task
.
rawAttributes
.
uid
).
not
.
to
.
be
.
defined
;
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
taskId
).
to
.
be
.
defined
;
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
taskId
.
allowNull
).
to
.
be
.
false
;
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
taskId
.
references
).
to
.
equal
(
Task
.
getTableName
());
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
taskId
.
referencesKey
).
to
.
equal
(
'id'
);
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
uid
).
to
.
be
.
defined
;
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
uid
.
allowNull
).
to
.
be
.
false
;
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
uid
.
references
).
to
.
equal
(
User
.
getTableName
());
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
uid
.
referencesKey
).
to
.
equal
(
'id'
);
});
it
(
'works when taking a column directly from the object'
,
function
()
{
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
type
:
Sequelize
.
INTEGER
,
defaultValue
:
42
}
})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
}
});
User
.
hasMany
(
Project
,
{
foreignKey
:
Project
.
rawAttributes
.
user_id
});
expect
(
Project
.
rawAttributes
.
user_id
).
to
.
be
.
defined
;
expect
(
Project
.
rawAttributes
.
user_id
.
references
).
to
.
equal
(
User
.
getTableName
());
expect
(
Project
.
rawAttributes
.
user_id
.
referencesKey
).
to
.
equal
(
'uid'
);
expect
(
Project
.
rawAttributes
.
user_id
.
defaultValue
).
to
.
equal
(
42
);
});
it
(
'works when merging with an existing definition'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{
userId
:
{
defaultValue
:
42
,
type
:
Sequelize
.
INTEGER
}
})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{});
User
.
hasMany
(
Task
,
{
foreignKey
:
{
allowNull
:
true
}});
expect
(
Task
.
rawAttributes
.
userId
).
to
.
be
.
defined
;
expect
(
Task
.
rawAttributes
.
userId
.
defaultValue
).
to
.
equal
(
42
);
expect
(
Task
.
rawAttributes
.
userId
.
allowNull
).
to
.
be
.
ok
;
});
});
it
(
'should throw an error if foreignKey and as result in a name clash'
,
function
()
{
...
...
test/associations/has-one.test.js
View file @
6094380
...
...
@@ -471,7 +471,25 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
})
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
describe
(
'allows the user to provide an attribute definition object as foreignKey'
,
function
()
{
it
(
'works with a column that hasnt been defined before'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{})
,
Profile
=
this
.
sequelize
.
define
(
'project'
,
{})
User
.
hasOne
(
Profile
,
{
foreignKey
:
{
allowNull
:
false
,
fieldName
:
'uid'
}
});
expect
(
Profile
.
rawAttributes
.
uid
).
to
.
be
.
defined
expect
(
Profile
.
rawAttributes
.
uid
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Profile
.
rawAttributes
.
uid
.
referencesKey
).
to
.
equal
(
'id'
)
expect
(
Profile
.
rawAttributes
.
uid
.
allowNull
).
to
.
be
.
false
;
});
it
(
'works when taking a column directly from the object'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
...
...
@@ -487,25 +505,35 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
User
.
hasOne
(
Profile
,
{
foreignKey
:
Profile
.
rawAttributes
.
user_id
})
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
}
expect
(
Profile
.
rawAttributes
.
user_id
).
to
.
be
.
defined
expect
(
Profile
.
rawAttributes
.
user_id
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Profile
.
rawAttributes
.
user_id
.
referencesKey
).
to
.
equal
(
'uid'
)
expect
(
Profile
.
rawAttributes
.
user_id
.
allowNull
).
to
.
be
.
false
;
});
User
.
hasOne
(
Project
,
{
foreignKey
:
{
allowNull
:
false
it
(
'works when merging with an existing definition'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
}
})
,
Project
=
this
.
sequelize
.
define
(
'project'
,
{
userUid
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
42
}
});
expect
(
Project
.
rawAttributes
.
userUid
.
allowNull
).
to
.
be
.
false
;
User
.
hasOne
(
Project
,
{
foreignKey
:
{
allowNull
:
false
}})
;
expect
(
Profile
.
rawAttributes
.
user_id
).
to
.
be
.
defined
expect
(
Profile
.
rawAttributes
.
user_id
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Profile
.
rawAttributes
.
user_id
.
referencesKey
).
to
.
equal
(
'uid'
)
expect
(
Profile
.
rawAttributes
.
user_id
.
allowNull
).
to
.
be
.
false
})
expect
(
Project
.
rawAttributes
.
userUid
).
to
.
be
.
defined
;
expect
(
Project
.
rawAttributes
.
userUid
.
allowNull
).
to
.
be
.
false
;
expect
(
Project
.
rawAttributes
.
userUid
.
references
).
to
.
equal
(
User
.
getTableName
())
expect
(
Project
.
rawAttributes
.
userUid
.
referencesKey
).
to
.
equal
(
'uid'
)
expect
(
Project
.
rawAttributes
.
userUid
.
defaultValue
).
to
.
equal
(
42
);
});
});
it
(
'should throw an error if an association clashes with the name of an already define attribute'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
...
...
test/include/findAll.test.js
View file @
6094380
...
...
@@ -935,7 +935,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
it
(
"should be possible to define a belongsTo include as required with child hasMany not required"
,
function
(
done
)
{
it
.
only
(
"should be possible to define a belongsTo include as required with child hasMany not required"
,
function
(
done
)
{
var
S
=
this
.
sequelize
,
Address
=
S
.
define
(
'Address'
,
{
'active'
:
DataTypes
.
BOOLEAN
})
,
Street
=
S
.
define
(
'Street'
,
{
'active'
:
DataTypes
.
BOOLEAN
})
...
...
test/support.js
View file @
6094380
...
...
@@ -74,7 +74,7 @@ var Support = {
var
sequelizeOptions
=
_
.
defaults
(
options
,
{
host
:
options
.
host
||
config
.
host
,
logging
:
false
,
//
logging: false,
dialect
:
options
.
dialect
,
port
:
options
.
port
||
process
.
env
.
SEQ_PORT
||
config
.
port
,
pool
:
config
.
pool
,
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment