pivot-libre / tideman

Implementation of the Tideman ranked pairs algorithm
Apache License 2.0
9 stars 3 forks source link

InvalidArgumentException: This Margin has already been registered #57

Closed neckberg closed 6 years ago

neckberg commented 6 years ago

I'm encountering an InvalidArgumentException with the message "This Margin has already been registered", when I try to GET the results of my first election (http://pivot.app/api/election/1/result). My other elections are not getting this error.

I am making the GET request as the creator of the election, and the election has only one user in the electorate (the creator), and that user has cast a ballot for the election that does not have any tied candidates.

Here are some GET responses that show further details of the election (stack trace is at the end): /api/election/1: { "id": 1, "creator_id": 1, "name": "Triceritops Rex", "created_at": "2017-09-24 14:44:57", "updated_at": "2017-09-24 14:44:57", "deleted_at": null, "creator": { "id": 1, "name": "Nathan Eckberg", "email": "nathan.eckberg@gmail.com", "created_at": "2017-09-10 16:04:50", "updated_at": "2017-09-10 16:04:50" } } /api/election/2/elector: [ { "id": 1, "name": "Nathan Eckberg", "email": "nathan.eckberg@gmail.com", "created_at": "2017-09-10 16:04:50", "updated_at": "2017-09-10 16:04:50", "pivot": { "election_id": 1, "user_id": 1 } } ] /api/election/1/batchvote: [ { "id": 1, "elector_id": 1, "candidate_id": 1, "rank": 1, "created_at": "2017-09-24 15:04:47", "updated_at": "2017-12-05 21:01:27" }, { "id": 2, "elector_id": 1, "candidate_id": 10, "rank": 2, "created_at": "2017-11-15 03:34:25", "updated_at": "2017-12-12 16:23:08" }, { "id": 7, "elector_id": 1, "candidate_id": 11, "rank": 3, "created_at": "2017-12-05 21:57:55", "updated_at": "2017-12-12 16:23:09" }, { "id": 9, "elector_id": 1, "candidate_id": 12, "rank": 4, "created_at": "2017-12-12 16:23:08", "updated_at": "2017-12-12 16:23:09" } ] /api/election/1/candidate: [ { "id": 1, "election_id": 1, "name": "Candidate 1", "created_at": "2017-09-24 14:55:43", "updated_at": "2017-09-24 14:55:43" }, { "id": 10, "election_id": 1, "name": "ne candidate", "created_at": "2017-11-12 15:53:27", "updated_at": "2017-11-12 15:53:27" }, { "id": 11, "election_id": 1, "name": "new candidate", "created_at": "2017-12-05 21:48:19", "updated_at": "2017-12-05 21:48:19" }, { "id": 12, "election_id": 1, "name": "yet another candidate", "created_at": "2017-12-05 21:58:23", "updated_at": "2017-12-05 21:58:23" } ]

Stack Trace: {errors: "Sorry, something went wrong.", exception: "InvalidArgumentException",…} errors: "Sorry, something went wrong." exception: "InvalidArgumentException" message: "This Margin has already been registered." trace: [{file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/MarginCalculator.php", line: 29,…},…] 0: {file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/MarginCalculator.php", line: 29,…} 1: {file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/MarginCalculator.php", line: 90,…} 2: {file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/RankedPairsCalculator.php", line: 99,…} 3: {file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/RankedPairsCalculator.php", line: 75,…} 4: {file: "/home/vagrant/Code/pivot/vendor/pivot-libre/tideman/src/RankedPairsCalculator.php", line: 53,…} 5: {file: "/home/vagrant/Code/pivot/app/Http/Controllers/ResultController.php", line: 139,…} 6: {function: "index", class: "App\Http\Controllers\ResultController", type: "->",…} 7: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",…} 8: {,…} 9: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Route.php", line: 203,…} 10: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Route.php", line: 160,…} 11: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Router.php",…} 12: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 13: {,…} 14: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 15: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 16: {,…} 17: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 18: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 19: {,…} 20: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 21: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 22: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 23: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Router.php",…} 24: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Router.php",…} 25: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Router.php",…} 26: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",…} 27: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 28: {,…} 29: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 30: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 31: {,…} 32: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 33: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 34: {,…} 35: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 36: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 37: {,…} 38: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 39: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",…} 40: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",…} 41: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",…} 42: {file: "/home/vagrant/Code/pivot/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",…} 43: {file: "/home/vagrant/Code/pivot/public/index.php", line: 53, function: "handle",…}

carlschroedl commented 6 years ago

This is a great bug report. Excellent detail. Thank you!

I am hoping we can use this as our first foray into php integration tests (or, in Laravelspeak, "Feature tests") for the pivot app, and possibly as a few new unit tests for the tideman library.

In case anyone else gets a head start, here are the most promising-looking Laravel docs: https://laravel.com/docs/5.4/testing https://laravel.com/docs/5.4/http-tests

carlschroedl commented 6 years ago

Should probably also reset the database before and after each feature test via the DatabaseMigrations trait: https://laravel.com/docs/5.4/database-testing#resetting-the-database-after-each-test

carlschroedl commented 6 years ago

If you run a composer update pivot-libre/tideman and get the election result again, the stack trace might be slightly more helpful now.