Skip to content

Commit

Permalink
Fix tests and structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed Mar 19, 2024
1 parent 36808ac commit 8bdadcc
Show file tree
Hide file tree
Showing 44 changed files with 369 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
profile = janestreet
break-cases = fit
margin = 117
wrap-comments = true
line-endings = lf

# To make ocaml-ci happy
version = ignore
version-check = false
4 changes: 4 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
(and
:with-test
(>= 1.3.0)))
(alcotest-lwt
(and
:with-test
(>= 1.7.0)))
(bisect_ppx
(and
:with-test
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions lib/api/community/advertisement.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open Lwt.Syntax

let get game domain send =
let base_url = Uri.make ~scheme:"https" ~host:domain ~path:"/community/advertisement/findAdvertisements" () in
let url = Uri.with_query' base_url [ "title", Data.Game.to_str game ] in
let* json = send url in
match json with
| Some j ->
let model = Models.Response.Community.Advertisement.from_json j in
Lwt.return @@ Some model
| None -> Lwt.return None
;;
Empty file added lib/api/community/clan.ml
Empty file.
Empty file.
Empty file added lib/api/community/external.ml
Empty file.
Empty file added lib/api/community/item.ml
Empty file.
Empty file.
Empty file added lib/api/community/news.ml
Empty file.
Empty file added lib/api/game/account.ml
Empty file.
Empty file added lib/api/game/achievement.ml
Empty file.
Empty file added lib/api/game/advertisement.ml
Empty file.
Empty file added lib/api/game/automatch.ml
Empty file.
Empty file added lib/api/game/automatch2.ml
Empty file.
Empty file added lib/api/game/challenge.ml
Empty file.
Empty file added lib/api/game/chat.ml
Empty file.
Empty file added lib/api/game/clan.ml
Empty file.
Empty file added lib/api/game/cloud.ml
Empty file.
Empty file added lib/api/game/community_event.ml
Empty file.
Empty file added lib/api/game/invitation.ml
Empty file.
Empty file added lib/api/game/item.ml
Empty file.
Empty file added lib/api/game/leaderboard.ml
Empty file.
Empty file added lib/api/game/login.ml
Empty file.
Empty file added lib/api/game/news.ml
Empty file.
Empty file added lib/api/game/party.ml
Empty file.
Empty file added lib/api/game/playerreport.ml
Empty file.
Empty file added lib/api/game/relationship.ml
Empty file.
25 changes: 25 additions & 0 deletions lib/client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
open Lwt.Syntax

type client =
{ domain : string
; game : Data.Game.game
}

let create domain game = { domain; game }

let get_json url =
let* resp, body = Cohttp_lwt_unix.Client.get url in
let status = Cohttp.Response.status resp in
if Cohttp.Code.code_of_status status = 200
then
let* body = Cohttp_lwt.Body.to_string body in
let json = Yojson.Basic.from_string body in
Lwt.return @@ Some json
else Lwt.return None
;;

let get ?requester endpoint client =
let actual_requester = match requester with Some r -> r | None -> get_json in
let* json = endpoint client.game client.domain actual_requester in
Lwt.return json
;;
9 changes: 9 additions & 0 deletions lib/data/game.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[@@@warning "-37"]

type game =
| Age1
| Age2
| Age3
| Age4

let to_str = function Age1 -> "age1" | Age2 -> "age2" | Age3 -> "age3" | Age4 -> "age4"
7 changes: 7 additions & 0 deletions lib/data/game.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type game =
| Age1
| Age2
| Age3
| Age4

val to_str : game -> string
Empty file added lib/lib.ml
Empty file.
20 changes: 20 additions & 0 deletions lib/models/response/community/advertisement.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type advertisement_response =
{ result : Stub.Response.header
; matches : Stub.Advertisement.advertisement list
; avatars : Stub.Avatar.avatar list
}

let to_json r =
`Assoc
[ "result", Stub.Response.to_json r.result
; "avatars", `List (List.map Stub.Avatar.to_json r.avatars)
; "matches", `List (List.map Stub.Advertisement.to_json r.matches)
]
;;

let from_json json =
{ result = Yojson.Basic.Util.(json |> member "result" |> Stub.Response.from_json)
; matches = Yojson.Basic.Util.(json |> member "matches" |> to_list |> List.map Stub.Advertisement.from_json)
; avatars = Yojson.Basic.Util.(json |> member "avatars" |> to_list |> List.map Stub.Avatar.from_json)
}
;;
74 changes: 74 additions & 0 deletions lib/models/stub/advertisement.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
type advertisement =
{ id : int
; steamlobbyid : int
; goodoldgameslobbyid : int
; xboxsessionid : string
; host_profile_id : int
; state : int
; description : string
; visible : int
; mapname : string
; options : string
; passwordprotected : int
; maxplayers : int
; matchtype_id : int
; matchmembers : Match_member.match_member list
; observernum : int
; observermax : int
; isobservable : int
; observerdelay : int
; hasobserverpassword : int
; servicetype : int
; relayserver_region : string
}

let to_json a =
`Assoc
[ "id", `Int a.id
; "steamlobbyid", `Int a.steamlobbyid
; "goodoldgameslobbyid", `Int a.goodoldgameslobbyid
; "xboxsessionid", `String a.xboxsessionid
; "host_profile_id", `Int a.host_profile_id
; "state", `Int a.state
; "description", `String a.description
; "visible", `Int a.visible
; "mapname", `String a.mapname
; "options", `String a.options
; "passwordprotected", `Int a.passwordprotected
; "maxplayers", `Int a.maxplayers
; "matchtype_id", `Int a.matchtype_id
; "matchmembers", `List (List.map Match_member.to_json a.matchmembers)
; "observernum", `Int a.observernum
; "observermax", `Int a.observermax
; "isobservable", `Int a.isobservable
; "observerdelay", `Int a.observerdelay
; "hasobserverpassword", `Int a.hasobserverpassword
; "servicetype", `Int a.servicetype
; "relayserver_region", `String a.relayserver_region
]
;;

let from_json json =
{ id = Yojson.Basic.Util.(json |> member "id" |> to_int)
; steamlobbyid = Yojson.Basic.Util.(json |> member "steamlobbyid" |> to_int)
; goodoldgameslobbyid = Yojson.Basic.Util.(json |> member "goodoldgameslobbyid" |> to_int)
; xboxsessionid = Yojson.Basic.Util.(json |> member "xboxsessionid" |> to_string)
; host_profile_id = Yojson.Basic.Util.(json |> member "host_profile_id" |> to_int)
; state = Yojson.Basic.Util.(json |> member "state" |> to_int)
; description = Yojson.Basic.Util.(json |> member "description" |> to_string)
; visible = Yojson.Basic.Util.(json |> member "visible" |> to_int)
; mapname = Yojson.Basic.Util.(json |> member "mapname" |> to_string)
; options = Yojson.Basic.Util.(json |> member "options" |> to_string)
; passwordprotected = Yojson.Basic.Util.(json |> member "passwordprotected" |> to_int)
; maxplayers = Yojson.Basic.Util.(json |> member "maxplayers" |> to_int)
; matchtype_id = Yojson.Basic.Util.(json |> member "matchtype_id" |> to_int)
; matchmembers = Yojson.Basic.Util.(json |> member "matchmembers" |> to_list |> List.map Match_member.from_json)
; observernum = Yojson.Basic.Util.(json |> member "observernum" |> to_int)
; observermax = Yojson.Basic.Util.(json |> member "observermax" |> to_int)
; isobservable = Yojson.Basic.Util.(json |> member "isobservable" |> to_int)
; observerdelay = Yojson.Basic.Util.(json |> member "observerdelay" |> to_int)
; hasobserverpassword = Yojson.Basic.Util.(json |> member "hasobserverpassword" |> to_int)
; servicetype = Yojson.Basic.Util.(json |> member "servicetype" |> to_int)
; relayserver_region = Yojson.Basic.Util.(json |> member "relayserver_region" |> to_string)
}
;;
35 changes: 35 additions & 0 deletions lib/models/stub/avatar.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type avatar =
{ profile_id : int
; name : string
; alias : string
; personal_statgroup_id : int
; xp : int
; level : int
; leaderboardregion_id : int
; country : string
}

let to_json a =
`Assoc
[ "profile_id", `Int a.profile_id
; "name", `String a.name
; "alias", `String a.alias
; "personal_statgroup_id", `Int a.personal_statgroup_id
; "xp", `Int a.xp
; "level", `Int a.level
; "leaderboardregion_id", `Int a.leaderboardregion_id
; "country", `String a.country
]
;;

let from_json json =
{ profile_id = Yojson.Basic.Util.(json |> member "profile_id" |> to_int)
; name = Yojson.Basic.Util.(json |> member "name" |> to_string)
; alias = Yojson.Basic.Util.(json |> member "alias" |> to_string)
; personal_statgroup_id = Yojson.Basic.Util.(json |> member "personal_statgroup_id" |> to_int)
; xp = Yojson.Basic.Util.(json |> member "xp" |> to_int)
; level = Yojson.Basic.Util.(json |> member "level" |> to_int)
; leaderboardregion_id = Yojson.Basic.Util.(json |> member "leaderboardregion_id" |> to_int)
; country = Yojson.Basic.Util.(json |> member "country" |> to_string)
}
;;
29 changes: 29 additions & 0 deletions lib/models/stub/match_member.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type match_member =
{ match_id : int
; profile_id : int
; ranking : int
; statgroup_id : int
; race_id : int
; teamid : int
}

let to_json m =
`Assoc
[ "match_id", `Int m.match_id
; "profile_id", `Int m.profile_id
; "ranking", `Int m.ranking
; "statgroup_id", `Int m.statgroup_id
; "race_id", `Int m.race_id
; "teamid", `Int m.teamid
]
;;

let from_json json =
{ match_id = Yojson.Basic.Util.(json |> member "match_id" |> to_int)
; profile_id = Yojson.Basic.Util.(json |> member "profile_id" |> to_int)
; ranking = Yojson.Basic.Util.(json |> member "ranking" |> to_int)
; statgroup_id = Yojson.Basic.Util.(json |> member "statgroup_id" |> to_int)
; race_id = Yojson.Basic.Util.(json |> member "race_id" |> to_int)
; teamid = Yojson.Basic.Util.(json |> member "teamid" |> to_int)
}
;;
12 changes: 12 additions & 0 deletions lib/models/stub/response.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type header =
{ code : int
; message : string
}

let to_json r = `Assoc [ "code", `Int r.code; "message", `String r.message ]

let from_json json =
{ code = Yojson.Basic.Util.(json |> member "code" |> to_int)
; message = Yojson.Basic.Util.(json |> member "message" |> to_string)
}
;;
2 changes: 1 addition & 1 deletion make/ocaml/_test.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: test
test:
find . -name '*.coverage' | xargs rm -f
dune runtest --instrument-with bisect_ppx --force
dune runtest --root $$(pwd) --instrument-with bisect_ppx --force
bisect-ppx-report html
bisect-ppx-report summary
1 change: 1 addition & 0 deletions relic-sdk.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ depends: [
"lwt" {>= "5.7.0"}
"lwt_ssl" {>= "1.2.0"}
"alcotest" {with-test & >= "1.3.0"}
"alcotest-lwt" {with-test & >= "1.7.0"}
"bisect_ppx" {with-test & >= "2.3.0"}
"ocamlformat" {with-dev-setup & >= "0.26.1"}
"base" {>= "v0.12.0"}
Expand Down
15 changes: 15 additions & 0 deletions tests/api.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open Lwt.Infix
open Relic_sdk

let request_with_file endpoint response_file =
let requester = Mock.Json_file.create_requester response_file in
let client = Client.create "aoe-api.worldsedgelink.com" Data.Game.Age2 in
Client.get endpoint client ~requester
;;

let test_get_advertisements () =
request_with_file Api.Community.Advertisement.get "findAdvertisements.json"
>>= function
| Some json -> Lwt.return @@ Alcotest.(check string) "Struct has id" "SUCCESS" json.result.message
| None -> Lwt.fail_with "Expected Some but got None"
;;
10 changes: 10 additions & 0 deletions tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(include_subdirs qualified)

(test
(name test)
(package relic-sdk)
(deps
(glob_files ./res/*.json))
(instrumentation
(backend bisect_ppx))
(libraries alcotest alcotest-lwt relic-sdk))
4 changes: 4 additions & 0 deletions tests/mock/json_file.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let create_requester filename =
let path = "res/" ^ filename in
fun _ -> Lwt.return @@ Some (Yojson.Basic.from_file path)
;;
96 changes: 96 additions & 0 deletions tests/res/findAdvertisements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"result": {
"code": 0,
"message": "SUCCESS"
},
"matches": [
{
"id": 100000001,
"steamlobbyid": 123456789012345678,
"goodoldgameslobbyid": 0,
"xboxsessionid": "session-1",
"host_profile_id": 1000001,
"state": 1,
"description": "Test match 1",
"visible": 1,
"mapname": "test map",
"options": "options-string-1",
"passwordprotected": 0,
"maxplayers": 8,
"slotinfo": "slot-info-1",
"matchtype_id": 1,
"matchmembers": [
{
"match_id": 100000001,
"profile_id": 1000001,
"ranking": 10,
"statgroup_id": 10001,
"race_id": 1,
"teamid": 1
}
],
"observernum": 1,
"observermax": 10,
"isobservable": 1,
"observerdelay": 1,
"hasobserverpassword": 0,
"servicetype": 1,
"relayserver_region": "testregion1"
},
{
"id": 100000002,
"steamlobbyid": 123456789012345679,
"goodoldgameslobbyid": 0,
"xboxsessionid": "session-2",
"host_profile_id": 1000002,
"state": 1,
"description": "Test match 2",
"visible": 1,
"mapname": "test map 2",
"options": "options-string-2",
"passwordprotected": 0,
"maxplayers": 8,
"slotinfo": "slot-info-2",
"matchtype_id": 1,
"matchmembers": [
{
"match_id": 100000002,
"profile_id": 1000002,
"ranking": 20,
"statgroup_id": 10002,
"race_id": 2,
"teamid": 2
}
],
"observernum": 2,
"observermax": 20,
"isobservable": 1,
"observerdelay": 2,
"hasobserverpassword": 0,
"servicetype": 2,
"relayserver_region": "testregion2"
}
],
"avatars": [
{
"profile_id": 1000001,
"name": "avatar1",
"alias": "Test Avatar 1 人",
"personal_statgroup_id": 10001,
"xp": 10,
"level": 1,
"leaderboardregion_id": 1,
"country": "testcountry1"
},
{
"profile_id": 1000002,
"name": "avatar2",
"alias": "Test Avatar 2",
"personal_statgroup_id": 10002,
"xp": 20,
"level": 2,
"leaderboardregion_id": 2,
"country": "testcountry2"
}
]
}
Loading

0 comments on commit 8bdadcc

Please sign in to comment.