<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://air.imag.fr/index.php?action=history&amp;feed=atom&amp;title=D%C3%A9monstration</id>
	<title>Démonstration - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://air.imag.fr/index.php?action=history&amp;feed=atom&amp;title=D%C3%A9monstration"/>
	<link rel="alternate" type="text/html" href="https://air.imag.fr/index.php?title=D%C3%A9monstration&amp;action=history"/>
	<updated>2026-06-12T22:36:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://air.imag.fr/index.php?title=D%C3%A9monstration&amp;diff=46786&amp;oldid=prev</id>
		<title>Jade.vandal: Created page with &quot;= Démonstration de Elixir et Phoenix =   Cette démonstration nécessite d&#039;avoir au préalable installé tout ce qu&#039;il faut pour que Elixir et Phoenix fonctionnent correcteme...&quot;</title>
		<link rel="alternate" type="text/html" href="https://air.imag.fr/index.php?title=D%C3%A9monstration&amp;diff=46786&amp;oldid=prev"/>
		<updated>2020-01-05T16:02:40Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Démonstration de Elixir et Phoenix =   Cette démonstration nécessite d&amp;#039;avoir au préalable installé tout ce qu&amp;#039;il faut pour que Elixir et Phoenix fonctionnent correcteme...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Démonstration de Elixir et Phoenix = &lt;br /&gt;
&lt;br /&gt;
Cette démonstration nécessite d&amp;#039;avoir au préalable installé tout ce qu&amp;#039;il faut pour que Elixir et Phoenix fonctionnent correctement.&lt;br /&gt;
&lt;br /&gt;
== Génération d&amp;#039;un nouveau projet ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
mix phx.new project --app project&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lorsqu&amp;#039;il est demandé : &amp;lt;code&amp;gt;Fetch and install dependencies?&amp;lt;/code&amp;gt; entrer &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Entrer dans le dossier crée : &amp;lt;code&amp;gt;cd project&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Configurer la base de données si besoin puis entrer : &amp;lt;code&amp;gt;mix ecto.create&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Générer un CRUD ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
mix phoenix.gen.html Accounts user name:string mail:string phone:string address:string age:integer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ensuite, ajouter dans le router &amp;lt;code&amp;gt;web/router.ex&amp;lt;/code&amp;gt; le chemin vers la page &amp;lt;code&amp;gt;/user&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Elixir&amp;quot;&amp;gt;&lt;br /&gt;
 scope &amp;quot;/&amp;quot;, helloPhoenix do&lt;br /&gt;
    pipe_through :browser&lt;br /&gt;
&lt;br /&gt;
    get &amp;quot;/&amp;quot;, PageController, :index&lt;br /&gt;
    + resources &amp;quot;/user&amp;quot;, AccountsController #à ajouter&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Puis migrer la base de données avec la commande &amp;lt;code&amp;gt;mix ecto.migrate&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Visualisation de l&amp;#039;app == &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
