Skip to content

Commit

Permalink
Add Country, Address test entities/data
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarner committed Aug 21, 2020
1 parent 8257f53 commit aaf5dd5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/RelationMapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { graphql, GraphQLResolveInfo, GraphQLSchema } from 'graphql';
import { addMocksToSchema, makeExecutableSchema } from 'graphql-tools';
import { Connection, createConnection } from 'typeorm';
import { insertMockData, TestMockData } from '../test/data';
import { Country } from '../test/entities/country';
import { Image } from '../test/entities/image';
import { ImageFile } from '../test/entities/imagefile';
import { Owner } from '../test/entities/owner';
Expand All @@ -21,7 +22,7 @@ describe('RelationMapper', () => {
connection = await createConnection({
type: 'sqlite',
database: 'test/test.sqlite',
entities: [Product, Owner, Store, Image, ImageFile, Video],
entities: [Country, Product, Owner, Store, Image, ImageFile, Video],
synchronize: true,
dropSchema: true,
});
Expand Down
16 changes: 15 additions & 1 deletion test/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Connection } from 'typeorm';
import { Country } from './entities/country';
import { Image } from './entities/image';
import { ImageFile } from './entities/imagefile';
import { Owner } from './entities/owner';
Expand All @@ -7,6 +8,7 @@ import { Store } from './entities/store';
import { Video } from './entities/video';

export interface TestMockData {
countryA: Country;
ownerA: Owner;
storeA: Store;
productA: Product;
Expand All @@ -15,15 +17,27 @@ export interface TestMockData {
}

export const insertMockData = async (connection: Connection): Promise<TestMockData> => {
const countryRepo = connection.getRepository(Country);
const ownerRepo = connection.getRepository(Owner);
const storeRepo = connection.getRepository(Store);
const productRepo = connection.getRepository(Product);
const imageRepo = connection.getRepository(Image);
const imageFileRepo = connection.getRepository(ImageFile);
const videoRepo = connection.getRepository(Video);

// COUNTRIES
const countryA = await countryRepo.save(countryRepo.create({ name: 'Country A' }));

// OWNERS
const ownerA = await ownerRepo.save(ownerRepo.create({ name: 'Owner A' }));
const ownerA = await ownerRepo.save(
ownerRepo.create({
name: 'Owner A',
address: {
street: 'Street',
country: countryA,
},
}),
);

// STORES
const storeA = await storeRepo.save(
Expand Down
11 changes: 11 additions & 0 deletions test/entities/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity, Column, ManyToOne } from 'typeorm';
import { Country } from './country';

@Entity()
export class Address {
@Column()
public street: string;

@ManyToOne(_type => Country)
public country: Country;
}
10 changes: 10 additions & 0 deletions test/entities/country.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Country {
@PrimaryGeneratedColumn()
public id: number;

@Column()
public name: string;
}
4 changes: 4 additions & 0 deletions test/entities/owner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import { Address } from './address';

@Entity()
export class Owner {
Expand All @@ -7,4 +8,7 @@ export class Owner {

@Column()
public name: string;

@Column(_type => Address)
public address: Address;
}
11 changes: 11 additions & 0 deletions test/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ export interface TestResolverContext {

// language=GraphQL
export const typeDefs = `
type Country {
id: Int!
name: String
}
type Address {
street: String
country: Country
}
type Owner {
id: Int!
name: String
address: Address
}
type Product {
Expand Down

0 comments on commit aaf5dd5

Please sign in to comment.