Skip to content

Commit

Permalink
added additional test cases and set test file system base url to test…
Browse files Browse the repository at this point in the history
…-files
  • Loading branch information
tomgobich committed Jun 10, 2024
1 parent 75b27a4 commit 81fe7fe
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 40 deletions.
6 changes: 5 additions & 1 deletion bin/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { assert } from '@japa/assert'
import { configure, processCLIArgs, run } from '@japa/runner'
import { fileSystem } from '@japa/file-system'

processCLIArgs(process.argv.splice(2))

configure({
files: ['tests/**/*.spec.ts'],
plugins: [assert()],
plugins: [
assert(),
fileSystem({ basePath: new URL('../test-files', import.meta.url), autoClean: false }),
],
})

run()
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,8 @@
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package"
},
"prettier": "@adonisjs/prettier-config"
"prettier": "@adonisjs/prettier-config",
"dependencies": {
"@japa/file-system": "^2.3.0"
}
}
100 changes: 100 additions & 0 deletions test-files/app/models/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// @ts-nocheck
import { DateTime } from 'luxon'
import { BaseModel, belongsTo, column, computed, hasMany, hasOne } from '@adonisjs/lucid/orm'
import User from './user.js'
import type { BelongsTo, HasMany, HasOne } from '@adonisjs/lucid/types/relations'
import AccountType from '#models/account_type'
import Payee from '#models/payee'
import Stock from '#models/stock'
import Transaction from '#models/transaction'
import AccountTypeService from '#services/account_type_service'
import { columnCurrency } from '#start/orm/column'
import type { AccountGroupConfig } from '#config/account'

export default class Account extends BaseModel {
// region Columns

@column({ isPrimary: true })
declare id: number

@column()
declare userId: number

@column()
declare accountTypeId: number

@column()
declare name: string

@column()
declare note: string

@column.date()
declare dateOpened: DateTime | null

@column.date()
declare dateClosed: DateTime | null

@columnCurrency()
declare balance: number

@columnCurrency()
declare startingBalance: number

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime

// endregion

// region Unmapped Properties

aggregations: Record<string, number> = {}

// endregion

// region Relationships

@belongsTo(() => User)
declare user: BelongsTo<typeof User>

@belongsTo(() => AccountType)
declare accountType: BelongsTo<typeof AccountType>

@hasOne(() => Payee)
declare payee: HasOne<typeof Payee>

@hasMany(() => Stock)
declare stocks: HasMany<typeof Stock>

@hasMany(() => Transaction)
declare transactions: HasMany<typeof Transaction>

// endregion

// region Computed Properties

@computed()
get accountGroup(): AccountGroupConfig {
return AccountTypeService.getAccountTypeGroup(this.accountTypeId)
}

@computed()
get isCreditIncrease(): boolean {
return AccountTypeService.isCreditIncreaseById(this.accountTypeId)
}

@computed()
get isBudgetable() {
return AccountTypeService.isBudgetable(this.accountTypeId)
}

@computed()
get balanceDisplay() {
return '$' + this.balance.toLocaleString('en-US')
}

// endregion
}
14 changes: 14 additions & 0 deletions test-files/app/models/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-nocheck
import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'

export default class Test extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime
}
56 changes: 56 additions & 0 deletions test-files/app/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// @ts-nocheck
import { DateTime } from 'luxon'
import hash from '@adonisjs/core/services/hash'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel, column, hasMany } from '@adonisjs/lucid/orm'
import { withAuthFinder } from '@adonisjs/auth/mixins/lucid'
import Payee from '#models/payee'
import type { HasMany } from '@adonisjs/lucid/types/relations'
import Transaction from '#models/transaction'
import Income from '#models/income'
import StockPurchase from '#models/stock_purchase'
import Stock from '#models/stock'
import Account from '#models/account'

const AuthFinder = withAuthFinder(() => hash.use('scrypt'), {
uids: ['email'],
passwordColumnName: 'password',
})

export default class User extends compose(BaseModel, AuthFinder) {
@column({ isPrimary: true })
declare id: number

@column()
declare fullName: string | null

@column()
declare email: string

@column({ serializeAs: null })
declare password: string

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime | null

@hasMany(() => Payee)
declare payees: HasMany<typeof Payee>

@hasMany(() => Transaction)
declare transactions: HasMany<typeof Transaction>

@hasMany(() => Income)
declare incomes: HasMany<typeof Income>

@hasMany(() => StockPurchase)
declare stockPurchases: HasMany<typeof StockPurchase>

@hasMany(() => Stock)
declare stocks: HasMany<typeof Stock>

@hasMany(() => Account)
declare accounts: HasMany<typeof Account>
}
60 changes: 60 additions & 0 deletions test-files/expectations/account.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Account from '#models/account'
import UserDto from '#dtos/user'
import AccountTypeDto from '#dtos/account_type'
import PayeeDto from '#dtos/payee'
import StockDto from '#dtos/stock'
import TransactionDto from '#dtos/transaction'
import { AccountGroupConfig } from '#config/account'

