|
1 | 1 | defmodule SchoolTest do
|
2 | 2 | use ExUnit.Case
|
3 | 3 |
|
4 |
| - @db %{} |
| 4 | + def make_school_with_students(students) do |
| 5 | + {results, school} = |
| 6 | + Enum.reduce(students, {[], School.new()}, fn {student, grade}, {results, school} -> |
| 7 | + {result, school} = School.add(school, student, grade) |
| 8 | + {[result | results], school} |
| 9 | + end) |
| 10 | + |
| 11 | + {Enum.reverse(results), school} |
| 12 | + end |
5 | 13 |
|
6 |
| - test "add student" do |
7 |
| - actual = School.add(@db, "Aimee", 2) |
8 |
| - assert actual == %{2 => ["Aimee"]} |
| 14 | + # @tag :pending |
| 15 | + test "Roster is empty when no student is added" do |
| 16 | + assert School.roster(School.new()) == [] |
9 | 17 | end
|
10 | 18 |
|
11 |
| - test "a student can only be added to the same grade once" do |
12 |
| - actual = |
13 |
| - @db |
14 |
| - |> School.add("Aimee", 2) |
15 |
| - |> School.add("Aimee", 2) |
| 19 | + @tag :pending |
| 20 | + test "Add a student" do |
| 21 | + {result, school} = School.add(School.new(), "Aimee", 2) |
16 | 22 |
|
17 |
| - assert actual == %{2 => ["Aimee"]} |
| 23 | + assert result == :ok |
| 24 | + assert School.roster(school) == ["Aimee"] |
18 | 25 | end
|
19 | 26 |
|
20 |
| - test "a student cannot be added to more than one grade" do |
21 |
| - actual = |
22 |
| - @db |
23 |
| - |> School.add("Aimee", 2) |
24 |
| - |> School.add("Aimee", 1) |
| 27 | + @tag :pending |
| 28 | + test "Adding multiple students in the same grade in the roster" do |
| 29 | + students = [{"Blair", 2}, {"James", 2}, {"Paul", 2}] |
| 30 | + {results, school} = make_school_with_students(students) |
25 | 31 |
|
26 |
| - assert actual == %{2 => ["Aimee"]} |
| 32 | + assert results == [:ok, :ok, :ok] |
| 33 | + assert School.roster(school) == ["Blair", "James", "Paul"] |
27 | 34 | end
|
28 | 35 |
|
29 | 36 | @tag :pending
|
30 |
| - test "add more students in same class" do |
31 |
| - actual = |
32 |
| - @db |
33 |
| - |> School.add("James", 2) |
34 |
| - |> School.add("Blair", 2) |
35 |
| - |> School.add("Paul", 2) |
36 |
| - |
37 |
| - assert Enum.sort(actual[2]) == ["Blair", "James", "Paul"] |
| 37 | + test "Cannot add student to same grade in the roster more than once" do |
| 38 | + students = [{"Blair", 2}, {"James", 2}, {"James", 2}, {"Paul", 2}] |
| 39 | + {results, school} = make_school_with_students(students) |
| 40 | + |
| 41 | + assert results == [:ok, :ok, :error, :ok] |
| 42 | + assert School.roster(school) == ["Blair", "James", "Paul"] |
38 | 43 | end
|
39 | 44 |
|
40 | 45 | @tag :pending
|
41 |
| - test "add students to different grades" do |
42 |
| - actual = |
43 |
| - @db |
44 |
| - |> School.add("Chelsea", 3) |
45 |
| - |> School.add("Logan", 7) |
| 46 | + test "Adding students in multiple grades" do |
| 47 | + students = [{"Chelsea", 3}, {"Logan", 7}] |
| 48 | + {results, school} = make_school_with_students(students) |
46 | 49 |
|
47 |
| - assert actual == %{3 => ["Chelsea"], 7 => ["Logan"]} |
| 50 | + assert results == [:ok, :ok] |
| 51 | + assert School.roster(school) == ["Chelsea", "Logan"] |
48 | 52 | end
|
49 | 53 |
|
50 | 54 | @tag :pending
|
51 |
| - test "get students in a grade" do |
52 |
| - actual = |
53 |
| - @db |
54 |
| - |> School.add("Bradley", 5) |
55 |
| - |> School.add("Franklin", 5) |
56 |
| - |> School.add("Jeff", 1) |
57 |
| - |> School.grade(5) |
58 |
| - |
59 |
| - assert Enum.sort(actual) == ["Bradley", "Franklin"] |
| 55 | + test "Cannot add same student to multiple grades in the roster" do |
| 56 | + students = [{"Blair", 2}, {"James", 2}, {"James", 3}, {"Paul", 3}] |
| 57 | + {results, school} = make_school_with_students(students) |
| 58 | + |
| 59 | + assert results == [:ok, :ok, :error, :ok] |
| 60 | + assert School.roster(school) == ["Blair", "James", "Paul"] |
60 | 61 | end
|
61 | 62 |
|
62 | 63 | @tag :pending
|
63 |
| - test "get students in a non existent grade" do |
64 |
| - assert [] == School.grade(@db, 1) |
| 64 | + test "Students are sorted by grades in the roster" do |
| 65 | + students = [{"Jim", 3}, {"Peter", 2}, {"Anna", 1}] |
| 66 | + {_results, school} = make_school_with_students(students) |
| 67 | + |
| 68 | + assert School.roster(school) == ["Anna", "Peter", "Jim"] |
65 | 69 | end
|
66 | 70 |
|
67 | 71 | @tag :pending
|
68 |
| - test "sort school by grade and by student name" do |
69 |
| - actual = |
70 |
| - @db |
71 |
| - |> School.add("Peter", 2) |
72 |
| - |> School.add("Anna", 1) |
73 |
| - |> School.add("Barb", 1) |
74 |
| - |> School.add("Zoe", 2) |
75 |
| - |> School.add("Alex", 2) |
76 |
| - |> School.add("Jim", 3) |
77 |
| - |> School.add("Charlie", 1) |
78 |
| - |> School.sort() |
79 |
| - |
80 |
| - expected = [ |
81 |
| - {1, ["Anna", "Barb", "Charlie"]}, |
82 |
| - {2, ["Alex", "Peter", "Zoe"]}, |
83 |
| - {3, ["Jim"]} |
| 72 | + test "Students are sorted by name in the roster" do |
| 73 | + students = [{"Peter", 2}, {"Zoe", 2}, {"Alex", 2}] |
| 74 | + {_results, school} = make_school_with_students(students) |
| 75 | + |
| 76 | + assert School.roster(school) == ["Alex", "Peter", "Zoe"] |
| 77 | + end |
| 78 | + |
| 79 | + @tag :pending |
| 80 | + test "Students are sorted by grades and then by name in the roster" do |
| 81 | + students = [ |
| 82 | + {"Peter", 2}, |
| 83 | + {"Anna", 1}, |
| 84 | + {"Barb", 1}, |
| 85 | + {"Zoe", 2}, |
| 86 | + {"Alex", 2}, |
| 87 | + {"Jim", 3}, |
| 88 | + {"Charlie", 1} |
84 | 89 | ]
|
85 | 90 |
|
86 |
| - assert expected == actual |
| 91 | + {_results, school} = make_school_with_students(students) |
| 92 | + |
| 93 | + assert School.roster(school) == ["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"] |
| 94 | + end |
| 95 | + |
| 96 | + @tag :pending |
| 97 | + test "Grade is empty if no students in the roster" do |
| 98 | + assert School.grade(School.new(), 1) == [] |
| 99 | + end |
| 100 | + |
| 101 | + @tag :pending |
| 102 | + test "Grade is empty if no students in that grade" do |
| 103 | + students = [{"Peter", 2}, {"Zoe", 2}, {"Alex", 2}, {"Jim", 3}] |
| 104 | + {_results, school} = make_school_with_students(students) |
| 105 | + |
| 106 | + assert School.grade(school, 1) == [] |
| 107 | + end |
| 108 | + |
| 109 | + @tag :pending |
| 110 | + test "Student not added to same grade more than once" do |
| 111 | + students = [{"Blair", 2}, {"James", 2}, {"James", 2}, {"Paul", 2}] |
| 112 | + {_results, school} = make_school_with_students(students) |
| 113 | + |
| 114 | + assert School.roster(school) == ["Blair", "James", "Paul"] |
| 115 | + end |
| 116 | + |
| 117 | + @tag :pending |
| 118 | + test "Student not added to multiple grades" do |
| 119 | + students = [{"Blair", 2}, {"James", 2}, {"James", 3}, {"Paul", 3}] |
| 120 | + {_results, school} = make_school_with_students(students) |
| 121 | + |
| 122 | + assert School.grade(school, 2) == ["Blair", "James"] |
| 123 | + assert School.grade(school, 3) == ["Paul"] |
| 124 | + end |
| 125 | + |
| 126 | + @tag :pending |
| 127 | + test "Students are sorted by name in a grade" do |
| 128 | + students = [{"Franklin", 5}, {"Bradley", 5}, {"Jeff", 1}] |
| 129 | + {_results, school} = make_school_with_students(students) |
| 130 | + |
| 131 | + assert School.grade(school, 5) == ["Bradley", "Franklin"] |
87 | 132 | end
|
88 | 133 | end
|
0 commit comments