mix phx.server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
sur le serveur local [http://localhost:4000 localhost:4000].&lt;br /&gt;
&lt;br /&gt;
Vous pouvez accéder à la page : [http://localhost:4000/user localhost:4000/user] et créer de nouveaux utilisateurs, les modifier, les supprimer, etc.&lt;br /&gt;
&lt;br /&gt;
== Lancement des tests ==&lt;br /&gt;
&lt;br /&gt;
Ajout d&amp;#039;un test dans : &amp;lt;code&amp;gt;test/{le-dossier-de-votre-choix}&amp;lt;/code&amp;gt;, par exemple &amp;lt;code&amp;gt;test/models/accounts_test.exs&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
	assert 1+1 == 2&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Puis exécuter :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
mix test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
À la fin il y aura :  &amp;lt;code style=&amp;quot;color:green&amp;quot;&amp;gt;17 tests, 0 failures&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Et si on ajoute un test qui ne fonctionne pas :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash shell scripts&amp;quot;&amp;gt;&lt;br /&gt;
test &amp;quot;won&amp;#039;t work&amp;quot; do&lt;br /&gt;
	assert 1+1 == 3&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Après avoir lancé &amp;lt;code&amp;gt;mix test&amp;lt;/code&amp;gt; on obtient alors &amp;lt;code style=&amp;quot;color:red&amp;quot;&amp;gt;18 tests, 1 failures&amp;lt;/code&amp;gt; et les détails suivants :&lt;br /&gt;
&lt;br /&gt;
  1) test won&amp;#039;t work (Project.AccountsTest)&lt;br /&gt;
     project/models/accounts_test.exs:23&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;Assertion with == failed&amp;lt;/span&amp;gt;&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:blue&amp;quot;&amp;gt;code&amp;lt;/span&amp;gt;:  assert 1 + 1 == 3&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:blue&amp;quot;&amp;gt;left&amp;lt;/span&amp;gt;:  &amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;2&amp;lt;/span&amp;gt;&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:blue&amp;quot;&amp;gt;right&amp;lt;/span&amp;gt;: &amp;lt;span style=&amp;quot;color:green&amp;quot;&amp;gt;3&amp;lt;/span&amp;gt;&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:blue&amp;quot;&amp;gt;stacktrace:&amp;lt;/span&amp;gt;&lt;br /&gt;
       project/models/accounts_test.exs:24: (test)&lt;br /&gt;
&lt;br /&gt;
== Ajout de changeset ==&lt;br /&gt;
&lt;br /&gt;
Dans &amp;lt;code&amp;gt;web/models/accounts.ex&amp;lt;/code&amp;gt;, on ajoute les lignes suivantes :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Elixir&amp;quot;&amp;gt;&lt;br /&gt;
def changeset(struct, params \\ %{}) do&lt;br /&gt;
    struct&lt;br /&gt;
    |&amp;gt; cast(params, [:name, :mail, :phone, :address, :age])&lt;br /&gt;
    |&amp;gt; validate_required([:name, :mail, :phone, :address, :age])&lt;br /&gt;
+   |&amp;gt; validate_format(:mail, ~r/@/)  #à ajouter&lt;br /&gt;
+   |&amp;gt; validate_inclusion(:age, 0..100) #à ajouter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Et on voit alors sur l&amp;#039;app web qu&amp;#039;il n&amp;#039;est plus possible d&amp;#039;insérer un nouvel utilisateur avec un age qui n&amp;#039;est pas compris entre 0 et 100 ou dont l&amp;#039;adresse email n&amp;#039;a pas un format valide.&lt;br /&gt;
&lt;br /&gt;
Maintenant si on lance &amp;lt;code&amp;gt;mix test&amp;lt;/code&amp;gt; on voit que certains tests ne passent plus. Si on regarde les premières lignes des fichiers &amp;lt;code&amp;gt;test/models/accounts_test.exs&amp;lt;/code&amp;gt; et &amp;lt;code&amp;gt;test/controllers/accounts_controller_test.exs&amp;lt;/code&amp;gt; on voit alors que les données entrées ne correspondent pas aux requirements du changeset :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Elixir&amp;quot;&amp;gt;&lt;br /&gt;
@valid_attrs %{address: &amp;quot;some content&amp;quot;, age: 42, mail: &amp;quot;some content&amp;quot;, name: &amp;quot;some content&amp;quot;, phone: &amp;quot;some content&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
doit être remplacé par :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Elixir&amp;quot;&amp;gt;&lt;br /&gt;
@valid_attrs %{address: &amp;quot;Polytech&amp;quot;, age: 42, mail: &amp;quot;toto@titi.fr&amp;quot;, name: &amp;quot;Toto&amp;quot;, phone: &amp;quot;0600000000&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=  Problèmes / Solutions =&lt;br /&gt;
&lt;br /&gt;
Quelques erreurs rencontrées lors de l&amp;#039;utilisation de Phoenix et leurs solutions&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable centre&amp;quot; style=&amp;quot;width:100%;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=col | Commande&lt;br /&gt;
! scope=col | Erreur&lt;br /&gt;
! scope=col | Solution&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:33%; text-align:center;&amp;quot; |&lt;br /&gt;
&amp;lt;code&amp;gt;mix ecto.create&amp;lt;/code&amp;gt;&lt;br /&gt;
| style=&amp;quot;width:34%;&amp;quot; |&lt;br /&gt;
FATAL 3D000: database &amp;quot;postgres&amp;quot; does not exist. The database couldn&amp;#039;t be created, database &amp;quot;postgres&amp;quot; does not exist&lt;br /&gt;
| style=&amp;quot;width:33%; text-align:center;&amp;quot; |&lt;br /&gt;
&amp;lt;code&amp;gt;createdb postgres&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:33%; text-align:center;&amp;quot; |&lt;br /&gt;
&amp;lt;code&amp;gt;mix ecto.migrate&amp;lt;/code&amp;gt;&lt;br /&gt;
| style=&amp;quot;width:34%;&amp;quot; |&lt;br /&gt;
ERROR 42P07 (duplicate_table): relation &amp;quot;table&amp;quot; already exists&lt;br /&gt;
| style=&amp;quot;width:33%;&amp;quot; |&lt;br /&gt;
Depuis Postico, supprimer la base de donnée en question&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Jade.vandal</name></author>
	</entry>
</feed>