Skip to content

Commit 4d2a3d6

Browse files
committed
Add test
1 parent 1262c8d commit 4d2a3d6

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
component table="inventory_batches" extends="quick.models.BaseEntity" accessors="true" {
2+
3+
property name="id";
4+
5+
function scopeAddTradeCount( qb ) {
6+
qb.selectRaw(" (SELECT count(id) FROM trades WHERE trades.inventoryBatchId = inventory_batches.id) as tradeCount");
7+
appendVirtualAttribute( "tradeCount" );
8+
}
9+
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
component {
2+
3+
function up( schema, qb ) {
4+
schema.create( "inventory_batches", function( t ) {
5+
t.unsignedInteger( "id" ).primaryKey();
6+
t.string( "name" );
7+
} );
8+
9+
schema.create( "trades", function( t ) {
10+
t.increments( "id" );
11+
t.unsignedInteger( "inventoryBatchId" ).references( "id" ).onTable( "inventory_batches" );
12+
} );
13+
14+
qb.newQuery().table( "inventory_batches" ).insert( [
15+
{ "id": 1, "name": "Batch 1" },
16+
{ "id": 2, "name": "Batch 2" },
17+
{ "id": 3, "name": "Batch 3" }
18+
] );
19+
20+
qb.table( "trades" ).insert( [
21+
{ "id": 1, "inventoryBatchId": 1 },
22+
{ "id": 2, "inventoryBatchId": 1 },
23+
{ "id": 3, "inventoryBatchId": 2 },
24+
{ "id": 4, "inventoryBatchId": 3 },
25+
{ "id": 5, "inventoryBatchId": 3 },
26+
{ "id": 6, "inventoryBatchId": 3 }
27+
] );
28+
}
29+
30+
function down( schema, qb ) {
31+
schema.drop( "trades" );
32+
schema.drop( "inventory_batches" );
33+
}
34+
35+
}

tests/specs/integration/BaseEntity/AttributeSpec.cfc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ component extends="tests.resources.ModuleIntegrationSpec" {
1717
expect( attributesFromFresh ).notToInclude( "latestPostId" );
1818
} );
1919

20+
it( "can load virtual attributes from a query", () => {
21+
var batches = getInstance( "InventoryBatch" )
22+
.addTradeCount()
23+
.orderByAsc( "id" )
24+
.get();
25+
26+
expect( batches ).toBeArray();
27+
expect( batches ).toHaveLength( 3 );
28+
29+
expect( batches[ 1 ].getId() ).toBe( 1 );
30+
expect( batches[ 1 ].getTradeCount() ).toBe( 2 );
31+
32+
expect( batches[ 2 ].getId() ).toBe( 2 );
33+
expect( batches[ 2 ].getTradeCount() ).toBe( 1 );
34+
35+
expect( batches[ 3 ].getId() ).toBe( 3 );
36+
expect( batches[ 3 ].getTradeCount() ).toBe( 3 );
37+
} );
38+
2039
it( "can get any attribute using the `getColumnName` magic methods", function() {
2140
var user = getInstance( "User" ).find( 1 );
2241
expect( user.getId() ).toBe( 1 );

0 commit comments

Comments
 (0)