Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

copy tests from woodegg repository

  • Loading branch information...
commit cd84cfa70c5f74ea9c86f2babc16240cdcb8a7bc 1 parent 216d05a
@sivers sivers authored
Showing with 460 additions and 0 deletions.
  1. +460 −0 test/woodegg.rb
View
460 test/woodegg.rb
@@ -0,0 +1,460 @@
+$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
+require 'd50b/test'
+require 'd50b/woodegg'
+
+class TestResearcher < Minitest::Test
+ def test_researcher
+ x = Researcher[1]
+ assert_equal x.name, @fixtures['Person']['gongli']['name']
+ assert_equal x.email, @fixtures['Person']['gongli']['email']
+ x = Researcher[2]
+ assert_equal x.name, @fixtures['Person']['yoko']['name']
+ assert_equal x.email, @fixtures['Person']['yoko']['email']
+ x = Researcher[3]
+ assert_equal x.name, @fixtures['Person']['oompa']['name']
+ assert_equal x.email, @fixtures['Person']['oompa']['email']
+ rap = Researcher.all_people
+ assert_equal 3, rap.count
+ assert_equal({:active => true, :id=>2, :person_id=>8, :bio=>'Yes I am Yoko Ono', :name=>'Yoko Ono', :email=>'yoko@ono.com'}, rap[1].values)
+ end
+
+ def test_researcher_assocations
+ assert_equal [6,7,8,9], Researcher[2].answers.map(&:id)
+ assert_equal [], Researcher[3].answers
+ assert_equal [Book[3]], Researcher[3].books
+ assert_equal [], Researcher[1].topics_unfinished
+ assert_equal [Topic[2]], Researcher[2].topics_unfinished
+ assert_equal [Topic[1],Topic[2]], Researcher[3].topics_unfinished
+ assert_equal [], Researcher[1].subtopics_unfinished_in_topic(2)
+ assert_equal [], Researcher[2].subtopics_unfinished_in_topic(1)
+ assert_equal [Subtopic[3],Subtopic[4]], Researcher[2].subtopics_unfinished_in_topic(2)
+ assert_equal [Subtopic[1],Subtopic[2]], Researcher[3].subtopics_unfinished_in_topic(1)
+ assert_equal [Subtopic[3],Subtopic[4]], Researcher[3].subtopics_unfinished_in_topic(2)
+ assert_equal [Question[3],Question[4]], Researcher[3].questions_unfinished_in_subtopic(3)
+ assert_equal 5, Researcher[1].answers_finished_count
+ assert_equal [5,4,3,2,1], Researcher[1].answers_finished.map(&:id)
+ assert_equal 3, Researcher[2].answers_finished_count
+ assert_equal [8,7,6], Researcher[2].answers_finished.map(&:id)
+ assert_equal 0, Researcher[3].answers_finished_count
+ assert_equal [], Researcher[3].answers_finished
+ assert_equal [1,2,3,4,5], Researcher[1].questions_answered.map(&:id)
+ assert_equal [6,7,8], Researcher[2].questions_answered.map(&:id)
+ assert_equal [], Researcher[3].questions_answered
+ assert_equal Book[3].questions, Researcher[3].questions
+ assert_equal [], Researcher[1].questions_unanswered
+ assert_equal [Question[9],Question[10]], Researcher[2].questions_unanswered
+ assert_equal [1,2,3,4,5], Researcher[3].questions_unanswered.map(&:id)
+ assert_equal [Answer[9]], Researcher[2].answers_unfinished
+ assert_equal [], Researcher[3].answers_unfinished
+ assert_equal [Answer[6],Answer[7]], Researcher[2].answers_unpaid
+ assert_equal [Answer[4],Answer[5]], Researcher[1].answers_unjudged
+ assert_equal [6,7,8,9], Researcher[2].question_ids_answered
+ assert_equal [], Researcher[3].question_ids_answered
+ assert_equal 0, Researcher[1].howmany_unassigned
+ assert_equal 1, Researcher[2].howmany_unassigned
+ assert_equal 5, Researcher[3].howmany_unassigned
+ end
+
+ def test_new_researcher
+ r = Researcher.create(person_id: 6)
+ r.add_book(Book[2])
+ assert_equal 5, r.howmany_unassigned
+ assert_equal [6,7,8,9,10], r.questions_unanswered.map(&:id)
+ end
+
+ def test_researcher_all_people
+ a = Researcher.all
+ a.each do |r|
+ refute r.values.has_key? :name
+ end
+ a = Researcher.all_people
+ a.each do |r|
+ assert r.values.has_key? :name
+ end
+ assert_equal [1, 2, 3], a.map(&:id)
+ Researcher[2].update(active: false)
+ a = Researcher.all_people(active: true)
+ assert_equal [1, 3], a.map(&:id)
+ end
+
+ def test_add_book_country
+ a = Researcher.all
+ a.each do |r|
+ refute r.respond_to? :book_country
+ end
+ Researcher.add_book_country(a)
+ a.each do |r|
+ assert ['CN', 'JP'].include? r.book_country
+ end
+ end
+
+ def test_researcher_group_by_book
+ b = Researcher.group_by_book(Researcher.all)
+ assert b.instance_of? Hash
+ assert_equal 3, b.size
+ assert_equal [Researcher[1]], b[1]
+ assert_equal [Researcher[2]], b[2]
+ assert_equal [Researcher[3]], b[3]
+ end
+
+ def test_add_answer_count
+ a = Researcher.order(:id).all
+ Researcher.add_answer_count(a)
+ r = a.shift
+ assert_equal 1, r.id
+ assert_equal 3, r[:answers_done]
+ assert_equal 0, r[:answers_open]
+ assert_equal 2, r[:answers_limbo]
+ r = a.shift
+ assert_equal 2, r.id
+ assert_equal 2, r[:answers_done]
+ assert_equal 1, r[:answers_open]
+ assert_equal 1, r[:answers_limbo]
+ r = a.shift
+ assert_equal 3, r.id
+ assert_equal 0, r[:answers_done]
+ assert_equal 0, r[:answers_open]
+ assert_equal 0, r[:answers_limbo]
+ end
+end
+
+class TestWriter < Minitest::Test
+ def test_writer
+ x = Writer[1]
+ assert_equal x.name, @fixtures['Person']['veruca']['name']
+ assert_equal x.email, @fixtures['Person']['veruca']['email']
+ x = Writer[2]
+ assert_equal x.name, @fixtures['Person']['charlie']['name']
+ assert_equal x.email, @fixtures['Person']['charlie']['email']
+ end
+
+ def test_newest_book_for_country
+ assert_equal Book[1], Writer[1].newest_book_for_country('CN')
+ assert_equal Book[2], Writer[2].newest_book_for_country('JP')
+ end
+end
+
+class TestCustomer < Minitest::Test
+ def test_customer
+ x = Customer[1]
+ assert_equal x.name, @fixtures['Person']['augustus']['name']
+ assert_equal x.email, @fixtures['Person']['augustus']['email']
+ end
+end
+
+class TestEditor < Minitest::Test
+ def test_editor
+ x = Editor[1]
+ assert_equal x.name, @fixtures['Person']['derek']['name']
+ assert_equal x.email, @fixtures['Person']['derek']['email']
+ x = Editor[2]
+ assert_equal x.name, @fixtures['Person']['wonka']['name']
+ assert_equal x.email, @fixtures['Person']['wonka']['email']
+ end
+
+ def test_editor_essays
+ x = Editor[2]
+ assert_equal [Essay[6]], x.done_essays
+ assert_equal [Essay[7]], x.todo_essays
+ assert_equal [Question[6]], x.done_questions
+ assert_equal [Question[7]], x.todo_questions
+ end
+end
+
+class TestTopic < Minitest::Test
+ def test_topic
+ x = Topic[1]
+ assert_equal 'Country', x.topic
+ assert_equal [Subtopic[1], Subtopic[2]], x.subtopics
+ end
+
+ def test_topic_assocations
+ x = Topic[2]
+ assert_equal [TemplateQuestion[3], TemplateQuestion[4], TemplateQuestion[5]], x.template_questions
+ assert_equal [3,4,5,8,9,10], x.questions.map(&:id)
+ end
+end
+
+class TestSubtopic < Minitest::Test
+ def test_subtopic
+ x = Subtopic[3]
+ assert_equal 'is it fun?', x.subtopic
+ assert_equal Topic[2], x.topic
+ assert_equal [TemplateQuestion[3], TemplateQuestion[4]], x.template_questions
+ end
+
+ def test_subtopic_questions
+ x = Subtopic[3]
+ assert_equal [Question[3], Question[4], Question[8], Question[9]], x.questions
+ assert_equal [Question[3], Question[4]], x.questions_for_country('CN')
+ end
+end
+
+class TestTemplateQuestion < Minitest::Test
+ def test_template_question
+ x = TemplateQuestion[1]
+ assert_equal 'how big is {COUNTRY}?', x.question
+ assert_equal Subtopic[1], x.subtopic
+ assert_equal Topic[1], x.topic
+ assert_equal [Question[1], Question[6]], x.questions
+ end
+
+ def test_template_question_assocations
+ x = TemplateQuestion[3]
+ assert_equal [Answer[3], Answer[8]], x.answers
+ assert_equal [Essay[3],Essay[8]], x.essays
+ end
+end
+
+class TestQuestion < Minitest::Test
+ def test_question
+ x = Question[1]
+ assert_equal 'how big is China?', x.question
+ assert_equal [Book[1], Book[3]], x.books
+ assert_equal [Answer[1]], x.answers
+ assert_equal [Essay[1]], x.essays
+ assert_equal TemplateQuestion[1], x.template_question
+ assert_equal Subtopic[1], x.subtopic
+ assert_equal Topic[1], x.topic
+ assert_equal [Researcher[1]], x.researchers
+ assert_equal [Editor[1]], x.editors
+ assert_equal [], x.tidbits
+ x = Question[4]
+ assert_equal [Tidbit[1]], x.tidbits
+ end
+
+ def test_question_class
+ assert_equal 5, Question.total_for_country('CN')
+ assert_equal 0, Question.total_for_country('ZZ')
+ assert_equal({1=>'how big is China?', 2=>'how old is China?', 3=>'what is fun in China?', 4=>'do they laugh in China?', 5=>'what language in China?'}, Question.hash_for_country('CN'))
+ assert_equal [Question[3],Question[4]], Question.for_subtopic_and_country(3, 'CN')
+ assert_equal({6=>{topic: 'Country', subtopic: 'how big'}, 7=>{topic: 'Country', subtopic: 'how old'}, 8=>{topic: 'Culture', subtopic: 'is it fun?'}, 9=>{topic: 'Culture', subtopic: 'is it fun?'}, 10=>{topic: 'Culture', subtopic: 'what language?'}}, Question.topichash('JP'))
+ th = Question.topicnest(Book[2].questions, Question.topichash('JP'))
+ assert_equal 2, th.size
+ assert_equal({'how big' => [Question[6]], 'how old' => [Question[7]]}, th['Country'])
+ assert_equal({1 => Question[1], 3=> Question[3]}, Question.for_these(Answer.where(id: [1,3]).all))
+ assert_equal({2 => Question[2], 4=> Question[4]}, Question.for_these(Essay.where(id: [2,4]).all))
+ end
+
+ def test_needing_essays
+ assert_equal [], Question.needing_essays_from_writer_for_country(1, 'CN')
+ assert_equal [Question[10]], Question.needing_essays_from_writer_for_country(2, 'JP')
+ assert_equal [], Question.needing_essays_for_book(1)
+ assert_equal [Question[10]], Question.needing_essays_for_book(2)
+ end
+end
+
+class TestAnswer < Minitest::Test
+ def test_answer
+ x = Answer[7]
+ assert_equal Question[7], x.question
+ assert_equal Researcher[2], x.researcher
+ assert_equal [Essay[7]], x.essays
+ assert_equal [Book[2]], x.books
+ assert_equal Subtopic[2], x.subtopic
+ assert_equal Topic[1], x.topic
+ assert x.finished?
+ refute Answer[9].finished?
+ end
+
+ def test_answer_class
+ assert_equal [Answer[4], Answer[5]], Answer.unjudged
+ assert_equal [Answer[9]], Answer.unfinished
+ assert_equal({'CN' => 5, 'JP' => 4}, Answer.count_per_country_hash)
+ end
+
+ def test_answer_fj
+ assert_equal Answer[4], Answer.unjudged_next
+ assert_equal Answer[9], Answer.unfinished_next
+ end
+end
+
+class TestBook < Minitest::Test
+ def test_book
+ x = Book[1]
+ assert_equal 'China 2013: How To', x.title
+ assert_equal 'China 2013', x.short_title
+ assert_equal 'How To', x.sub_title
+ assert_equal({'pdf' => 'ChinaStartupGuide2013.pdf', 'epub' => 'ChinaStartupGuide2013.epub', 'mobi' => 'ChinaStartupGuide2013.mobi'}, x.filename_hash)
+ end
+
+ def test_download_url
+ # https://s3-ap-southeast-1.amazonaws.com/woodegg/ChinaStartupGuide2013.pdf?AWSAccessKeyId=BLAHBLAHBLAH&Expires=1372837301&Signature=bLaHbLaH
+ x = Book[1]
+ u = x.download_url('pdf')
+ assert_match /^https:\/\//, u
+ assert u.include? 'ChinaStartupGuide2013.pdf'
+ assert_match /AWSAccessKeyId=[A-Z0-9]{20}&/, u
+ assert_match /Expires=\d+&/, u
+ assert_match /Signature=\S+\Z/, u
+ end
+
+ def test_book_associations
+ x = Book[1]
+ assert_equal 2, x.topics.size
+ assert_equal 'Country', x.topics[0].topic
+ assert_equal 4, x.subtopics.size
+ assert_equal 'how big', x.subtopics[0].subtopic
+ assert_equal 5, x.template_questions.size
+ assert_equal 'how big is {COUNTRY}?', x.template_questions[0].question
+ assert_equal [1,2,3,4,5], x.questions.map(&:id)
+ assert x.questions.map(&:question).all? {|q| q.include? 'China'}
+ assert_equal [1,2,3,4,5], x.answers.map(&:id)
+ assert x.answers.map(&:answer).all? {|q| q.include? 'China'}
+ assert_equal [1,2,3,4,5], x.essays.map(&:id)
+ assert x.essays.map(&:content).all? {|q| q.include? 'China'}
+ assert_equal [Researcher[1]], x.researchers
+ assert_equal [Writer[1]], x.writers
+ assert_equal [Editor[1]], x.editors
+ assert_equal [Customer[1]], x.customers
+ x = Book[3]
+ assert_equal [1,2,3,4,5], x.questions.map(&:id)
+ assert_equal [1,2,3,4,5], x.answers.map(&:id)
+ assert_equal [], x.essays
+ end
+
+ def test_books
+ assert_equal [Book[1],Book[2],Book[3]], Book.order(:id).all
+ assert_equal [Book[1]], Book.available
+ assert_equal [Book[1]], Book.done
+ assert_equal [Book[2],Book[3]], Book.not_done
+ assert Book[1].done?
+ refute Book[2].done?
+ refute Book[3].done?
+ end
+
+ def test_books_missing
+ assert_equal [], Book[1].questions_missing_essays
+ assert_equal 0, Book[1].questions_missing_essays_count
+ assert_equal [Question[10]], Book[2].questions_missing_essays
+ assert_equal 1, Book[2].questions_missing_essays_count
+ assert_equal [], Book[1].essays_unedited.all
+ assert_equal 2, Book[2].essays_unedited.count
+ assert_equal [Essay[7],Essay[8]], Book[2].essays_unedited.all
+ end
+
+end
+
+class TestAnEssay < Minitest::Test
+ def test_essay
+ x = Essay[1]
+ assert_equal Writer[1], x.writer
+ assert_equal [Editor[1]], x.editors
+ assert_equal Book[1], x.book
+ assert_equal Question[1], x.question
+ assert_equal Subtopic[1], x.subtopic
+ assert_equal Topic[1], x.topic
+ assert Essay[1].finished?
+ refute Essay[9].finished?
+ end
+
+ def test_essay_class
+ assert_equal [Essay[8]], Essay.unjudged
+ assert_equal [Essay[9]], Essay.unfinished
+ assert_equal [Essay[6],Essay[7],Essay[8],Essay[9]], Essay.for_country('JP')
+ assert_equal({'CN' => 5, 'JP' => 4}, Essay.country_howmany)
+ assert_equal 2, Essay.howmany_unedited
+ assert_equal Essay[7], Essay.next_unedited_for(2)
+ assert_equal Essay[8], Essay.next_unedited
+ end
+
+end
+
+class TestTag < Minitest::Test
+ def test_tag
+ x = Tag[1]
+ assert_equal 'China', x.name
+ assert_equal [Tidbit[1]], x.tidbits
+ end
+end
+
+class TestTidbit < Minitest::Test
+ def test_tidbit
+ x = Tidbit[1]
+ assert /China/ === x.content
+ x = Tidbit[2]
+ assert /Japan/ === x.content
+ assert_equal [Question[6],Question[7]], x.questions
+ assert_equal [Tag[2]], x.tags
+ end
+end
+
+class TestUpload < Minitest::Test
+ def test_upload
+ r = Researcher[3]
+ x = Upload[3]
+ assert_equal 3, r.uploads.count
+ assert_equal x, r.uploads.pop
+ assert_equal r, x.researcher
+ end
+
+ def test_uploaded_status
+ assert_equal 'uploaded', Upload[1].uploaded_status
+ assert_match /uploading/, Upload[2].uploaded_status
+ assert_match /not uploaded/, Upload[3].uploaded_status
+ end
+
+ def test_transcription_status
+ Upload.all.each do |u|
+ assert Upload.statuses.include? u.status
+ end
+ assert_equal [Upload[1]], Upload.filter(status: 'done').order(:id).all
+ assert_equal [Upload[2], Upload[3]], Upload.filter(status: 'new').order(:id).all
+ end
+
+ def test_missing_info
+ assert_equal [Upload[2], Upload[3]], Upload.missing_info.all
+ r = Researcher[3]
+ assert_equal [Upload[2], Upload[3]], Upload.missing_info_for(r.id)
+ refute Upload[1].missing_info?
+ assert Upload[2].missing_info?
+ assert Upload[3].missing_info?
+ end
+end
+
+class TestTestEssay < Minitest::Test
+ def test_for_pq
+ assert_equal TestEssay[1], TestEssay.for_pq(8, 6)
+ assert_nil TestEssay.for_pq(8, 99)
+ end
+
+ def test_finished_person_ids
+ assert_equal [6, 8], TestEssay.finished_person_ids
+ end
+
+ def test_association
+ assert_equal [TestEssay[1], TestEssay[2]], Person[8].test_essays
+ assert_equal [TestEssay[3], TestEssay[4]], Person[6].test_essays
+ assert_equal [], Person[1].test_essays
+ assert_equal Question[1], TestEssay[3].question
+ end
+end
+
+class TestInterviewees < Minitest::Test
+ def test_interviewees
+ i = Person.interviewees
+ assert i.instance_of? Hash
+ assert_equal 16, i.size # one per country
+ p = i['MN'].pop
+ assert p.instance_of? Person
+ assert /importing chocolate/ === p[:interviewee_bio]
+ end
+
+ def test_ibio
+ assert_nil Person[5].interviewee_bio
+ assert /importing chocolate/ === Person[6].interviewee_bio
+ end
+
+ def test_set_ibio
+ nubio = 'I dove into a chocolate lake.'
+ us = Userstat.filter(statkey: 'woodegg-bio').all
+ assert_equal [Userstat[7]], us
+ Person[6].set_interviewee_bio(nubio)
+ assert_equal nubio, Userstat[7].statvalue
+ Userstat[7].destroy
+ Person[6].set_interviewee_bio(nubio)
+ assert_equal nubio, Person[6].interviewee_bio
+ assert_equal nubio, Userstat[8].statvalue # change someday if add new Userstat fixture
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.