forked from xebia-functional/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHelper.scala
More file actions
123 lines (103 loc) · 4.22 KB
/
Copy pathTestHelper.scala
File metadata and controls
123 lines (103 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2016-2018 47 Degrees, LLC. <http://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fetch
import cats.data.NonEmptyList
object TestHelper {
case class DidNotFound() extends Throwable
case class One(id: Int)
implicit object OneSource extends DataSource[One, Int] {
override def name = "OneSource"
override def fetchOne(id: One): Query[Option[Int]] =
Query.sync(Option(id.id))
override def fetchMany(ids: NonEmptyList[One]): Query[Map[One, Int]] =
Query.sync(ids.toList.map(one => (one, one.id)).toMap)
}
def one(id: Int): Fetch[Int] = Fetch(One(id))
case class AnotherOne(id: Int)
implicit object AnotheroneSource extends DataSource[AnotherOne, Int] {
override def name = "AnotherOneSource"
override def fetchOne(id: AnotherOne): Query[Option[Int]] =
Query.sync(Option(id.id))
override def fetchMany(ids: NonEmptyList[AnotherOne]): Query[Map[AnotherOne, Int]] =
Query.sync(ids.toList.map(anotherone => (anotherone, anotherone.id)).toMap)
}
def anotherOne(id: Int): Fetch[Int] = Fetch(AnotherOne(id))
case class Many(n: Int)
implicit object ManySource extends DataSource[Many, List[Int]] {
override def name = "ManySource"
override def fetchOne(id: Many): Query[Option[List[Int]]] =
Query.sync(Option(0 until id.n toList))
override def fetchMany(ids: NonEmptyList[Many]): Query[Map[Many, List[Int]]] =
Query.sync(ids.toList.map(m => (m, 0 until m.n toList)).toMap)
}
def many(id: Int): Fetch[List[Int]] = Fetch(Many(id))
case class Never()
implicit object NeverSource extends DataSource[Never, Int] {
override def name = "NeverSource"
override def fetchOne(id: Never): Query[Option[Int]] =
Query.sync(None)
override def fetchMany(ids: NonEmptyList[Never]): Query[Map[Never, Int]] =
Query.sync(Map.empty[Never, Int])
}
// Async DataSources
case class ArticleId(id: Int)
case class Article(id: Int, content: String) {
def author: Int = id + 1
}
implicit object ArticleAsync extends DataSource[ArticleId, Article] {
override def name = "ArticleAsync"
override def fetchOne(id: ArticleId): Query[Option[Article]] =
Query.async((ok, fail) => {
ok(Option(Article(id.id, "An article with id " + id.id)))
})
override def fetchMany(ids: NonEmptyList[ArticleId]): Query[Map[ArticleId, Article]] =
batchingNotSupported(ids)
}
def article(id: Int): Fetch[Article] = Fetch(ArticleId(id))
case class AuthorId(id: Int)
case class Author(id: Int, name: String)
implicit object AuthorAsync extends DataSource[AuthorId, Author] {
override def name = "AuthorAsync"
override def fetchOne(id: AuthorId): Query[Option[Author]] =
Query.async((ok, fail) => {
ok(Option(Author(id.id, "@egg" + id.id)))
})
override def fetchMany(ids: NonEmptyList[AuthorId]): Query[Map[AuthorId, Author]] =
batchingNotSupported(ids)
}
def author(a: Article): Fetch[Author] = Fetch(AuthorId(a.author))
// check Env
def requestFetches(r: FetchRequest): Int =
r match {
case FetchOne(_, _) => 1
case FetchMany(ids, _) => ids.toList.size
case Concurrent(requests) => requests.toList.map(requestFetches).sum
}
def totalFetched(rs: Seq[Round]): Int =
rs.map((round: Round) => requestFetches(round.request)).toList.sum
def requestBatches(r: FetchRequest): Int =
r match {
case FetchOne(_, _) => 0
case FetchMany(ids, _) => 1
case Concurrent(requests) =>
requests.toList.count {
case FetchMany(_, _) => true
case _ => false
}
}
def totalBatches(rs: Seq[Round]): Int =
rs.map((round: Round) => requestBatches(round.request)).toList.sum
}