@@ -49,4 +49,100 @@ describe('Model', () => {
4949 } ) ;
5050 } ) ;
5151 } ) ;
52+
53+ describe ( '.isFiedNumber' , ( ) => {
54+ it ( 'returns true when the field is numeric' , ( ) => {
55+ model = context . getModel ( 'post' ) ;
56+ expect ( Model . isFieldNumber ( model . fields . get ( 'otherId' ) ) ) . toEqual ( true ) ;
57+ expect ( Model . isFieldNumber ( model . fields . get ( 'id' ) ) ) . toEqual ( true ) ;
58+ } ) ;
59+
60+ it ( 'returns false when the field is not numeric' , ( ) => {
61+ model = context . getModel ( 'post' ) ;
62+ expect ( Model . isFieldNumber ( model . fields . get ( 'title' ) ) ) . toEqual ( false ) ;
63+ expect ( Model . isFieldNumber ( model . fields . get ( 'user' ) ) ) . toEqual ( false ) ;
64+ } ) ;
65+ } ) ;
66+
67+ describe ( '.isFieldAttribute' , ( ) => {
68+ it ( 'returns true when the field is a attribute' , ( ) => {
69+ model = context . getModel ( 'post' ) ;
70+ expect ( Model . isFieldAttribute ( model . fields . get ( 'title' ) ) ) . toEqual ( true ) ;
71+ expect ( Model . isFieldAttribute ( model . fields . get ( 'id' ) ) ) . toEqual ( true ) ;
72+ expect ( Model . isFieldAttribute ( model . fields . get ( 'userId' ) ) ) . toEqual ( true ) ;
73+ } ) ;
74+
75+ it ( 'returns false when the field is a relation' , ( ) => {
76+ model = context . getModel ( 'post' ) ;
77+ expect ( Model . isFieldAttribute ( model . fields . get ( 'user' ) ) ) . toEqual ( false ) ;
78+ } ) ;
79+ } ) ;
80+
81+ describe ( '.augment' , ( ) => {
82+ it ( 'adds $isPersited to the fields' , ( ) => {
83+ // FIXME how to test that?
84+ } ) ;
85+ } ) ;
86+
87+ describe ( '.skipField' , ( ) => {
88+ it ( 'returns true for a field which starts with a $' , ( ) => {
89+ const model = context . getModel ( 'post' ) ;
90+ expect ( model . skipField ( '$isPersisted' ) ) . toEqual ( true ) ;
91+ } ) ;
92+
93+ it ( 'returns true for a field which is listed within skipFields' , ( ) => {
94+ const model = context . getModel ( 'video' ) ;
95+ expect ( model . skipField ( 'ignoreMe' ) ) . toEqual ( true ) ;
96+ } ) ;
97+
98+ it ( 'returns true for a field which is the foreignKey of a belongsTo or hasOne relation' , ( ) => {
99+ const model = context . getModel ( 'post' ) ;
100+ expect ( model . skipField ( 'userId' ) ) . toEqual ( true ) ;
101+ } ) ;
102+
103+ it ( 'returns false for normal fields' , ( ) => {
104+ const model = context . getModel ( 'post' ) ;
105+ expect ( model . skipField ( 'id' ) ) . toEqual ( false ) ;
106+ expect ( model . skipField ( 'title' ) ) . toEqual ( false ) ;
107+ } ) ;
108+ } ) ;
109+
110+ describe ( '.isTypeFieldOfPolymorphicRelation' , ( ) => {
111+ it ( 'returns true for the type field of a polymorphic relation' , ( ) => {
112+ const model = context . getModel ( 'comment' ) ;
113+ expect ( model . isTypeFieldOfPolymorphicRelation ( 'subjectType' ) ) . toEqual ( true ) ;
114+ } ) ;
115+
116+ it ( 'returns false for a normal attribute which just ends with `Type`' , ( ) => {
117+ const model = context . getModel ( 'contract' ) ;
118+ expect ( model . isTypeFieldOfPolymorphicRelation ( 'contractType' ) ) . toEqual ( false ) ;
119+ } ) ;
120+ } ) ;
121+
122+ describe ( '.getRecordWithId' , ( ) => {
123+ it ( 'returns the record with the id of the model type' , ( ) => {
124+ const model = context . getModel ( 'post' ) ;
125+ const expectedRecord = model . baseModel . query ( ) . withAllRecursive ( ) . where ( 'id' , 2 ) . first ( ) ;
126+ expect ( model . getRecordWithId ( 2 ) ) . toEqual ( expectedRecord ) ;
127+ } ) ;
128+ } ) ;
129+
130+ describe ( '.shouldEagerLoadRelation' , ( ) => {
131+ it ( 'returns true if field is a belongsTo or hasOne relation' , ( ) => {
132+ const model = context . getModel ( 'post' ) ;
133+ expect ( model . shouldEagerLoadRelation ( model . fields . get ( 'user' ) , context . getModel ( 'user' ) ) ) . toEqual ( true ) ;
134+
135+ // TODO test hasOne
136+ } ) ;
137+
138+ it ( 'returns true if field is in the eagerLoad array' , ( ) => {
139+ const model = context . getModel ( 'post' ) ;
140+ expect ( model . shouldEagerLoadRelation ( model . fields . get ( 'comments' ) , context . getModel ( 'comment' ) ) ) . toEqual ( true ) ;
141+ } ) ;
142+
143+ it ( 'returns false if field neither belongsTo/hasOne nor in the eagerLoad array' , ( ) => {
144+ const model = context . getModel ( 'user' ) ;
145+ expect ( model . shouldEagerLoadRelation ( model . fields . get ( 'comments' ) , context . getModel ( 'comment' ) ) ) . toEqual ( false ) ;
146+ } ) ;
147+ } ) ;
52148} ) ;
0 commit comments