Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Models and their tests #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
16 changes: 12 additions & 4 deletions db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const path = require('path');
const { Sequelize, Model } = require('sequelize');
//TODO - create the new sequelize connection
const path = require("path");
const { Sequelize, Model, DataTypes } = require("sequelize");

const sequelize = new Sequelize({
dialect:"sqlite",
storage:path.join(__dirname,"sequelize.sqlite")
})


// TODO - create the new sequelize connection

module.exports = {
sequelize,
Sequelize
Sequelize,
Model,
DataTypes
};
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ const { Musician } = require('./models/Musician')
const { Song } = require("./models/Song")
// Define associations here

//Multiple musicians can be added to a band
//Every musician has only one band
Band.hasMany(Musician);
Musician.belongsTo(Band);

//Multiple songs can be added to a Band.
//Multiple bands can have the same Song.

Band.hasMany(Song);
Song.hasMany(Band);






module.exports = {
Expand Down
74 changes: 65 additions & 9 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,87 @@ describe('Band, Musician, and Song Models', () => {

test('can create a Band', async () => {
// TODO - test creating a band
expect('NO TEST').toBe('EXPECTED VALUE HERE');
const testBand = await Band.create({ name:'Canadian-American',genre:'rock' });
expect(testBand.name).toBe('Canadian-American');
})

test('can create a Musician', async () => {
// TODO - test creating a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
const testMusician = await Musician.create({ name:'Billie Eilish',instrument:'Violin' });
expect(testMusician.name).toBe('Billie Eilish');
})

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

const bandId = 1;
await Band.update({ genre: 'rock' }, { where: { id: bandId } });
const updatedBand = await Band.findOne({ where: { id: bandId } });
expect(updatedBand.genre).toBe('rock');
})

test('can update a Musician', async () => {
// TODO - test updating a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
const MusicianId = 1;
await Musician.update({ name: 'Mickel' }, { where: { id: MusicianId } });
const updatedMusician = await Musician.findOne({ where: { id: MusicianId } });
expect(updatedMusician.name).toBe('Mickel');
})

test('can delete a Band', async () => {
// TODO - test deleting a band
expect('NO TEST').toBe('EXPECTED VALUE HERE');
})
const BandId = 1;
// Delete the Band
await Band.destroy({ where: { id: BandId } });
// Try to find the deleted band
const deletedBand = await Band.findOne({ where: { id: BandId } });
// Expect the deletedBand to be null
expect(deletedBand).toBeNull();
});

test('can delete a Musician', async () => {
// TODO - test deleting a musician
expect('NO TEST').toBe('EXPECTED VALUE HERE');
const MusicianId = 1;
await Musician.destroy({ where: { id: MusicianId } })
const deletedMusician = await Musician.findOne({ where: { id: MusicianId } })
expect(deletedMusician).toBeNull();

})
//one to many
test("Band can have Many Musician",async function(){
const testBand = await Band.create({ name:'Canadian-American',genre:'rock' });
const testMusician1 = await Musician.create({ name:'Billie Eilish',instrument:'Violin' });
const testMusician2 = await Musician.create({ name:'Mickel',instrument:'drum' });

await testBand.addMusicians(testMusician1);
await testBand.addMusicians(testMusician2);
const associateMusician = await testBand.getMusicians();
expect (associateMusician.length).toBe(2);
expect (associateMusician instanceof Musician).toBeTruthy;

})
//many to many
test("Band can have Many Song",async function(){
const testBand = await Band.create({ name:'Canadian-American',genre:'rock' });
const testSong1 = await Song.create({ title:'Moon',year:2020,length:9 });
const testSong2 = await Song.create({ title:'Light',year:2022,length:7 });

await testBand.addSongs(testSong1);
await testBand.addSongs(testSong2);
const associateSong = await testBand.getSongs();
expect (associateSong.length).toBe(2);
expect (associateSong instanceof Song).toBeTruthy;

})
test("Song can have Many Band",async function(){
const testSong = await Song.create({ title:'Moon',year:2020,length:9 });
const testBand1 = await Band.create({ name:'American',genre:'rock' });
const testBand2 = await Band.create({ name:'American',genre:'Pop' });

await testSong.addBands(testBand1);
await testSong.addBands(testBand2);
const associateBand = await testSong.getBands();
expect (associateBand.length).toBe(2);
expect (associateBand instanceof Band).toBeTruthy;

})

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

// TODO - define the Band model
let Band;

class Band extends Model {};

Band.init({
name : DataTypes.STRING ,
genre: DataTypes.STRING,
},{
sequelize : sequelize,
modelName: "Band"
})

module.exports = {
Band
Expand Down
13 changes: 11 additions & 2 deletions models/Musician.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const {Sequelize, sequelize} = require('../db');
const {Sequelize, sequelize,DataTypes,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
16 changes: 13 additions & 3 deletions models/Song.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const {Sequelize, sequelize} = require('../db');
const {Sequelize, sequelize,DataTypes,Model} = require('../db');

// TODO - define the Song model
let Song;
// TODO - define the Band model

class Song extends Model {};

Song.init({
title : DataTypes.STRING ,
year: DataTypes.INTEGER,
length : DataTypes.INTEGER,
},{
sequelize : sequelize,
modelName: "Song"
})

module.exports = {
Song
Expand Down