Skip to content

Commit

Permalink
Add/update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sha256 committed Dec 20, 2023
1 parent 11c212c commit dd45434
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion test/models/ModelWithQueryMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ModelWithQueryMethod extends Model<ModelWithQueryMethod>() {
password?: string

static findByEmail(email: string){
return ModelWithQueryMethod.find({email: email} as any).one()
return this.find({email: email} as any).one()
}

}
23 changes: 22 additions & 1 deletion test/models/ModelWithRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ export class Address extends Model<Address>(){
}


@model()
export class Product extends Model<Product>(){
@field()
title?: string
}

@model()
export class User extends Model<User>(){
@field()
name?: string

@field({ref: [Product]})
products?: Ref<Product>[]
}

@model()
export class ModelWithRef extends Model<ModelWithRef>() {

Expand All @@ -21,7 +36,13 @@ export class ModelWithRef extends Model<ModelWithRef>() {
@field()
name?: string

@field({type: Address})
@field({ref: Address})
address?: Ref<Address>

@field({ref: [Product]})
products?: Ref<Product>[]

@field({type: [User]})
users?: User[]

}
53 changes: 53 additions & 0 deletions test/tests/populate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {Address, ModelWithRef, Product, User} from "../models/ModelWithRef";


it('should populate 1 level', async () => {
const address = new Address({city: "Saint Martins2"})
await address.save()
const ob = await ModelWithRef.create({email: "pop3@gmail.com", address})
const retrieved = await ModelWithRef.find({email: "pop3@gmail.com"}).one().orThrow().populate({
path: "address"
})
expect(address._id.toString()).toBe((<Address>retrieved.address)._id.toString())
});


it('should populate array', async () => {
const product1 = new Product({title: "Apple"})
await product1.save()

const product2 = new Product({title: "Mango"})
await product2.save()

const ob = await ModelWithRef.create({email: "pop4@gmail.com", products: [product1, product2]})

const retrieved = await ModelWithRef.find({email: "pop4@gmail.com"}).one().populate({
path: "products"
})
// @ts-ignore
expect(["Mango", "Apple"]).toContain((<Product>retrieved.products![0]).title)

});


it('should populate level 2 array', async () => {
const product1 = new Product({title: "Apple"})
await product1.save()

const product2 = new Product({title: "Mango"})
await product2.save()

const user1 = new User({name: "User1", products: [product1]})
const user2 = new User({name: "User1", products: [product2]})

const ob = await ModelWithRef.create({email: "pop5@gmail.com", users: [user2, user1]})

const retrieved = await ModelWithRef.find({email: "pop5@gmail.com"}).one().populate({
path: "users.products"
}).orThrow()
expect(["Mango", "Apple"]).toContain((<Product[]>retrieved.users![0].products)![0].title)

});



18 changes: 16 additions & 2 deletions test/tests/save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ it('should save models properly', async () => {


it('should have same _id after saving', async () => {
const staff = new User({lastName: "Shamim", firstName: "Model", email: "ddx@example.com"})
const staff = new User({lastName: "Shamim", firstName: "Model", email: "ddx2@example.com"})
const _id = staff._id
await staff.save()
const saved = await staff.save()
expect(_id).toBe(staff._id)
expect(_id).toBe(saved._id)
const retrieved = await User.find({email: "ddx2@example.com"}).one().orThrow()
expect(_id!.toString()).toBe(retrieved._id!.toString())
});


Expand Down Expand Up @@ -49,4 +52,15 @@ it('should save and retrieve date properly', async () => {
await datable.save()
const retrieved = await BasicDate.find().one().orThrow()
expect(date).toEqual(retrieved.created)
});

it('should not save extra properties', async () => {
const p = {lastName: "Doe", email: "withextra@example.com", extra: "extra value"}
const b = new Basic(p)
await b.save()
// @ts-ignore
const getExtra = (ob) => ob.extra

const retrieved = Basic.find({email: "withextra@example.com"}).one().orThrow()
expect(getExtra(retrieved)).toBeUndefined()
});

0 comments on commit dd45434

Please # to comment.