export default class AccountDto {
declare id: number
declare userId: number
declare accountTypeId: number
declare name: string
declare note: string
declare dateOpened: string | null
declare dateClosed: string | null
declare balance: number
declare startingBalance: number
declare createdAt: string
declare updatedAt: string
aggregations: Record<string, number> = {}
declare user: UserDto | null
declare accountType: AccountTypeDto | null
declare payee: PayeeDto | null
declare stocks: StockDto[]
declare transactions: TransactionDto[]
declare accountGroup: AccountGroupConfig
declare isCreditIncrease: boolean
declare isBudgetable: boolean
declare balanceDisplay: string

constructor(account: Account) {
this.id = account.id
this.userId = account.userId
this.accountTypeId = account.accountTypeId
this.name = account.name
this.note = account.note
this.dateOpened = account.dateOpened?.toISO()!
this.dateClosed = account.dateClosed?.toISO()!
this.balance = account.balance
this.startingBalance = account.startingBalance
this.createdAt = account.createdAt.toISO()!
this.updatedAt = account.updatedAt.toISO()!
this.aggregations = account.aggregations
this.user = account.user && new UserDto(account.user)
this.accountType = account.accountType && new AccountTypeDto(account.accountType)
this.payee = account.payee && new PayeeDto(account.payee)
this.stocks = StockDto.fromArray(account.stocks)
this.transactions = TransactionDto.fromArray(account.transactions)
this.accountGroup = account.accountGroup
this.isCreditIncrease = account.isCreditIncrease
this.isBudgetable = account.isBudgetable
this.balanceDisplay = account.balanceDisplay
}

static fromArray(accounts: Account[]) {
if (!accounts) return []
return accounts.map((account) => new AccountDto(account))
}
}
16 changes: 16 additions & 0 deletions test-files/expectations/some_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Test from '#models/test'

export default class SomeTestDto {
declare id: number
declare createdAt: string

constructor(test: Test) {
this.id = test.id
this.createdAt = test.createdAt.toISO()!
}

static fromArray(tests: Test[]) {
if (!tests) return []
return tests.map((test) => new SomeTestDto(test))
}
}
16 changes: 16 additions & 0 deletions test-files/expectations/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Test from '#models/test'

export default class TestDto {
declare id: number
declare createdAt: string

constructor(test: Test) {
this.id = test.id
this.createdAt = test.createdAt.toISO()!
}

static fromArray(tests: Test[]) {
if (!tests) return []
return tests.map((test) => new TestDto(test))
}
}
39 changes: 39 additions & 0 deletions test-files/expectations/user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import User from '#models/user'
import PayeeDto from '#dtos/payee'
import TransactionDto from '#dtos/transaction'
import IncomeDto from '#dtos/income'
import StockPurchaseDto from '#dtos/stock_purchase'
import StockDto from '#dtos/stock'

export default class UserDto {
declare id: number
declare fullName: string | null
declare email: string
declare password: string
declare createdAt: string
declare updatedAt: string | null
declare payees: PayeeDto[]
declare transactions: TransactionDto[]
declare incomes: IncomeDto[]
declare stockPurchases: StockPurchaseDto[]
declare stocks: StockDto[]

constructor(user: User) {
this.id = user.id
this.fullName = user.fullName
this.email = user.email
this.password = user.password
this.createdAt = user.createdAt.toISO()!
this.updatedAt = user.updatedAt?.toISO()!
this.payees = PayeeDto.fromArray(user.payees)
this.transactions = TransactionDto.fromArray(user.transactions)
this.incomes = IncomeDto.fromArray(user.incomes)
this.stockPurchases = StockPurchaseDto.fromArray(user.stockPurchases)
this.stocks = StockDto.fromArray(user.stocks)
}

static fromArray(users: User[]) {
if (!users) return []
return users.map((user) => new UserDto(user))
}
}
38 changes: 0 additions & 38 deletions test-helpers/models/user.txt

This file was deleted.

Loading

0 comments on commit 81fe7fe

Please sign in to comment.