Iridium icon indicating copy to clipboard operation
Iridium copied to clipboard

problem creating instance

Open SET001 opened this issue 9 years ago • 4 comments
trafficstars

In my game I have a chunk entity which declared something like this:

export interface Chunk{
    _id?: string
    name: string
    entities: GameEntity[]
    position: Vector2
    size: Vector2
}

@Index({ name: 1 })
@Collection('chunks')
export class Chunk extends Instance<Interfaces.Chunk, Chunk> implements Interfaces.Chunk{
    @ObjectID _id: string = ''
    @Property(String) name: string = ''
    @Property(GameEntity) entities: Interfaces.GameEntity[]
    @Property(Vector2) position: Vector2 = new Vector2()
    @Property(Vector2) size: Vector2 = new Vector2()
}

Now I want to have some unit test for Chunk and for this I have to make Chunk instance. From Iridium docs

Iridium models are created with a reference to their Core (which provides the database connection) and an InstanceType which is composed of a constructor function as well as a number of static properties providing configuration information for the instance.

So I'm trying something like this

var storage = kernel.default.getNamed<Interfaces.Storage>('IService', 'Storage');
var chunk = new Model<Interfaces.Chunk, Chunk>(storage, Chunk);

and then got error

Argument of type 'Storage' is not assignable to parameter of type 'Core'. Property 'mongoConnectAsyc' is missing in type 'Storage'.

My storage declaration is simple as this

export class Storage extends Core implements Iterfaces.Storage{
    Accounts: Model<Iterfaces.Account, Account> = new Model<Iterfaces.Account, Account>(this, Account);
    Players: Model<Iterfaces.Player, Player> = new Model<Iterfaces.Player, Player>(this, Player);
    Chunks: Model<Iterfaces.Chunk, Chunk> = new Model<Iterfaces.Chunk, Chunk>(this, Chunk);
    init(){

    }
}

why am I getting that error and what am I doing wrong?

SET001 avatar Aug 30 '16 17:08 SET001

I suspect it's because your Interfaces.Storage doesn't inherit from Iridium.Core, I'd imagine that your code would actually run correctly in other words, its just the typescript compiler which is unhappy.

notheotherben avatar Aug 30 '16 17:08 notheotherben

Thank you. That helps. But now I have even more questions ) Currently I have this code to create my test instance:

var storage = kernel.default.getNamed<Interfaces.Storage>('IService', 'Storage');
var ChunkModel = new Model<Interfaces.Chunk, Chunk>(storage, Chunk);
var chunk = new ChunkModel.Instance({
    name: 'test',
    entities: [],
    position: new Vector2(),
    size: new Vector2()
})

Which look a bit awkward.. Instead of just having class and class instance, we now have Interface, Class, Model and Instance for each entity. Cant this be a bit easier? Normally I would like to have something as easy as :

interface IChunk extends Iridum.Whatever{
...
}

class Chunk implements IChunk{

}

chunk  = new Chunk()

Also why it does not use default values and I have to set up them manually in ChunkModel.Instance. Doing this each time I need new instance is quite unhandy.

SET001 avatar Aug 31 '16 16:08 SET001

I guess it depends on what you're testing, if you don't rely on the ability to save to the database, then you should easily be able to do the following:

const readonly DefaultChunkData = {
    name: 'test',
    entities: [],
    position: new Vector2(),
    size: new Vector2()
}

const ChunkModel = new Model<Interfaces.Chunk, Chunk>(null, Chunk);
let chunk = new ChunkModel.Instance(DefaultChunkData);

On the topic of default values, I assume you're referring to the presence of values on your property definitions? Something like this:

class Chunk {
  x: string = "default"
}

In that case, I recall there being a reason for them not being handled - will need to find the issue in which I delve into it. It has to do with the way bulk inserts are handled from what I recall.

notheotherben avatar Aug 31 '16 16:08 notheotherben

Yes I need to test basic logic of some class that in turn need chunk as parameter so I don't need anything database related in my tests. But seems like I can't put null instead of core object:

ChunkModel = new Model<Interfaces.Chunk, Chunk>(null, Chunk);

You failed to provide a valid Iridium core for this model

SET001 avatar Aug 31 '16 16:08 SET001