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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
155 additions
and
83 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,8 +510,27 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
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
,
primaryKey
:
true
...
...
@@ -524,41 +543,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}
})
Profile
.
belongsTo
(
User
,
{
foreignKey
:
Profile
.
rawAttributes
.
user_id
})
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
})
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
}
});
Profile
.
belongsTo
(
User
,
{
foreignKey
:
Profile
.
rawAttributes
.
user_id
})
Task
.
belongsTo
(
User
,
{
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
(
Task
.
rawAttributes
.
userUid
.
allowNull
).
to
.
be
.
false
;
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
}
});
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,8 +2206,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
var
Task
=
this
.
sequelize
.
define
(
'task'
,
{})
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'
,
{});
User
.
hasMany
(
Task
,
{
foreignKey
:
{
fieldName
:
'uid'
,
allowNull
:
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
:
{
allowNull
:
false
}
});
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
,
defaultValue
:
42
}
})
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
...
...
@@ -2215,33 +2256,29 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}
});
User
.
hasMany
(
Task
,
{
foreignKey
:
{
fieldName
:
'user_id'
,
allowNull
:
false
}
});
expect
(
Task
.
rawAttributes
.
user_id
.
allowNull
).
to
.
be
.
false
;
User
.
hasMany
(
Project
,
{
foreignKey
:
Project
.
rawAttributes
.
user_id
});
Task
.
hasMany
(
User
,
{
foreignKey
:
{
allowNull
:
false
}
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
);
});
expect
(
Task
.
associations
.
tasksusers
.
through
.
rawAttributes
.
taskId
.
allowNull
).
to
.
be
.
false
;
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
}
});
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
(
Project
,
{
foreignKey
:
Project
.
rawAttributes
.
user_id
});
User
.
hasMany
(
Task
,
{
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
.
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,41 +471,69 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
})
it
(
'allows the user to provide an attribute definition as foreignKey'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
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
,
primaryKey
:
true
}
})
,
Profile
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
false
}
})
User
.
hasOne
(
Profile
,
{
foreignKey
:
Profile
.
rawAttributes
.
user_id
})
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
;
});
it
(
'works when merging with an existing definition'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
uid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
}
})
,
Pro
file
=
this
.
sequelize
.
define
(
'project'
,
{
user
_
id
:
{
,
Pro
ject
=
this
.
sequelize
.
define
(
'project'
,
{
user
U
id
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
false
defaultValue
:
42
}
})
})
;
User
.
hasOne
(
Profile
,
{
foreignKey
:
Profile
.
rawAttributes
.
user_id
})
User
.
hasOne
(
Project
,
{
foreignKey
:
{
allowNull
:
false
}});
var
Project
=
this
.
sequelize
.
define
(
'project'
,
{
user_id
:
{
type
:
Sequelize
.
INTEGER
}
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
);
});
User
.
hasOne
(
Project
,
{
foreignKey
:
{
allowNull
:
false
}
})
expect
(
Project
.
rawAttributes
.
userUid
.
allowNull
).
to
.
be
.
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
})
});
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