Skip to content

Commit

Permalink
Merge pull request #12 from RobDWaller/unit-tests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
RobDWaller authored Sep 3, 2018
2 parents edbcebd + c0df661 commit eff92a1
Show file tree
Hide file tree
Showing 26 changed files with 288 additions and 62 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# [Fableau](http://fableau.io) (Beta)
[![Build Status](https://travis-ci.org/RobDWaller/fableau.svg?branch=master)](https://travis-ci.org/RobDWaller/fableau) [![codecov](https://codecov.io/gh/RobDWaller/fableau/branch/master/graph/badge.svg)](https://codecov.io/gh/RobDWaller/fableau)
[![Build Status](https://travis-ci.org/RobDWaller/fableau.svg?branch=master)](https://travis-ci.org/RobDWaller/fableau) [![codecov](https://codecov.io/gh/RobDWaller/fableau/branch/master/graph/badge.svg)](https://codecov.io/gh/RobDWaller/fableau) [![Maintainability](https://api.codeclimate.com/v1/badges/fae0ad4fd07fef1f8a4b/maintainability)](https://codeclimate.com/github/RobDWaller/fableau/maintainability)

A Tableau Web Data Connector for Facebook that allows you to pull your Facebook post and
page metrics data into Tableau.
Expand All @@ -18,10 +18,13 @@ The Web Data Connector pulls data from the following Facebook Graph API end poin
https://graph.facebook.com/{page-id}/posts?fields=created_time,message,id,link,type&access_token={access-token}
//Post Metric Data
https://graph.facebook.com/{page-id}/posts?fields=insights.metric(post_stories,post_storytellers,post_story_adds,post_story_adds_unique,post_engaged_users,post_consumptions,post_consumptions_unique,post_impressions,post_impressions_unique,post_impressions_paid,post_impressions_paid_unique,post_reactions_like_total,post_reactions_love_total,post_reactions_wow_total,post_reactions_haha_total,post_reactions_sorry_total,post_reactions_anger_total)&access_token={access-token}
https://graph.facebook.com/{page-id}/posts?fields=insights.metric(post_impressions,post_impressions_unique,post_impressions_paid,post_impressions_paid_unique,post_reactions_like_total,post_reactions_love_total,post_reactions_wow_total,post_reactions_haha_total,post_reactions_sorry_total,post_reactions_anger_total)&access_token={access-token}
//Page Meta Data
https://graph.facebook.com/{page-id}?fields=link,name,category,about&access_token={access-token}
//Page Metric Data
https://graph.facebook.com/{page-id}/insights/page_impressions,page_impressions_unique,page_impressions_paid,page_impressions_organic,page_stories,page_engaged_users,page_consumptions,page_consumptions_unique,page_negative_feedback,page_negative_feedback_unique,page_fan_adds_unique,page_views_total,page_views_logged_in_unique,page_posts_impressions,page_posts_impressions_unique,page_posts_impressions_paid,page_posts_impressions_organic,page_post_engagements,page_video_views?access_token={access-token}&since={date}&until=${date}
https://graph.facebook.com/{page-id}/insights/page_impressions,page_impressions_unique,page_impressions_paid,page_impressions_organic,page_engaged_users,page_consumptions,page_consumptions_unique,page_negative_feedback,page_negative_feedback_unique,page_fan_adds_unique,page_views_total,page_views_logged_in_unique,page_posts_impressions,page_posts_impressions_unique,page_posts_impressions_paid,page_posts_impressions_organic,page_post_engagements,page_video_views?access_token={access-token}&since={date}&until=${date}
```

## Notes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"build": "webpack --mode=production",
"dev": "webpack --mode=development",
"test": "jest",
"test": "jest --collectCoverage=false",
"analyse": "eslint ./src/js",
"codecov": "jest && codecov"
},
Expand Down
2 changes: 1 addition & 1 deletion public/privacy.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h3>Additional Information</h3>
</ul>
</div>
<div class="footer">
<small>Be aware this Web Data Connector is currently in Alpha, stability cannot be guaranteed.</small>
<small>Be aware this Web Data Connector is currently in Beta, stability cannot be guaranteed.</small>
<small>For more information on how Fableau.io works and how you can use it please read our <a href="https://github.com/RobDWaller/fableau/blob/master/README.md">documentation</a>.</small>
<small>Fableau.io built by <a href="https://twitter.com/RobDWaller">@RobDWaller</a>, please feel free to connect and send any questions you have.</small>
<small>&copy; Rob Waller 2017, released under the MIT License, <a href="/privacy.html">Privacy Policy</a>.</small>
Expand Down
13 changes: 8 additions & 5 deletions src/js/helper/date-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,22 @@ class DateTime {
}

/**
* Return a date timestamp string, this is specific to the way Facebook
* returns date timestamp strings from their API.
* Convert a Facebook time string to a Tableau time string
*
* @param string dateString
* @return string
* @todo possibly not the best place for this.
*/
getTableauTimeString (dateString) {
let dateParts = dateString.split('T')
if (dateString.includes('T') && dateString.includes('+')) {
let dateParts = dateString.split('T')

let timeParts = dateParts[1].split('+')
let timeParts = dateParts[1].split('+')

return `${dateParts[0]} ${timeParts[0]}`
return `${dateParts[0]} ${timeParts[0]}`
}

throw new Error('Invalid Facebook date string [' + dateString + '], cannot convert')
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/mapper/page-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class Page {
class PageMeta {
/**
* @param array data
*/
Expand Down Expand Up @@ -39,4 +39,4 @@ class Page {
}
}

export default Page
export default PageMeta
4 changes: 2 additions & 2 deletions src/js/mapper/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DateTime from '../helper/date-time.js'
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class Post {
class PostMeta {
/**
* @param array data
* @param int pageId
Expand Down Expand Up @@ -55,4 +55,4 @@ class Post {
}
}

export default Post
export default PostMeta
13 changes: 6 additions & 7 deletions src/js/service/facebook/helper/url-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class FacebookUrlParts {
class UrlParts {
/**
* Break the Facebook authentication URL down into its component parts
*
Expand All @@ -16,7 +16,7 @@ class FacebookUrlParts {
getParts (url) {
let result = {}

let parts = this.splitUrl(url)
let parts = this.splitUrl(url, '#')

result['url'] = parts[0]

Expand All @@ -33,12 +33,12 @@ class FacebookUrlParts {
* @param string url
* @return array
*/
splitUrl (url) {
return url.split('#')
splitUrl (url, needle) {
return url.split(needle)
}

/**
* Retrieve a specific part of the URL string.
* Retrieve a specific part of the URL query string.
*
* @param string partString
* @param string part
Expand All @@ -51,12 +51,11 @@ class FacebookUrlParts {
let parts = b.split('=')

a[parts[0]] = parts[1]

return a
}, {})

return result[part]
}
}

export default FacebookUrlParts
export default UrlParts
10 changes: 5 additions & 5 deletions src/js/service/facebook/requests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import Page from '../../mapper/page-meta.js'
import Post from '../../mapper/post-meta.js'
import PageMeta from '../../mapper/page-meta.js'
import PostMeta from '../../mapper/post-meta.js'
import PageMetrics from '../../mapper/page-metrics.js'
import PostMetrics from '../../mapper/post-metrics.js'
import DateTime from '../../helper/date-time.js'
Expand Down Expand Up @@ -33,7 +33,7 @@ class FacebookRequests {
getPosts (page) {
return this.facebookData.getDataPaginate(`${this.urlPrepend}/${page.id}/posts?fields=created_time,message,id,link,type&access_token=${page.access_token}`)
.then((result) => {
return new Post(result)
return new PostMeta(result)
})
}

Expand All @@ -60,7 +60,7 @@ class FacebookRequests {
getPage (page) {
return this.facebookData.getData(`${this.urlPrepend}/${page.id}/?fields=link,name,category,about&access_token=${page.access_token}`)
.then((result) => {
return new Page([result])
return new PageMeta([result])
})
}

Expand All @@ -73,7 +73,7 @@ class FacebookRequests {
getPages (accessToken) {
return this.facebookData.getDataPaginate(`${this.urlPrepend}/me/accounts?access_token=${accessToken}`)
.then((result) => {
return new Page(result)
return new PageMeta(result)
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/js/tableau/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Ajax from '../helper/ajax.js'
import FacebookPageLoop from '../service/facebook/helper/page-loop.js'
import FacebookRequests from '../service/facebook/requests.js'
import FacebookData from '../service/facebook/data.js'
import PostColumns from '../tableau/columns/posts.js'
import PageColumns from '../tableau/columns/pages.js'
import PostMetricsColumns from '../tableau/columns/post-metrics.js'
import PageMetricsColumns from '../tableau/columns/page-metrics.js'
import PostMetaColumns from '../tableau/columns/post-meta-columns.js'
import PageMetaColumns from '../tableau/columns/page-meta-columns.js'
import PostMetricsColumns from '../tableau/columns/post-metric-columns.js'
import PageMetricsColumns from '../tableau/columns/page-metric-columns.js'
import TableFactory from './table-factory.js'

/**
Expand Down Expand Up @@ -48,8 +48,8 @@ class TableauBuilder {

tableauConnector.getSchema = (schemaCallback) => {
schemaCallback([
tableFactory.makeTable('posts', 'Posts Meta Data', new PostColumns(this.tableau.dataTypeEnum)).getTable(),
tableFactory.makeTable('pages', 'Pages Meta Data', new PageColumns(this.tableau.dataTypeEnum)).getTable(),
tableFactory.makeTable('posts', 'Posts Meta Data', new PostMetaColumns(this.tableau.dataTypeEnum)).getTable(),
tableFactory.makeTable('pages', 'Pages Meta Data', new PageMetaColumns(this.tableau.dataTypeEnum)).getTable(),
tableFactory.makeTable('post_metrics', 'Posts Metric Data', new PostMetricsColumns(this.tableau.dataTypeEnum)).getTable(),
tableFactory.makeTable('page_metrics', 'Pages Metric Data', new PageMetricsColumns(this.tableau.dataTypeEnum)).getTable()
])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

/**
* Define the columns that make up the Tableau pages table
* Define the columns that make up the Tableau pages meta table
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class PageColumns {
class PageMetaColumns {
/**
* @param Object
*/
Expand Down Expand Up @@ -47,4 +47,4 @@ class PageColumns {
}
}

export default PageColumns
export default PageMetaColumns
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class PageImpressionColumns {
class PageMetricColumns {
/**
* @param Object
*/
Expand Down Expand Up @@ -247,4 +247,4 @@ class PageImpressionColumns {
}
}

export default PageImpressionColumns
export default PageMetricColumns
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

/**
* Define the columns that make up the Tableau posts table
* Define the columns that make up the Tableau posts meta table
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class PostColumns {
class PostMetaColumns {
/**
* @param Object
*/
Expand Down Expand Up @@ -39,4 +39,4 @@ class PostColumns {
}
}

export default PostColumns
export default PostMetaColumns
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author Rob Waller <rdwaller1984@googlemail.com>
*/
class PageColumns {
class PostMetricColumns {
/**
* @param Object
*/
Expand Down Expand Up @@ -75,4 +75,4 @@ class PageColumns {
}
}

export default PageColumns
export default PostMetricColumns
55 changes: 55 additions & 0 deletions tests/helper/date-time.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import DateTime from '../../src/js/helper/date-time.js'

test('Get date time object', () => {
let date = new DateTime()

expect(date.getDate()).toBeInstanceOf(Date)
})

test('Get unix timestamp', () => {
let date = new DateTime()

expect(date.getUnixTimestamp()).toBe(Math.floor(Date.now() / 1000))
})

test('Get timestamp minus days', () => {
let date = new DateTime()

expect(date.getUnixTimestampMinusDays(3)).toBe(date.getUnixTimestamp() - (3 * 24 * 3600))
})

test('Convert one day to seconds', () => {
let date = new DateTime()

expect(date.convertDaysToSeconds(1)).toBe(3600*24)
})

test('Convert zero days to seconds', () => {
let date = new DateTime()

expect(date.convertDaysToSeconds(0)).toBe(0)
})

test('Convert five days to seconds', () => {
let date = new DateTime()

expect(date.convertDaysToSeconds(5)).toBe((3600*24)*5)
})

test('Get tableau time string', () => {
let date = new DateTime()

expect(date.getTableauTimeString('2018-08-05T07:00:00+0000')).toBe('2018-08-05 07:00:00')
})

test('Get tableau time string convert zero', () => {
let date = new DateTime()

expect(() => {date.getTableauTimeString('0')}).toThrowError('Invalid Facebook date string')
})

test('Get tableau time string convert empty', () => {
let date = new DateTime()

expect(() => {date.getTableauTimeString('')}).toThrowError('Invalid Facebook date string')
})
17 changes: 17 additions & 0 deletions tests/mapper/page-meta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import PageMeta from '../../src/js/mapper/page-meta.js'

test('Make page meta class', () => {
let pageMeta = new PageMeta([])

expect(pageMeta).toBeInstanceOf(PageMeta)
})

test('Make page meta class fail', () => {
expect(() => {new PageMeta()}).toThrow()
})

test('Get Tableau data fail', () => {
let pageMeta = new PageMeta(null)

expect(() => {pageMeta.getTableauData()}).toThrow()
})
17 changes: 17 additions & 0 deletions tests/mapper/page-metrics.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import PageMetrics from '../../src/js/mapper/page-metrics.js'

test('Make page metrics class', () => {
let pageMetrics = new PageMetrics([])

expect(pageMetrics).toBeInstanceOf(PageMetrics)
})

test('Make page metrics class fail', () => {
expect(() => {new PageMetrics()}).toThrow()
})

test('Get Tableau data fail', () => {
let pageMetrics = new PageMetrics(null)

expect(() => {pageMetrics.getTableauData()}).toThrow()
})
17 changes: 17 additions & 0 deletions tests/mapper/post-meta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import PostMeta from '../../src/js/mapper/post-meta.js'

test('Make post meta class', () => {
let postMeta = new PostMeta([])

expect(postMeta).toBeInstanceOf(PostMeta)
})

test('Make post meta class fail', () => {
expect(() => {new PostMeta()}).toThrow()
})

test('Get Tableau data fail', () => {
let postMeta = new PostMeta(null)

expect(() => {postMeta.getTableauData()}).toThrow()
})
Loading

0 comments on commit eff92a1

Please sign in to comment.