Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions semana19/aula63-testes/exercícios-escritos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1) usuario fez a compra ou não
recebe um
user={
name: 'ana',
balance: 10,
}
value= 5

3)
sistema de filas: quem pode entrar?

const cassino={
name: string,
country: ENUM(Brasil, Eua),
}

const user={
nacionality: AMERICAN / BRASILIAN,
age: number
}

Usuario nos eua = age>=21
Usuario nos brs = age>=18

return
brasileiros |
permitidos: []
não permitidos: []

americanos
permitidos: []
não permitidos: []

testes:
a)user br, casino no br
b)user america, casino no br
c) 2 users br, 2 america = 19 anos, casino eua
d) 2 users br(19 anos), 2 america(21 anos), casino eua
8 changes: 8 additions & 0 deletions semana19/aula63-testes/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
roots: ["<rootDir>/tests"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
6,209 changes: 6,209 additions & 0 deletions semana19/aula63-testes/package-lock.json

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions semana19/aula63-testes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "aula63-testes",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest",
"purchase": "ts-node ./src/performPurchase.ts",
"verifyAge": "ts-node ./src/verifyAge.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/dotenv": "^8.2.0",
"@types/jest": "^26.0.10",
"@types/mysql": "^2.15.15",
"@types/node": "^14.6.2",
"dotenv": "^8.2.0",
"jest": "^26.4.2",
"knex": "^0.21.5",
"moment": "^2.27.0",
"mysql": "^2.18.1",
"ts-jest": "^26.3.0",
"ts-node": "^9.0.0",
"ts-node-dev": "^1.0.0-pre.61",
"typescript": "^4.0.2"
}
}
29 changes: 29 additions & 0 deletions semana19/aula63-testes/src/data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import knex from "knex";
import Knex from "knex";

export default abstract class BaseDatabase {
private static connection: Knex | null = null;

protected getConnection(): Knex {
if (!BaseDatabase.connection) {
BaseDatabase.connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
},
});
}
return BaseDatabase.connection;
}

public static async destroyConnection() {
if (BaseDatabase.connection) {
await BaseDatabase.connection.destroy();
BaseDatabase.connection = null;
}
}
}
52 changes: 52 additions & 0 deletions semana19/aula63-testes/src/data/PostDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import BaseDatabase from "./BaseDatabase";

export interface Post {
postId: string,
urlPhoto: string,
description: string,
creationDate: string,
type: string,
userCreatorId: string
}
export default class PostsDatabase extends BaseDatabase {
private static TABLE_NAME = "labook_post";

public async createPost(
postId: string,
urlPhoto: string,
description: string,
creationDate: string,
type: string,
userCreatorId: string
): Promise<void> {
await this.getConnection()
.insert({
post_id: postId,
url_photo: urlPhoto,
description: description,
creation_date: creationDate,
type: type,
user_creator_id: userCreatorId,
})
.into(PostsDatabase.TABLE_NAME);
await BaseDatabase.destroyConnection();
}

public async deletePost(id: string) {
await this.getConnection()
.select()
.from(PostsDatabase.TABLE_NAME)
.where(id);
await BaseDatabase.destroyConnection();
}

public async getPostById(id: string): Promise<any> {
const result = await this.getConnection()
.select()
.where(id)
.from(PostsDatabase.TABLE_NAME)

return result
await BaseDatabase.destroyConnection();
}
}
19 changes: 19 additions & 0 deletions semana19/aula63-testes/src/performPurchase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface User {
name: string,
balance: number
}

export const performPurchase = (user: User, value: number): User | undefined => {
if (user.balance >= value) {
const updateUser: User = {
name: user.name,
balance: user.balance - value
}
console.log(updateUser)
return updateUser
} else {
console.log(undefined)
return undefined
}
}

100 changes: 100 additions & 0 deletions semana19/aula63-testes/src/verifyAge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
export enum LOCATION {
BRAZIL = 'BRAZIL',
EUA = 'EUA'
}

export enum NACIONALITY {
BRAZILIAN = 'BRAZILIAN',
AMERICAN = 'AMERICAN'
}

export interface Casino {
name: string,
country: LOCATION
}

export interface UserCasino {
name: string;
age: number,
nacionality: NACIONALITY
}

interface Result {
brazilians: ResultItem;
americans: ResultItem;
}

interface ResultItem {
allowed: string[];
unallowed: string[];
}

let allUsers: Result = {
brazilians: {
allowed: [],
unallowed: []
},
americans: {
allowed: [],
unallowed: []
}
}

export const verifyAge = (casino: Casino, users: UserCasino[]): Result => {
for (const user of users) {
const allowed: UserCasino[] = []
const unallowed: UserCasino[] = []

switch (casino.country) {
case 'BRAZIL':
if (user.age >= 18) {
allowed.push(user)
} else {
unallowed.push(user)
}
break;
case 'EUA':
if (user.age >= 21) {
allowed.push(user)
} else {
unallowed.push(user)
}
break;
default:
console.log('Incorrect country')
}

allowed.forEach((user: UserCasino) => {
if (user.nacionality === NACIONALITY.BRAZILIAN) {
allUsers.brazilians.allowed.push(user.name)
} else {
allUsers.americans.allowed.push(user.name)
}
})

unallowed.forEach((user: UserCasino) => {
if (user.nacionality === NACIONALITY.AMERICAN) {
allUsers.americans.unallowed.push(user.name)
} else {
allUsers.brazilians.unallowed.push(user.name)
}
})

}
console.table(allUsers)
return allUsers
}

export const deleteUsers = () => {
allUsers = {
brazilians: {
allowed: [],
unallowed: []
},
americans: {
allowed: [],
unallowed: []
}
}
}

21 changes: 21 additions & 0 deletions semana19/aula63-testes/tests/data/PostDatabase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import moment from 'moment';
import { Post } from './../../src/data/PostDatabase';
import PostDatabase from '../../src/data/PostDatabase'
import dotenv from 'dotenv'
dotenv.config()

describe('Test PostDatabase', () => {
const postDatabase = new PostDatabase()

afterEach(async () => {
await postDatabase.deletePost('99b')
})

test('Create a post correctly', async () => {
await postDatabase.createPost('99b', 'Post1_url', 'descrição 01', '2020-08-27 18:18:26', 'EVENT', '6b16dc83-1a73-4511-abf1-7af02315fd85')
const postFromDb = await postDatabase.getPostById('99b')
expect(postFromDb.description).toEqual('descrição 01')
expect(postFromDb).not.toBe(undefined)

})
})
46 changes: 46 additions & 0 deletions semana19/aula63-testes/tests/purchase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { performPurchase, User } from '../src/performPurchase'

describe('Verify puchase function', () => {
test('must return a user to greater balance than purchase', () => {
const user: User = {
name: 'astrodev',
balance: 60
}
const value: number = 20
const result = performPurchase(user, value)
const updateUser: User = {
name: 'astrodev',
balance: 60 - value
}
expect(result).toEqual(updateUser)
})

test('must return a user when the balance equal to purchase', () => {
const user: User = {
name: 'astrodev',
balance: 60
}
const value: number = 60
const result = performPurchase(user, value)
const updateUser: User = {
name: 'astrodev',
balance: 60 - value
}
expect(result).toEqual(updateUser)
})

test('must return undefined to balance less than purchase', () => {
const user: User = {
name: 'astrodev',
balance: 60
}
const value: number = 90
const result = performPurchase(user, value)
expect(result).toEqual(undefined)
})
})


//a) usuario com saldo maior
//b) usuario com saldo igual
//c) usuario com saldo menor que a compra
Loading