Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ node_modules/
.idea
.env


#sqlite
*.sqlite
6 changes: 5 additions & 1 deletion db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const path = require('path');
const { Sequelize, Model } = require('sequelize');

// TODO - create the new sequelize connection
const sequelize = new Sequelize({
dialect:'sqlite',
storage: path.join(__dirname, 'db.sqlite'),
})

module.exports = {
sequelize,
Model,
Sequelize
};
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const { Band } = require('./models/Band')
const { Musician } = require('./models/Musician')
const { Song } = require("./models/Song")
const { Manager } = require("./models/Manager");
// Define associations here

Band.hasMany(Musician);
Musician.belongsTo(Band);

Song.belongsToMany(Band, {through:"songband"})
Band.belongsToMany(Song, {through: "songband"})

Band.hasOne(Manager);
Manager.belongsTo(Band);

module.exports = {
Band,
Musician,
Song
Song,
Manager,
};
185 changes: 141 additions & 44 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,141 @@
const { sequelize } = require('./db');
const { Band, Musician, Song } = require('./index')

describe('Band, Musician, and Song Models', () => {
/**
* Runs the code prior to all tests
*/
beforeAll(async () => {
// the 'sync' method will create tables based on the model class
// by setting 'force:true' the tables are recreated each time the
// test suite is run
await sequelize.sync({ force: true });
})

test('can create a Band', async () => {
// TODO - test creating a band
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})

test('can create a Musician', async () => {
// TODO - test creating a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})

test('can update a Band', async () => {
// TODO - test updating a band
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})

test('can update a Musician', async () => {
// TODO - test updating a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})

test('can delete a Band', async () => {
// TODO - test deleting a band
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})

test('can delete a Musician', async () => {
// TODO - test deleting a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})
})
const { sequelize } = require("./db");
const { Band, Manager, Musician, Song } = require("./index");

describe("Band, Musician, and Song Models", () => {
/**
* Runs the code prior to all tests
*/
beforeAll(async () => {
// the 'sync' method will create tables based on the model class
// by setting 'force:true' the tables are recreated each time the
// test suite is run
await sequelize.sync({ force: true });
});

afterAll(async () => {
await sequelize.close();
});

test("can create a Band", async () => {
// TODO - test creating a band
const testBand = await Band.create({ name: "XYZ", genre: "Rock" });
expect(testBand.name).toBe("XYZ");
expect(testBand.genre).toBe("Rock");
});

test("can create a Musician", async () => {
// TODO - test creating a musician
const testMusician = await Musician.create({
name: "Joe",
instrument: "Banjo",
});
expect(testMusician.name).toBe("Joe");
expect(testMusician.instrument).toBe("Banjo");
});

test("can update a Band", async () => {
const testBand2 = await Band.create({ name: "BMTH", genre: "Metalcore" });
await testBand2.update({ genre: "Metal" });
expect(testBand2.genre).toEqual("Metal");
});

test("can update a Musician", async () => {
const testMusician2 = await Musician.create({
name: "Michael",
instrument: "Guitar",
});
await testMusician2.update({ name: "Mike" });
expect(testMusician2.name).toBe("Mike");
});

test("can delete a Band", async () => {
// TODO - test deleting a band
const testBand = await Band.create({
name: "XYZ",
genre: "Rock",
});
const deletedBand = await testBand.destroy();
expect(deletedBand).toEqual(
expect.objectContaining({
name: "XYZ",
genre: "Rock",
})
);
});

test("can delete a Musician", async () => {
// TODO - test deleting a musician
const testMusician = await Musician.create({
name: "John",
instrument: "Drums",
});
const destroyedMusician = await testMusician.destroy();
expect(destroyedMusician).toEqual(
expect.objectContaining({
name: "John",
instrument: "Drums",
})
);
});

test("each Band has Musician", async () => {
const testMusician = await Musician.create({
name: "John",
instrument: "Drums",
});
const testMusician2 = await Musician.create({
name: "Rob",
instrument: "Vocals",
});

const Korn = await Band.create({name: "Korn", genre: "Pop"});
const Poppy = await Band.create({name: "Poppy", genre: "HipHop"});

// const allBands = await Band.findAll();
// const allMusicians = await Musician.findAll();

//left table, setter, right table value
await Korn.setMusicians(testMusician)
//comboName, left table, getter
let kornMusician = await Korn.getMusicians()

expect(kornMusician[0].name).toEqual("John");
});

test("many bands have many songs AND a song can have many bands", async () => {
const Korn = await Band.create({name: "Korn", genre: "Pop"});
const Poppy = await Band.create({name: "Poppy", genre: "HipHop"});
const song1 = await Song.create({title: 'Hello'})
const song2 = await Song.create({title: 'Farewell'})

await Korn.addSongs([song1, song2])

const foundSongs = await Korn.getSongs()
expect(foundSongs.length).toBe(2)

await song2.addBand(Poppy)
const foundBands = await song2.getBands();
expect(foundBands.map(band => band.name)).toContain('Poppy')
})

test("Testing one-to-one association between Manager and Band", async () => {
const testBand = await Band.create({
name: "Test Band",
instrument: "Drums",
});
const testManager = await Manager.create({
name: "Rob",
email: "Rob@gmail.com",
salary: 1000000,
dateHired: "5/15/2025"
});

await testBand.setManager(testManager);

let testBandManager = await testBand.getManager();

expect(testBandManager.name).toEqual("Rob");
}, 10000)


});
6 changes: 5 additions & 1 deletion models/Band.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const { DataTypes } = require('sequelize');
const {Sequelize, sequelize} = require('../db');

// TODO - define the Band model
let Band;
let Band = sequelize.define("band", {
name: DataTypes.STRING,
genre: DataTypes.STRING
})

module.exports = {
Band
Expand Down
14 changes: 14 additions & 0 deletions models/Manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { sequelize } = require("../db");
const { DataTypes } = require("sequelize");

const Manager = sequelize.define("Manager", {
name: DataTypes.STRING,
email: DataTypes.STRING,
salary: DataTypes.NUMBER,
dateHired: DataTypes.DATEONLY,
})


module.exports = {
Manager
}
14 changes: 12 additions & 2 deletions models/Musician.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const {Sequelize, sequelize} = require('../db');
const { DataTypes } = require('sequelize');
const {Sequelize, sequelize, Model} = require('../db');

// TODO - define the Musician model
let Musician;

class Musician extends Model {};

Musician.init({
name: DataTypes.STRING,
instrument: DataTypes.STRING,
}, {
sequelize: sequelize,
modelName: "Musician",
})

module.exports = {
Musician
Expand Down
9 changes: 7 additions & 2 deletions models/Song.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const {Sequelize, sequelize} = require('../db');
const { DataTypes } = require('sequelize');
const {Sequelize, sequelize, Model} = require('../db');

// TODO - define the Song model
let Song;
let Song = sequelize.define("song", {
title: DataTypes.STRING,
year: DataTypes.NUMBER,
length: DataTypes.NUMBER
})

module.exports = {
Song
Expand Down
Loading